Skip to content

Commit 63fa646

Browse files
committed
Add to and from strings for binding mnemonics
1 parent 2e4c5cb commit 63fa646

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

orange-sdk/src/ffi/ldk_node.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{impl_from_core_type, impl_into_core_type};
44
use ldk_node::ChannelDetails as LDKChannelDetails;
55
use ldk_node::bip39::Mnemonic as LDKMnemonic;
66
use ldk_node::bitcoin::Network as LDKNetwork;
7+
use std::str::FromStr;
78

89
impl From<Network> for LDKNetwork {
910
fn from(network: Network) -> Self {
@@ -28,6 +29,18 @@ impl Mnemonic {
2829
Err(_) => Err(ConfigError::InvalidEntropySize(entropy.len() as u32)),
2930
}
3031
}
32+
33+
#[uniffi::constructor]
34+
pub fn from_str(str: &str) -> Result<Self, ConfigError> {
35+
match LDKMnemonic::from_str(str) {
36+
Ok(mnemonic) => Ok(Mnemonic(mnemonic)),
37+
Err(_) => Err(ConfigError::InvalidMnemonic),
38+
}
39+
}
40+
41+
pub fn to_string(&self) -> String {
42+
self.0.to_string()
43+
}
3144
}
3245

3346
impl_from_core_type!(LDKMnemonic, Mnemonic);

orange-sdk/src/ffi/orange/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::fmt::Display;
55
#[derive(Debug, uniffi::Error)]
66
pub enum ConfigError {
77
InvalidEntropySize(u32),
8+
InvalidMnemonic,
89
InvalidLspAddress(String),
910
InvalidLspNodeId(String),
1011
}
@@ -15,6 +16,9 @@ impl Display for ConfigError {
1516
ConfigError::InvalidEntropySize(size) => {
1617
write!(f, "Invalid entropy size: {size}. Entropy must be 64 bytes.")
1718
},
19+
ConfigError::InvalidMnemonic => {
20+
write!(f, "Invalid mnemonic phrase")
21+
},
1822
ConfigError::InvalidLspAddress(address) => write!(f, "Invalid LSP address: {address}"),
1923
ConfigError::InvalidLspNodeId(node_id) => write!(f, "Invalid LSP node ID: {node_id}"),
2024
}

0 commit comments

Comments
 (0)