From f92b45db6a235a67287e96278a43684d09d6b7c5 Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Mon, 26 Sep 2022 10:36:40 -0400 Subject: [PATCH] Add ability to retrieve private keys as bytes This feature is needed for compatibility with LDKLite, where the initial entropy given to LDK is the private key of the root of the BIP32 derivation tree. Closes #188 --- CHANGELOG.md | 4 ++++ src/bdk.udl | 2 ++ src/lib.rs | 16 ++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 721b0c3..cb2553a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +- APIs Added + - Add ability to retrieve any private key inside a descriptor as bytes [#199] + +[#199]: https://github.com/bitcoindevkit/bdk-ffi/pull/199 ## [v0.9.0] - Breaking Changes diff --git a/src/bdk.udl b/src/bdk.udl index e995080..f1a4429 100644 --- a/src/bdk.udl +++ b/src/bdk.udl @@ -283,6 +283,8 @@ interface DescriptorSecretKey { DescriptorPublicKey as_public(); + sequence secret_key_bytes(); + string as_string(); }; diff --git a/src/lib.rs b/src/lib.rs index 8f772ca..c7d9972 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -907,6 +907,22 @@ impl DescriptorSecretKey { }) } + /// Get the private key as bytes. + fn secret_key_bytes(&self) -> Vec { + let descriptor_secret_key = self.descriptor_secret_key_mutex.lock().unwrap(); + let secret_key_bytes: Vec = match descriptor_secret_key.deref() { + BdkDescriptorSecretKey::XPrv(descriptor_x_key) => { + descriptor_x_key.xkey.private_key.secret_bytes().to_vec() + } + BdkDescriptorSecretKey::SinglePriv(descriptor_x_key) => { + // unreachable!() + descriptor_x_key.key.inner.secret_bytes().to_vec() + } + }; + + secret_key_bytes + } + fn as_string(&self) -> String { self.descriptor_secret_key_mutex.lock().unwrap().to_string() }