feat: add display trait to descriptor

This commit is contained in:
Matthew 2024-06-12 20:08:14 -05:00
parent 94d31ff7ed
commit e97e9b731c
No known key found for this signature in database
GPG Key ID: 8D4FCD82DD54DDD2
9 changed files with 44 additions and 42 deletions

View File

@ -15,7 +15,7 @@ class OfflineDescriptorTest {
assertEquals( assertEquals(
expected = "tr([be1eec8f/86'/1'/0']tpubDCTtszwSxPx3tATqDrsSyqScPNnUChwQAVAkanuDUCJQESGBbkt68nXXKRDifYSDbeMa2Xg2euKbXaU3YphvGWftDE7ozRKPriT6vAo3xsc/0/*)#m7puekcx", expected = "tr([be1eec8f/86'/1'/0']tpubDCTtszwSxPx3tATqDrsSyqScPNnUChwQAVAkanuDUCJQESGBbkt68nXXKRDifYSDbeMa2Xg2euKbXaU3YphvGWftDE7ozRKPriT6vAo3xsc/0/*)#m7puekcx",
actual = descriptor.asString() actual = descriptor.toString()
) )
} }
} }

View File

@ -29,7 +29,7 @@ class OfflineWalletTest {
val descriptorSecretKey: DescriptorSecretKey = DescriptorSecretKey(Network.TESTNET, mnemonic, null) val descriptorSecretKey: DescriptorSecretKey = DescriptorSecretKey(Network.TESTNET, mnemonic, null)
val descriptor: Descriptor = Descriptor.newBip86(descriptorSecretKey, KeychainKind.EXTERNAL, Network.TESTNET) 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 @Test

View File

@ -502,6 +502,7 @@ interface DescriptorPublicKey {
string as_string(); string as_string();
}; };
[Traits=(Display)]
interface Descriptor { interface Descriptor {
[Throws=DescriptorError] [Throws=DescriptorError]
constructor(string descriptor, Network network); constructor(string descriptor, Network network);
@ -530,9 +531,7 @@ interface Descriptor {
[Name=new_bip86_public] [Name=new_bip86_public]
constructor([ByRef] DescriptorPublicKey public_key, string fingerprint, KeychainKind keychain, Network network); constructor([ByRef] DescriptorPublicKey public_key, string fingerprint, KeychainKind keychain, Network network);
string as_string(); string to_string_with_secret();
string as_string_private();
}; };
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

View File

@ -1,6 +1,7 @@
use crate::error::DescriptorError; use crate::error::DescriptorError;
use crate::keys::DescriptorPublicKey; use crate::keys::DescriptorPublicKey;
use crate::keys::DescriptorSecretKey; use crate::keys::DescriptorSecretKey;
use std::fmt::Display;
use bdk_wallet::bitcoin::bip32::Fingerprint; use bdk_wallet::bitcoin::bip32::Fingerprint;
use bdk_wallet::bitcoin::key::Secp256k1; use bdk_wallet::bitcoin::key::Secp256k1;
@ -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 descriptor = &self.extended_descriptor;
let key_map = &self.key_map; let key_map = &self.key_map;
descriptor.to_string_with_secret(key_map) descriptor.to_string_with_secret(key_map)
} }
}
pub(crate) fn as_string(&self) -> String { impl Display for Descriptor {
self.extended_descriptor.to_string() fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.extended_descriptor)
} }
} }
@ -321,10 +324,10 @@ mod test {
let template_private_86 = let template_private_86 =
Descriptor::new_bip86(&master, KeychainKind::External, Network::Testnet); 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 // 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 49: {}", template_private_49);
println!("Template 44: {}", template_private_44.as_string()); println!("Template 44: {}", template_private_44);
println!("Template 84: {}", template_private_84.as_string()); println!("Template 84: {}", template_private_84);
println!("Template 86: {}", template_private_86.as_string()); println!("Template 86: {}", template_private_86);
let template_public_44 = Descriptor::new_bip44_public( let template_public_44 = Descriptor::new_bip44_public(
&handmade_public_44, &handmade_public_44,
"d1d04177".to_string(), "d1d04177".to_string(),
@ -349,43 +352,43 @@ mod test {
KeychainKind::External, KeychainKind::External,
Network::Testnet, Network::Testnet,
); );
println!("Template public 49: {}", template_public_49.as_string()); println!("Template public 49: {}", template_public_49);
println!("Template public 44: {}", template_public_44.as_string()); println!("Template public 44: {}", template_public_44);
println!("Template public 84: {}", template_public_84.as_string()); println!("Template public 84: {}", template_public_84);
println!("Template public 86: {}", template_public_86.as_string()); println!("Template public 86: {}", template_public_86);
// when using a public key, both as_string and as_string_private return the same string // when using a public key, both to_string and as_string_private return the same string
assert_eq!( assert_eq!(
template_public_44.as_string_private(), template_public_44.to_string_with_secret(),
template_public_44.as_string() template_public_44.to_string()
); );
assert_eq!( assert_eq!(
template_public_49.as_string_private(), template_public_49.to_string_with_secret(),
template_public_49.as_string() template_public_49.to_string()
); );
assert_eq!( assert_eq!(
template_public_84.as_string_private(), template_public_84.to_string_with_secret(),
template_public_84.as_string() template_public_84.to_string()
); );
assert_eq!( assert_eq!(
template_public_86.as_string_private(), template_public_86.to_string_with_secret(),
template_public_86.as_string() 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!( assert_eq!(
template_private_44.as_string(), template_private_44.to_string(),
template_public_44.as_string() template_public_44.to_string()
); );
assert_eq!( assert_eq!(
template_private_49.as_string(), template_private_49.to_string(),
template_public_49.as_string() template_public_49.to_string()
); );
assert_eq!( assert_eq!(
template_private_84.as_string(), template_private_84.to_string(),
template_public_84.as_string() template_public_84.to_string()
); );
assert_eq!( assert_eq!(
template_private_86.as_string(), template_private_86.to_string(),
template_public_86.as_string() template_public_86.to_string()
); );
} }
#[test] #[test]

View File

@ -36,8 +36,8 @@ impl Wallet {
persistence_backend_path: String, persistence_backend_path: String,
network: Network, network: Network,
) -> Result<Self, WalletCreationError> { ) -> Result<Self, WalletCreationError> {
let descriptor = descriptor.as_string_private(); let descriptor = descriptor.to_string_with_secret();
let change_descriptor = change_descriptor.map(|d| d.as_string_private()); let change_descriptor = change_descriptor.map(|d| d.to_string_with_secret());
let connection = Connection::open(persistence_backend_path)?; let connection = Connection::open(persistence_backend_path)?;
let db = Store::new(connection)?; let db = Store::new(connection)?;
@ -54,8 +54,8 @@ impl Wallet {
change_descriptor: Option<Arc<Descriptor>>, change_descriptor: Option<Arc<Descriptor>>,
network: Network, network: Network,
) -> Result<Self, DescriptorError> { ) -> Result<Self, DescriptorError> {
let descriptor = descriptor.as_string_private(); let descriptor = descriptor.to_string_with_secret();
let change_descriptor = change_descriptor.map(|d| d.as_string_private()); let change_descriptor = change_descriptor.map(|d| d.to_string_with_secret());
let wallet: BdkWallet = let wallet: BdkWallet =
BdkWallet::new_no_persist(&descriptor, change_descriptor.as_ref(), network)?; BdkWallet::new_no_persist(&descriptor, change_descriptor.as_ref(), network)?;

View File

@ -12,7 +12,7 @@ class OfflineDescriptorTest {
assertEquals( assertEquals(
expected = "tr([be1eec8f/86'/1'/0']tpubDCTtszwSxPx3tATqDrsSyqScPNnUChwQAVAkanuDUCJQESGBbkt68nXXKRDifYSDbeMa2Xg2euKbXaU3YphvGWftDE7ozRKPriT6vAo3xsc/0/*)#m7puekcx", expected = "tr([be1eec8f/86'/1'/0']tpubDCTtszwSxPx3tATqDrsSyqScPNnUChwQAVAkanuDUCJQESGBbkt68nXXKRDifYSDbeMa2Xg2euKbXaU3YphvGWftDE7ozRKPriT6vAo3xsc/0/*)#m7puekcx",
actual = descriptor.asString() actual = descriptor.toString()
) )
} }
} }

View File

@ -27,7 +27,7 @@ class OfflineWalletTest {
val descriptorSecretKey: DescriptorSecretKey = DescriptorSecretKey(Network.TESTNET, mnemonic, null) val descriptorSecretKey: DescriptorSecretKey = DescriptorSecretKey(Network.TESTNET, mnemonic, null)
val descriptor: Descriptor = Descriptor.newBip86(descriptorSecretKey, KeychainKind.EXTERNAL, Network.TESTNET) 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 @Test

View File

@ -10,7 +10,7 @@ class OfflineDescriptorTest(unittest.TestCase):
self.assertEqual( self.assertEqual(
"tr([be1eec8f/86'/1'/0']tpubDCTtszwSxPx3tATqDrsSyqScPNnUChwQAVAkanuDUCJQESGBbkt68nXXKRDifYSDbeMa2Xg2euKbXaU3YphvGWftDE7ozRKPriT6vAo3xsc/0/*)#m7puekcx", "tr([be1eec8f/86'/1'/0']tpubDCTtszwSxPx3tATqDrsSyqScPNnUChwQAVAkanuDUCJQESGBbkt68nXXKRDifYSDbeMa2Xg2euKbXaU3YphvGWftDE7ozRKPriT6vAo3xsc/0/*)#m7puekcx",
descriptor.as_string() descriptor.__str__()
) )

View File

@ -15,6 +15,6 @@ final class OfflineDescriptorTests: XCTestCase {
network: Network.testnet 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")
} }
} }