Skip to content

Commit

Permalink
Add bip44, bip49, and bip84 private templates
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Nov 24, 2022
1 parent 0850905 commit 8883c2c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 24 deletions.
20 changes: 8 additions & 12 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -343,26 +343,22 @@ interface Descriptor {
[Name=new_bip44]
constructor(DescriptorSecretKey secret_key, KeychainKind keychain);

[Name=new_bip44_public]
constructor(DescriptorPublicKey public_key, string? fingerprint, KeychainKind keychain);
// [Name=new_bip44_public]
// constructor(DescriptorPublicKey public_key, string fingerprint, KeychainKind keychain);

[Name=new_bip49]
constructor(DescriptorSecretKey secret_key, KeychainKind keychain);

[Name=new_bip49_public]
constructor(DescriptorPublicKey public_key, string? fingerprint, KeychainKind keychain);
// [Name=new_bip49_public]
// constructor(DescriptorPublicKey public_key, string fingerprint, KeychainKind keychain);

[Name=new_bip84]
constructor(DescriptorSecretKey secret_key, KeychainKind keychain);

[Name=new_bip84_public]
constructor(DescriptorPublicKey public_key, string? fingerprint, KeychainKind keychain);
// [Name=new_bip84_public]
// constructor(DescriptorPublicKey public_key, string fingerprint, KeychainKind keychain);

[Name=new_bip86]
constructor(DescriptorSecretKey secret_key, KeychainKind keychain);

[Name=new_bip86_public]
constructor(DescriptorPublicKey public_key, string? fingerprint, KeychainKind keychain);
string as_string_public();

string as_string();
string as_string_private();
};
85 changes: 73 additions & 12 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use bdk::blockchain::GetHeight;
use bdk::blockchain::{
electrum::ElectrumBlockchainConfig, esplora::EsploraBlockchainConfig, ConfigurableBlockchain,
};
use bdk::descriptor::Descriptor as BdkDescriptor;
use bdk::blockchain::{Blockchain as BdkBlockchain, Progress as BdkProgress};
use bdk::database::any::{AnyDatabase, SledDbConfiguration, SqliteDbConfiguration};
use bdk::database::{AnyDatabaseConfig, ConfigurableDatabase};
Expand All @@ -34,7 +35,7 @@ use std::fmt;
use std::ops::Deref;
use std::str::FromStr;
use std::sync::{Arc, Mutex, MutexGuard};
use bdk::template::{Bip44, DescriptorTemplate, DescriptorTemplateOut};
use bdk::template::{Bip44, Bip44Public, Bip49, Bip84, DescriptorTemplate, DescriptorTemplateOut};

uniffi_macros::include_scaffolding!("bdk");

Expand Down Expand Up @@ -1026,11 +1027,11 @@ impl DescriptorSecretKey {
}

#[derive(Debug)]
struct DescriptorPR260 {
struct Descriptor {
pub descriptor: DescriptorTemplateOut,
}

impl DescriptorPR260 {
impl Descriptor {
fn new_bip44(secret_key: DescriptorSecretKey, keychain_kind: KeychainKind, network: Network) -> Self {
let derivable_key = secret_key.descriptor_secret_key_mutex.lock().unwrap();
match derivable_key.deref() {
Expand All @@ -1047,12 +1048,61 @@ impl DescriptorPR260 {
}
}

// Note that DescriptorTemplateOut is a type alias for (ExtendedDescriptor, KeyMap, ValidNetworks)
// And ExtendedDescriptor is a type alias for Descriptor<DescriptorPublicKey>
// So the following only ever prints the xpub
// I'm not sure how to best retrieve a "clean" string for the private descriptor
// The kind we could return to the user so they can save it
fn as_string(&self) -> String {
// fn new_bip44_public(secret_key: DescriptorSecretKey, fingerprint: [u8; 4], keychain_kind: KeychainKind, network: Network) -> Self {
// let derivable_key = secret_key.descriptor_secret_key_mutex.lock().unwrap();
// match derivable_key.deref() {
// BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
// let derivable_key = descriptor_x_key.xkey;
// let descriptor_template_out = Bip44Public(derivable_key, fingerprint, keychain_kind).build(network).unwrap();
// Self {
// descriptor: descriptor_template_out
// }
// }
// BdkDescriptorSecretKey::Single(_) => {
// unreachable!()
// }
// }
// }

fn new_bip49(secret_key: DescriptorSecretKey, keychain_kind: KeychainKind, network: Network) -> Self {
let derivable_key = secret_key.descriptor_secret_key_mutex.lock().unwrap();
match derivable_key.deref() {
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
let derivable_key = descriptor_x_key.xkey;
let descriptor_template_out = Bip49(derivable_key, keychain_kind).build(network).unwrap();
Self {
descriptor: descriptor_template_out
}
}
BdkDescriptorSecretKey::Single(_) => {
unreachable!()
}
}
}

fn new_bip84(secret_key: DescriptorSecretKey, keychain_kind: KeychainKind, network: Network) -> Self {
let derivable_key = secret_key.descriptor_secret_key_mutex.lock().unwrap();
match derivable_key.deref() {
BdkDescriptorSecretKey::XPrv(descriptor_x_key) => {
let derivable_key = descriptor_x_key.xkey;
let descriptor_template_out = Bip84(derivable_key, keychain_kind).build(network).unwrap();
Self {
descriptor: descriptor_template_out
}
}
BdkDescriptorSecretKey::Single(_) => {
unreachable!()
}
}
}

fn as_string_private(&self) -> String {
let descriptor = &self.descriptor.0;
let key_map = &self.descriptor.1;
descriptor.to_string_with_secret(key_map)
}

fn as_string_public(&self) -> String {
self.descriptor.0.to_string()
}
}
Expand Down Expand Up @@ -1325,9 +1375,20 @@ mod test {

#[test]
fn test_descriptor_template() {
let master_dsk: DescriptorSecretKey = get_descriptor_secret_key();
let bip44_descriptor = DescriptorPR260::new_bip44(master_dsk, KeychainKind::External, Network::Testnet);
println!("{:?}", bip44_descriptor);
let master_dsk0: DescriptorSecretKey = get_descriptor_secret_key();
let master_dsk1: DescriptorSecretKey = get_descriptor_secret_key();
let master_dsk2: DescriptorSecretKey = get_descriptor_secret_key();

let bip44_descriptor = DescriptorPR260::new_bip44(master_dsk0, KeychainKind::External, Network::Testnet);
let bip49_descriptor = DescriptorPR260::new_bip49(master_dsk1, KeychainKind::External, Network::Testnet);
let bip84_descriptor = DescriptorPR260::new_bip84(master_dsk2, KeychainKind::External, Network::Testnet);

println!("{}", bip44_descriptor.as_string());
println!("{}", bip49_descriptor.as_string());
println!("{}", bip84_descriptor.as_string());

println!("{}", bip44_descriptor.as_string_public());
println!("{}", bip49_descriptor.as_string_public());
println!("{}", bip84_descriptor.as_string_public());
}
}

0 comments on commit 8883c2c

Please sign in to comment.