Skip to content

Commit

Permalink
feat: add display trait to descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Jun 13, 2024
1 parent 94d31ff commit e97e9b7
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OfflineDescriptorTest {

assertEquals(
expected = "tr([be1eec8f/86'/1'/0']tpubDCTtszwSxPx3tATqDrsSyqScPNnUChwQAVAkanuDUCJQESGBbkt68nXXKRDifYSDbeMa2Xg2euKbXaU3YphvGWftDE7ozRKPriT6vAo3xsc/0/*)#m7puekcx",
actual = descriptor.asString()
actual = descriptor.toString()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class OfflineWalletTest {
val descriptorSecretKey: DescriptorSecretKey = DescriptorSecretKey(Network.TESTNET, mnemonic, null)
val descriptor: Descriptor = Descriptor.newBip86(descriptorSecretKey, KeychainKind.EXTERNAL, Network.TESTNET)

assertTrue(descriptor.asString().startsWith("tr"), "Bip86 Descriptor does not start with 'tr'")
assertTrue(descriptor.toString().startsWith("tr"), "Bip86 Descriptor does not start with 'tr'")
}

@Test
Expand Down
5 changes: 2 additions & 3 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ interface DescriptorPublicKey {
string as_string();
};

[Traits=(Display)]
interface Descriptor {
[Throws=DescriptorError]
constructor(string descriptor, Network network);
Expand Down Expand Up @@ -530,9 +531,7 @@ interface Descriptor {
[Name=new_bip86_public]
constructor([ByRef] DescriptorPublicKey public_key, string fingerprint, KeychainKind keychain, Network network);

string as_string();

string as_string_private();
string to_string_with_secret();
};

// ------------------------------------------------------------------------
Expand Down
61 changes: 32 additions & 29 deletions bdk-ffi/src/descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::DescriptorError;
use crate::keys::DescriptorPublicKey;
use crate::keys::DescriptorSecretKey;
use std::fmt::Display;

use bdk_wallet::bitcoin::bip32::Fingerprint;
use bdk_wallet::bitcoin::key::Secp256k1;
Expand Down Expand Up @@ -260,14 +261,16 @@ impl Descriptor {
}
}

pub(crate) fn as_string_private(&self) -> String {
pub(crate) fn to_string_with_secret(&self) -> String {
let descriptor = &self.extended_descriptor;
let key_map = &self.key_map;
descriptor.to_string_with_secret(key_map)
}
}

pub(crate) fn as_string(&self) -> String {
self.extended_descriptor.to_string()
impl Display for Descriptor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.extended_descriptor)
}
}

