kaspa-consensus-core 0.15.0

Kaspa consensus core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
use alloc::borrow::Cow;
use borsh::{BorshDeserialize, BorshSerialize};
use core::fmt::Formatter;
use js_sys::Object;
use kaspa_utils::{
    hex::{FromHex, ToHex},
    serde_bytes::FromHexVisitor,
};
use serde::{
    de::{Error, Visitor},
    Deserialize, Deserializer, Serialize, Serializer,
};
use smallvec::SmallVec;
use std::{
    collections::HashSet,
    str::{self, FromStr},
};
use wasm_bindgen::prelude::*;
use workflow_wasm::prelude::*;

/// Size of the underlying script vector of a script.
pub const SCRIPT_VECTOR_SIZE: usize = 36;

/// Used as the underlying type for script public key data, optimized for the common p2pk script size (34).
pub type ScriptVec = SmallVec<[u8; SCRIPT_VECTOR_SIZE]>;

/// Represents the ScriptPublicKey Version
pub type ScriptPublicKeyVersion = u16;

/// Alias the `smallvec!` macro to ease maintenance
pub use smallvec::smallvec as scriptvec;
use wasm_bindgen::prelude::wasm_bindgen;

//Represents a Set of [`ScriptPublicKey`]s
pub type ScriptPublicKeys = HashSet<ScriptPublicKey>;

#[wasm_bindgen(typescript_custom_section)]
const TS_SCRIPT_PUBLIC_KEY: &'static str = r#"
/**
 * Interface defines the structure of a Script Public Key.
 * 
 * @category Consensus
 */
export interface IScriptPublicKey {
    version : number;
    script: HexString;
}
"#;

/// Represents a Kaspad ScriptPublicKey
/// @category Consensus
#[derive(Default, PartialEq, Eq, Clone, Hash, CastFromJs)]
#[wasm_bindgen(inspectable)]
pub struct ScriptPublicKey {
    pub version: ScriptPublicKeyVersion,
    pub(super) script: ScriptVec, // Kept private to preserve read-only semantics
}

impl std::fmt::Debug for ScriptPublicKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ScriptPublicKey").field("version", &self.version).field("script", &self.script.to_hex()).finish()
    }
}

impl FromHex for ScriptPublicKey {
    type Error = faster_hex::Error;

    fn from_hex(hex_str: &str) -> Result<Self, Self::Error> {
        ScriptPublicKey::from_str(hex_str)
    }
}

#[derive(Default, Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Hash)]
#[serde(rename_all = "camelCase")]
#[serde(rename = "ScriptPublicKey")]
struct ScriptPublicKeyInternal<'a> {
    version: ScriptPublicKeyVersion,
    script: &'a [u8],
}

impl Serialize for ScriptPublicKey {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if serializer.is_human_readable() {
            let mut hex = vec![0u8; self.script.len() * 2 + 4];
            faster_hex::hex_encode(&self.version.to_be_bytes(), &mut hex).map_err(serde::ser::Error::custom)?;
            faster_hex::hex_encode(&self.script, &mut hex[4..]).map_err(serde::ser::Error::custom)?;
            serializer.serialize_str(unsafe { str::from_utf8_unchecked(&hex) })
        } else {
            ScriptPublicKeyInternal { version: self.version, script: &self.script }.serialize(serializer)
        }
    }
}

