Skip to content

Commit

Permalink
upgrading to match bdk v29
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrichard committed Jul 5, 2024
1 parent 144006a commit 89e3314
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 44 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bdk-reserves"
version = "0.28.1"
version = "0.29.0"
authors = ["Richard Ulrich <richard.ulrich@aminagroup.com>"]
edition = "2018"
description = "Proof of reserves for bitcoin dev kit"
Expand All @@ -10,14 +10,14 @@ license = "MIT OR Apache-2.0"
repository = "https://github.com/bitcoindevkit/bdk-reserves"

[dependencies]
bdk = { version = "0.28", default-features = false }
bdk = { version = "0.29", default-features = false, features = ["std"] }
bitcoinconsensus = "0.19.0-3"
log = "^0.4"

[dev-dependencies]
rstest = "^0.11"
bdk-testutils = "^0.4"
bdk = { version = "0.28", default-features = true }
electrsd = { version = "0.21", features = ["bitcoind_22_0", "electrs_0_9_1"] }
bdk = { version = "0.29", default-features = true }
electrsd = { version = "0.24", features = ["bitcoind_22_0", "electrs_0_9_1"] }
# base64ct versions at 1.6.0 and higher have MSRV 1.60.0
base64ct = { version = "<1.6.0", features = ["alloc"] }
45 changes: 18 additions & 27 deletions src/reserves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
//! https://github.com/bitcoin/bips/blob/master/bip-0127.mediawiki
//! https://github.com/bitcoin/bips/blob/master/bip-0322.mediawiki
use bdk::bitcoin::address::Payload;
use bdk::bitcoin::blockdata::opcodes;
use bdk::bitcoin::blockdata::script::{Builder, Script};
use bdk::bitcoin::blockdata::transaction::{EcdsaSighashType, OutPoint, TxIn, TxOut};
use bdk::bitcoin::blockdata::transaction::{OutPoint, TxIn, TxOut};
use bdk::bitcoin::consensus::encode::serialize;
use bdk::bitcoin::hash_types::{PubkeyHash, Txid};
use bdk::bitcoin::hashes::{hash160, sha256d, Hash};
use bdk::bitcoin::util::address::Payload;
use bdk::bitcoin::util::psbt::{Input, PartiallySignedTransaction as PSBT};
use bdk::bitcoin::psbt::{Input, PartiallySignedTransaction as PSBT};
use bdk::bitcoin::sighash::EcdsaSighashType;
use bdk::bitcoin::{Address, Network, Sequence};
use bdk::database::BatchDatabase;
use bdk::wallet::tx_builder::TxOrdering;
Expand Down Expand Up @@ -114,16 +115,13 @@ where
value: 0,
script_pubkey: Builder::new().push_opcode(opcodes::OP_TRUE).into_script(),
}),
final_script_sig: Some(Script::default()), /* "finalize" the input with an empty scriptSig */
final_script_sig: Some(Script::empty().into()), /* "finalize" the input with an empty scriptSig */
..Default::default()
};

let pkh = PubkeyHash::from_hash(hash160::Hash::hash(&[0]));
let out_script_unspendable = Address {
payload: Payload::PubkeyHash(pkh),
network: self.network(),
}
.script_pubkey();
let pkh = PubkeyHash::from_raw_hash(hash160::Hash::hash(&[0]));
let out_script_unspendable =
Address::new(self.network(), Payload::PubkeyHash(pkh)).script_pubkey();

let mut builder = self.build_tx();
builder
Expand Down Expand Up @@ -257,12 +255,8 @@ pub fn verify_proof(
}

