Skip to content

Commit

Permalink
Refactor duplicate blockchain code in purse (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
terror authored Aug 16, 2022
1 parent 28a2362 commit e617773
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions src/purse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::*;

#[derive(Debug)]
pub(crate) struct Purse {
pub(crate) wallet: bdk::wallet::Wallet<SqliteDatabase>,
pub(crate) blockchain: RpcBlockchain,
pub(crate) wallet: bdk::wallet::Wallet<SqliteDatabase>,
}

impl Purse {
Expand Down Expand Up @@ -35,23 +35,7 @@ impl Purse {
),
)?;

wallet.sync(
&RpcBlockchain::from_config(&RpcConfig {
url: options.rpc_url(),
auth: Auth::Cookie {
file: options.cookie_file()?,
},
network: options.network,
wallet_name: wallet_name_from_descriptor(
Bip84((seed, None), KeychainKind::External),
None,
options.network,
&Secp256k1::new(),
)?,
skip_blocks: None,
})?,
SyncOptions::default(),
)?;
wallet.sync(&Self::blockchain(options, seed)?, SyncOptions::default())?;

eprintln!("Wallet initialized.");

Expand All @@ -67,13 +51,10 @@ impl Purse {
return Err(anyhow!("Wallet doesn't exist."));
}

let key = (
Mnemonic::from_entropy(&fs::read(path.join("entropy"))?)?,
None,
);
let seed = Mnemonic::from_entropy(&fs::read(path.join("entropy"))?)?;

let wallet = bdk::wallet::Wallet::new(
Bip84(key.clone(), KeychainKind::External),
Bip84((seed.clone(), None), KeychainKind::External),
None,
options.network,
SqliteDatabase::new(
Expand All @@ -85,24 +66,11 @@ impl Purse {
),
)?;

let blockchain = RpcBlockchain::from_config(&RpcConfig {
url: options.rpc_url(),
auth: Auth::Cookie {
file: options.cookie_file()?,
},
network: options.network,
wallet_name: wallet_name_from_descriptor(
Bip84(key, KeychainKind::External),
None,
options.network,
&Secp256k1::new(),
)?,
skip_blocks: None,
})?;
let blockchain = Self::blockchain(options, seed)?;

wallet.sync(&blockchain, SyncOptions::default())?;

Ok(Self { wallet, blockchain })
Ok(Self { blockchain, wallet })
}

pub(crate) fn find(&self, options: &Options, ordinal: Ordinal) -> Result<LocalUtxo> {
Expand All @@ -120,4 +88,21 @@ impl Purse {

bail!("No utxo contains {}˚.", ordinal);
}

fn blockchain(options: &Options, key: Mnemonic) -> Result<RpcBlockchain> {
Ok(RpcBlockchain::from_config(&RpcConfig {
url: options.rpc_url(),
auth: Auth::Cookie {
file: options.cookie_file()?,
},
network: options.network,
wallet_name: wallet_name_from_descriptor(
Bip84(key, KeychainKind::External),
None,
options.network,
&Secp256k1::new(),
)?,
skip_blocks: None,
})?)
}
}

0 comments on commit e617773

Please sign in to comment.