impl<'de: 'a, 'a> Deserialize<'de> for ScriptPublicKey {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Default)]
        pub struct ScriptPublicKeyVisitor<'de> {
            from_hex_visitor: FromHexVisitor<'de, ScriptPublicKey>,
            marker: std::marker::PhantomData<ScriptPublicKey>,
            lifetime: std::marker::PhantomData<&'de ()>,
        }
        impl<'de> Visitor<'de> for ScriptPublicKeyVisitor<'de> {
            type Value = ScriptPublicKey;

            fn expecting(&self, formatter: &mut Formatter) -> core::fmt::Result {
                #[cfg(target_arch = "wasm32")]
                {
                    write!(formatter, "string-type: string, str; bytes-type: slice of bytes, vec of bytes; map; number-type - pointer")
                }
                #[cfg(not(target_arch = "wasm32"))]
                {
                    write!(formatter, "string-type: string, str; bytes-type: slice of bytes, vec of bytes; map")
                }
            }

            // TODO - review integer conversions (SPK & Address)
            // This is currently used to allow for deserialization
            // of JsValues created via wasm-bindgen bindings in the
            // WASM context. This is not used in the native context
            // as serialization will never produce objects.
            // - review multiple integer mappings (are they all needed?)
            // - consider manual marshaling of RPC data structures
            // (which is now possible due to the introduction of the kaspa-consensus-wasm crate)
            #[cfg(target_arch = "wasm32")]
            fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                self.visit_u32(v as u32)
            }
            #[cfg(target_arch = "wasm32")]
            fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                self.visit_u32(v as u32)
            }

            #[cfg(target_arch = "wasm32")]
            fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                self.visit_u32(v as u32)
            }
            #[cfg(target_arch = "wasm32")]
            fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                self.visit_u32(v as u32)
            }
            #[cfg(target_arch = "wasm32")]
            fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                use wasm_bindgen::convert::RefFromWasmAbi;
                let instance_ref = unsafe { Self::Value::ref_from_abi(v) }; // todo add checks for safecast
                Ok(instance_ref.clone())
            }
            #[cfg(target_arch = "wasm32")]
            fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                self.visit_u32(v as u32)
            }

            #[inline]
            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: Error,
            {
                self.from_hex_visitor.visit_str(v)
            }

            #[inline]
            fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
            where
                E: Error,
            {
                self.from_hex_visitor.visit_borrowed_str(v)
            }

            #[inline]
            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
            where
                E: Error,
            {
                self.from_hex_visitor.visit_string(v)
            }

            #[inline]
            fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
            where
                E: Error,
            {
                self.from_hex_visitor.visit_bytes(v)
            }

            #[inline]
            fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
            where
                E: Error,
            {
                self.from_hex_visitor.visit_borrowed_bytes(v)
            }

            #[inline]
            fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
            where
                E: Error,
            {
                self.from_hex_visitor.visit_byte_buf(v)
            }

            #[inline]
            fn visit_map<A>(self, mut access: A) -> Result<Self::Value, A::Error>
            where
                A: serde::de::MapAccess<'de>,
            {
                #[derive(Deserialize, Copy, Clone)]
                #[serde(field_identifier, rename_all = "lowercase")]
                enum Field {
                    Version,
                    Script,
                }

                #[derive(Debug, Clone, Deserialize)]
                #[serde(untagged)]
                pub enum Value<'a> {
                    U16(u16),
                    #[serde(borrow)]
                    String(Cow<'a, String>),
                }
                impl From<Value<'_>> for u16 {
                    fn from(value: Value<'_>) -> Self {
                        let Value::U16(v) = value else { panic!("unexpected conversion: {value:?}") };
                        v
                    }
                }

                impl TryFrom<Value<'_>> for Vec<u8> {
                    type Error = faster_hex::Error;

                    fn try_from(value: Value) -> Result<Self, Self::Error> {
                        match value {
                            Value::U16(_) => {
                                panic!("unexpected conversion: {value:?}")
                            }
                            Value::String(script) => {
                                let mut script_bytes = vec![0u8; script.len() / 2];
                                faster_hex::hex_decode(script.as_bytes(), script_bytes.as_mut_slice())?;

                                Ok(script_bytes)
                            }
                        }
                    }
                }

                let mut version: Option<u16> = None;
                let mut script: Option<Vec<u8>> = None;

                while let Some((key, value)) = access.next_entry::<Field, Value>()? {
                    match key {
                        Field::Version => {
                            version = Some(value.into());
                        }
                        Field::Script => script = Some(value.try_into().map_err(Error::custom)?),
                    }
                    if version.is_some() && script.is_some() {
                        break;
                    }
                }
                let (version, script) = match (version, script) {
                    (Some(version), Some(script)) => Ok((version, script)),
                    (None, _) => Err(serde::de::Error::missing_field("version")),
                    (_, None) => Err(serde::de::Error::missing_field("script")),
                }?;

                Ok(ScriptPublicKey::from_vec(version, script))
            }
        }

        if deserializer.is_human_readable() {
            deserializer.deserialize_any(ScriptPublicKeyVisitor::default())
        } else {
            ScriptPublicKeyInternal::deserialize(deserializer)
                .map(|ScriptPublicKeyInternal { script, version }| Self { version, script: SmallVec::from_slice(script) })
        }
    }
}

