Skip to content

Commit

Permalink
[BTC]: Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Dec 12, 2024
1 parent cfe0b38 commit c0d3556
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 48 deletions.
14 changes: 9 additions & 5 deletions rust/chains/tw_bitcoin/src/babylon/claims/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::babylon::conditions;
use bitcoin::hashes::Hash;
use lazy_static::lazy_static;
use tw_coin_entry::error::prelude::{OrTWError, ResultContext, SigningErrorType, SigningResult};
use tw_coin_entry::error::prelude::*;
use tw_hash::{H256, H264};
use tw_keypair::schnorr;
use tw_utxo::script::Script;
Expand All @@ -16,7 +16,8 @@ lazy_static! {
pub static ref UNSPENDABLE_KEY_PATH_BYTES: H264 =
H264::from("0250929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0");
pub static ref UNSPENDABLE_KEY_PATH: schnorr::PublicKey =
schnorr::PublicKey::try_from(UNSPENDABLE_KEY_PATH_BYTES.as_slice());
schnorr::PublicKey::try_from(UNSPENDABLE_KEY_PATH_BYTES.as_slice())
.expect("Expected a valid unspendable key path");
}

pub struct StakingSpendInfo {
Expand All @@ -38,8 +39,7 @@ impl StakingSpendInfo {
let covenants_xonly: Vec<_> = covenants.iter().map(|pk| pk.x_only().bytes()).collect();
let fp_xonly: Vec<_> = vec![finality_provider.x_only().bytes()];

let timelock_script =
conditions::new_timelock_script(&staker_xonly, staking_locktime).into();
let timelock_script = conditions::new_timelock_script(&staker_xonly, staking_locktime);
let unbonding_script = conditions::new_unbonding_script(
&staker_xonly,
covenants_xonly.clone(),
Expand All @@ -55,14 +55,18 @@ impl StakingSpendInfo {
.context("Invalid number of finality providers")?;

// IMPORTANT - order and leaf depths are important!
let internal_pubkey =
bitcoin::key::UntweakedPublicKey::from_slice(&staker_xonly.as_slice())
.tw_err(|_| SigningErrorType::Error_invalid_params)
.context("Invalid stakerPublicKey")?;
let spend_info = bitcoin::taproot::TaprootBuilder::new()
.add_leaf(2, timelock_script.clone().into())
.expect("Leaf added at a valid depth")
.add_leaf(2, unbonding_script.clone().into())
.expect("Leaf added at a valid depth")
.add_leaf(1, slashing_script.clone().into())
.expect("Leaf added at a valid depth")
.finalize(&secp256k1::SECP256K1, xonly)
.finalize(&secp256k1::SECP256K1, internal_pubkey)
.expect("Expected a valid Taproot tree");

Ok(StakingSpendInfo {
Expand Down
10 changes: 5 additions & 5 deletions rust/chains/tw_bitcoin/src/babylon/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ pub trait BabylonOutputBuilder: Sized {
self,
staker: &schnorr::PublicKey,
staking_locktime: u16,
finality_providers: &[schnorr::PublicKey],
finality_provider: &schnorr::PublicKey,
covenants: &[schnorr::PublicKey],
covenant_quorum: u32,
) -> TransactionOutput;
) -> SigningResult<TransactionOutput>;

fn babylon_staking_op_return(
self,
tag: &[u8],
tag: &H32,
staker: &schnorr::PublicKey,
finality_providers: &[schnorr::PublicKey],
finality_provider: &schnorr::PublicKey,
staking_locktime: u16,
) -> SigningResult<TransactionOutput>;
) -> TransactionOutput;
}

impl BabylonOutputBuilder for OutputBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// Copyright © 2017 Trust Wallet.

use crate::babylon;
use crate::babylon::tx_builder::BabylonOutputBuilder;
use crate::modules::tx_builder::BitcoinChainInfo;
use std::borrow::Cow;
Expand Down Expand Up @@ -59,7 +58,6 @@ impl<'a, Context: UtxoContext> OutputProtobuf<'a, Context> {
},
BuilderType::None => SigningError::err(SigningErrorType::Error_invalid_params)
.context("No Output Builder type provided"),
_ => todo!(),
},
RecipientType::custom_script_pubkey(ref script) => self.custom_script(script.to_vec()),
RecipientType::to_address(ref address) => self.recipient_address(address),
Expand Down Expand Up @@ -187,8 +185,8 @@ impl<'a, Context: UtxoContext> OutputProtobuf<'a, Context> {
&self,
op_return: &Proto::mod_Output::BabylonStakingOpReturn,
) -> SigningResult<TransactionOutput> {
let tag = H32::try_from(&op_return.tag)
.into_tw()
let tag = H32::try_from(op_return.tag.as_ref())
.tw_err(|_| SigningErrorType::Error_invalid_params)
.context("Expected exactly 4 bytes tag")?;
let staker =
parse_schnorr_pk(&op_return.staker_public_key).context("Invalid stakerPublicKey")?;
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_bitcoincash/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fmt;
use std::str::FromStr;
use tw_coin_entry::coin_context::CoinContext;
use tw_coin_entry::coin_entry::CoinAddress;
use tw_coin_entry::error::prelude::{AddressError, AddressResult};
use tw_coin_entry::error::prelude::*;
use tw_coin_entry::prefix::BitcoinBase58Prefix;
use tw_keypair::ecdsa;
use tw_memory::Data;
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_bitcoincash/src/cash_address/unchecked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::cash_address::checksum::{calculate_checksum, CHECKSUM_LEN};
use crate::cash_address::{cash_base32, CashAddress, CashAddressType};
use std::fmt;
use std::str::FromStr;
use tw_coin_entry::error::prelude::{AddressError, AddressResult};
use tw_coin_entry::error::prelude::*;
use tw_encoding::bech32;
use tw_hash::H160;
use tw_memory::Data;
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_pactus/src/encoder/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use tw_coin_entry::error::prelude::{SigningError, SigningErrorType};
use tw_coin_entry::error::prelude::*;

/// Errors encountered when encoding or decoding data.
#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_solana/src/modules/message_decompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::address::SolanaAddress;
use crate::transaction::versioned::VersionedMessage;
use crate::transaction::CompiledInstruction;
use tw_coin_entry::error::prelude::{OrTWError, ResultContext, SigningErrorType, SigningResult};
use tw_coin_entry::error::prelude::*;
use tw_memory::Data;

/// [`Instruction`] without `accounts` field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::address::address_data::AddressData;
use crate::crc::CRC_16_XMODEM;
use tw_coin_entry::error::prelude::{AddressError, AddressResult};
use tw_coin_entry::error::prelude::*;
use tw_encoding::base64;
use tw_encoding::base64::{NO_PAD, URL_NO_PAD};
use tw_hash::{H256, H288};
Expand Down
2 changes: 1 addition & 1 deletion rust/frameworks/tw_ton_sdk/src/boc/binary_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::error::{CellErrorType, CellResult};
use bitstream_io::{BigEndian, BitWrite, BitWriter, Numeric};
use tw_coin_entry::error::prelude::{MapTWError, OrTWError, ResultContext};
use tw_coin_entry::error::prelude::*;
use tw_memory::Data;

pub struct BinaryWriter {
Expand Down
2 changes: 1 addition & 1 deletion rust/frameworks/tw_ton_sdk/src/boc/boc_to_raw_boc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::error::{CellErrorType, CellResult};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::sync::Arc;
use tw_coin_entry::error::prelude::{OrTWError, ResultContext};
use tw_coin_entry::error::prelude::*;
use tw_hash::H256;

type IndexedCellRef = RefCell<IndexedCell>;
Expand Down
2 changes: 1 addition & 1 deletion rust/frameworks/tw_ton_sdk/src/cell/cell_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::cell::{Cell, CellArc};
use crate::error::{CellError, CellErrorType, CellResult};
use bitstream_io::Numeric;
use std::sync::Arc;
use tw_coin_entry::error::prelude::{MapTWError, ResultContext};
use tw_coin_entry::error::prelude::*;
use tw_number::U256;

const MAX_CELL_BITS: usize = 1023;
Expand Down
2 changes: 1 addition & 1 deletion rust/frameworks/tw_ton_sdk/src/cell/cell_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::address::address_data::AddressData;
use crate::error::{CellError, CellErrorType, CellResult};
use bitreader::BitReader;
use num_bigint::BigUint;
use tw_coin_entry::error::prelude::{MapTWError, ResultContext};
use tw_coin_entry::error::prelude::*;
use tw_hash::H256;
use tw_memory::Data;
use tw_number::U256;
Expand Down
2 changes: 1 addition & 1 deletion rust/frameworks/tw_ton_sdk/src/cell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::boc::binary_writer::BinaryWriter;
use crate::cell::cell_parser::CellParser;
use std::fmt;
use std::sync::Arc;
use tw_coin_entry::error::prelude::{MapTWError, OrTWError, ResultContext};
use tw_coin_entry::error::prelude::*;
use tw_encoding::base64::{self, URL_NO_PAD};
use tw_encoding::hex::ToHex;
use tw_hash::sha2::sha256;
Expand Down
2 changes: 1 addition & 1 deletion rust/frameworks/tw_utxo/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::script::Script;
use std::str::FromStr;
use tw_coin_entry::error::prelude::{AddressError, SigningResult};
use tw_coin_entry::error::prelude::*;

pub struct AddressPrefixes {
pub p2pkh_prefix: u8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::transaction::transaction_sighash::fork_id_sighash::ForkIdSighash;
use crate::transaction::transaction_sighash::legacy_sighash::LegacySighash;
use crate::transaction::transaction_sighash::witness0_sighash::Witness0Sighash;
use crate::transaction::{TransactionPreimage, UtxoPreimageArgs};
use tw_coin_entry::error::prelude::{ResultContext, SigningError, SigningErrorType, SigningResult};
use tw_coin_entry::error::prelude::*;
use tw_hash::hasher::sha256_d;
use tw_hash::H256;

Expand Down
28 changes: 6 additions & 22 deletions src/proto/BitcoinV2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,10 @@ message Output {
OutputBrc20Inscription brc20_inscribe = 9;
// OP_RETURN output. In most cases, with a zero-amount.
bytes op_return = 12;
// Create a Babylon `Staking Output`.
// Create a Babylon `Staking` or `Unbonding` output.
BabylonStakingOutput babylon_staking = 14;
// Create a Babylon Staking OP_RETURN output.
BabylonStakingOpReturn babylon_staking_op_return = 15;
// Create a Babylon `Unbonding Output`.
BabylonUnbondingOutput babylon_unbonding = 16;
}
}

Expand Down Expand Up @@ -215,14 +213,16 @@ message Output {
string transfer_amount = 3;
}

// Staker enters the system through the creation of a staking transaction which locks Bitcoin in the Bitcoin Staking script.
// Creates `Staking Output` which can only be spent through a script spending path.
// Bitcoin Babylon lock transaction. It can be used to create either Staking or Unbonding output.
// The lock output can only be spent through a script spending path.
message BabylonStakingOutput {
// User's public key.
bytes staker_public_key = 1;
// Finality provider's public key chosen by the user.
bytes finality_provider_public_key = 2;
// global_parameters.min_staking_time <= staking_time <= global_parameters.max_staking_time.
// If it's a Staking output, it should be:
// `global_parameters.min_staking_time <= staking_time <= global_parameters.max_staking_time`.
// If it's an Unbonding output, it should be equal to `global_parameters.unbonding_time`.
uint32 staking_time = 3;
// Retrieved from global_parameters.covenant_pks.
// Babylon nodes that can approve Unbonding tx or Slash the staked position when acting bad.
Expand All @@ -243,22 +243,6 @@ message Output {
// global_parameters.min_staking_time <= staking_time <= global_parameters.max_staking_time.
uint32 staking_time = 4;
}

// Creates `Unbonding Output` that allows staker to on-demand unbond their locked Bitcoin stake prior to its original timelock expiration.
message BabylonUnbondingOutput {
// User's public key.
bytes staker_public_key = 1;
// Finality provider's public key chosen by the user.
bytes finality_provider_public_key = 2;
// Retrieved from global_parameters.covenant_pks.
// Babylon nodes that can approve Unbonding tx or Slash the staked position when acting bad.
repeated bytes covenant_committee_public_keys = 3;
// Retrieved from global_parameters.covenant_quorum.
// Specifies the quorum required by the covenant committee for unbonding transactions to be confirmed.
uint32 covenant_committee_quorum = 4;
// Retrieved from global_parameters.unbonding_time.
uint32 unbonding_time = 5;
}
}

message ChainInfo {
Expand Down

0 comments on commit c0d3556

Please sign in to comment.