Skip to content

Commit

Permalink
add mnemonic conversion for c-bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Alesfatalis committed May 4, 2024
1 parent f6712f0 commit 46f5ca7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions bindings/ergo-lib-c-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ pub use crate::error::*;
pub mod derivation_path;
mod error;
pub mod ext_pub_key;
pub mod mnemonic;
#[cfg(feature = "rest")]
pub mod rest;
15 changes: 15 additions & 0 deletions bindings/ergo-lib-c-core/src/mnemonic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::Error;
use ergo_lib::ergotree_interpreter::sigma_protocol::private_input::DlogProverInput;
use ergo_lib::wallet::mnemonic::Mnemonic as InnerMnemonic;

/// Convert a mnemonic phrase into a mnemonic seed
/// mnemonic_pass is optional and is used to salt the seed
pub unsafe fn mnemonic_to_seed(
mnemonic_phrase: &str,
mnemonic_pass: &str,
output: *mut u8,
) -> Result<(), Error> {
let src: Vec<u8> = InnerMnemonic::to_seed(mnemonic_phrase, mnemonic_pass).into();
std::ptr::copy_nonoverlapping(src.as_ptr(), output, DlogProverInput::SIZE_BYTES);
Ok(())
}
1 change: 1 addition & 0 deletions bindings/ergo-lib-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod rest;

mod derivation_path;
mod ext_pub_key;
mod mnemonic;
mod secret_key;
mod token;
mod transaction;
Expand Down
17 changes: 17 additions & 0 deletions bindings/ergo-lib-c/src/mnemonic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use ergo_lib_c_core::mnemonic::mnemonic_to_seed;
use std::ffi::CStr;
use std::os::raw::c_char;

/// Convert a mnemonic phrase into a mnemonic seed
/// mnemonic_pass is optional and is used to salt the seed
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_mnemonic_to_seed(
mnemonic_phrase: *const c_char,
mnemonic_pass: *const c_char,
output: *mut u8,
) {
let mnemonic_phrase = CStr::from_ptr(mnemonic_phrase).to_string_lossy();
let mnemonic_pass = CStr::from_ptr(mnemonic_pass).to_string_lossy();
#[allow(clippy::unwrap_used)]
mnemonic_to_seed(&mnemonic_phrase, &mnemonic_pass, output).unwrap()
}

0 comments on commit 46f5ca7

Please sign in to comment.