Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dash/examples/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ extern crate dashcore;
use std::str::FromStr;
use std::{env, process};

use dashcore::PublicKey;
use dashcore::address::Address;
use dashcore::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, ExtendedPubKey};
use dashcore::hashes::hex::FromHex;
use dashcore::secp256k1::Secp256k1;
use dashcore::secp256k1::ffi::types::AlignedType;
use dashcore::secp256k1::Secp256k1;
use dashcore::PublicKey;

fn main() {
// This example derives root xprv from a 32-byte seed,
Expand Down
43 changes: 34 additions & 9 deletions dash/examples/ecdsa-psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,19 @@ impl ColdStorage {
let input_xpriv = master_xpriv.derive_priv(secp, &path)?;
let input_xpub = ExtendedPubKey::from_priv(secp, &input_xpriv);

let wallet = ColdStorage { master_xpriv, master_xpub };
let wallet = ColdStorage {
master_xpriv,
master_xpub,
};
let fingerprint = wallet.master_fingerprint();

Ok((wallet, fingerprint, account_0_xpub, input_xpub))
}

/// Returns the fingerprint for the master extended public key.
fn master_fingerprint(&self) -> Fingerprint { self.master_xpub.fingerprint() }
fn master_fingerprint(&self) -> Fingerprint {
self.master_xpub.fingerprint()
}

/// Signs `psbt` with this signer.
fn sign_psbt<C: Signing>(&self, secp: &Secp256k1<C>, mut psbt: Psbt) -> Result<Psbt> {
Expand Down Expand Up @@ -169,7 +174,11 @@ impl WatchOnly {
input_xpub: ExtendedPubKey,
master_fingerprint: Fingerprint,
) -> Self {
WatchOnly { account_0_xpub, input_xpub, master_fingerprint }
WatchOnly {
account_0_xpub,
input_xpub,
master_fingerprint,
}
}

/// Creates the PSBT, in BIP174 parlance this is the 'Creater'.
Expand All @@ -184,13 +193,19 @@ impl WatchOnly {
version: 2,
lock_time: 0,
input: vec![TxIn {
previous_output: OutPoint { txid: INPUT_UTXO_TXID.parse()?, vout: INPUT_UTXO_VOUT },
previous_output: OutPoint {
txid: INPUT_UTXO_TXID.parse()?,
vout: INPUT_UTXO_VOUT,
},
script_sig: ScriptBuf::new(),
sequence: u32::MAX, // Disable LockTime and RBF.
witness: Witness::default(),
}],
output: vec![
TxOut { value: to_amount.to_sat(), script_pubkey: to_address.script_pubkey() },
TxOut {
value: to_amount.to_sat(),
script_pubkey: to_address.script_pubkey(),
},
TxOut {
value: change_amount.to_sat(),
script_pubkey: change_address.script_pubkey(),
Expand All @@ -206,7 +221,10 @@ impl WatchOnly {

/// Updates the PSBT, in BIP174 parlance this is the 'Updater'.
fn update_psbt(&self, mut psbt: Psbt) -> Result<Psbt> {
let mut input = Input { witness_utxo: Some(previous_output()), ..Default::default() };
let mut input = Input {
witness_utxo: Some(previous_output()),
..Default::default()
};

let pk = self.input_xpub.to_pub();
let wpkh = pk.wpubkey_hash().expect("a compressed pubkey");
Expand Down Expand Up @@ -281,15 +299,22 @@ fn previous_output() -> TxOut {
.expect("failed to parse input utxo scriptPubkey");
let amount = Amount::from_str(INPUT_UTXO_VALUE).expect("failed to parse input utxo value");

TxOut { value: amount.to_sat(), script_pubkey }
TxOut {
value: amount.to_sat(),
script_pubkey,
}
}

struct Error(Box<dyn std::error::Error>);

impl<T: std::error::Error + 'static> From<T> for Error {
fn from(e: T) -> Self { Error(Box::new(e)) }
fn from(e: T) -> Self {
Error(Box::new(e))
}
}

impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.0, f) }
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
2 changes: 1 addition & 1 deletion dash/examples/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::net::{IpAddr, Ipv4Addr, Shutdown, SocketAddr, TcpStream};
use std::time::{SystemTime, UNIX_EPOCH};
use std::{env, process};

use dashcore::consensus::{Decodable, encode};
use dashcore::consensus::{encode, Decodable};
use dashcore::network::{address, constants, message, message_network};
use dashcore::secp256k1;
use dashcore::secp256k1::rand::Rng;
Expand Down
80 changes: 62 additions & 18 deletions dash/examples/taproot-psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ use dashcore::secp256k1::Secp256k1;
use dashcore::sighash::{self, SighashCache, TapSighash, TapSighashType};
use dashcore::taproot::{self, LeafVersion, TapLeafHash, TaprootBuilder, TaprootSpendInfo};
use dashcore::{
Address, Amount, Network, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Witness, absolute,
script,
absolute, script, Address, Amount, Network, OutPoint, ScriptBuf, Transaction, TxIn, TxOut,
Witness,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -119,8 +119,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set these fields with valid data for the UTXO from step 5 above
UTXO_1,
vec![
TxOut { value: amount_to_send_in_sats, script_pubkey: to_address.script_pubkey() },
TxOut { value: change_amount, script_pubkey: change_address.script_pubkey() },
TxOut {
value: amount_to_send_in_sats,
script_pubkey: to_address.script_pubkey(),
},
TxOut {
value: change_amount,
script_pubkey: change_address.script_pubkey(),
},
],
)?);
println!(
Expand Down Expand Up @@ -234,7 +240,10 @@ fn generate_bip86_key_spend_tx(
version: 2,
lock_time: 0,
input: vec![TxIn {
previous_output: OutPoint { txid: input_utxo.txid.parse()?, vout: input_utxo.vout },
previous_output: OutPoint {
txid: input_utxo.txid.parse()?,
vout: input_utxo.vout,
},
script_sig: ScriptBuf::new(),
sequence: 0xFFFFFFFF, // Ignore nSequence.
witness: Witness::default(),
Expand Down Expand Up @@ -262,7 +271,10 @@ fn generate_bip86_key_spend_tx(
.expect("failed to parse input utxo scriptPubkey");
let amount = Amount::from_sat(from_amount);

Some(TxOut { value: amount.to_sat(), script_pubkey })
Some(TxOut {
value: amount.to_sat(),
script_pubkey,
})
},
tap_key_origins: origins,
..Default::default()
Expand Down Expand Up @@ -380,7 +392,10 @@ impl BenefactorWallet {
lock_time: absolute::LockTime,
input_utxo: P2trUtxo,
) -> Result<(Transaction, Psbt), Box<dyn std::error::Error>> {
if let ChildNumber::Normal { index } = self.next {
if let ChildNumber::Normal {
index,
} = self.next
{
if index > 0 && self.current_spend_info.is_some() {
return Err("Transaction already exists, use refresh_inheritance_timelock to refresh the timelock".into());
}
Expand Down Expand Up @@ -414,15 +429,21 @@ impl BenefactorWallet {
&self.secp,
self.master_xpriv,
input_utxo,
vec![TxOut { script_pubkey: script_pubkey.clone(), value }],
vec![TxOut {
script_pubkey: script_pubkey.clone(),
value,
}],
)?;

// CREATOR + UPDATER
let next_tx = Transaction {
version: 2,
lock_time: lock_time.to_consensus_u32(),
input: vec![TxIn {
previous_output: OutPoint { txid: tx.txid(), vout: 0 },
previous_output: OutPoint {
txid: tx.txid(),
vout: 0,
},
script_sig: ScriptBuf::new(),
sequence: 0xFFFFFFFD, // enable locktime and opt-in RBF
witness: Witness::default(),
Expand Down Expand Up @@ -452,7 +473,10 @@ impl BenefactorWallet {
let script_pubkey = script_pubkey;
let amount = Amount::from_sat(value);

Some(TxOut { value: amount.to_sat(), script_pubkey })
Some(TxOut {
value: amount.to_sat(),
script_pubkey,
})
},
tap_key_origins: origins,
tap_merkle_root: taproot_spend_info.merkle_root(),
Expand Down Expand Up @@ -509,8 +533,10 @@ impl BenefactorWallet {
taproot_spend_info.merkle_root(),
);

psbt.unsigned_tx.output =
vec![TxOut { script_pubkey: output_script_pubkey.clone(), value: output_value }];
psbt.unsigned_tx.output = vec![TxOut {
script_pubkey: output_script_pubkey.clone(),
value: output_value,
}];
psbt.outputs = vec![Output::default()];
psbt.unsigned_tx.lock_time = 0;

Expand Down Expand Up @@ -562,15 +588,21 @@ impl BenefactorWallet {
// EXTRACTOR
let tx = psbt.extract_tx();
tx.verify(|_| {
Some(TxOut { value: input_value, script_pubkey: output_script_pubkey.clone() })
Some(TxOut {
value: input_value,
script_pubkey: output_script_pubkey.clone(),
})
})
.expect("failed to verify transaction");

let next_tx = Transaction {
version: 2,
lock_time: lock_time.to_consensus_u32(),
input: vec![TxIn {
previous_output: OutPoint { txid: tx.txid(), vout: 0 },
previous_output: OutPoint {
txid: tx.txid(),
vout: 0,
},
script_sig: ScriptBuf::new(),
sequence: 0xFFFFFFFD, // enable locktime and opt-in RBF
witness: Witness::default(),
Expand Down Expand Up @@ -598,7 +630,10 @@ impl BenefactorWallet {
let script_pubkey = output_script_pubkey;
let amount = Amount::from_sat(output_value);

Some(TxOut { value: amount.to_sat(), script_pubkey })
Some(TxOut {
value: amount.to_sat(),
script_pubkey,
})
},
tap_key_origins: origins,
tap_merkle_root: taproot_spend_info.merkle_root(),
Expand Down Expand Up @@ -628,7 +663,10 @@ struct BeneficiaryWallet {

impl BeneficiaryWallet {
fn new(master_xpriv: ExtendedPrivKey) -> Result<Self, Box<dyn std::error::Error>> {
Ok(Self { master_xpriv, secp: Secp256k1::new() })
Ok(Self {
master_xpriv,
secp: Secp256k1::new(),
})
}

fn master_xpub(&self) -> ExtendedPubKey {
Expand Down Expand Up @@ -707,7 +745,10 @@ impl BeneficiaryWallet {
// EXTRACTOR
let tx = psbt.extract_tx();
tx.verify(|_| {
Some(TxOut { value: input_value, script_pubkey: input_script_pubkey.clone() })
Some(TxOut {
value: input_value,
script_pubkey: input_script_pubkey.clone(),
})
})
.expect("failed to verify transaction");

Expand Down Expand Up @@ -746,7 +787,10 @@ fn sign_psbt_taproot(

let sig = secp.sign_schnorr(&hash.into(), &keypair);

let final_signature = taproot::Signature { sig, hash_ty };
let final_signature = taproot::Signature {
sig,
hash_ty,
};

if let Some(lh) = leaf_hash {
psbt_input.tap_script_sigs.insert((pubkey, lh), final_signature);
Expand Down
Loading
Loading