Skip to content

Commit f024521

Browse files
committed
remove and replaced FFIExtendedPublicKey with FFIExtendedPubKey
1 parent 383b306 commit f024521

4 files changed

Lines changed: 37 additions & 29 deletions

File tree

key-wallet-ffi/FFI_API.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,11 +1461,11 @@ Derive extended private key at a specific path Returns an opaque FFIExtendedPriv
14611461
#### `wallet_derive_extended_public_key`
14621462

14631463
```c
1464-
wallet_derive_extended_public_key(wallet: *const FFIWallet, derivation_path: *const c_char, error: *mut FFIError,) -> *mut FFIExtendedPublicKey
1464+
wallet_derive_extended_public_key(wallet: *const FFIWallet, derivation_path: *const c_char, error: *mut FFIError,) -> *mut FFIExtendedPubKey
14651465
```
14661466

14671467
**Description:**
1468-
Derive extended public key at a specific path Returns an opaque FFIExtendedPublicKey pointer that must be freed with extended_public_key_free # Safety - `wallet` must be a valid pointer to an FFIWallet - `derivation_path` must be a valid null-terminated C string - `error` must be a valid pointer to an FFIError - The returned pointer must be freed with `extended_public_key_free`
1468+
Derive extended public key at a specific path Returns an opaque FFIExtendedPubKey pointer that must be freed with extended_public_key_free # Safety - `wallet` must be a valid pointer to an FFIWallet - `derivation_path` must be a valid null-terminated C string - `error` must be a valid pointer to an FFIError - The returned pointer must be freed with `extended_public_key_free`
14691469

14701470
**Safety:**
14711471
- `wallet` must be a valid pointer to an FFIWallet - `derivation_path` must be a valid null-terminated C string - `error` must be a valid pointer to an FFIError - The returned pointer must be freed with `extended_public_key_free`
@@ -3907,7 +3907,7 @@ Get extended private key as string (xprv format) Returns the extended private k
39073907
#### `extended_public_key_free`
39083908

39093909
```c
3910-
extended_public_key_free(key: *mut FFIExtendedPublicKey) -> ()
3910+
extended_public_key_free(key: *mut FFIExtendedPubKey) -> ()
39113911
```
39123912

39133913
**Description:**
@@ -3923,14 +3923,14 @@ Free an extended public key # Safety - `key` must be a valid pointer created b
39233923
#### `extended_public_key_get_public_key`
39243924

39253925
```c
3926-
extended_public_key_get_public_key(extended_key: *const FFIExtendedPublicKey, error: *mut FFIError,) -> *mut FFIPublicKey
3926+
extended_public_key_get_public_key(extended_key: *const FFIExtendedPubKey, error: *mut FFIError,) -> *mut FFIPublicKey
39273927
```
39283928

39293929
**Description:**
3930-
Get the public key from an extended public key Extracts the non-extended public key from an extended public key. # Safety - `extended_key` must be a valid pointer to an FFIExtendedPublicKey - `error` must be a valid pointer to an FFIError - The returned FFIPublicKey must be freed with `public_key_free`
3930+
Get the public key from an extended public key Extracts the non-extended public key from an extended public key. # Safety - `extended_key` must be a valid pointer to an FFIExtendedPubKey - `error` must be a valid pointer to an FFIError - The returned FFIPublicKey must be freed with `public_key_free`
39313931

39323932
**Safety:**
3933-
- `extended_key` must be a valid pointer to an FFIExtendedPublicKey - `error` must be a valid pointer to an FFIError - The returned FFIPublicKey must be freed with `public_key_free`
3933+
- `extended_key` must be a valid pointer to an FFIExtendedPubKey - `error` must be a valid pointer to an FFIError - The returned FFIPublicKey must be freed with `public_key_free`
39343934

39353935
**Module:** `keys`
39363936

@@ -3939,14 +3939,14 @@ Get the public key from an extended public key Extracts the non-extended public
39393939
#### `extended_public_key_to_string`
39403940

39413941
```c
3942-
extended_public_key_to_string(key: *const FFIExtendedPublicKey, network: FFINetwork, error: *mut FFIError,) -> *mut c_char
3942+
extended_public_key_to_string(key: *const FFIExtendedPubKey, network: FFINetwork, error: *mut FFIError,) -> *mut c_char
39433943
```
39443944

