Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mnemonics #1215

Merged
merged 9 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
82 changes: 57 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ anyhow = { version = "1.0.56", features = ["backtrace"] }
axum = "0.6.1"
axum-server = "0.4.0"
bech32 = "0.9.1"
bip39 = "1.0.1"
bitcoin = { version = "0.29.1", features = ["rand"] }
boilerplate = { version = "0.2.3", features = ["axum"] }
chrono = "0.4.19"
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use {
tally::Tally,
},
anyhow::{anyhow, bail, Context, Error},
bip39::Mnemonic,
bitcoin::{
blockdata::constants::COIN_VALUE,
consensus::{self, Decodable, Encodable},
Expand Down
98 changes: 93 additions & 5 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
use {super::*, fee_rate::FeeRate, transaction_builder::TransactionBuilder};
use {
super::*,
bitcoin::secp256k1::{
rand::{self, RngCore},
All, Secp256k1,
},
bitcoin::{
util::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey, Fingerprint},
Network,
},
bitcoincore_rpc::bitcoincore_rpc_json::{ImportDescriptors, Timestamp},
fee_rate::FeeRate,
miniscript::descriptor::{Descriptor, DescriptorSecretKey, DescriptorXKey, Wildcard},
transaction_builder::TransactionBuilder,
};

mod balance;
pub(crate) mod create;
pub(crate) mod inscribe;
mod inscriptions;
mod outputs;
mod receive;
mod restore;
mod sats;
mod send;
mod transaction_builder;
Expand All @@ -15,17 +30,19 @@ mod transactions;
pub(crate) enum Wallet {
#[clap(about = "Get wallet balance")]
Balance,
#[clap(about = "Create a new wallet")]
#[clap(about = "Create new wallet")]
Create,
#[clap(about = "Create an inscription")]
#[clap(about = "Create inscription")]
Inscribe(inscribe::Inscribe),
#[clap(about = "List wallet inscriptions")]
Inscriptions,
#[clap(about = "Generate a receive address")]
#[clap(about = "Generate receive address")]
Receive,
#[clap(about = "Restore wallet")]
Restore(restore::Restore),
#[clap(about = "List wallet satoshis")]
Sats(sats::Sats),
#[clap(about = "Send a satoshi or inscription")]
#[clap(about = "Send sat or inscription")]
Send(send::Send),
#[clap(about = "See wallet transactions")]
Transactions(transactions::Transactions),
Expand All @@ -41,6 +58,7 @@ impl Wallet {
Self::Inscribe(inscribe) => inscribe.run(options),
Self::Inscriptions => inscriptions::run(options),
Self::Receive => receive::run(options),
Self::Restore(restore) => restore.run(options),
Self::Sats(sats) => sats.run(options),
Self::Send(send) => send.run(options),
Self::Transactions(transactions) => transactions.run(options),
Expand Down Expand Up @@ -110,3 +128,73 @@ fn get_change_addresses(options: &Options, n: usize) -> Result<Vec<Address>> {

Ok(addresses)
}

fn initialize_wallet(options: &Options, seed: [u8; 64]) -> Result {
let client = options.bitcoin_rpc_client_for_wallet_command(true)?;
let network = options.chain().network();

client.create_wallet(&options.wallet, None, Some(true), None, None)?;

let secp = Secp256k1::new();

let master_private_key = ExtendedPrivKey::new_master(network, &seed)?;

let fingerprint = master_private_key.fingerprint(&secp);

let derivation_path = DerivationPath::master()
.child(ChildNumber::Hardened { index: 86 })
.child(ChildNumber::Hardened {
index: u32::from(network != Network::Bitcoin),
})
.child(ChildNumber::Hardened { index: 0 });

let derived_private_key = master_private_key.derive_priv(&secp, &derivation_path)?;

for change in [false, true] {
derive_and_import_descriptor(
&client,
&secp,
(fingerprint, derivation_path.clone()),
derived_private_key,
change,
)?;
}

Ok(())
}

fn derive_and_import_descriptor(
client: &Client,
secp: &Secp256k1<All>,
origin: (Fingerprint, DerivationPath),
derived_private_key: ExtendedPrivKey,
change: bool,
) -> Result {
let secret_key = DescriptorSecretKey::XPrv(DescriptorXKey {
origin: Some(origin),
xkey: derived_private_key,
derivation_path: DerivationPath::master().child(ChildNumber::Normal {
index: change.into(),
}),
wildcard: Wildcard::Unhardened,
});

let public_key = secret_key.to_public(secp)?;

let mut key_map = std::collections::HashMap::new();
key_map.insert(public_key.clone(), secret_key);

let desc = Descriptor::new_tr(public_key, None)?;

client.import_descriptors(ImportDescriptors {
descriptor: desc.to_string_with_secret(&key_map),
timestamp: Timestamp::Now,
active: Some(true),
range: None,
next_index: None,
internal: Some(!change),
label: None,
})?;

Ok(())
}
Loading