Skip to content

Commit cda22ff

Browse files
committed
Add serde serialization
1 parent a057e14 commit cda22ff

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ license = "MIT"
1111
repository = "https://github.com/BitVM/rust-bitcoin-script"
1212

1313
[dependencies]
14-
bitcoin = { version = "0.32.5", features = ["rand-std"] }
14+
bitcoin = { version = "0.32.5", features = ["rand-std", "serde"] }
1515
lazy_static = "1.5.0"
1616
script-macro = { path = "./macro" }
1717
stdext = "0.3.3"
18+
serde = { version = "1", features = ["derive"] }
19+
bincode = "1.3.3"

src/builder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use bitcoin::blockdata::script::{Instruction, PushBytes, PushBytesBuf, ScriptBuf
33
use bitcoin::opcodes::{OP_0, OP_TRUE};
44
use bitcoin::script::write_scriptint;
55
use bitcoin::Witness;
6+
use serde::{Deserialize, Serialize};
67
use std::collections::HashMap;
78
use std::convert::TryFrom;
89
use std::hash::{DefaultHasher, Hash, Hasher};
910

10-
#[derive(Clone, Debug, Hash)]
11+
#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq)]
1112
pub enum Block {
1213
Call(u64),
1314
Script(ScriptBuf),
@@ -20,7 +21,7 @@ impl Block {
2021
}
2122
}
2223

23-
#[derive(Clone, Debug)]
24+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
2425
pub struct StructuredScript {
2526
size: usize,
2627
pub debug_identifier: String,

tests/test.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,24 @@ fn test_push_witness() {
303303
reference_script.compile().as_bytes()
304304
);
305305
}
306+
307+
#[test]
308+
fn test_serialization() {
309+
let script = script! {
310+
// Example script
311+
for i in 0..10 {
312+
{i}
313+
{i*2}
314+
{i*4}
315+
OP_ADD
316+
OP_ADD
317+
}
318+
};
319+
320+
let binary_data = bincode::serialize(&script).unwrap();
321+
println!("Script size: {} bytes", script.len());
322+
println!("Binary size: {} bytes", binary_data.len());
323+
324+
let deserialized: Script = bincode::deserialize(&binary_data).unwrap();
325+
assert_eq!(deserialized, script);
326+
}

0 commit comments

Comments
 (0)