39453945
**Description:**
3946-
Get extended public key as string (xpub format) Returns the extended public key in base58 format (xpub... for mainnet, tpub... for testnet) # Safety - `key` must be a valid pointer to an FFIExtendedPublicKey - `network` is ignored; the network is encoded in the extended key - `error` must be a valid pointer to an FFIError - The returned string must be freed with `string_free`
3946+
Get extended public key as string (xpub format) Returns the extended public key in base58 format (xpub... for mainnet, tpub... for testnet) # Safety - `key` must be a valid pointer to an FFIExtendedPubKey - `network` is ignored; the network is encoded in the extended key - `error` must be a valid pointer to an FFIError - The returned string must be freed with `string_free`
39473947

39483948
**Safety:**
3949-
- `key` must be a valid pointer to an FFIExtendedPublicKey - `network` is ignored; the network is encoded in the extended key - `error` must be a valid pointer to an FFIError - The returned string must be freed with `string_free`
3949+
- `key` must be a valid pointer to an FFIExtendedPubKey - `network` is ignored; the network is encoded in the extended key - `error` must be a valid pointer to an FFIError - The returned string must be freed with `string_free`
39503950

39513951
**Module:** `keys`
39523952

key-wallet-ffi/cbindgen.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ item_types = ["functions", "enums", "structs", "typedefs", "opaque", "constants"
3535
"FFIPrivateKey" = "{ unsigned char _private[0]; }"
3636
"FFIExtendedPrivateKey" = "{ unsigned char _private[0]; }"
3737
"FFIPublicKey" = "{ unsigned char _private[0]; }"
38-
"FFIExtendedPublicKey" = "{ unsigned char _private[0]; }"
38+
"FFIExtendedPubKey" = "{ unsigned char _private[0]; }"
3939
"FFIWalletManager" = "{ unsigned char _private[0]; }"
4040
"FFIWallet" = "{ unsigned char _private[0]; }"
4141
"FFIAccount" = "{ unsigned char _private[0]; }"

key-wallet-ffi/src/derivation.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! BIP32 and DIP9 derivation path functions
22
33
use crate::error::{FFIError, FFIErrorCode};
4+
use crate::keys::FFIExtendedPubKey;
45
use crate::types::FFINetwork;
56
use dashcore::Network;
67
use key_wallet::{ExtendedPrivKey, ExtendedPubKey};
@@ -38,11 +39,6 @@ pub struct FFIExtendedPrivKey {
3839
inner: key_wallet::bip32::ExtendedPrivKey,
3940
}
4041

41-
/// Extended public key structure
42-
pub struct FFIExtendedPubKey {
43-
inner: key_wallet::bip32::ExtendedPubKey,
44-
}
45-
4642
/// Create a new master extended private key from seed
4743
///
4844
/// # Safety
@@ -537,9 +533,7 @@ pub unsafe extern "C" fn derivation_xpriv_to_xpub(
537533
let xpub = ExtendedPubKey::from_priv(&secp, &xpriv.inner);
538534

539535
FFIError::set_success(error);
540-
Box::into_raw(Box::new(FFIExtendedPubKey {
541-
inner: xpub,
542-
}))
536+
Box::into_raw(Box::new(FFIExtendedPubKey::from_inner(xpub)))
543537
}
544538
}
545539

