Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions key-wallet-ffi/FFI_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1461,11 +1461,11 @@ Derive extended private key at a specific path Returns an opaque FFIExtendedPriv
#### `wallet_derive_extended_public_key`

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

**Description:**
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`
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`

**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`
Expand Down Expand Up @@ -3907,7 +3907,7 @@ Get extended private key as string (xprv format) Returns the extended private k
#### `extended_public_key_free`

```c
extended_public_key_free(key: *mut FFIExtendedPublicKey) -> ()
extended_public_key_free(key: *mut FFIExtendedPubKey) -> ()
```

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

```c
extended_public_key_get_public_key(extended_key: *const FFIExtendedPublicKey, error: *mut FFIError,) -> *mut FFIPublicKey
extended_public_key_get_public_key(extended_key: *const FFIExtendedPubKey, error: *mut FFIError,) -> *mut FFIPublicKey
```

**Description:**
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`
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`

**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`
- `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`

**Module:** `keys`

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

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

**Description:**
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`
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`

**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`
- `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`

**Module:** `keys`

Expand Down
2 changes: 1 addition & 1 deletion key-wallet-ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ item_types = ["functions", "enums", "structs", "typedefs", "opaque", "constants"
"FFIPrivateKey" = "{ unsigned char _private[0]; }"
"FFIExtendedPrivateKey" = "{ unsigned char _private[0]; }"
"FFIPublicKey" = "{ unsigned char _private[0]; }"
"FFIExtendedPublicKey" = "{ unsigned char _private[0]; }"
"FFIExtendedPubKey" = "{ unsigned char _private[0]; }"
"FFIWalletManager" = "{ unsigned char _private[0]; }"
"FFIWallet" = "{ unsigned char _private[0]; }"
"FFIAccount" = "{ unsigned char _private[0]; }"
Expand Down
14 changes: 4 additions & 10 deletions key-wallet-ffi/src/derivation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::error::{FFIError, FFIErrorCode};
use crate::keys::FFIExtendedPrivKey;
use crate::keys::FFIExtendedPubKey;
use crate::types::FFINetwork;
use dashcore::Network;
use key_wallet::{ExtendedPrivKey, ExtendedPubKey};
Expand Down Expand Up @@ -34,11 +35,6 @@ pub enum FFIDerivationPathType {
PathRoot = 255,
}

/// Extended public key structure
pub struct FFIExtendedPubKey {
inner: key_wallet::bip32::ExtendedPubKey,
}

/// Create a new master extended private key from seed
///
/// # Safety
Expand Down Expand Up @@ -529,9 +525,7 @@ pub unsafe extern "C" fn derivation_xpriv_to_xpub(
let xpub = ExtendedPubKey::from_priv(&secp, xpriv.inner());

FFIError::set_success(error);
Box::into_raw(Box::new(FFIExtendedPubKey {
inner: xpub,
}))
Box::into_raw(Box::new(FFIExtendedPubKey::from_inner(xpub)))
}
}

Expand Down Expand Up @@ -600,7 +594,7 @@ pub unsafe extern "C" fn derivation_xpub_to_string(

unsafe {
let xpub = &*xpub;
let xpub_str = xpub.inner.to_string();
let xpub_str = xpub.inner().to_string();

match CString::new(xpub_str) {
Ok(c_str) => {
Expand Down Expand Up @@ -639,7 +633,7 @@ pub unsafe extern "C" fn derivation_xpub_fingerprint(

unsafe {
let xpub = &*xpub;
let fingerprint = xpub.inner.fingerprint();
let fingerprint = xpub.inner().fingerprint();
let bytes = fingerprint.to_bytes();

ptr::copy_nonoverlapping(bytes.as_ptr(), fingerprint_out, 4);
Expand Down
32 changes: 23 additions & 9 deletions key-wallet-ffi/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct FFIPublicKey {
}

/// Opaque type for an extended public key
pub struct FFIExtendedPublicKey {
pub struct FFIExtendedPubKey {
inner: key_wallet::bip32::ExtendedPubKey,
}
Comment thread
ZocoLini marked this conversation as resolved.

Expand All @@ -40,6 +40,20 @@ impl FFIExtendedPrivKey {
}
}

impl FFIExtendedPubKey {
#[inline]
pub(crate) fn inner(&self) -> &key_wallet::bip32::ExtendedPubKey {
&self.inner
}

#[inline]
pub(crate) fn from_inner(inner: key_wallet::bip32::ExtendedPubKey) -> Self {
FFIExtendedPubKey {
inner,
}
}
}

impl FFIPrivateKey {
#[inline]
pub(crate) fn from_secret(inner: secp256k1::SecretKey) -> Self {
Expand Down Expand Up @@ -571,7 +585,7 @@ pub unsafe extern "C" fn wallet_derive_public_key(
}

/// Derive extended public key at a specific path
/// Returns an opaque FFIExtendedPublicKey pointer that must be freed with extended_public_key_free
/// Returns an opaque FFIExtendedPubKey pointer that must be freed with extended_public_key_free
///
/// # Safety
///
Expand All @@ -584,7 +598,7 @@ pub unsafe extern "C" fn wallet_derive_extended_public_key(
wallet: *const FFIWallet,
derivation_path: *const c_char,
error: *mut FFIError,
) -> *mut FFIExtendedPublicKey {
) -> *mut FFIExtendedPubKey {
if wallet.is_null() || derivation_path.is_null() {
FFIError::set_error(error, FFIErrorCode::InvalidInput, "Null pointer provided".to_string());
return ptr::null_mut();
Expand Down Expand Up @@ -624,7 +638,7 @@ pub unsafe extern "C" fn wallet_derive_extended_public_key(
match wallet.inner().derive_extended_public_key(&path) {
Ok(extended_public_key) => {
FFIError::set_success(error);
Box::into_raw(Box::new(FFIExtendedPublicKey {
Box::into_raw(Box::new(FFIExtendedPubKey {
inner: extended_public_key,
}))
}
Expand Down Expand Up @@ -739,7 +753,7 @@ pub unsafe extern "C" fn public_key_free(key: *mut FFIPublicKey) {
/// - `key` must be a valid pointer created by extended public key functions or null
/// - After calling this function, the pointer becomes invalid
#[no_mangle]
pub unsafe extern "C" fn extended_public_key_free(key: *mut FFIExtendedPublicKey) {
pub unsafe extern "C" fn extended_public_key_free(key: *mut FFIExtendedPubKey) {
if !key.is_null() {
unsafe {
let _ = Box::from_raw(key);
Expand All @@ -753,13 +767,13 @@ pub unsafe extern "C" fn extended_public_key_free(key: *mut FFIExtendedPublicKey
///
/// # Safety
///
/// - `key` must be a valid pointer to an FFIExtendedPublicKey
/// - `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`
#[no_mangle]
pub unsafe extern "C" fn extended_public_key_to_string(
key: *const FFIExtendedPublicKey,
key: *const FFIExtendedPubKey,
network: FFINetwork,
error: *mut FFIError,
) -> *mut c_char {
Expand Down Expand Up @@ -798,12 +812,12 @@ pub unsafe extern "C" fn extended_public_key_to_string(
///
/// # Safety
///
/// - `extended_key` must be a valid pointer to an FFIExtendedPublicKey
/// - `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`
#[no_mangle]
pub unsafe extern "C" fn extended_public_key_get_public_key(
extended_key: *const FFIExtendedPublicKey,
extended_key: *const FFIExtendedPubKey,
error: *mut FFIError,
) -> *mut FFIPublicKey {
if extended_key.is_null() {
Expand Down
Loading