Skip to content

Commit

Permalink
Add --address-type flag to wallet create and wallet restore.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmart7t2 committed Nov 4, 2023
1 parent 814528e commit d0b5b9d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/subcommand/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ impl Preview {

super::wallet::Wallet::Create(super::wallet::create::Create {
passphrase: "".into(),
address_type: super::wallet::AddressType::Bech32m,
})
.run(options.clone())?;

Expand Down
26 changes: 23 additions & 3 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ pub(crate) enum Wallet {
Cardinals,
}

#[derive(clap::ValueEnum, Clone, Debug)]
pub(crate) enum AddressType {
Legacy,
P2SHSegwit,
Bech32,
Bech32m,
}

impl Wallet {
pub(crate) fn run(self, options: Options) -> SubcommandResult {
match self {
Expand Down Expand Up @@ -80,7 +88,7 @@ fn get_change_address(client: &Client, chain: Chain) -> Result<Address> {
)
}

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

Expand All @@ -93,7 +101,12 @@ pub(crate) fn initialize_wallet(options: &Options, seed: [u8; 64]) -> Result {
let fingerprint = master_private_key.fingerprint(&secp);

let derivation_path = DerivationPath::master()
.child(ChildNumber::Hardened { index: 86 })
.child(ChildNumber::Hardened { index: match address_type {
AddressType::Legacy => 44,
AddressType::P2SHSegwit => 49,
AddressType::Bech32 => 84,
AddressType::Bech32m => 86,
} })
.child(ChildNumber::Hardened {
index: u32::from(network != Network::Bitcoin),
})
Expand All @@ -108,6 +121,7 @@ pub(crate) fn initialize_wallet(options: &Options, seed: [u8; 64]) -> Result {
(fingerprint, derivation_path.clone()),
derived_private_key,
change,
&address_type,
)?;
}

Expand All @@ -120,6 +134,7 @@ fn derive_and_import_descriptor(
origin: (Fingerprint, DerivationPath),
derived_private_key: ExtendedPrivKey,
change: bool,
address_type: &AddressType,
) -> Result {
let secret_key = DescriptorSecretKey::XPrv(DescriptorXKey {
origin: Some(origin),
Expand All @@ -135,7 +150,12 @@ fn derive_and_import_descriptor(
let mut key_map = std::collections::HashMap::new();
key_map.insert(public_key.clone(), secret_key);

let desc = Descriptor::new_tr(public_key, None)?;
let desc = match address_type {
AddressType::Legacy => Descriptor::new_pkh(public_key),
AddressType::P2SHSegwit => Descriptor::new_sh_wpkh(public_key),
AddressType::Bech32 => Descriptor::new_wpkh(public_key),
AddressType::Bech32m => Descriptor::new_tr(public_key, None),
}?;

client.import_descriptors(ImportDescriptors {
descriptor: desc.to_string_with_secret(&key_map),
Expand Down
4 changes: 3 additions & 1 deletion src/subcommand/wallet/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub(crate) struct Create {
help = "Use <PASSPHRASE> to derive wallet seed."
)]
pub(crate) passphrase: String,
#[arg(long, value_enum, default_value="bech32m")]
pub(crate) address_type: AddressType,
}

impl Create {
Expand All @@ -23,7 +25,7 @@ impl Create {

let mnemonic = Mnemonic::from_entropy(&entropy)?;

initialize_wallet(&options, mnemonic.to_seed(self.passphrase.clone()))?;
initialize_wallet(&options, mnemonic.to_seed(self.passphrase.clone()), self.address_type)?;

Ok(Box::new(Output {
mnemonic,
Expand Down
4 changes: 3 additions & 1 deletion src/subcommand/wallet/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ pub(crate) struct Restore {
help = "Use <PASSPHRASE> when deriving wallet"
)]
pub(crate) passphrase: String,
#[arg(long, value_enum, default_value="bech32m")]
pub(crate) address_type: AddressType,
}

impl Restore {
pub(crate) fn run(self, options: Options) -> SubcommandResult {
initialize_wallet(&options, self.mnemonic.to_seed(self.passphrase))?;
initialize_wallet(&options, self.mnemonic.to_seed(self.passphrase), self.address_type)?;
Ok(Box::new(Empty {}))
}
}

0 comments on commit d0b5b9d

Please sign in to comment.