Browse Source

Move `dummy_wallet` to `daemon::bdk_ext::new_test_wallet`

This wallet is also useful for unit tests. Move it out of the harness
into the library.
feature/force-stop-button
Thomas Eizinger 3 years ago
parent
commit
6dd95d01c3
No known key found for this signature in database GPG Key ID: 651AC83A6C6C8B96
  1. 34
      daemon/src/bdk_ext.rs
  2. 1
      daemon/src/lib.rs
  3. 35
      daemon/tests/harness/maia.rs
  4. 4
      daemon/tests/harness/mocks/wallet.rs

34
daemon/src/bdk_ext.rs

@ -0,0 +1,34 @@
use anyhow::Result;
use bdk::bitcoin::util::bip32::ExtendedPrivKey;
use bdk::bitcoin::{self, Amount, Network};
use rand::{CryptoRng, RngCore};
pub fn new_test_wallet(
rng: &mut (impl RngCore + CryptoRng),
utxo_amount: Amount,
num_utxos: u8,
) -> Result<bdk::Wallet<(), bdk::database::MemoryDatabase>> {
use bdk::{populate_test_db, testutils};
let mut seed = [0u8; 32];
rng.fill_bytes(&mut seed);
let key = ExtendedPrivKey::new_master(Network::Regtest, &seed)?;
let descriptors = testutils!(@descriptors (&format!("wpkh({}/*)", key)));
let mut database = bdk::database::MemoryDatabase::new();
for index in 0..num_utxos {
populate_test_db!(
&mut database,
testutils! {
@tx ( (@external descriptors, index as u32) => utxo_amount.as_sat() ) (@confirmations 1)
},
Some(100)
);
}
let wallet = bdk::Wallet::new_offline(&descriptors.0, None, Network::Regtest, database)?;
Ok(wallet)
}

1
daemon/src/lib.rs

@ -23,6 +23,7 @@ pub mod sqlx_ext; // Must come first because it is a macro.
pub mod actors;
pub mod address_map;
pub mod auth;
pub mod bdk_ext;
pub mod bitmex_price_feed;
pub mod cfd_actors;
pub mod collab_settlement_taker;

35
daemon/tests/harness/maia.rs

@ -1,42 +1,7 @@
use anyhow::Result;
use bdk::bitcoin;
use bdk::bitcoin::util::bip32::ExtendedPrivKey;
use bdk::bitcoin::{Amount, Network};
use maia::secp256k1_zkp::rand::{CryptoRng, RngCore};
use maia::secp256k1_zkp::{schnorrsig, SecretKey};
use maia::Announcement;
use std::str::FromStr;
pub fn dummy_wallet(
rng: &mut (impl RngCore + CryptoRng),
utxo_amount: Amount,
num_utxos: u8,
) -> Result<bdk::Wallet<(), bdk::database::MemoryDatabase>> {
use bdk::{populate_test_db, testutils};
let mut seed = [0u8; 32];
rng.fill_bytes(&mut seed);
let key = ExtendedPrivKey::new_master(Network::Regtest, &seed)?;
let descriptors = testutils!(@descriptors (&format!("wpkh({}/*)", key)));
let mut database = bdk::database::MemoryDatabase::new();
for index in 0..num_utxos {
populate_test_db!(
&mut database,
testutils! {
@tx ( (@external descriptors, index as u32) => utxo_amount.as_sat() ) (@confirmations 1)
},
Some(100)
);
}
let wallet = bdk::Wallet::new_offline(&descriptors.0, None, Network::Regtest, database)?;
Ok(wallet)
}
#[allow(dead_code)]
pub struct OliviaData {
id: String,

4
daemon/tests/harness/mocks/wallet.rs

@ -1,10 +1,10 @@
use crate::harness::maia::dummy_wallet;
use anyhow::Result;
use bdk::bitcoin::util::psbt::PartiallySignedTransaction;
use bdk::bitcoin::{ecdsa, Amount, Txid};
use bdk::wallet::tx_builder::TxOrdering;
use bdk::wallet::AddressIndex;
use bdk::FeeRate;
use daemon::bdk_ext::new_test_wallet;
use daemon::model::{Timestamp, WalletInfo};
use daemon::wallet;
use maia::secp256k1_zkp::Secp256k1;
@ -75,7 +75,7 @@ fn dummy_wallet_info() -> Result<WalletInfo> {
pub fn build_party_params(msg: wallet::BuildPartyParams) -> Result<PartyParams> {
let mut rng = thread_rng();
let wallet = dummy_wallet(&mut rng, Amount::from_btc(0.4).unwrap(), 5).unwrap();
let wallet = new_test_wallet(&mut rng, Amount::from_btc(0.4).unwrap(), 5).unwrap();
let mut builder = wallet.build_tx();

Loading…
Cancel
Save