@@ -608,7 +602,7 @@ pub unsafe extern "C" fn derivation_xpub_to_string(
608602

609603
unsafe {
610604
let xpub = &*xpub;
611-
let xpub_str = xpub.inner.to_string();
605+
let xpub_str = xpub.inner().to_string();
612606

613607
match CString::new(xpub_str) {
614608
Ok(c_str) => {
@@ -647,7 +641,7 @@ pub unsafe extern "C" fn derivation_xpub_fingerprint(
647641

648642
unsafe {
649643
let xpub = &*xpub;
650-
let fingerprint = xpub.inner.fingerprint();
644+
let fingerprint = xpub.inner().fingerprint();
651645
let bytes = fingerprint.to_bytes();
652646

653647
ptr::copy_nonoverlapping(bytes.as_ptr(), fingerprint_out, 4);

key-wallet-ffi/src/keys.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct FFIPublicKey {
2222
}
2323

2424
/// Opaque type for an extended public key
25-
pub struct FFIExtendedPublicKey {
25+
pub struct FFIExtendedPubKey {
2626
inner: key_wallet::bip32::ExtendedPubKey,
2727
}
2828

@@ -40,6 +40,20 @@ impl FFIExtendedPrivateKey {
4040
}
4141
}
4242

43+
impl FFIExtendedPubKey {
44+
#[inline]
45+
pub(crate) fn inner(&self) -> &key_wallet::bip32::ExtendedPubKey {
46+
&self.inner
47+
}
48+
49+
#[inline]
50+
pub(crate) fn from_inner(inner: key_wallet::bip32::ExtendedPubKey) -> Self {
51+
FFIExtendedPubKey {
52+
inner,
53+
}
54+
}
55+
}
56+
4357
impl FFIPrivateKey {
4458
#[inline]
4559
pub(crate) fn from_secret(inner: secp256k1::SecretKey) -> Self {
@@ -571,7 +585,7 @@ pub unsafe extern "C" fn wallet_derive_public_key(
571585
}
572586

573587
/// Derive extended public key at a specific path
574-
/// Returns an opaque FFIExtendedPublicKey pointer that must be freed with extended_public_key_free
588+
/// Returns an opaque FFIExtendedPubKey pointer that must be freed with extended_public_key_free
575589
///
576590
/// # Safety
577591
///
@@ -584,7 +598,7 @@ pub unsafe extern "C" fn wallet_derive_extended_public_key(
584598
wallet: *const FFIWallet,
585599
derivation_path: *const c_char,
586600
error: *mut FFIError,
587-
) -> *mut FFIExtendedPublicKey {
601+
) -> *mut FFIExtendedPubKey {
588602
if wallet.is_null() || derivation_path.is_null() {
589603
FFIError::set_error(error, FFIErrorCode::InvalidInput, "Null pointer provided".to_string());
590604
return ptr::null_mut();
@@ -624,7 +638,7 @@ pub unsafe extern "C" fn wallet_derive_extended_public_key(
624638
match wallet.inner().derive_extended_public_key(&path) {
625639
Ok(extended_public_key) => {
626640
FFIError::set_success(error);
627-
Box::into_raw(Box::new(FFIExtendedPublicKey {
641+
Box::into_raw(Box::new(FFIExtendedPubKey {
628642
inner: extended_public_key,
629643
}))
630644
}
@@ -739,7 +753,7 @@ pub unsafe extern "C" fn public_key_free(key: *mut FFIPublicKey) {
739753
/// - `key` must be a valid pointer created by extended public key functions or null
740754
/// - After calling this function, the pointer becomes invalid
741755
#[no_mangle]
742-
pub unsafe extern "C" fn extended_public_key_free(key: *mut FFIExtendedPublicKey) {
756+
pub unsafe extern "C" fn extended_public_key_free(key: *mut FFIExtendedPubKey) {
743757
if !key.is_null() {
744758
unsafe {
745759
let _ = Box::from_raw(key);
@@ -753,13 +767,13 @@ pub unsafe extern "C" fn extended_public_key_free(key: *mut FFIExtendedPublicKey
753767
///
754768
/// # Safety
755769
///
756-
/// - `key` must be a valid pointer to an FFIExtendedPublicKey
770+
/// - `key` must be a valid pointer to an FFIExtendedPubKey
757771
/// - `network` is ignored; the network is encoded in the extended key
758772
/// - `error` must be a valid pointer to an FFIError
759773
/// - The returned string must be freed with `string_free`
760774
#[no_mangle]
761775
pub unsafe extern "C" fn extended_public_key_to_string(
762-
key: *const FFIExtendedPublicKey,
776+
key: *const FFIExtendedPubKey,
763777
network: FFINetwork,
764778
error: *mut FFIError,
765779
) -> *mut c_char {
@@ -798,12 +812,12 @@ pub unsafe extern "C" fn extended_public_key_to_string(
798812
///
799813
/// # Safety
800814
///
801-
/// - `extended_key` must be a valid pointer to an FFIExtendedPublicKey
815+
/// - `extended_key` must be a valid pointer to an FFIExtendedPubKey
802816
/// - `error` must be a valid pointer to an FFIError
803817
/// - The returned FFIPublicKey must be freed with `public_key_free`
804818
#[no_mangle]
805819
pub unsafe extern "C" fn extended_public_key_get_public_key(
806-
extended_key: *const FFIExtendedPublicKey,
820+
extended_key: *const FFIExtendedPubKey,
807821
error: *mut FFIError,
808822
) -> *mut FFIPublicKey {
809823
if extended_key.is_null() {

0 commit comments

Comments
 (0)