Expand Down Expand Up @@ -321,10 +324,10 @@ mod test {
let template_private_86 =
Descriptor::new_bip86(&master, KeychainKind::External, Network::Testnet);
// the extended public keys are the same when creating them manually as they are with the templates
println!("Template 49: {}", template_private_49.as_string());
println!("Template 44: {}", template_private_44.as_string());
println!("Template 84: {}", template_private_84.as_string());
println!("Template 86: {}", template_private_86.as_string());
println!("Template 49: {}", template_private_49);
println!("Template 44: {}", template_private_44);
println!("Template 84: {}", template_private_84);
println!("Template 86: {}", template_private_86);
let template_public_44 = Descriptor::new_bip44_public(
&handmade_public_44,
"d1d04177".to_string(),
Expand All @@ -349,43 +352,43 @@ mod test {
KeychainKind::External,
Network::Testnet,
);
println!("Template public 49: {}", template_public_49.as_string());
println!("Template public 44: {}", template_public_44.as_string());
println!("Template public 84: {}", template_public_84.as_string());
println!("Template public 86: {}", template_public_86.as_string());
// when using a public key, both as_string and as_string_private return the same string
println!("Template public 49: {}", template_public_49);
println!("Template public 44: {}", template_public_44);
println!("Template public 84: {}", template_public_84);
println!("Template public 86: {}", template_public_86);
// when using a public key, both to_string and as_string_private return the same string
assert_eq!(
template_public_44.as_string_private(),
template_public_44.as_string()
template_public_44.to_string_with_secret(),
template_public_44.to_string()
);
assert_eq!(
template_public_49.as_string_private(),
template_public_49.as_string()
template_public_49.to_string_with_secret(),
template_public_49.to_string()
);
assert_eq!(
template_public_84.as_string_private(),
template_public_84.as_string()
template_public_84.to_string_with_secret(),
template_public_84.to_string()
);
assert_eq!(
template_public_86.as_string_private(),
template_public_86.as_string()
template_public_86.to_string_with_secret(),
template_public_86.to_string()
);
// when using as_string on a private key, we get the same result as when using it on a public key
// when using to_string on a private key, we get the same result as when using it on a public key
assert_eq!(
template_private_44.as_string(),
template_public_44.as_string()
template_private_44.to_string(),
template_public_44.to_string()
);
assert_eq!(
template_private_49.as_string(),
template_public_49.as_string()
template_private_49.to_string(),
template_public_49.to_string()
);
assert_eq!(
template_private_84.as_string(),
template_public_84.as_string()
template_private_84.to_string(),
template_public_84.to_string()
);
assert_eq!(
template_private_86.as_string(),
template_public_86.as_string()
template_private_86.to_string(),
template_public_86.to_string()
);
}
#[test]
Expand Down
8 changes: 4 additions & 4 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ impl Wallet {
persistence_backend_path: String,
network: Network,
) -> Result<Self, WalletCreationError> {
let descriptor = descriptor.as_string_private();
let change_descriptor = change_descriptor.map(|d| d.as_string_private());
let descriptor = descriptor.to_string_with_secret();
let change_descriptor = change_descriptor.map(|d| d.to_string_with_secret());
let connection = Connection::open(persistence_backend_path)?;
let db = Store::new(connection)?;

Expand All @@ -54,8 +54,8 @@ impl Wallet {
change_descriptor: Option<Arc<Descriptor>>,
network: Network,
) -> Result<Self, DescriptorError> {
let descriptor = descriptor.as_string_private();
let change_descriptor = change_descriptor.map(|d| d.as_string_private());
let descriptor = descriptor.to_string_with_secret();
let change_descriptor = change_descriptor.map(|d| d.to_string_with_secret());

let wallet: BdkWallet =
BdkWallet::new_no_persist(&descriptor, change_descriptor.as_ref(), network)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class OfflineDescriptorTest {

assertEquals(
expected = "tr([be1eec8f/86'/1'/0']tpubDCTtszwSxPx3tATqDrsSyqScPNnUChwQAVAkanuDUCJQESGBbkt68nXXKRDifYSDbeMa2Xg2euKbXaU3YphvGWftDE7ozRKPriT6vAo3xsc/0/*)#m7puekcx",
actual = descriptor.asString()
actual = descriptor.toString()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class OfflineWalletTest {
val descriptorSecretKey: DescriptorSecretKey = DescriptorSecretKey(Network.TESTNET, mnemonic, null)
val descriptor: Descriptor = Descriptor.newBip86(descriptorSecretKey, KeychainKind.EXTERNAL, Network.TESTNET)

assertTrue(descriptor.asString().startsWith("tr"), "Bip86 Descriptor does not start with 'tr'")
assertTrue(descriptor.toString().startsWith("tr"), "Bip86 Descriptor does not start with 'tr'")
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion bdk-python/tests/test_offline_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_descriptor_bip86(self):

self.assertEqual(
"tr([be1eec8f/86'/1'/0']tpubDCTtszwSxPx3tATqDrsSyqScPNnUChwQAVAkanuDUCJQESGBbkt68nXXKRDifYSDbeMa2Xg2euKbXaU3YphvGWftDE7ozRKPriT6vAo3xsc/0/*)#m7puekcx",
descriptor.as_string()
descriptor.__str__()
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ final class OfflineDescriptorTests: XCTestCase {
network: Network.testnet
)

XCTAssertEqual(descriptor.asString(), "tr([be1eec8f/86'/1'/0']tpubDCTtszwSxPx3tATqDrsSyqScPNnUChwQAVAkanuDUCJQESGBbkt68nXXKRDifYSDbeMa2Xg2euKbXaU3YphvGWftDE7ozRKPriT6vAo3xsc/0/*)#m7puekcx")
XCTAssertEqual(descriptor.description, "tr([be1eec8f/86'/1'/0']tpubDCTtszwSxPx3tATqDrsSyqScPNnUChwQAVAkanuDUCJQESGBbkt68nXXKRDifYSDbeMa2Xg2euKbXaU3YphvGWftDE7ozRKPriT6vAo3xsc/0/*)#m7puekcx")
}
}

0 comments on commit e97e9b7

Please sign in to comment.