impl FromStr for ScriptPublicKey {
    type Err = faster_hex::Error;
    fn from_str(hex_str: &str) -> Result<Self, Self::Err> {
        let hex_len = hex_str.len();
        if hex_len < 4 {
            return Err(faster_hex::Error::InvalidLength(hex_len));
        }
        let mut bytes = vec![0u8; hex_len / 2];
        faster_hex::hex_decode(hex_str.as_bytes(), bytes.as_mut_slice())?;
        let version = u16::from_be_bytes(bytes[0..2].try_into().unwrap());
        Ok(Self { version, script: SmallVec::from_slice(&bytes[2..]) })
    }
}

impl ScriptPublicKey {
    pub fn new(version: ScriptPublicKeyVersion, script: ScriptVec) -> Self {
        Self { version, script }
    }

    pub fn from_vec(version: ScriptPublicKeyVersion, script: Vec<u8>) -> Self {
        Self { version, script: ScriptVec::from_vec(script) }
    }

    pub fn version(&self) -> ScriptPublicKeyVersion {
        self.version
    }

    pub fn script(&self) -> &[u8] {
        &self.script
    }
}

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(typescript_type = "ScriptPublicKey | HexString")]
    pub type ScriptPublicKeyT;
}

#[wasm_bindgen]
impl ScriptPublicKey {
    #[wasm_bindgen(constructor)]
    pub fn constructor(version: u16, script: JsValue) -> Result<ScriptPublicKey, JsError> {
        let script = script.try_as_vec_u8()?;
        Ok(ScriptPublicKey::new(version, script.into()))
    }

    #[wasm_bindgen(getter = script)]
    pub fn script_as_hex(&self) -> String {
        self.script.to_hex()
    }
}

//
// Borsh serializers need to be manually implemented for `ScriptPublicKey` since
// smallvec does not currently support Borsh
//

impl BorshSerialize for ScriptPublicKey {
    fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
        borsh::BorshSerialize::serialize(&self.version, writer)?;
        // Vectors and slices are all serialized internally the same way
        borsh::BorshSerialize::serialize(&self.script.as_slice(), writer)?;
        Ok(())
    }
}

impl BorshDeserialize for ScriptPublicKey {
    fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
        // Deserialize into vec first since we have no custom smallvec support
        Ok(Self::from_vec(borsh::BorshDeserialize::deserialize_reader(reader)?, borsh::BorshDeserialize::deserialize_reader(reader)?))
    }
}