// verify the unspendable output
let pkh = PubkeyHash::from_hash(hash160::Hash::hash(&[0]));
let out_script_unspendable = Address {
payload: Payload::PubkeyHash(pkh),
network,
}
.script_pubkey();
let pkh = PubkeyHash::from_raw_hash(hash160::Hash::hash(&[0]));
let out_script_unspendable = Address::new(network, Payload::PubkeyHash(pkh)).script_pubkey();
if tx.output[0].script_pubkey != out_script_unspendable {
return Err(ProofError::InvalidOutput);
}
Expand Down Expand Up @@ -326,7 +320,7 @@ fn challenge_txin(message: &str) -> TxIn {
let message = "Proof-of-Reserves: ".to_string() + message;
let message = sha256d::Hash::hash(message.as_bytes());
TxIn {
previous_output: OutPoint::new(Txid::from_hash(message), 0),
previous_output: OutPoint::new(Txid::from_raw_hash(message), 0),
sequence: Sequence(0xFFFFFFFF),
..Default::default()
}
Expand All @@ -336,10 +330,10 @@ fn challenge_txin(message: &str) -> TxIn {
mod test {
use super::*;
use base64ct::{Base64, Encoding};
use bdk::bitcoin::consensus::encode::deserialize;
use bdk::bitcoin::hashes::sha256;
use bdk::bitcoin::secp256k1::{ecdsa::SerializedSignature, Message, Secp256k1, SecretKey};
use bdk::bitcoin::{Address, EcdsaSighashType, Network, Witness};
use bdk::bitcoin::sighash::EcdsaSighashType;
use bdk::bitcoin::{Address, Network, Witness};
use bdk::wallet::get_funded_wallet;
use std::str::FromStr;

Expand All @@ -350,7 +344,7 @@ mod test {

let message = "This belongs to me.";
let psbt = wallet.create_proof(message).unwrap();
let psbt_ser = serialize(&psbt);
let psbt_ser = psbt.serialize();
let psbt_b64 = Base64::encode_string(&psbt_ser);
let expected = r#"cHNidP8BAH4BAAAAAmw1RvG4UzfnSafpx62EPTyha6VslP0Er7n3TxjEpeBeAAAAAAD/////2johM0znoXIXT1lg+ySrvGrtq1IGXPJzpfi/emkV9iIAAAAAAP////8BUMMAAAAAAAAZdqkUn3/QltN+0sDj9/DPySS+70/862iIrAAAAAAAAQEKAAAAAAAAAAABUQEHAAABAR9QwwAAAAAAABYAFOzlJlcQU9qGRUyeBmd56vnRUC5qIgYDKwVYB4vsOGlKhJM9ZZMD4lddrn6RaFkRRUEVv9ZEh+ME7OUmVwAA"#;

Expand Down Expand Up @@ -382,7 +376,7 @@ mod test {
fn get_signed_proof() -> PSBT {
let psbt = "cHNidP8BAH4BAAAAAmw1RvG4UzfnSafpx62EPTyha6VslP0Er7n3TxjEpeBeAAAAAAD/////2johM0znoXIXT1lg+ySrvGrtq1IGXPJzpfi/emkV9iIAAAAAAP////8BUMMAAAAAAAAZdqkUn3/QltN+0sDj9/DPySS+70/862iIrAAAAAAAAQEKAAAAAAAAAAABUQEHAAABAR9QwwAAAAAAABYAFOzlJlcQU9qGRUyeBmd56vnRUC5qAQcAAQhrAkcwRAIgDSE4PQ57JDiZ7otGkTqz35bi/e1pexYaYKWaveuvRd4CIFzVB4sAmgtdEVz2vHzs1iXc9iRKJ+KQOQb+C2DtPyvzASEDKwVYB4vsOGlKhJM9ZZMD4lddrn6RaFkRRUEVv9ZEh+MAAA==";
let psbt = Base64::decode_vec(psbt).unwrap();
deserialize(&psbt).unwrap()
PSBT::deserialize(&psbt).unwrap()
}

#[test]
Expand Down Expand Up @@ -540,12 +534,9 @@ mod test {
let message = "This belongs to me.";
let mut psbt = get_signed_proof();

let pkh = PubkeyHash::from_hash(hash160::Hash::hash(&[0, 1, 2, 3]));
let out_script_unspendable = Address {
payload: Payload::PubkeyHash(pkh),
network: Network::Testnet,
}
.script_pubkey();
let pkh = PubkeyHash::from_raw_hash(hash160::Hash::hash(&[0, 1, 2, 3]));
let out_script_unspendable =
Address::new(Network::Testnet, Payload::PubkeyHash(pkh)).script_pubkey();
psbt.unsigned_tx.output[0].script_pubkey = out_script_unspendable;

wallet.verify_proof(&psbt, message, None).unwrap();
Expand Down
4 changes: 3 additions & 1 deletion tests/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ fn unconfirmed() -> Result<(), ProofError> {

let spendable = wallet.verify_proof(&psbt, message, None)?;
dbg!(&new_balance);
assert_eq!(
assert!(
spendable <= new_balance.untrusted_pending + new_balance.confirmed,
"spendable ({}) <= new_balance.untrusted_pending + new_balance.confirmed ({})",
spendable,
new_balance.untrusted_pending + new_balance.confirmed
);
Expand Down
15 changes: 10 additions & 5 deletions tests/multi_sig.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod regtestenv;
use bdk::bitcoin::key::{PrivateKey, PublicKey};
use bdk::bitcoin::psbt::PartiallySignedTransaction as PSBT;
use bdk::bitcoin::secp256k1::Secp256k1;
use bdk::bitcoin::util::key::{PrivateKey, PublicKey};
use bdk::bitcoin::util::psbt::PartiallySignedTransaction as PSBT;
use bdk::bitcoin::Network;
use bdk::database::memory::MemoryDatabase;
use bdk::wallet::{AddressIndex, Wallet};
Expand Down Expand Up @@ -98,8 +98,8 @@ fn test_proof_multisig(
wallets.iter().enumerate().for_each(|(i, wallet)| {
let balance = wallet.get_balance().unwrap();
assert!(
(4_999_999_256..=4_999_999_596).contains(&balance.confirmed),
"balance of wallet {} is {} but should be between 4'999'999'256 and 4'999'999'596",
(49_999_999_256..=49_999_999_596).contains(&balance.confirmed),
"balance of wallet {} is {} but should be between 49'999'999'256 and 49'999'999'596",
i,
balance
);
Expand Down Expand Up @@ -158,7 +158,12 @@ fn test_proof_multisig(

let spendable = wallets[0].verify_proof(&psbt, message, None)?;
let balance = wallets[0].get_balance()?;
assert_eq!(spendable, balance.confirmed);
assert!(
spendable <= balance.confirmed,
"spendable ({}) <= balance.confirmed ({})",
spendable,
balance.confirmed,
);

Ok(())
}
18 changes: 12 additions & 6 deletions tests/regtestenv.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use bdk::blockchain::{electrum::ElectrumBlockchain, Blockchain};
use bdk::database::memory::MemoryDatabase;
use bdk::electrum_client::Client;
use bdk::electrum_client::ElectrumApi;
use bdk::wallet::{AddressIndex, SyncOptions, Wallet};
use bdk::SignOptions;
use electrsd::bitcoind::bitcoincore_rpc::{bitcoin::Address, RpcApi};
use electrsd::bitcoind::bitcoincore_rpc::{
bitcoin::{network::constants::Network, Address},
RpcApi,
};
use electrsd::bitcoind::BitcoinD;
use electrsd::electrum_client::ElectrumApi;
use electrsd::ElectrsD;
use std::str::FromStr;
use std::time::Duration;
Expand Down Expand Up @@ -47,10 +50,13 @@ impl RegTestEnv {
let addr2 = wallets[0].get_address(AddressIndex::Peek(1)).unwrap();
let addr1 = wallets[0].get_address(AddressIndex::Peek(0)).unwrap();
const MY_FOREIGN_ADDR: &str = "mpSFfNURcFTz2yJxBzRY9NhnozxeJ2AUC8";
let foreign_addr = Address::from_str(MY_FOREIGN_ADDR).unwrap();
let foreign_addr = Address::from_str(MY_FOREIGN_ADDR)
.unwrap()
.require_network(Network::Testnet)
.unwrap();

// generate to the first receiving address of the test wallet
self.generate_to_address(10, &addr2);
self.generate_to_address(10, &addr2.address);
// make the newly mined coins spendable
self.generate_to_address(100, &foreign_addr);

Expand All @@ -60,7 +66,7 @@ impl RegTestEnv {
wallet.sync(&blockchain, SyncOptions::default()).unwrap();
let balance = wallet.get_balance().unwrap();
assert!(
balance.confirmed == 5_000_000_000,
balance.confirmed == 50_000_000_000,
"balance of wallet {} is {} but should be 5'000'000'000",
i,
balance
Expand All @@ -69,7 +75,7 @@ impl RegTestEnv {

let mut builder = wallets[0].build_tx();
builder
.add_recipient(addr1.script_pubkey(), 1_000_000)
.add_recipient(addr1.address.script_pubkey(), 1_000_000)
.fee_rate(bdk::FeeRate::from_sat_per_vb(2.0));
let (mut psbt, _) = builder.finish().unwrap();
let signopts = SignOptions {
Expand Down
2 changes: 1 addition & 1 deletion tests/tampering.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bdk::bitcoin::blockdata::transaction::EcdsaSighashType;
use bdk::bitcoin::sighash::EcdsaSighashType;
use bdk::wallet::get_funded_wallet;
use bdk::SignOptions;
use bdk_reserves::reserves::*;
Expand Down

0 comments on commit 89e3314

Please sign in to comment.