From 0c3f9af6a0f4b42246b1918b436746d0028c9f79 Mon Sep 17 00:00:00 2001 From: Nicola Busanello Date: Mon, 5 Dec 2022 16:07:25 +0100 Subject: [PATCH] psbt bundle: add close method + set tapret dsf path or opret_host if needed --- Cargo.lock | 1 + Cargo.toml | 1 + src/bin/rgb.rs | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fbdc2e20..59290603 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -956,6 +956,7 @@ version = "0.9.0-rc.1" dependencies = [ "amplify", "bitcoin", + "bitcoin_scripts", "bp-core", "clap", "commit_verify", diff --git a/Cargo.toml b/Cargo.toml index 532b2813..70e3c448 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ required-features = ["cli", "serde"] [dependencies] amplify = "3.13.0" +bitcoin_scripts = "0.9.0-rc.1" lnpbp_bech32 = "~0.9.0-rc.1" strict_encoding = { version = "~0.9.0-rc.2", features = ["crypto", "chrono", "bitcoin"] } commit_verify = "~0.9.0-rc.1" diff --git a/src/bin/rgb.rs b/src/bin/rgb.rs index 2d2c2a54..2efca9a1 100644 --- a/src/bin/rgb.rs +++ b/src/bin/rgb.rs @@ -24,6 +24,7 @@ use std::str::FromStr; use amplify::hex::{FromHex, ToHex}; use bitcoin::psbt::serialize::{Deserialize, Serialize}; use bitcoin::OutPoint; +use bitcoin_scripts::taproot::{DfsOrder, DfsPath}; use bp::seals::txout::CloseMethod; use clap::Parser; use commit_verify::ConsensusCommit; @@ -193,6 +194,10 @@ pub enum PsbtCommand { /// Output file to save the PSBT updated with state transition(s) /// information. If not given, the source PSBT file is overwritten. psbt_out: Option, + + /// Method for seal closing ('tapret1st' or 'opret1st') + #[clap(short, long, default_value = "tapret1st")] + method: CloseMethod, }, /// Analyze PSBT file and print out all RGB-related information from it @@ -382,11 +387,39 @@ fn main() -> Result<(), Error> { } }, Command::Psbt { subcommand } => match subcommand { - PsbtCommand::Bundle { psbt_in, psbt_out } => { + PsbtCommand::Bundle { + psbt_in, + psbt_out, + method, + } => { let psbt_bytes = fs::read(&psbt_in)?; let mut psbt = Psbt::deserialize(&psbt_bytes)?; - let count = psbt.rgb_bundle_to_lnpbp4()?; + let mut count: usize = 0; + match method { + CloseMethod::TapretFirst => { + if let Some(output) = + psbt.outputs.iter_mut().find(|o| o.script.is_v1_p2tr()) + { + if output.tapret_dfs_path().is_none() { + output.set_tapret_dfs_path(&DfsPath::with([&DfsOrder::Last]))?; + } + } + count = psbt.rgb_bundle_to_lnpbp4()?; + } + CloseMethod::OpretFirst => { + count = psbt.rgb_bundle_to_lnpbp4()?; + if let Some(output) = + psbt.outputs.iter_mut().find(|o| o.script.is_op_return()) + { + if !output.is_opret_host() { + output.set_opret_host()?; + } + } + } + _ => {} + }; + println!("Total {} bundles converted", count); let psbt_bytes = psbt.serialize();