type CastError = workflow_wasm::error::Error;
impl TryCastFromJs for ScriptPublicKey {
    type Error = workflow_wasm::error::Error;
    fn try_cast_from<'a, R>(value: &'a R) -> Result<Cast<Self>, Self::Error>
    where
        R: AsRef<JsValue> + 'a,
    {
        Self::resolve(value, || {
            if let Some(hex_str) = value.as_ref().as_string() {
                Ok(Self::from_str(&hex_str).map_err(CastError::custom)?)
            } else if let Some(object) = Object::try_from(value.as_ref()) {
                let version = object.try_get_value("version")?.ok_or(CastError::custom(
                    "ScriptPublicKey must be a hex string or an object with 'version' and 'script' properties",
                ))?;

                let version = if let Ok(version) = version.try_as_u16() {
                    version
                } else {
                    return Err(CastError::custom("Invalid version value '{version:?}'"));
                };

                let script = object.get_vec_u8("script")?;

                Ok(ScriptPublicKey::from_vec(version, script))
            } else {
                Err(CastError::custom(format!("Unable to convert ScriptPublicKey from: {:?}", value.as_ref())))
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use js_sys::Object;
    use wasm_bindgen::__rt::IntoJsResult;

    #[test]
    fn test_spk_serde_json() {
        let vec = (0..SCRIPT_VECTOR_SIZE as u8).collect::<Vec<_>>();
        let spk = ScriptPublicKey::from_vec(0xc0de, vec.clone()); // 0xc0de == 49374,
        let hex: String = serde_json::to_string(&spk).unwrap();
        assert_eq!("\"c0de000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223\"", hex);
        let spk = serde_json::from_str::<ScriptPublicKey>(&hex).unwrap();
        assert_eq!(spk.version, 0xc0de);
        assert_eq!(spk.script.as_slice(), vec.as_slice());
        let result = "00".parse::<ScriptPublicKey>();
        assert!(matches!(result, Err(faster_hex::Error::InvalidLength(2))));
        let result = "0000".parse::<ScriptPublicKey>();
        let _empty = ScriptPublicKey { version: 0, script: ScriptVec::new() };
        assert!(matches!(result, Ok(_empty)));
    }

    #[test]
    fn test_spk_borsh() {
        // Tests for ScriptPublicKey Borsh ser/deser since we manually implemented them
        let spk = ScriptPublicKey::from_vec(12, vec![32; 20]);
        let bin = borsh::to_vec(&spk).unwrap();
        let spk2: ScriptPublicKey = BorshDeserialize::try_from_slice(&bin).unwrap();
        assert_eq!(spk, spk2);

        let spk = ScriptPublicKey::from_vec(55455, vec![11; 200]);
        let bin = borsh::to_vec(&spk).unwrap();
        let spk2: ScriptPublicKey = BorshDeserialize::try_from_slice(&bin).unwrap();
        assert_eq!(spk, spk2);
    }

    use wasm_bindgen::convert::IntoWasmAbi;
    use wasm_bindgen_test::wasm_bindgen_test;
    use workflow_wasm::serde::{from_value, to_value};

    #[wasm_bindgen_test]
    pub fn test_wasm_serde_constructor() {
        let version = 0xc0de;
        let vec: Vec<u8> = (0..SCRIPT_VECTOR_SIZE as u8).collect();
        let spk = ScriptPublicKey::from_vec(version, vec.clone());
        let str = "c0de000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223";
        let js = to_value(&spk).unwrap();
        assert_eq!(js.as_string().unwrap(), str);
        let script_hex = spk.script_as_hex();
        let script_hex_js: JsValue = JsValue::from_str(&script_hex);

        let spk_js = to_value(&ScriptPublicKey::constructor(version, script_hex_js).map_err(|_| ()).unwrap()).unwrap();
        assert_eq!(JsValue::from_str(str), spk_js.clone());
        assert_eq!(spk, from_value(spk_js.clone()).map_err(|_| ()).unwrap());
        assert_eq!(JsValue::from_str("string"), spk_js.js_typeof());
    }

    #[wasm_bindgen_test]
    pub fn test_wasm_serde_js_spk_object() {
        let version = 0xc0de;
        let vec: Vec<u8> = (0..SCRIPT_VECTOR_SIZE as u8).collect();
        let spk = ScriptPublicKey::from_vec(version, vec.clone());

        let script_hex = spk.script_as_hex();

        let version_js: JsValue = version.into();
        let script_hex_js: JsValue = JsValue::from_str(&script_hex);

        let obj_hex = Object::new();
        obj_hex.set("version", &version_js).unwrap();
        obj_hex.set("script", &script_hex_js).unwrap();

        let actual: ScriptPublicKey = from_value(obj_hex.into_js_result().unwrap()).unwrap();
        assert_eq!(spk, actual);
    }

    #[wasm_bindgen_test]
    pub fn test_wasm_serde_spk_object() {
        let version = 0xc0de;
        let vec: Vec<u8> = (0..SCRIPT_VECTOR_SIZE as u8).collect();
        let spk = ScriptPublicKey::from_vec(version, vec.clone());

        let script_hex = spk.script_as_hex();
        let script_hex_js: JsValue = JsValue::from_str(&script_hex);

        let expected = ScriptPublicKey::constructor(version, script_hex_js).map_err(|_| ()).unwrap();
        let wasm_js_value: JsValue = expected.clone().into_abi().into();

        let actual = from_value(wasm_js_value).unwrap();
        assert_eq!(expected, actual);
    }
}