From 57447ea4c978746b91ebfa6a2573c39c2fd72716 Mon Sep 17 00:00:00 2001 From: Satoshi Otomakan Date: Mon, 16 Dec 2024 18:23:03 +0100 Subject: [PATCH] [BTC]: Add Babylon Unbonding UTXO --- rust/Cargo.lock | 1 + rust/chains/tw_bitcoin/Cargo.toml | 1 + .../tw_bitcoin/src/babylon/conditions.rs | 47 ++++++------ .../src/babylon/covenant_committee.rs | 33 +++++++++ rust/chains/tw_bitcoin/src/babylon/mod.rs | 4 +- .../tw_bitcoin/src/babylon/spending_data.rs | 55 ++++++++++++++ .../{claims/mod.rs => spending_info.rs} | 43 +++++------ .../tw_bitcoin/src/babylon/tx_builder/mod.rs | 10 +++ .../src/babylon/tx_builder/output.rs | 45 ++++------- .../tw_bitcoin/src/babylon/tx_builder/utxo.rs | 68 ++++++++++++----- .../tw_bitcoin/src/modules/tx_builder/mod.rs | 6 +- .../src/modules/tx_builder/output_protobuf.rs | 20 ++--- .../src/modules/tx_builder/utxo_protobuf.rs | 74 ++++++++++++++++--- .../src/script/standard_script/claims.rs | 11 ++- rust/frameworks/tw_utxo/src/signature.rs | 1 + .../src/spending_data/standard_constructor.rs | 2 +- .../standard_transaction/builder/utxo.rs | 29 ++++++-- rust/tw_keypair/src/schnorr/public.rs | 11 ++- src/proto/BitcoinV2.proto | 26 +++++-- 19 files changed, 351 insertions(+), 136 deletions(-) create mode 100644 rust/chains/tw_bitcoin/src/babylon/covenant_committee.rs create mode 100644 rust/chains/tw_bitcoin/src/babylon/spending_data.rs rename rust/chains/tw_bitcoin/src/babylon/{claims/mod.rs => spending_info.rs} (76%) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 4afa179ffc6..3b6ce25bf99 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1788,6 +1788,7 @@ name = "tw_bitcoin" version = "0.1.0" dependencies = [ "bitcoin", + "itertools", "lazy_static", "secp256k1", "serde", diff --git a/rust/chains/tw_bitcoin/Cargo.toml b/rust/chains/tw_bitcoin/Cargo.toml index 8cb164c5a89..ce4101b5407 100644 --- a/rust/chains/tw_bitcoin/Cargo.toml +++ b/rust/chains/tw_bitcoin/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] bitcoin = { version = "0.30.0", features = ["rand-std", "serde"] } +itertools = "0.10.5" lazy_static = "1.4.0" secp256k1 = { version = "0.27.0", features = ["global-context", "rand-std"] } serde = { version = "1.0", features = ["derive"] } diff --git a/rust/chains/tw_bitcoin/src/babylon/conditions.rs b/rust/chains/tw_bitcoin/src/babylon/conditions.rs index 0eaaee159b4..b06048ef871 100644 --- a/rust/chains/tw_bitcoin/src/babylon/conditions.rs +++ b/rust/chains/tw_bitcoin/src/babylon/conditions.rs @@ -3,7 +3,8 @@ // Copyright © 2017 Trust Wallet. use tw_coin_entry::error::prelude::*; -use tw_hash::{H256, H32}; +use tw_hash::H32; +use tw_keypair::schnorr; use tw_utxo::script::standard_script::opcodes::*; use tw_utxo::script::Script; @@ -19,15 +20,15 @@ const FINALITY_PROVIDERS_QUORUM: u32 = 1; pub fn new_op_return_script( tag: &H32, version: u8, - staker_xonly: &H256, - finality_provider_xonly: &H256, + staker_key: &schnorr::XOnlyPublicKey, + finality_provider_key: &schnorr::XOnlyPublicKey, locktime: u16, ) -> Script { let mut buf = Vec::with_capacity(71); buf.extend_from_slice(tag.as_slice()); buf.push(version); - buf.extend_from_slice(staker_xonly.as_slice()); - buf.extend_from_slice(finality_provider_xonly.as_slice()); + buf.extend_from_slice(staker_key.as_slice()); + buf.extend_from_slice(finality_provider_key.as_slice()); buf.extend_from_slice(&locktime.to_be_bytes()); let mut s = Script::new(); @@ -42,9 +43,9 @@ pub fn new_op_return_script( /// ```txt /// OP_CHECKSIGVERIFY OP_CHECKSEQUENCEVERIFY /// ``` -pub fn new_timelock_script(staker_xonly: &H256, locktime: u16) -> Script { +pub fn new_timelock_script(staker_key: &schnorr::XOnlyPublicKey, locktime: u16) -> Script { let mut s = Script::with_capacity(64); - append_single_sig(&mut s, staker_xonly, VERIFY); + append_single_sig(&mut s, staker_key, VERIFY); s.push_int(locktime as i64); s.push(OP_CHECKSEQUENCEVERIFY); s @@ -59,16 +60,16 @@ pub fn new_timelock_script(staker_xonly: &H256, locktime: u16) -> Script { /// OP_NUMEQUAL /// ``` pub fn new_unbonding_script( - staker_xonly: &H256, - covenants_xonly: Vec, + staker_key: &schnorr::XOnlyPublicKey, + covenants_keys: &[schnorr::XOnlyPublicKey], covenant_quorum: u32, ) -> SigningResult