Skip to content

Commit 519088c

Browse files
Merge pull request #10 from uncomputable/2025-03-cleanup
2 parents f5f318a + c1a8023 commit 519088c

File tree

6 files changed

+32
-22
lines changed

6 files changed

+32
-22
lines changed

Cargo.toml

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ members = ["macro"]
33

44
[package]
55
name = "bitcoin-script"
6-
version = "0.3.0"
6+
version = "0.4.0"
77
authors = ["Lukas George <[email protected]>"]
88
edition = "2021"
99
description = "Inline Bitcoin scripts"
1010
license = "MIT"
1111
repository = "https://github.com/BitVM/rust-bitcoin-script"
1212

13+
[features]
14+
serde = ["dep:serde", "bitcoin/serde"]
15+
1316
[dependencies]
14-
bitcoin = { version = "0.32.5", features = ["rand-std", "serde"] }
15-
lazy_static = "1.5.0"
17+
bitcoin = { version = "0.32.5" }
1618
script-macro = { path = "./macro" }
1719
stdext = "0.3.3"
18-
serde = { version = "1", features = ["derive"] }
20+
serde = { version = "1", features = ["derive"], optional = true }
21+
22+
[dev-dependencies]
1923
bincode = "1.3.3"

macro/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "script-macro"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
authors = ["Lukas George [email protected]"]
55
edition = "2021"
66
description = "Inline Bitcoin scripts proc_macro"
@@ -13,6 +13,4 @@ proc-macro = true
1313
bitcoin = "0.32.5"
1414
quote = "1.0.23"
1515
proc-macro-error = "1.0.4"
16-
lazy_static = "1.4.0"
17-
hex = "0.4.3"
1816
proc-macro2 = "1.0.51"

macro/src/generate.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use proc_macro2::{Ident, Span, TokenStream};
44
use quote::{quote, quote_spanned};
55

66
pub fn generate(syntax: Vec<(Syntax, Span)>) -> TokenStream {
7-
let mut tokens = quote!(::bitcoin_script::Script::new(::bitcoin_script::function_name!()));
7+
let mut tokens = quote!(::bitcoin_script::Script::new(
8+
::bitcoin_script::function_name!()
9+
));
810

911
for (item, span) in syntax {
1012
let push = match item {

macro/src/parse.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use bitcoin::hex::FromHex;
12
use bitcoin::{
23
blockdata::opcodes::Opcode,
34
opcodes::{all::*, OP_0, OP_FALSE, OP_NOP2, OP_NOP3, OP_TRUE},
@@ -354,9 +355,7 @@ pub fn parse(tokens: TokenStream) -> Vec<(Syntax, Span)> {
354355
// Wrap if-else statements such that they return a Vec<ScriptBuf>
355356
(Ident(_), "if") => parse_if(token, &mut tokens),
356357
// Replace DEBUG with OP_RESERVED
357-
(Ident(_), "DEBUG") => {
358-
(Syntax::Opcode(OP_RESERVED), token.span())
359-
}
358+
(Ident(_), "DEBUG") => (Syntax::Opcode(OP_RESERVED), token.span()),
360359

361360
// identifier, look up opcode
362361
(Ident(_), _) => match parse_opcode(&token_str) {
@@ -541,7 +540,7 @@ fn parse_data(token: TokenTree) -> (Syntax, Span) {
541540

542541
fn parse_bytes(token: TokenTree) -> (Syntax, Span) {
543542
let hex_bytes = &token.to_string()[2..];
544-
let bytes = hex::decode(hex_bytes).unwrap_or_else(|err| {
543+
let bytes = Vec::<u8>::from_hex(hex_bytes).unwrap_or_else(|err| {
545544
emit_error!(token.span(), "invalid hex literal ({})", err);
546545
});
547546
(Syntax::Bytes(bytes), token.span())

src/builder.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ 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};
76
use std::collections::HashMap;
87
use std::convert::TryFrom;
98
use std::hash::{DefaultHasher, Hash, Hasher};
109

11-
#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq)]
10+
#[cfg(feature = "serde")]
11+
use serde::{Deserialize, Serialize};
12+
13+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
14+
#[derive(Clone, Debug, Hash, PartialEq)]
1215
pub enum Block {
1316
Call(u64),
1417
Script(ScriptBuf),
@@ -21,7 +24,8 @@ impl Block {
2124
}
2225
}
2326

24-
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
27+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
28+
#[derive(Clone, Debug, PartialEq)]
2529
pub struct StructuredScript {
2630
size: usize,
2731
pub debug_identifier: String,
@@ -43,15 +47,18 @@ fn calculate_hash<T: Hash>(t: &T) -> u64 {
4347

4448
impl StructuredScript {
4549
pub fn new(debug_info: &str) -> Self {
46-
let blocks = Vec::new();
4750
StructuredScript {
4851
size: 0,
4952
debug_identifier: debug_info.to_string(),
50-
blocks,
53+
blocks: Vec::new(),
5154
script_map: HashMap::new(),
5255
}
5356
}
5457

58+
pub fn is_empty(&self) -> bool {
59+
self.size == 0
60+
}
61+
5562
pub fn len(&self) -> usize {
5663
self.size
5764
}
@@ -63,7 +70,7 @@ impl StructuredScript {
6370
pub fn get_structured_script(&self, id: &u64) -> &StructuredScript {
6471
self.script_map
6572
.get(id)
66-
.expect(&format!("script id: {} not found in script_map.", id))
73+
.unwrap_or_else(|| panic!("script id: {} not found in script_map.", id))
6774
}
6875

6976
// Return the debug information of the Opcode at position
@@ -133,10 +140,10 @@ impl StructuredScript {
133140
}
134141

135142
pub fn push_env_script(mut self, mut data: StructuredScript) -> StructuredScript {
136-
if data.len() == 0 {
143+
if data.is_empty() {
137144
return self;
138145
}
139-
if self.len() == 0 {
146+
if self.is_empty() {
140147
return data;
141148
}
142149

@@ -342,8 +349,7 @@ impl NotU8Pushable for u32 {
342349
}
343350
impl NotU8Pushable for usize {
344351
fn bitcoin_script_push(self, builder: StructuredScript) -> StructuredScript {
345-
builder
346-
.push_int(i64::try_from(self).unwrap_or_else(|_| panic!("Usize does not fit in i64")))
352+
builder.push_int(i64::try_from(self).expect("Usize does not fit in i64"))
347353
}
348354
}
349355
impl NotU8Pushable for Vec<u8> {

tests/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ fn test_push_witness() {
304304
);
305305
}
306306

307+
#[cfg(feature = "serde")]
307308
#[test]
308309
fn test_serialization() {
309310
let script = script! {

0 commit comments

Comments
 (0)