bp-wg / bp-core Goto Github PK
View Code? Open in Web Editor NEWBP Core Lib: consensus data structures and client-side-validation library for bitcoin protocol
License: Apache License 2.0
BP Core Lib: consensus data structures and client-side-validation library for bitcoin protocol
License: Apache License 2.0
Codespell linter tool is very important for code and system quality.
So I want to add codespell lint job into workflows.
According to the results of LNP-BP/LNPBPs#83
Matching RGB issue: RGB-WG/rgb-core#87
ShortId is a type used outside of the client-side-validation consensus rules. BP Core library must be restricted in its scope and must be ossified, thus it is better to move this type to https://github.com/LNP-BP/rust-lnpbp/
Looks like bp-core
(more precisely bp-seals
) fails to compile on Rust 1.64 (stable). To confirm this, I've tried every minor version from 1.59 and the build succeeds on every one of them except for 1.64.
$ rustup default 1.64 # or stable
info: using existing install for '1.64-x86_64-apple-darwin'
info: default toolchain set to '1.64-x86_64-apple-darwin'
1.64-x86_64-apple-darwin unchanged - rustc 1.64.0 (a55dd71d5 2022-09-19)
$ cargo build --release --bins --all-features
Compiling bp-seals v0.8.0 (/Users/lounesksouri/Developer/RGB/bp-core/seals)
error: named argument `_0` is not used by name
--> seals/src/txout/error.rs:58:28
|
58 | pub struct MethodParseError(pub String);
| ^^^^^^^^^^^^ this named argument is referred to by position in formatting string
|
::: seals/src/txout/explicit.rs:1:49
|
1 | // BP Core Library implementing LNP/BP specifications & standards related to
| - this formatting argument uses named argument `_0` by position
|
note: the lint level is defined here
--> seals/src/lib.rs:20:34
|
20 | #![deny(dead_code, missing_docs, warnings)]
| ^^^^^^^^
= note: `#[deny(named_arguments_used_positionally)]` implied by `#[deny(warnings)]`
help: use the named argument by name to avoid ambiguity
--> seals/src/txout/explicit.rs:1:49
|
1 | // BP Core Library implementing LNP/BP specifica_0ions & standards related to
|
impl TapLeafHash {
pub fn with_leaf_script(leaf_script: &LeafScript) -> Self {
let mut engine = Sha256::from_tag(MIDSTATE_TAPLEAF);
engine.input_raw(&[leaf_script.version.to_consensus_u8()]);
engine.input_with_len::<U32>(leaf_script.script.as_slice());
Self(engine.finish().into())
}
pub fn with_tap_script(tap_script: &TapScript) -> Self {
let mut engine = Sha256::from_tag(MIDSTATE_TAPLEAF);
engine.input_raw(&[TAPROOT_LEAF_TAPSCRIPT]);
engine.input_with_len::<U32>(tap_script.as_slice());
Self(engine.finish().into())
}
}
The function with_leaf_script directly uses a slice of the script as part of constructing the hash. However, according to BIP341, it is required to use the script encoded with CompactSize before including it in the hash. The actual difference is that the length prefix is missing, which results in a different value for the Merkle tree root compared to the standard approach.
//[80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 106, 33, 242, 163, 220, 185, 239, 79, 165, 64, 83, 110, 199, 176, 31, 215, 84, 134, 136, 166, 12, 144, 195, 241, 128, 12, 14, 18, 218, 137, 128, 240, 96, 251, 0]
//[64, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 106, 33, 242, 163, 220, 185, 239, 79, 165, 64, 83, 110, 199, 176, 31, 215, 84, 134, 136, 166, 12, 144, 195, 241, 128, 12, 14, 18, 218, 137, 128, 240, 96, 251, 0]
#[test]
fn test_hash() {
const MIDSTATE_TAPLEAF: [u8; 7] = *b"TapLeaf";
let script_vec = vec![
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 106, 33, 242, 163, 220, 185, 239, 79, 165, 64, 83, 110, 199, 176,
31, 215, 84, 134, 136, 166, 12, 144, 195, 241, 128, 12, 14, 18, 218, 137, 128, 240, 96,
251, 0,
];
let mut bp_script_encode: Vec<u8> = vec![];
let mut bdk_script_encode: Vec<u8> = vec![];
////////////////////////////////////////bp/////////////////////////////////////////////////
let bp_script = bp::ScriptBytes::try_from(script_vec.clone()).unwrap();
let _ = bp::ConsensusEncode::consensus_encode(&bp_script, &mut bp_script_encode);
let mut engine = Sha256::from_tag(MIDSTATE_TAPLEAF);
engine.input_raw(&[LeafVer::TapScript.to_consensus_u8()]);
//The hash values of these three are different.
// engine.input_with_len::<U32>(&bp_script_encode);
// engine.input_with_len::<U32>(bp_script.as_slice());
engine.input_raw(&bp_script_encode);
let bp_result = engine.finish().to_lower_hex_string();
println!("bp_script_encode{:?}", bp_script_encode);
println!("bp_result: {:#?} \n", bp_result);
////////////////////////////////////////bdk/////////////////////////////////////////////////
let mut eng = bdk_chain::bitcoin::taproot::TapLeafHash::engine();
let ver = bdk_chain::bitcoin::taproot::LeafVersion::TapScript;
let bdk_script = bdk_chain::bitcoin::Script::from_bytes(&script_vec);
ver.to_consensus()
.consensus_encode(&mut eng)
.expect("engines don't error");
bdk_script
.consensus_encode(&mut eng)
.expect("engines don't error");
let bdk_hash_result = bdk_chain::bitcoin::taproot::TapLeafHash::from_engine(eng);
let _ = bdk_script.consensus_encode(&mut bdk_script_encode);
println!("bdk_script_encode{:?}", bdk_script_encode);
println!("bdk_hash_result to hex: {:#?}", bdk_hash_result.to_hex());
assert_eq!(bdk_hash_result.to_hex(), bp_result);
}
cargo new demo
Cargo.toml
[dependencies]
bp-core = { version = "0.10.11" }
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2"
rand = { version = "0.8.4", optional = true }
getrandom = { version = "0.2", features = ["js"] }
cargo build --target wasm32-unknown-unknown
Finished dev [unoptimized + debuginfo] target(s) in 9.02s
Cargo.toml
[dependencies]
bp-core = { version = "~0.11.0-beta.1" }
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2"
rand = { version = "0.8.4", optional = true }
getrandom = { version = "0.2", features = ["js"] }
cargo build --target wasm32-unknown-unknown
Updating crates.io index
Compiling secp256k1-sys v0.9.0
Compiling single_use_seals v0.11.0-beta.1
Compiling commit_verify v0.11.0-beta.1
The following warnings were emitted during compilation:
warning: In file included from depend/secp256k1/src/precomputed_ecmult_gen.c:3:
warning: In file included from depend/secp256k1/src/group.h:10:
warning: In file included from depend/secp256k1/src/field.h:10:
warning: depend/secp256k1/src/util.h:32:5: error: call to undeclared library function 'printf' with type 'int (const char *, ...)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
warning: 32 | printf("{");
warning: | ^
warning: depend/secp256k1/src/util.h:32:5: note: include the header <stdio.h> or explicitly provide a declaration for 'printf'
warning: depend/secp256k1/src/util.h:150:17: error: call to undeclared library function 'malloc' with type 'void *(unsigned long)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
warning: 150 | void *ret = malloc(size);
warning: | ^
warning: depend/secp256k1/src/util.h:150:17: note: include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
warning: 2 errors generated.
warning: In file included from depend/secp256k1/src/precomputed_ecmult_gen.c:3:
warning: In file included from depend/secp256k1/src/group.h:10:
warning: In file included from depend/secp256k1/src/field.h:10:
warning: depend/secp256k1/src/util.h:32:5: error: call to undeclared library function 'printf' with type 'int (const char *, ...)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
warning: 32 | printf("{");
warning: | ^
warning: depend/secp256k1/src/util.h:32:5: note: include the header <stdio.h> or explicitly provide a declaration for 'printf'
warning: depend/secp256k1/src/util.h:150:17: error: call to undeclared library function 'malloc' with type 'void *(unsigned long)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
warning: 150 | void *ret = malloc(size);
warning: | ^
warning: depend/secp256k1/src/util.h:150:17: note: include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
warning: 2 errors generated.
error: failed to run custom build command for `secp256k1-sys v0.9.0`
Caused by:
process didn't exit successfully: `/private/tmp/m2/target/debug/build/secp256k1-sys-4a891837fe0332c7/build-script-build` (exit status: 1)
--- stdout
TARGET = Some("wasm32-unknown-unknown")
OPT_LEVEL = Some("0")
HOST = Some("aarch64-apple-darwin")
cargo:rerun-if-env-changed=CC_wasm32-unknown-unknown
CC_wasm32-unknown-unknown = None
cargo:rerun-if-env-changed=CC_wasm32_unknown_unknown
CC_wasm32_unknown_unknown = None
cargo:rerun-if-env-changed=TARGET_CC
TARGET_CC = None
cargo:rerun-if-env-changed=CC
CC = None
cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("true")
cargo:rerun-if-env-changed=CFLAGS_wasm32-unknown-unknown
CFLAGS_wasm32-unknown-unknown = None
cargo:rerun-if-env-changed=CFLAGS_wasm32_unknown_unknown
CFLAGS_wasm32_unknown_unknown = None
cargo:rerun-if-env-changed=TARGET_CFLAGS
TARGET_CFLAGS = None
cargo:rerun-if-env-changed=CFLAGS
CFLAGS = None
cargo:rerun-if-env-changed=CC_wasm32-unknown-unknown
CC_wasm32-unknown-unknown = None
cargo:rerun-if-env-changed=CC_wasm32_unknown_unknown
CC_wasm32_unknown_unknown = None
cargo:rerun-if-env-changed=TARGET_CC
TARGET_CC = None
cargo:rerun-if-env-changed=CC
CC = None
cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
CRATE_CC_NO_DEFAULTS = None
cargo:rerun-if-env-changed=CFLAGS_wasm32-unknown-unknown
CFLAGS_wasm32-unknown-unknown = None
cargo:rerun-if-env-changed=CFLAGS_wasm32_unknown_unknown
CFLAGS_wasm32_unknown_unknown = None
cargo:rerun-if-env-changed=TARGET_CFLAGS
TARGET_CFLAGS = None
cargo:rerun-if-env-changed=CFLAGS
CFLAGS = None
running: "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm/wasm-sysroot" "-Wall" "-Wextra" "-DSECP256K1_API=" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DENABLE_MODULE_ELLSWIFT=1" "-DECMULT_GEN_PREC_BITS=4" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-o" "/private/tmp/m2/target/wasm32-unknown-unknown/debug/build/secp256k1-sys-3d2166817edfbebd/out/wasm/wasm.o" "-c" "wasm/wasm.c"
exit status: 0
running: "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm/wasm-sysroot" "-Wall" "-Wextra" "-DSECP256K1_API=" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DENABLE_MODULE_ELLSWIFT=1" "-DECMULT_GEN_PREC_BITS=4" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-o" "/private/tmp/m2/target/wasm32-unknown-unknown/debug/build/secp256k1-sys-3d2166817edfbebd/out/depend/secp256k1/contrib/lax_der_parsing.o" "-c" "depend/secp256k1/contrib/lax_der_parsing.c"
exit status: 0
running: "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm/wasm-sysroot" "-Wall" "-Wextra" "-DSECP256K1_API=" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DENABLE_MODULE_ELLSWIFT=1" "-DECMULT_GEN_PREC_BITS=4" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-o" "/private/tmp/m2/target/wasm32-unknown-unknown/debug/build/secp256k1-sys-3d2166817edfbebd/out/depend/secp256k1/src/precomputed_ecmult_gen.o" "-c" "depend/secp256k1/src/precomputed_ecmult_gen.c"
cargo:warning=In file included from depend/secp256k1/src/precomputed_ecmult_gen.c:3:
cargo:warning=In file included from depend/secp256k1/src/group.h:10:
cargo:warning=In file included from depend/secp256k1/src/field.h:10:
cargo:warning=depend/secp256k1/src/util.h:32:5: error: call to undeclared library function 'printf' with type 'int (const char *, ...)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
cargo:warning= 32 | printf("{");
cargo:warning= | ^
cargo:warning=depend/secp256k1/src/util.h:32:5: note: include the header <stdio.h> or explicitly provide a declaration for 'printf'
cargo:warning=depend/secp256k1/src/util.h:150:17: error: call to undeclared library function 'malloc' with type 'void *(unsigned long)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
cargo:warning= 150 | void *ret = malloc(size);
cargo:warning= | ^
cargo:warning=depend/secp256k1/src/util.h:150:17: note: include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
cargo:warning=2 errors generated.
exit status: 1
running: "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm/wasm-sysroot" "-I" "wasm/wasm-sysroot" "-Wall" "-Wextra" "-DSECP256K1_API=" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DENABLE_MODULE_ELLSWIFT=1" "-DECMULT_GEN_PREC_BITS=4" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-o" "/private/tmp/m2/target/wasm32-unknown-unknown/debug/build/secp256k1-sys-3d2166817edfbebd/out/wasm/wasm.o" "-c" "wasm/wasm.c"
exit status: 0
running: "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm/wasm-sysroot" "-I" "wasm/wasm-sysroot" "-Wall" "-Wextra" "-DSECP256K1_API=" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DENABLE_MODULE_ELLSWIFT=1" "-DECMULT_GEN_PREC_BITS=4" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-o" "/private/tmp/m2/target/wasm32-unknown-unknown/debug/build/secp256k1-sys-3d2166817edfbebd/out/depend/secp256k1/contrib/lax_der_parsing.o" "-c" "depend/secp256k1/contrib/lax_der_parsing.c"
exit status: 0
running: "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm/wasm-sysroot" "-I" "wasm/wasm-sysroot" "-Wall" "-Wextra" "-DSECP256K1_API=" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DENABLE_MODULE_ELLSWIFT=1" "-DECMULT_GEN_PREC_BITS=4" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-o" "/private/tmp/m2/target/wasm32-unknown-unknown/debug/build/secp256k1-sys-3d2166817edfbebd/out/depend/secp256k1/src/precomputed_ecmult_gen.o" "-c" "depend/secp256k1/src/precomputed_ecmult_gen.c"
cargo:warning=In file included from depend/secp256k1/src/precomputed_ecmult_gen.c:3:
cargo:warning=In file included from depend/secp256k1/src/group.h:10:
cargo:warning=In file included from depend/secp256k1/src/field.h:10:
cargo:warning=depend/secp256k1/src/util.h:32:5: error: call to undeclared library function 'printf' with type 'int (const char *, ...)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
cargo:warning= 32 | printf("{");
cargo:warning= | ^
cargo:warning=depend/secp256k1/src/util.h:32:5: note: include the header <stdio.h> or explicitly provide a declaration for 'printf'
cargo:warning=depend/secp256k1/src/util.h:150:17: error: call to undeclared library function 'malloc' with type 'void *(unsigned long)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
cargo:warning= 150 | void *ret = malloc(size);
cargo:warning= | ^
cargo:warning=depend/secp256k1/src/util.h:150:17: note: include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
cargo:warning=2 errors generated.
exit status: 1
--- stderr
error occurred: Command "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm/wasm-sysroot" "-I" "wasm/wasm-sysroot" "-Wall" "-Wextra" "-DSECP256K1_API=" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DENABLE_MODULE_ELLSWIFT=1" "-DECMULT_GEN_PREC_BITS=4" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-o" "/private/tmp/m2/target/wasm32-unknown-unknown/debug/build/secp256k1-sys-3d2166817edfbebd/out/depend/secp256k1/src/precomputed_ecmult_gen.o" "-c" "depend/secp256k1/src/precomputed_ecmult_gen.c" with args "clang" did not execute successfully (status code exit status: 1).
warning: build failed, waiting for other jobs to finish...
In bp-esplora-client, there's a TxStatus type, but it's slightly different than the one used in bp-wallet. Compare:
And:
The bp-wallet enum has the same data, but it's a slightly different type. I actually prefer the richer type. Should we include it in bp-core so we can use it in both bp-wallet and bp-esplora-client?
If so, where should it go?
PSBT receives invalid script pubkey: instead of taproot v1 output it gets the taproot script tree leaf with op_return code.
More details: https://github.com/BP-WG/bp-core/discussions/20
In order to abstract single-use-seals from a specific bitcoin layer 1 we should:
Chain
typeSee LNP-BP/LNPBPs#116 for the details
Users should not be able to create an invalid ShortId.
Details: #10 (comment)
Prerequisites:
secp256k1
library.secp256k1-zkp
secp256k1-zkp
secp256k1
@zoedberg: can you please move the current implementation of the opret commitments in RGB here? I can't find in which library they do exist today, and I think they must be happening in the same place as tapret commitments (i.e. in this library).
Hi @dr-orlovsky,
I tried use baid58 parse to TapretCommitment and received the follow error:
running 1 test
thread 'tapret::tapscript::test::commiment_serialization' panicked at 'exact size match: Decode(Io(Kind(InvalidInput)))', dbc/src/tapret/tapscript.rs:66:10
Test Code
#[test]
pub fn tapret_commitment_baid58() {
let commitment = commitment();
let _ = TapretCommitment::from_baid58(commitment.to_baid58()).unwrap();
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.