From e1711a20d421bba5d4c997da44a83389330043a9 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Fri, 19 Jan 2024 14:37:56 +0100 Subject: [PATCH 1/5] resolver: rename esplora into esplora_blocking and do a dedicated mod --- .github/workflows/build.yml | 2 +- Cargo.toml | 6 ++--- cli/Cargo.toml | 2 +- cli/src/args.rs | 3 ++- src/lib.rs | 6 ++--- .../esplora_blocking.rs} | 0 src/resolvers/mod.rs | 23 +++++++++++++++++++ src/runtime.rs | 2 +- 8 files changed, 33 insertions(+), 11 deletions(-) rename src/{resolver.rs => resolvers/esplora_blocking.rs} (100%) create mode 100644 src/resolvers/mod.rs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 89ca89c..c4106ff 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -44,7 +44,7 @@ jobs: fail-fast: false matrix: feature: - - esplora + - esplora_blocking - log - serde steps: diff --git a/Cargo.toml b/Cargo.toml index e61af5b..74a0218 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,9 +84,9 @@ serde_with = { workspace = true, optional = true } log = { workspace = true, optional = true } [features] -default = ["esplora"] -all = ["esplora", "serde", "log"] -esplora = ["bp-esplora", "bp-wallet/esplora"] +default = ["esplora_blocking"] +all = ["esplora_blocking", "serde", "log"] +esplora_blocking = ["bp-esplora", "bp-wallet/esplora"] serde = ["serde_crate", "serde_with", "serde_yaml", "bp-std/serde", "bp-wallet/serde", "descriptors/serde", "rgb-psbt/serde"] [package.metadata.docs.rs] diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 2354bc1..3b8ab77 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -27,7 +27,7 @@ bp-wallet = { workspace = true } bp-util = { workspace = true } psbt = { workspace = true } rgb-std = { workspace = true, features = ["serde"] } -rgb-runtime = { version = "0.11.0-beta.1", path = "..", features = ["esplora", "log", "serde"] } +rgb-runtime = { version = "0.11.0-beta.1", path = "..", features = ["esplora_blocking", "log", "serde"] } log = { workspace = true } env_logger = "0.10.1" clap = { version = "4.4.8", features = ["derive", "env"] } diff --git a/cli/src/args.rs b/cli/src/args.rs index 32be798..caad6f8 100644 --- a/cli/src/args.rs +++ b/cli/src/args.rs @@ -23,7 +23,8 @@ use bp_util::{Config, DescriptorOpts}; use bpstd::{Wpkh, XpubDerivable}; -use rgb_rt::{Resolver, ResolverError, RgbDescr, Runtime, RuntimeError, TapretKey}; +use rgb_rt::esplora_blocking::{Resolver, ResolverError}; +use rgb_rt::{RgbDescr, Runtime, RuntimeError, TapretKey}; use crate::Command; diff --git a/src/lib.rs b/src/lib.rs index 5897306..046dc1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,11 +31,9 @@ extern crate serde_with; mod runtime; mod descriptor; mod pay; -#[cfg(feature = "esplora")] -mod resolver; +mod resolvers; pub use descriptor::{DescriptorRgb, RgbDescr, RgbKeychain, TapTweakAlreadyAssigned, TapretKey}; pub use pay::{CompletionError, CompositionError, PayError, TransferParams}; -#[cfg(feature = "esplora")] -pub use resolver::{AnchorResolverError, Resolver, ResolverError}; +pub use resolvers::*; pub use runtime::{ContractOutpointsFilter, Runtime, RuntimeError}; diff --git a/src/resolver.rs b/src/resolvers/esplora_blocking.rs similarity index 100% rename from src/resolver.rs rename to src/resolvers/esplora_blocking.rs diff --git a/src/resolvers/mod.rs b/src/resolvers/mod.rs new file mode 100644 index 0000000..cc54841 --- /dev/null +++ b/src/resolvers/mod.rs @@ -0,0 +1,23 @@ +// RGB smart contracts for Bitcoin & Lightning +// +// SPDX-License-Identifier: Apache-2.0 +// +// Written in 2019-2023 by +// Dr Maxim Orlovsky +// +// Copyright (C) 2019-2023 LNP/BP Standards Association. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#[cfg(feature = "esplora_blocking")] +pub mod esplora_blocking; diff --git a/src/runtime.rs b/src/runtime.rs index 11c1451..f61e745 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -103,7 +103,7 @@ pub enum RuntimeError { #[from(bpwallet::LoadError)] Bp(bpwallet::RuntimeError), - #[cfg(feature = "esplora")] + #[cfg(feature = "esplora_blocking")] #[from] Esplora(esplora::Error), From dde9173aac8449c551b78ff4e13fe885d9efc38b Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sat, 20 Jan 2024 17:18:20 +0100 Subject: [PATCH 2/5] descriptor: make RgbKeychain trait and not an independent struct --- Cargo.lock | 286 ++++++++++++++++++++------------------------- Cargo.toml | 12 +- cli/src/command.rs | 8 +- src/descriptor.rs | 80 ++++--------- src/pay.rs | 4 +- 5 files changed, 167 insertions(+), 223 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 75d115b..bda5cb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,9 +125,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.5" +version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" dependencies = [ "anstyle", "anstyle-parse", @@ -233,9 +233,9 @@ checksum = "6107fe1be6682a68940da878d9e9f5e90ca5745b3dec9fd1bb393c8777d4f581" [[package]] name = "base64" -version = "0.21.5" +version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" [[package]] name = "base85" @@ -282,9 +282,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.4.1" +version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" +checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" [[package]] name = "blake3" @@ -311,13 +311,12 @@ dependencies = [ [[package]] name = "bp-consensus" version = "0.11.0-beta.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "190ac89a2a3c79d5bfb677f48e5393691832c540973341831572edaffdce0881" +source = "git+https://github.com/BP-WG/bp-core?branch=v0.11#d90f5774e3b8d41cb97bb2f1113d8c00351d2291" dependencies = [ "amplify", "chrono", "commit_verify", - "secp256k1 0.28.0", + "secp256k1", "serde", "strict_encoding", "strict_types", @@ -326,40 +325,39 @@ dependencies = [ [[package]] name = "bp-core" version = "0.11.0-beta.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69269d27e32d784e37f7ca7cdf964fe5e30c5bff1ca2e229a118c84c8efdd179" +source = "git+https://github.com/BP-WG/bp-core?branch=v0.11#d90f5774e3b8d41cb97bb2f1113d8c00351d2291" dependencies = [ "amplify", "bp-consensus", "bp-dbc", "bp-seals", "commit_verify", + "getrandom", "serde", "single_use_seals", "strict_encoding", "strict_types", + "wasm-bindgen", ] [[package]] name = "bp-dbc" version = "0.11.0-beta.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cef9a98c7ae4f1bb333ad8e55c9d8a91f071c347d35b62e19959faa07572b11" +source = "git+https://github.com/BP-WG/bp-core?branch=v0.11#d90f5774e3b8d41cb97bb2f1113d8c00351d2291" dependencies = [ "amplify", "base85", "bp-consensus", "commit_verify", - "secp256k1 0.28.0", + "secp256k1", "serde", "strict_encoding", ] [[package]] name = "bp-derive" -version = "0.11.0-beta.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "497e2b7324e0c7a65bdaabccd0fc7d05b003cca4ae3f3a1520e023aab3a9a24f" +version = "0.11.0-beta.3.1" +source = "git+https://github.com/BP-WG/bp-std?branch=v0.11#b37cdba13786091e6e153add0c2beda9c6650ea1" dependencies = [ "amplify", "bitcoin_hashes", @@ -388,9 +386,8 @@ dependencies = [ [[package]] name = "bp-invoice" -version = "0.11.0-beta.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a40b7fb87b58b2371b7b3b010568baa9ba463007707f9af9bb9b77a63d77e8f" +version = "0.11.0-beta.3.1" +source = "git+https://github.com/BP-WG/bp-std?branch=v0.11#b37cdba13786091e6e153add0c2beda9c6650ea1" dependencies = [ "amplify", "bech32", @@ -402,8 +399,7 @@ dependencies = [ [[package]] name = "bp-seals" version = "0.11.0-beta.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d211bb77e320abf4bba6c272fc85d73e5203140ca39e299271f4714d8b6029fe" +source = "git+https://github.com/BP-WG/bp-core?branch=v0.11#d90f5774e3b8d41cb97bb2f1113d8c00351d2291" dependencies = [ "amplify", "baid58", @@ -418,9 +414,8 @@ dependencies = [ [[package]] name = "bp-std" -version = "0.11.0-beta.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d6847d1e7359ed49947092fd319433c82b9cced1a2d780a9415d212770cf93c" +version = "0.11.0-beta.3.1" +source = "git+https://github.com/BP-WG/bp-std?branch=v0.11#b37cdba13786091e6e153add0c2beda9c6650ea1" dependencies = [ "amplify", "bp-consensus", @@ -457,8 +452,7 @@ dependencies = [ [[package]] name = "bp-wallet" version = "0.11.0-beta.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2a0baa9e09b52085f7d1cd27df92395e06a8016a8ccb8cdc68556ba2b5ac831" +source = "git+https://github.com/BP-WG/bp-wallet?branch=v0.11#e8aa19d8475b14392ae7f1d9dfad1f41e0690056" dependencies = [ "amplify", "bp-esplora", @@ -513,7 +507,7 @@ checksum = "45565fc9416b9896014f5732ac776f810ee53a66730c17e4020c3ec064a8f88f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -533,9 +527,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.12" +version = "4.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcfab8ba68f3668e89f6ff60f5b205cea56aa7b769451a59f34b8682f51c056d" +checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" dependencies = [ "clap_builder", "clap_derive", @@ -543,9 +537,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.4.12" +version = "4.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb7fb5e4e979aec3be7791562fcba452f94ad85e954da024396433e0e25a79e9" +checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" dependencies = [ "anstream", "anstyle", @@ -562,7 +556,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -640,9 +634,9 @@ checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" [[package]] name = "cpufeatures" -version = "0.2.11" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce420fe07aecd3e67c5f910618fe65e94158f6dcc0adf44e00d69ce2bdfe0fd0" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" dependencies = [ "libc", ] @@ -693,7 +687,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -704,14 +698,14 @@ checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] name = "deranged" -version = "0.3.10" +version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8eb30d70a07a3b04884d2677f06bec33509dc67ca60d92949e5535352d3191dc" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" dependencies = [ "powerfmt", "serde", @@ -719,9 +713,8 @@ dependencies = [ [[package]] name = "descriptors" -version = "0.11.0-beta.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58cfb07301f4982a5470167155c186586871d21693a791078cfa15c173657626" +version = "0.11.0-beta.3.1" +source = "git+https://github.com/BP-WG/bp-std?branch=v0.11#b37cdba13786091e6e153add0c2beda9c6650ea1" dependencies = [ "amplify", "bp-derive", @@ -777,9 +770,9 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.10.1" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece" +checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" dependencies = [ "humantime", "is-terminal", @@ -910,9 +903,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.11" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" dependencies = [ "cfg-if", "js-sys", @@ -929,9 +922,9 @@ checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" [[package]] name = "h2" -version = "0.3.22" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d6250322ef6e60f93f9a2162799302cd6f68f79f6e5d85c8c16f14d1d958178" +checksum = "bb2c4422095b67ee78da96fbb51a4cc413b3b25883c7717ff7ca1ab31022c9c9" dependencies = [ "bytes", "fnv", @@ -975,9 +968,9 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" +checksum = "5d3d0e0f38255e7fa3cf31335b3a56f05febd18025f4db5ef7a0cfb4f8da651f" [[package]] name = "hex" @@ -1070,9 +1063,9 @@ dependencies = [ [[package]] name = "iana-time-zone" -version = "0.1.58" +version = "0.1.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8326b86b6cff230b97d0d312a6c40a60726df3332e721f72a1b035f451663b20" +checksum = "b6a67363e2aa4443928ce15e57ebae94fd8949958fd1223c4cfc0cd473ad7539" dependencies = [ "android_system_properties", "core-foundation-sys", @@ -1154,9 +1147,9 @@ checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" [[package]] name = "js-sys" -version = "0.3.66" +version = "0.3.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca" +checksum = "9a1d36f1235bc969acba30b7f5990b864423a6068a10f7c90ae8f0112e3a59d1" dependencies = [ "wasm-bindgen", ] @@ -1169,9 +1162,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.151" +version = "0.2.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" +checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" [[package]] name = "libredox" @@ -1179,16 +1172,16 @@ version = "0.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8" dependencies = [ - "bitflags 2.4.1", + "bitflags 2.4.2", "libc", "redox_syscall", ] [[package]] name = "linux-raw-sys" -version = "0.4.12" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" +checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" [[package]] name = "log" @@ -1282,11 +1275,11 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "openssl" -version = "0.10.62" +version = "0.10.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cde4d2d9200ad5909f8dac647e29482e07c3a35de8a13fce7c9c7747ad9f671" +checksum = "15c9d69dd87a29568d4d017cfe8ec518706046a05184e5aea92d0af890b803c8" dependencies = [ - "bitflags 2.4.1", + "bitflags 2.4.2", "cfg-if", "foreign-types", "libc", @@ -1303,7 +1296,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -1314,9 +1307,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.98" +version = "0.9.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1665caf8ab2dc9aef43d1c0023bd904633a6a05cb30b0ad59bec2ae986e57a7" +checksum = "22e1bf214306098e4832460f797824c05d25aacdf896f64a985fb0fd992454ae" dependencies = [ "cc", "libc", @@ -1356,9 +1349,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkg-config" -version = "0.3.28" +version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69d3587f8a9e599cc7ec2c00e331f71c4e69a5f9a4b8a6efd5b07466b9736f9a" +checksum = "2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb" [[package]] name = "powerfmt" @@ -1374,9 +1367,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.71" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8" +checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" dependencies = [ "unicode-ident", ] @@ -1384,8 +1377,7 @@ dependencies = [ [[package]] name = "psbt" version = "0.11.0-beta.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d1f67ff8093b5ca985100452af2dd181aa5b29bc635792f4101750250674fc1" +source = "git+https://github.com/BP-WG/bp-std?branch=v0.11#b37cdba13786091e6e153add0c2beda9c6650ea1" dependencies = [ "amplify", "base64", @@ -1401,9 +1393,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -1529,7 +1521,7 @@ dependencies = [ [[package]] name = "rgb-core" version = "0.11.0-beta.4" -source = "git+https://github.com/RGB-WG/rgb-core?branch=v0.11#2b59903039770df4766c9681bc74691382308ef5" +source = "git+https://github.com/RGB-WG/rgb-core?branch=v0.11#6e0cc2d73fa2527a9f31d4f498973d09400b959e" dependencies = [ "aluvm", "amplify", @@ -1549,7 +1541,7 @@ dependencies = [ [[package]] name = "rgb-invoice" version = "0.11.0-beta.4" -source = "git+https://github.com/RGB-WG/rgb-std?branch=v0.11#9db17e1a6b08d3817ffeebdfb85a4b405818fec4" +source = "git+https://github.com/RGB-WG/rgb-std?branch=v0.11#9933cb200fa91a5464ee0e991f4d493e8d8625ef" dependencies = [ "amplify", "baid58", @@ -1618,7 +1610,7 @@ dependencies = [ [[package]] name = "rgb-std" version = "0.11.0-beta.4" -source = "git+https://github.com/RGB-WG/rgb-std?branch=v0.11#9db17e1a6b08d3817ffeebdfb85a4b405818fec4" +source = "git+https://github.com/RGB-WG/rgb-std?branch=v0.11#9933cb200fa91a5464ee0e991f4d493e8d8625ef" dependencies = [ "amplify", "baid58", @@ -1693,11 +1685,11 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.28" +version = "0.38.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316" +checksum = "322394588aaf33c24007e8bb3238ee3e4c5c09c084ab32bc73890b99ff326bca" dependencies = [ - "bitflags 2.4.1", + "bitflags 2.4.2", "errno", "libc", "linux-raw-sys", @@ -1759,65 +1751,45 @@ dependencies = [ [[package]] name = "secp256k1" -version = "0.27.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f" -dependencies = [ - "rand", - "secp256k1-sys 0.8.1", - "serde", -] - -[[package]] -name = "secp256k1" -version = "0.28.0" +version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2acea373acb8c21ecb5a23741452acd2593ed44ee3d343e72baaa143bc89d0d5" +checksum = "3f622567e3b4b38154fb8190bcf6b160d7a4301d70595a49195b48c116007a27" dependencies = [ "rand", - "secp256k1-sys 0.9.1", + "secp256k1-sys", "serde", ] [[package]] name = "secp256k1-sys" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70a129b9e9efbfb223753b9163c4ab3b13cff7fd9c7f010fbac25ab4099fa07e" -dependencies = [ - "cc", -] - -[[package]] -name = "secp256k1-sys" -version = "0.9.1" +version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dd97a086ec737e30053fd5c46f097465d25bb81dd3608825f65298c4c98be83" +checksum = "e5d1746aae42c19d583c3c1a8c646bfad910498e2051c551a7f2e3c0c9fbb7eb" dependencies = [ "cc", ] [[package]] name = "secp256k1-zkp" -version = "0.9.2" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "026efcdacb95ee6aae5cc19144dc1549973eac36a4972700c28493de1ee5d69f" +checksum = "c4e48ef9c98bfbcb98bd15693ffa19676cb3e29426b75eda8b73c05cdd7959f8" dependencies = [ "bitcoin-private", "rand", - "secp256k1 0.27.0", + "secp256k1", "secp256k1-zkp-sys", "serde", ] [[package]] name = "secp256k1-zkp-sys" -version = "0.8.1" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d03ab1ca75a18e1899e8d9b8d28b5998ae1ddcb42fec5956769718543293c723" +checksum = "b4ead52f43074bae2ddbd1e0e66e6b170135e76117f5ea9916f33d7bd0b36e29" dependencies = [ "cc", - "secp256k1-sys 0.8.1", + "secp256k1-sys", ] [[package]] @@ -1845,29 +1817,29 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" +checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.193" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" +checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] name = "serde_json" -version = "1.0.108" +version = "1.0.111" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" +checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4" dependencies = [ "itoa", "ryu", @@ -1931,14 +1903,14 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] name = "serde_yaml" -version = "0.9.29" +version = "0.9.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a15e0ef66bf939a7c890a0bf6d5a733c70202225f9888a89ed5c62298b019129" +checksum = "b1bf28c79a99f70ee1f1d83d10c875d2e70618417fda01ad1785e027579d9d38" dependencies = [ "indexmap 2.1.0", "itoa", @@ -2085,9 +2057,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.43" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", @@ -2130,31 +2102,31 @@ dependencies = [ [[package]] name = "termcolor" -version = "1.4.0" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" dependencies = [ "winapi-util", ] [[package]] name = "thiserror" -version = "1.0.52" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83a48fd946b02c0a526b2e9481c8e2a17755e47039164a86c4070446e3a4614d" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.52" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7fbe9b594d6568a6a1443250a7e67d80b74e1e96f6d1715e1e21cc1888291d3" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] @@ -2325,9 +2297,9 @@ checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "unicode-bidi" -version = "0.3.14" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f2528f27a9eb2b21e69c95319b30bd0efd85d09c379741b0f78ea1d86be2416" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" [[package]] name = "unicode-ident" @@ -2421,9 +2393,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" +checksum = "b1223296a201415c7fad14792dbefaace9bd52b62d33453ade1c5b5f07555406" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -2431,24 +2403,24 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" +checksum = "fcdc935b63408d58a32f8cc9738a0bffd8f05cc7c002086c6ef20b7312ad9dcd" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-futures" -version = "0.4.39" +version = "0.4.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac36a15a220124ac510204aec1c3e5db8a22ab06fd6706d881dc6149f8ed9a12" +checksum = "bde2032aeb86bdfaecc8b261eef3cba735cc426c1f3a3416d1e0791be95fc461" dependencies = [ "cfg-if", "js-sys", @@ -2458,9 +2430,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" +checksum = "3e4c238561b2d428924c49815533a8b9121c664599558a5d9ec51f8a1740a999" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -2468,28 +2440,28 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" +checksum = "bae1abb6806dc1ad9e560ed242107c0f6c84335f1749dd4e8ddb012ebd5e25a7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" -version = "0.2.89" +version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" +checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b" [[package]] name = "wasm-bindgen-test" -version = "0.3.39" +version = "0.3.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cf9242c0d27999b831eae4767b2a146feb0b27d332d553e605864acd2afd403" +checksum = "139bd73305d50e1c1c4333210c0db43d989395b64a237bd35c10ef3832a7f70c" dependencies = [ "console_error_panic_hook", "js-sys", @@ -2501,20 +2473,20 @@ dependencies = [ [[package]] name = "wasm-bindgen-test-macro" -version = "0.3.39" +version = "0.3.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "794645f5408c9a039fd09f4d113cdfb2e7eba5ff1956b07bcf701cf4b394fe89" +checksum = "70072aebfe5da66d2716002c729a14e4aec4da0e23cc2ea66323dac541c93928" dependencies = [ "proc-macro2", "quote", - "syn 2.0.43", + "syn 2.0.48", ] [[package]] name = "web-sys" -version = "0.3.66" +version = "0.3.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50c24a44ec86bb68fbecd1b3efed7e85ea5621b39b35ef2766b66cd984f8010f" +checksum = "58cd2333b6e0be7a39605f0e255892fd7418a682d8da8fe042fe25128794d2ed" dependencies = [ "js-sys", "wasm-bindgen", @@ -2559,11 +2531,11 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-core" -version = "0.51.1" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.48.5", + "windows-targets 0.52.0", ] [[package]] @@ -2700,9 +2672,9 @@ checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" [[package]] name = "winnow" -version = "0.5.31" +version = "0.5.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97a4882e6b134d6c28953a387571f1acdd3496830d5e36c5e3a1075580ea641c" +checksum = "b7cf47b659b318dccbd69cc4797a39ae128f533dce7902a1096044d1967b9c16" dependencies = [ "memchr", ] diff --git a/Cargo.toml b/Cargo.toml index 74a0218..73d2e39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ strict_encoding = "2.6.2" strict_types = "1.6.3" bp-core = "0.11.0-beta.3" bp-seals = "0.11.0-beta.3" -bp-std = "0.11.0-beta.3" +bp-std = "0.11.0-beta.3.1" bp-wallet = "0.11.0-beta.4" bp-util = "0.11.0-beta.4" bp-esplora = "0.11.0-beta.2" @@ -93,6 +93,16 @@ serde = ["serde_crate", "serde_with", "serde_yaml", "bp-std/serde", "bp-wallet/s features = [ "all" ] [patch.crates-io] +bp-consensus = { git = "https://github.com/BP-WG/bp-core", branch = "v0.11" } +bp-dbc = { git = "https://github.com/BP-WG/bp-core", branch = "v0.11" } +bp-seals = { git = "https://github.com/BP-WG/bp-core", branch = "v0.11" } +bp-core = { git = "https://github.com/BP-WG/bp-core", branch = "v0.11" } +bp-invoice = { git = "https://github.com/BP-WG/bp-std", branch = "v0.11" } +bp-derive = { git = "https://github.com/BP-WG/bp-std", branch = "v0.11" } +descriptors = { git = "https://github.com/BP-WG/bp-std", branch = "v0.11" } +psbt = { git = "https://github.com/BP-WG/bp-std", branch = "v0.11" } +bp-std = { git = "https://github.com/BP-WG/bp-std", branch = "v0.11" } +bp-wallet = { git = "https://github.com/BP-WG/bp-wallet", branch = "v0.11" } rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "v0.11" } rgb-invoice = { git = "https://github.com/RGB-WG/rgb-std", branch = "v0.11" } rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "v0.11" } diff --git a/cli/src/command.rs b/cli/src/command.rs index 9ee4179..bf719a5 100644 --- a/cli/src/command.rs +++ b/cli/src/command.rs @@ -26,7 +26,7 @@ use std::str::FromStr; use amplify::confinement::U16; use bp_util::{BpCommand, Config, Exec}; -use bpstd::Sats; +use bpstd::{Keychain, Sats}; use psbt::{Psbt, PsbtVer}; use rgb_rt::{DescriptorRgb, RgbKeychain, RuntimeError, TransferParams}; use rgbstd::containers::{Bindle, BuilderSeal, Transfer, UniversalBindle}; @@ -664,16 +664,14 @@ impl Exec for RgbArgs { let outpoint = runtime .wallet() - .coinselect(Sats::ZERO, |utxo| { - RgbKeychain::contains_rgb(utxo.terminal.keychain) - }) + .coinselect(Sats::ZERO, |utxo| utxo.terminal.keychain.is_rgb()) .next(); let network = runtime.wallet().network(); let beneficiary = match (address_based, outpoint) { (true, _) | (false, None) => { let addr = runtime .wallet() - .addresses(RgbKeychain::Rgb) + .addresses(Keychain::RGB) .next() .expect("no addresses left") .addr; diff --git a/src/descriptor.rs b/src/descriptor.rs index 75107e2..41c4a4d 100644 --- a/src/descriptor.rs +++ b/src/descriptor.rs @@ -20,17 +20,14 @@ // limitations under the License. use std::collections::{BTreeSet, HashMap}; -use std::str::FromStr; use std::{iter, vec}; -use amplify::Wrapper; use bp::dbc::tapret::TapretCommitment; use bp::dbc::Method; use bp::seals::txout::CloseMethod; use bpstd::{ - CompressedPk, Derive, DeriveCompr, DeriveSet, DeriveXOnly, DerivedScript, Idx, IdxBase, - IndexError, IndexParseError, KeyOrigin, Keychain, NormalIndex, TapDerivation, TapScript, - TapTree, Terminal, XOnlyPk, XpubDerivable, XpubSpec, + CompressedPk, Derive, DeriveCompr, DeriveSet, DeriveXOnly, DerivedScript, KeyOrigin, Keychain, + NormalIndex, TapDerivation, TapScript, TapTree, Terminal, XOnlyPk, XpubDerivable, XpubSpec, }; use commit_verify::CommitVerify; use descriptors::{Descriptor, SpkClass, StdDescr, TrKey, Wpkh}; @@ -49,63 +46,35 @@ pub trait DescriptorRgb: Descriptor { ) -> Result<(), TapTweakAlreadyAssigned>; } -#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)] -#[derive(Serialize, Deserialize)] -#[serde(crate = "serde_crate", rename_all = "camelCase")] -#[repr(u8)] -pub enum RgbKeychain { - #[display("0", alt = "0")] - External = 0, +mod private { + pub trait Sealed {} + impl Sealed for super::Keychain {} +} - #[display("1", alt = "1")] - Internal = 1, +pub trait RgbKeychain: private::Sealed + Sized { + const RGB: Self; + const TAPRET: Self; + const RGB_ALL: [Self; 2] = [Self::RGB, Self::TAPRET]; - #[display("9", alt = "9")] - Rgb = 9, + fn is_rgb(&self) -> bool; - #[display("10", alt = "10")] - Tapret = 10, + fn for_method(method: Method) -> Keychain; } -impl RgbKeychain { - pub const RGB_ALL: [RgbKeychain; 2] = [RgbKeychain::Rgb, RgbKeychain::Tapret]; +impl RgbKeychain for Keychain { + const RGB: Keychain = Keychain::with(9); + const TAPRET: Keychain = Keychain::with(10); - pub fn contains_rgb(keychain: impl Into) -> bool { - let k = keychain.into().into_inner(); - k == Self::Rgb as u8 || k == Self::Tapret as u8 - } - pub fn is_seal(self) -> bool { self == Self::Rgb || self == Self::Tapret } + fn is_rgb(&self) -> bool { *self == Self::RGB || *self == Self::TAPRET } - pub const fn for_method(method: Method) -> Self { + fn for_method(method: Method) -> Keychain { match method { - Method::OpretFirst => Self::Rgb, - Method::TapretFirst => Self::Tapret, + Method::OpretFirst => Self::RGB, + Method::TapretFirst => Self::TAPRET, } } } -impl FromStr for RgbKeychain { - type Err = IndexParseError; - - fn from_str(s: &str) -> Result { - match NormalIndex::from_str(s)? { - NormalIndex::ZERO => Ok(RgbKeychain::External), - NormalIndex::ONE => Ok(RgbKeychain::Internal), - val => Err(IndexError { - what: "non-standard keychain", - invalid: val.index(), - start: 0, - end: 1, - } - .into()), - } - } -} - -impl From for Keychain { - fn from(keychain: RgbKeychain) -> Self { Keychain::from(keychain as u8) } -} - #[serde_as] #[derive(Clone, Eq, PartialEq, Debug)] #[derive(Serialize, Deserialize)] @@ -129,15 +98,10 @@ impl TapretKey { impl Derive for TapretKey { #[inline] - fn default_keychain(&self) -> Keychain { RgbKeychain::Rgb.into() } + fn default_keychain(&self) -> Keychain { Keychain::RGB } fn keychains(&self) -> BTreeSet { - bset![ - RgbKeychain::External.into(), - RgbKeychain::Internal.into(), - RgbKeychain::Rgb.into(), - RgbKeychain::Tapret.into(), - ] + bset![Keychain::OUTER, Keychain::INNER, Keychain::RGB, Keychain::TAPRET,] } fn derive( @@ -149,7 +113,7 @@ impl Derive for TapretKey { let index = index.into(); let terminal = Terminal::new(keychain, index); let internal_key = self.internal_key.derive(keychain, index); - if keychain.into_inner() == RgbKeychain::Tapret as u8 { + if keychain == Keychain::TAPRET { if let Some(tweak) = self.tweaks.get(&terminal) { let script_commitment = TapScript::commit(tweak); let tap_tree = TapTree::with_single_leaf(script_commitment); diff --git a/src/pay.rs b/src/pay.rs index a753507..4a3f734 100644 --- a/src/pay.rs +++ b/src/pay.rs @@ -25,7 +25,7 @@ use amplify::confinement::Confined; use bp::dbc::tapret::TapretProof; use bp::seals::txout::{CloseMethod, ExplicitSeal}; use bp::{Outpoint, Sats, ScriptPubkey, Vout}; -use bpstd::Address; +use bpstd::{Address, Keychain}; use bpwallet::{Beneficiary as BpBeneficiary, ConstructionError, PsbtMeta, TxParams}; use psbt::{CommitError, EmbedError, Psbt, RgbPsbt, TapretKeyError}; use rgbstd::containers::{Bindle, Transfer}; @@ -240,7 +240,7 @@ impl Runtime { .iter() .map(|o| o.as_reduced_unsafe()) .map(|o| Outpoint::new(o.txid, o.vout)); - params.tx.change_keychain = RgbKeychain::for_method(method).into(); + params.tx.change_keychain = Keychain::for_method(method); let (mut psbt, mut meta) = self.wallet_mut() .construct_psbt(outpoints, &beneficiaries, params.tx)?; From d69addc2fa9b5bd7fb17eecb67e56b4ec3eff699 Mon Sep 17 00:00:00 2001 From: Nicola Busanello Date: Thu, 25 Jan 2024 12:17:46 +0100 Subject: [PATCH 3/5] fix opret support in construct_psbt() --- src/pay.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/pay.rs b/src/pay.rs index 4a3f734..2d62fa8 100644 --- a/src/pay.rs +++ b/src/pay.rs @@ -251,12 +251,10 @@ impl Runtime { } else { None }; - let output = psbt - .outputs_mut() + psbt.outputs_mut() .find(|o| o.script.is_p2tr() && Some(&o.script) != beneficiary_script.as_ref()) - .ok_or(CompositionError::TapretRequired)?; + .map(|o| o.set_tapret_host().expect("just created")); // TODO: Add descriptor id to the tapret host data - output.set_tapret_host().expect("just created"); let change_script = meta .change_vout From fddfdb3c3b09ab6052a3e2eb31eb9211f17a5f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zoe=20Faltib=C3=A0?= Date: Tue, 23 Jan 2024 17:04:18 +0100 Subject: [PATCH 4/5] add electrum Resolver --- Cargo.lock | 53 ++++++++++- Cargo.toml | 7 +- src/resolvers/electrum.rs | 182 ++++++++++++++++++++++++++++++++++++++ src/resolvers/mod.rs | 2 + 4 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 src/resolvers/electrum.rs diff --git a/Cargo.lock b/Cargo.lock index bda5cb4..4dcde46 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -252,11 +252,35 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" +[[package]] +name = "bech32" +version = "0.10.0-beta" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98f7eed2b2781a6f0b5c903471d48e15f56fb4e1165df8a9a2337fd1a59d45ea" + +[[package]] +name = "bitcoin" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd00f3c09b5f21fb357abe32d29946eb8bb7a0862bae62c0b5e4a692acbbe73c" +dependencies = [ + "bech32 0.10.0-beta", + "bitcoin-internals", + "bitcoin_hashes", + "hex-conservative", + "hex_lit", + "secp256k1", + "serde", +] + [[package]] name = "bitcoin-internals" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9425c3bf7089c983facbae04de54513cce73b41c7f9ff8c845b54e7bc64ebbfb" +dependencies = [ + "serde", +] [[package]] name = "bitcoin-private" @@ -272,6 +296,7 @@ checksum = "1930a4dabfebb8d7d9992db18ebe3ae2876f0a305fab206fd168df931ede293b" dependencies = [ "bitcoin-internals", "hex-conservative", + "serde", ] [[package]] @@ -390,7 +415,7 @@ version = "0.11.0-beta.3.1" source = "git+https://github.com/BP-WG/bp-std?branch=v0.11#b37cdba13786091e6e153add0c2beda9c6650ea1" dependencies = [ "amplify", - "bech32", + "bech32 0.9.1", "bitcoin_hashes", "bp-consensus", "serde", @@ -759,6 +784,23 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" +[[package]] +name = "electrum-client" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89008f106be6f303695522f2f4c1f28b40c3e8367ed8b3bb227f1f882cb52cc2" +dependencies = [ + "bitcoin", + "byteorder", + "libc", + "log", + "rustls", + "serde", + "serde_json", + "webpki-roots", + "winapi", +] + [[package]] name = "encoding_rs" version = "0.8.33" @@ -984,6 +1026,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30ed443af458ccb6d81c1e7e661545f94d3176752fb1df2f543b902a1e0f51e2" +[[package]] +name = "hex_lit" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd" + [[package]] name = "http" version = "0.2.11" @@ -1589,6 +1637,7 @@ version = "0.11.0-beta.3" dependencies = [ "amplify", "baid58", + "bitcoin", "bp-core", "bp-esplora", "bp-std", @@ -1596,6 +1645,7 @@ dependencies = [ "chrono", "commit_verify", "descriptors", + "electrum-client", "indexmap 2.1.0", "log", "rgb-persist-fs", @@ -1755,6 +1805,7 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f622567e3b4b38154fb8190bcf6b160d7a4301d70595a49195b48c116007a27" dependencies = [ + "bitcoin_hashes", "rand", "secp256k1-sys", "serde", diff --git a/Cargo.toml b/Cargo.toml index 73d2e39..809979e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ license = "Apache-2.0" [workspace.dependencies] amplify = "4.5.0" baid58 = "0.4.4" +bitcoin = "0.31.1" commit_verify = "0.11.0-beta.3" strict_encoding = "2.6.2" strict_types = "1.6.3" @@ -36,6 +37,7 @@ bp-wallet = "0.11.0-beta.4" bp-util = "0.11.0-beta.4" bp-esplora = "0.11.0-beta.2" descriptors = "0.11.0-beta.3" +electrum-client = "0.19.0" psbt = { version = "0.11.0-beta.3", features = ["client-side-validation"] } rgb-std = { version = "0.11.0-beta.3", features = ["fs"] } rgb-psbt = { version = "0.11.0-beta.3", path = "psbt" } @@ -66,6 +68,7 @@ name = "rgb_rt" [dependencies] amplify = { workspace = true } baid58 = { workspace = true } +bitcoin = { workspace = true, optional = true } commit_verify = { workspace = true } strict_types = { workspace = true } bp-core = { workspace = true } @@ -73,6 +76,7 @@ bp-std = { workspace = true } bp-wallet = { workspace = true, features = ["fs"] } bp-esplora = { workspace = true, optional = true } descriptors = { workspace = true } +electrum-client = { workspace = true, optional = true } rgb-std = { workspace = true } rgb-psbt = { workspace = true } rgb-persist-fs = { version = "0.11.0", path = "fs" } @@ -85,8 +89,9 @@ log = { workspace = true, optional = true } [features] default = ["esplora_blocking"] -all = ["esplora_blocking", "serde", "log"] +all = ["esplora_blocking", "electrum", "serde", "log"] esplora_blocking = ["bp-esplora", "bp-wallet/esplora"] +electrum = ["electrum-client", "bitcoin"] serde = ["serde_crate", "serde_with", "serde_yaml", "bp-std/serde", "bp-wallet/serde", "descriptors/serde", "rgb-psbt/serde"] [package.metadata.docs.rs] diff --git a/src/resolvers/electrum.rs b/src/resolvers/electrum.rs new file mode 100644 index 0000000..49b33cb --- /dev/null +++ b/src/resolvers/electrum.rs @@ -0,0 +1,182 @@ +// RGB smart contracts for Bitcoin & Lightning +// +// SPDX-License-Identifier: Apache-2.0 +// +// Written in 2024 by +// Zoe FaltibĂ  +// +// Copyright (C) 2024 LNP/BP Standards Association. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::collections::HashMap; + +use amplify::ByteArray; +use bitcoin::hashes::Hash; +use bitcoin::Txid as BitcoinTxid; +use bp::ConsensusDecode; +use bpstd::{Tx, Txid}; +use electrum_client::{Client, ElectrumApi, Param}; +use rgbstd::containers::Consignment; +use rgbstd::resolvers::ResolveHeight; +use rgbstd::validation::{ResolveWitness, WitnessResolverError}; +use rgbstd::{WitnessAnchor, WitnessId, WitnessOrd, WitnessPos, XAnchor, XPubWitness}; + +pub struct Resolver { + electrum_client: Client, + terminal_txes: HashMap, +} + +#[allow(clippy::large_enum_variant)] +#[derive(Debug, Display, Error, From)] +#[display(doc_comments)] +pub enum AnchorResolverError { + #[from] + #[display(inner)] + Error(electrum_client::Error), + + /// impossible conversion + ImpossibleConversion, + + /// invalid anchor {0} + InvalidAnchor(String), +} + +impl Resolver { + #[allow(clippy::result_large_err)] + pub fn new(url: &str) -> Result { + let electrum_client = Client::new(url)?; + Ok(Self { + electrum_client, + terminal_txes: none!(), + }) + } + + pub fn add_terminals(&mut self, consignment: &Consignment) { + self.terminal_txes.extend( + consignment + .terminals + .values() + .filter_map(|t| t.as_reduced_unsafe().tx.as_ref()) + .map(|tx| (tx.txid(), tx.clone())), + ); + } +} + +impl ResolveHeight for Resolver { + type Error = AnchorResolverError; + + fn resolve_anchor(&mut self, anchor: &XAnchor) -> Result { + let XAnchor::Bitcoin(anchor) = anchor else { + panic!("Liquid is not yet supported") + }; + let txid = anchor + .txid() + .ok_or(AnchorResolverError::InvalidAnchor(format!("{:#?}", anchor)))?; + + if self.terminal_txes.contains_key(&txid) { + return Ok(WitnessAnchor { + witness_ord: WitnessOrd::OffChain, + witness_id: WitnessId::Bitcoin(txid), + }); + } + + fn get_block_height(electrum_client: &Client) -> Result { + electrum_client + .block_headers_subscribe()? + .height + .try_into() + .map_err(|_| AnchorResolverError::ImpossibleConversion) + } + + let last_block_height_min = get_block_height(&self.electrum_client)?; + let tx_details = self.electrum_client.raw_call( + "blockchain.transaction.get", + vec![Param::String(txid.to_string()), Param::Bool(true)], + )?; + let witness_ord = if let Some(confirmations) = tx_details.get("confirmations") { + let confirmations = confirmations + .as_u64() + .ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?; + let last_block_height_max = get_block_height(&self.electrum_client)?; + let skew = confirmations - 1; + let mut tx_height: u32 = 0; + for height in (last_block_height_min - skew)..=(last_block_height_max - skew) { + if let Ok(h) = self.electrum_client.transaction_get_merkle( + &BitcoinTxid::from_byte_array(txid.to_byte_array()), + height + .try_into() + .map_err(|_| AnchorResolverError::ImpossibleConversion)?, + ) { + tx_height = h + .block_height + .try_into() + .map_err(|_| AnchorResolverError::ImpossibleConversion)?; + break; + } else { + continue; + } + } + let block_time = tx_details + .get("blocktime") + .ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))? + .as_i64() + .ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?; + WitnessOrd::OnChain( + WitnessPos::new(tx_height, block_time) + .ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?, + ) + } else { + WitnessOrd::OffChain + }; + + Ok(WitnessAnchor { + witness_ord, + witness_id: WitnessId::Bitcoin(txid), + }) + } +} + +impl ResolveWitness for Resolver { + fn resolve_pub_witness( + &self, + witness_id: WitnessId, + ) -> Result { + let WitnessId::Bitcoin(txid) = witness_id else { + panic!("Liquid is not yet supported"); + }; + + if let Some(tx) = self.terminal_txes.get(&txid) { + return Ok(XPubWitness::Bitcoin(tx.clone())); + } + + match self + .electrum_client + .transaction_get_raw(&BitcoinTxid::from_byte_array(txid.to_byte_array())) + { + Ok(raw_tx) => { + let tx = Tx::consensus_deserialize(raw_tx).map_err(|_| { + WitnessResolverError::Other(witness_id, s!("cannot deserialize raw TX")) + })?; + Ok(XPubWitness::Bitcoin(tx)) + } + Err(e) + if e.to_string() + .contains("No such mempool or blockchain transaction") => + { + Err(WitnessResolverError::Unknown(witness_id)) + } + Err(e) => Err(WitnessResolverError::Other(witness_id, e.to_string())), + } + } +} diff --git a/src/resolvers/mod.rs b/src/resolvers/mod.rs index cc54841..ecd7ae3 100644 --- a/src/resolvers/mod.rs +++ b/src/resolvers/mod.rs @@ -21,3 +21,5 @@ #[cfg(feature = "esplora_blocking")] pub mod esplora_blocking; +#[cfg(feature = "electrum")] +pub mod electrum; From bc4a561e589d18a4492dd28b5029a13cd4d5e9ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zoe=20Faltib=C3=A0?= Date: Tue, 30 Jan 2024 11:46:06 +0100 Subject: [PATCH 5/5] fix electrum Resolver --- src/resolvers/electrum.rs | 82 ++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 35 deletions(-) diff --git a/src/resolvers/electrum.rs b/src/resolvers/electrum.rs index 49b33cb..b9b2eb0 100644 --- a/src/resolvers/electrum.rs +++ b/src/resolvers/electrum.rs @@ -100,44 +100,56 @@ impl ResolveHeight for Resolver { } let last_block_height_min = get_block_height(&self.electrum_client)?; - let tx_details = self.electrum_client.raw_call( - "blockchain.transaction.get", - vec![Param::String(txid.to_string()), Param::Bool(true)], - )?; - let witness_ord = if let Some(confirmations) = tx_details.get("confirmations") { - let confirmations = confirmations - .as_u64() - .ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?; - let last_block_height_max = get_block_height(&self.electrum_client)?; - let skew = confirmations - 1; - let mut tx_height: u32 = 0; - for height in (last_block_height_min - skew)..=(last_block_height_max - skew) { - if let Ok(h) = self.electrum_client.transaction_get_merkle( - &BitcoinTxid::from_byte_array(txid.to_byte_array()), - height - .try_into() - .map_err(|_| AnchorResolverError::ImpossibleConversion)?, - ) { - tx_height = h - .block_height - .try_into() - .map_err(|_| AnchorResolverError::ImpossibleConversion)?; - break; + let witness_ord = match self + .electrum_client + .raw_call("blockchain.transaction.get", vec![ + Param::String(txid.to_string()), + Param::Bool(true), + ]) { + Ok(tx_details) => { + if let Some(confirmations) = tx_details.get("confirmations") { + let confirmations = confirmations + .as_u64() + .ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?; + let last_block_height_max = get_block_height(&self.electrum_client)?; + let skew = confirmations - 1; + let mut tx_height: u32 = 0; + for height in (last_block_height_min - skew)..=(last_block_height_max - skew) { + if let Ok(h) = self.electrum_client.transaction_get_merkle( + &BitcoinTxid::from_byte_array(txid.to_byte_array()), + height + .try_into() + .map_err(|_| AnchorResolverError::ImpossibleConversion)?, + ) { + tx_height = h + .block_height + .try_into() + .map_err(|_| AnchorResolverError::ImpossibleConversion)?; + break; + } else { + continue; + } + } + let block_time = tx_details + .get("blocktime") + .ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))? + .as_i64() + .ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?; + WitnessOrd::OnChain( + WitnessPos::new(tx_height, block_time) + .ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?, + ) } else { - continue; + WitnessOrd::OffChain } } - let block_time = tx_details - .get("blocktime") - .ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))? - .as_i64() - .ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?; - WitnessOrd::OnChain( - WitnessPos::new(tx_height, block_time) - .ok_or(electrum_client::Error::InvalidResponse(tx_details.clone()))?, - ) - } else { - WitnessOrd::OffChain + Err(e) + if e.to_string() + .contains("No such mempool or blockchain transaction") => + { + WitnessOrd::OffChain + } + Err(e) => return Err(e.into()), }; Ok(WitnessAnchor {