Skip to content

Commit

Permalink
Merge pull request #217 from wthrajat/passphrase
Browse files Browse the repository at this point in the history
Move master `Xpriv` generation from `Store` to `Wallet`
  • Loading branch information
mojoX911 authored Jul 23, 2024
2 parents 3727d0b + 887249d commit 9c386c4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/taker/offers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct MakerAddress(OnionAddress);

impl MakerAddress {
pub fn new(address: String) -> Option<Self> {
if let Some((onion_addr, port)) = address.split_once(":") {
if let Some((onion_addr, port)) = address.split_once(':') {
Some(Self(OnionAddress {
port: port.to_string(),
onion_addr: onion_addr.to_string(),
Expand Down
11 changes: 8 additions & 3 deletions src/wallet/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{convert::TryFrom, fs, path::PathBuf, str::FromStr};
use std::collections::{HashMap, HashSet};

use bitcoin::{
bip32::{ChildNumber, DerivationPath, Xpub},
bip32::{ChildNumber, DerivationPath, Xpriv, Xpub},
hashes::{hash160::Hash as Hash160, hex::FromHex},
secp256k1,
secp256k1::{Secp256k1, SecretKey},
Expand Down Expand Up @@ -150,6 +150,12 @@ impl Wallet {
seedphrase: String,
passphrase: String,
) -> Result<Self, WalletError> {
// Xpriv Derivation from seedphrase
let mnemonic = bip39::Mnemonic::parse(seedphrase.clone())?;
let seed = mnemonic.to_seed(passphrase.clone());
let master_key = Xpriv::new_master(rpc_config.network, &seed)?;

// Initialise wallet
let file_name = path
.file_name()
.expect("file name expected")
Expand All @@ -162,8 +168,7 @@ impl Wallet {
file_name,
path,
rpc_config.network,
seedphrase,
passphrase,
master_key,
Some(wallet_birthday),
)?;
Ok(Self {
Expand Down
13 changes: 3 additions & 10 deletions src/wallet/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use std::{collections::HashMap, path::PathBuf};

use bip39::Mnemonic;
use bitcoin::{bip32::Xpriv, Network, OutPoint, ScriptBuf};
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -49,14 +48,9 @@ impl WalletStore {
file_name: String,
path: &PathBuf,
network: Network,
seedphrase: String,
passphrase: String,
master_key: Xpriv,
wallet_birthday: Option<u64>,
) -> Result<Self, WalletError> {
let mnemonic = Mnemonic::parse(seedphrase)?;
let seed = mnemonic.to_seed(passphrase);
let master_key = Xpriv::new_master(network, &seed)?;

let store = Self {
file_name,
network,
Expand Down Expand Up @@ -103,7 +97,7 @@ impl WalletStore {
#[cfg(test)]
mod tests {
use super::*;

use bip39::Mnemonic;
use bitcoind::tempfile::tempdir;

#[test]
Expand All @@ -116,8 +110,7 @@ mod tests {
"test_wallet".to_string(),
&file_path,
Network::Bitcoin,
mnemonic,
"passphrase".to_string(),
Xpriv::new_master(Network::Bitcoin, mnemonic.as_bytes()).unwrap(),
None,
)
.unwrap();
Expand Down

0 comments on commit 9c386c4

Please sign in to comment.