Skip to content

Commit a057e14

Browse files
committed
Merge branch 'StructuredScript'
2 parents 4af7ce2 + 3ef01f4 commit a057e14

File tree

12 files changed

+1331
-782
lines changed

12 files changed

+1331
-782
lines changed

.github/workflows/rust.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
/target
2-
Cargo.lock
1+
**/target
2+
**/Cargo.lock

Cargo.toml

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,17 @@
1+
[workspace]
2+
members = ["macro"]
3+
14
[package]
25
name = "bitcoin-script"
3-
version = "0.2.0"
4-
authors = ["Matt Bell <[email protected]>", "Lukas George [email protected]"]
6+
version = "0.3.0"
7+
authors = ["Lukas George <[email protected]>"]
58
edition = "2021"
69
description = "Inline Bitcoin scripts"
710
license = "MIT"
811
repository = "https://github.com/BitVM/rust-bitcoin-script"
912

10-
[lib]
11-
proc-macro = true
12-
13-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14-
1513
[dependencies]
16-
# bitcoin = "0.31.1"
17-
bitcoin = { git = "https://github.com/rust-bitcoin/rust-bitcoin", branch = "bitvm" }
18-
quote = "1.0.23"
19-
proc-macro-error = "1.0.4"
20-
lazy_static = "1.4.0"
21-
hex = "0.4.3"
22-
proc-macro2 = "1.0.51"
23-
24-
[patch.crates-io.base58check]
25-
git = "https://github.com/rust-bitcoin/rust-bitcoin"
26-
branch = "bitvm"
27-
28-
[patch.crates-io.bitcoin]
29-
git = "https://github.com/rust-bitcoin/rust-bitcoin"
30-
branch = "bitvm"
31-
32-
[patch.crates-io.bitcoin_hashes]
33-
git = "https://github.com/rust-bitcoin/rust-bitcoin"
34-
branch = "bitvm"
35-
36-
[patch.crates-io.bitcoin-internals]
37-
git = "https://github.com/rust-bitcoin/rust-bitcoin"
38-
branch = "bitvm"
39-
40-
[patch.crates-io.bitcoin-io]
41-
git = "https://github.com/rust-bitcoin/rust-bitcoin"
42-
branch = "bitvm"
43-
44-
[patch.crates-io.bitcoin-units]
45-
git = "https://github.com/rust-bitcoin/rust-bitcoin"
46-
branch = "bitvm"
14+
bitcoin = { version = "0.32.5", features = ["rand-std"] }
15+
lazy_static = "1.5.0"
16+
script-macro = { path = "./macro" }
17+
stdext = "0.3.3"

README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
# bitcoin-script
1+
# Bitvm Bitcoin Script
22

3-
[![Rust](https://github.com/mappum/rust-bitcoin-script/workflows/Rust/badge.svg)](https://github.com/mappum/rust-bitcoin-script/actions?query=workflow%3ARust)
4-
[![crates.io](https://img.shields.io/crates/v/bitcoin-script.svg)](https://crates.io/crates/bitcoin-script)
5-
[![docs.rs](https://docs.rs/bitcoin-script/badge.svg)](https://docs.rs/bitcoin-script)
6-
7-
**Bitcoin scripts inline in Rust.**
8-
9-
---
3+
Utilities used in the official [BitVM](https://github.com/BitVM/BitVM) implementation to generate Bitcoin Script. Heavily inspired by [rust-bitcoin-script's inline macro](https://github.com/mappum/rust-bitcoin-script).
104

115
## Usage
126

13-
This crate exports a `script!` macro which can be used to build Bitcoin scripts. The macro returns the [`Script`](https://docs.rs/bitcoin/latest/bitcoin/struct.ScriptBuf.html) type from the [`bitcoin`](https://github.com/rust-bitcoin/rust-bitcoin) crate.
7+
This crate exports a `script!` macro which can be used to build structured Bitcoin scripts and compiled to the [`Script`](https://docs.rs/bitcoin/latest/bitcoin/struct.ScriptBuf.html) type from the [`bitcoin`](https://github.com/rust-bitcoin/rust-bitcoin) crate.
148

159
**Example:**
1610

1711
```rust
18-
#![feature(proc_macro_hygiene)]
19-
2012
use bitcoin_script::bitcoin_script;
2113

2214
let htlc_script = script! {
@@ -28,6 +20,8 @@ let htlc_script = script! {
2820
OP_EQUALVERIFY
2921
OP_CHECKSIG
3022
};
23+
24+
let script_buf = htlc_script.compile();
3125
```
3226

3327
### Syntax
@@ -77,6 +71,7 @@ Rust expressions of the following types are supported:
7771
- [`bitcoin::PublicKey`](https://docs.rs/bitcoin/latest/bitcoin/struct.PublicKey.html)
7872
- [`bitcoin::XOnlyPublicKey`](https://docs.rs/bitcoin/latest/bitcoin/struct.XOnlyPublicKey.html)
7973
- [`bitcoin::ScriptBuf`](https://docs.rs/bitcoin/latest/bitcoin/struct.ScriptBuf.html)
74+
- `StructuredScript`
8075

8176
```rust
8277
let bytes = vec![1, 2, 3];
@@ -90,5 +85,22 @@ let script = script! {
9085
};
9186
```
9287

93-
### Optimality
94-
Obsolete Opcodes are automatically removed when using the `optimal_opcodes` branch. E.g. a sequence of `OP_0 OP_ROLL` will be optimized away.
88+
#### Conditional Scipt Generation
89+
90+
For-loops and if-else-statements are supported inside the script and will be unrolled when the scripts are generated.
91+
92+
```rust
93+
let loop_count = 10;
94+
95+
let script = script! {
96+
for i in 0..loop_count {
97+
if i % 2 == 0 {
98+
OP_ADD
99+
} else {
100+
OP_DUP
101+
OP_ADD
102+
}
103+
}
104+
};
105+
106+
```

macro/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "script-macro"
3+
version = "0.3.0"
4+
authors = ["Lukas George [email protected]"]
5+
edition = "2021"
6+
description = "Inline Bitcoin scripts proc_macro"
7+
license = "MIT"
8+
9+
[lib]
10+
proc-macro = true
11+
12+
[dependencies]
13+
bitcoin = "0.32.5"
14+
quote = "1.0.23"
15+
proc-macro-error = "1.0.4"
16+
lazy_static = "1.4.0"
17+
hex = "0.4.3"
18+
proc-macro2 = "1.0.51"

src/generate.rs renamed to macro/src/generate.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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!(pushable::Builder::new());
7+
let mut tokens = quote!(::bitcoin_script::Script::new(::bitcoin_script::function_name!()));
88

99
for (item, span) in syntax {
1010
let push = match item {
@@ -15,8 +15,7 @@ pub fn generate(syntax: Vec<(Syntax, Span)>) -> TokenStream {
1515
};
1616
tokens.extend(push);
1717
}
18-
19-
tokens.extend(quote!(.0.into_script()));
18+
// tokens.extend(quote! {.analyze_stack()}); // for debug
2019
tokens
2120
}
2221

macro/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
mod generate;
2+
mod parse;
3+
4+
use generate::generate;
5+
use parse::parse;
6+
use proc_macro::TokenStream;
7+
use proc_macro_error::{proc_macro_error, set_dummy};
8+
use quote::quote;
9+
10+
#[proc_macro]
11+
#[proc_macro_error]
12+
pub fn script(tokens: TokenStream) -> TokenStream {
13+
set_dummy(quote!((::bitcoin::Script::new())));
14+
generate(parse(tokens.into())).into()
15+
}

0 commit comments

Comments
 (0)