diff --git a/libs/gl-sdk-napi/src/lib.rs b/libs/gl-sdk-napi/src/lib.rs index 11b583856..20f679095 100644 --- a/libs/gl-sdk-napi/src/lib.rs +++ b/libs/gl-sdk-napi/src/lib.rs @@ -75,31 +75,6 @@ pub struct Node { inner: GlNode, } -// ============================================================================ -// Internal Implementations (non-NAPI) -// ============================================================================ - -impl Credentials { - /// Internal constructor for creating Credentials from GlCredentials - pub(crate) fn from_inner(inner: GlCredentials) -> Self { - Self { inner } - } -} - -impl Signer { - /// Internal constructor for creating Signer from GlSigner - pub(crate) fn from_inner(inner: GlSigner) -> Self { - Self { inner } - } -} - -impl Handle { - /// Internal constructor for creating Handle from GlHandle - pub(crate) fn from_inner(inner: GlHandle) -> Self { - Self { inner } - } -} - // ============================================================================ // NAPI Implementations // ============================================================================ @@ -110,15 +85,15 @@ impl Credentials { #[napi(factory)] pub fn load(raw: Buffer) -> Result { let inner = GlCredentials::load(raw.to_vec()) - .map_err(|e| Error::from_reason(format!("Failed to load credentials: {:?}", e)))?; - Ok(Credentials::from_inner(inner)) + .map_err(|e| Error::from_reason(e.to_string()))?; + Ok(Self { inner }) } /// Save credentials to raw bytes #[napi] pub fn save(&self) -> Result { let bytes = self.inner.save() - .map_err(|e| Error::from_reason(format!("Failed to save credentials: {:?}", e)))?; + .map_err(|e| Error::from_reason(e.to_string()))?; Ok(Buffer::from(bytes)) } @@ -143,7 +118,7 @@ impl Scheduler { }; let inner = GlScheduler::new(gl_network) - .map_err(|e| Error::from_reason(format!("Failed to create scheduler: {:?}", e)))?; + .map_err(|e| Error::from_reason(e.to_string()))?; Ok(Self { inner }) } @@ -157,9 +132,9 @@ impl Scheduler { pub fn register(&self, signer: &Signer, code: Option) -> Result { let inner = self.inner .register(&signer.inner, code) - .map_err(|e| Error::from_reason(format!("Registration failed: {:?}", e)))?; + .map_err(|e| Error::from_reason(e.to_string()))?; - Ok(Credentials::from_inner(inner)) + Ok(Credentials { inner }) } /// Recover node credentials @@ -170,9 +145,9 @@ impl Scheduler { pub fn recover(&self, signer: &Signer) -> Result { let inner = self.inner .recover(&signer.inner) - .map_err(|e| Error::from_reason(format!("Recovery failed: {:?}", e)))?; + .map_err(|e| Error::from_reason(e.to_string()))?; - Ok(Credentials::from_inner(inner)) + Ok(Credentials { inner }) } } @@ -185,7 +160,7 @@ impl Signer { #[napi(constructor)] pub fn new(phrase: String) -> Result { let inner = GlSigner::new(phrase) - .map_err(|e| Error::from_reason(format!("Failed to create signer: {:?}", e)))?; + .map_err(|e| Error::from_reason(e.to_string()))?; Ok(Self { inner }) } @@ -197,9 +172,9 @@ impl Signer { #[napi] pub fn authenticate(&self, credentials: &Credentials) -> Result { let inner = self.inner.authenticate(&credentials.inner) - .map_err(|e| Error::from_reason(format!("Authentication failed: {:?}", e)))?; + .map_err(|e| Error::from_reason(e.to_string()))?; - Ok(Signer::from_inner(inner)) + Ok(Signer { inner }) } /// Start the signer's background task @@ -207,9 +182,9 @@ impl Signer { #[napi] pub fn start(&self) -> Result { let inner = self.inner.start() - .map_err(|e| Error::from_reason(format!("Failed to start signer: {:?}", e)))?; + .map_err(|e| Error::from_reason(e.to_string()))?; - Ok(Handle::from_inner(inner)) + Ok(Handle { inner }) } /// Get the node ID for this signer @@ -238,7 +213,7 @@ impl Node { #[napi(constructor)] pub fn new(credentials: &Credentials) -> Result { let inner = GlNode::new(&credentials.inner) - .map_err(|e| Error::from_reason(format!("Failed to create node: {:?}", e)))?; + .map_err(|e| Error::from_reason(e.to_string()))?; Ok(Self { inner }) } @@ -266,10 +241,10 @@ impl Node { let amount = amount_msat.map(|a| a as u64); let response = self.inner.receive(label, description, amount) - .map_err(|e| Error::from_reason(format!("Failed to create invoice: {:?}", e)))?; + .map_err(|e| Error::from_reason(e.to_string()))?; Ok(ReceiveResponse { - bolt11: response.bolt11(), + bolt11: response.bolt11, }) } @@ -282,14 +257,14 @@ impl Node { pub fn send(&self, invoice: String, amount_msat: Option) -> Result { let amount = amount_msat.map(|a| a as u64); let response = self.inner.send(invoice, amount) - .map_err(|e| Error::from_reason(format!("Payment failed: {:?}", e)))?; + .map_err(|e| Error::from_reason(e.to_string()))?; Ok(SendResponse { - status: response.status() as u32, - preimage: Buffer::from(response.preimage()), - amount_msat: response.amount_msat() as i64, - amount_sent_msat: response.amount_sent_msat() as i64, - parts: response.parts(), + status: response.status as u32, + preimage: Buffer::from(response.preimage), + amount_msat: response.amount_msat as i64, + amount_sent_msat: response.amount_sent_msat as i64, + parts: response.parts, }) } @@ -305,12 +280,12 @@ impl Node { amount_or_all: String, ) -> Result { let response = self.inner.onchain_send(destination, amount_or_all) - .map_err(|e| Error::from_reason(format!("On-chain send failed: {:?}", e)))?; + .map_err(|e| Error::from_reason(e.to_string()))?; Ok(OnchainSendResponse { - tx: Buffer::from(response.tx()), - txid: Buffer::from(response.txid()), - psbt: response.psbt(), + tx: Buffer::from(response.tx), + txid: Buffer::from(response.txid), + psbt: response.psbt, }) } @@ -318,11 +293,11 @@ impl Node { #[napi] pub fn onchain_receive(&self) -> Result { let response = self.inner.onchain_receive() - .map_err(|e| Error::from_reason(format!("Failed to generate address: {:?}", e)))?; + .map_err(|e| Error::from_reason(e.to_string()))?; Ok(OnchainReceiveResponse { - bech32: response.bech32(), - p2tr: response.p2tr(), + bech32: response.bech32, + p2tr: response.p2tr, }) } } diff --git a/libs/gl-sdk/src/node.rs b/libs/gl-sdk/src/node.rs index 25371b51a..ede5b034a 100644 --- a/libs/gl-sdk/src/node.rs +++ b/libs/gl-sdk/src/node.rs @@ -191,30 +191,11 @@ impl Node { } } -#[allow(unused)] -#[derive(uniffi::Object)] +#[derive(uniffi::Record)] pub struct OnchainSendResponse { - tx: Vec, - txid: Vec, - psbt: String, -} - -#[uniffi::export] -impl OnchainSendResponse { - /// Get the raw transaction bytes - pub fn tx(&self) -> Vec { - self.tx.clone() - } - - /// Get the transaction ID - pub fn txid(&self) -> Vec { - self.txid.clone() - } - - /// Get the PSBT string - pub fn psbt(&self) -> String { - self.psbt.clone() - } + pub tx: Vec, + pub txid: Vec, + pub psbt: String, } impl From for OnchainSendResponse { @@ -227,24 +208,10 @@ impl From for OnchainSendResponse { } } -#[allow(unused)] -#[derive(uniffi::Object)] +#[derive(uniffi::Record)] pub struct OnchainReceiveResponse { - bech32: String, - p2tr: String, -} - -#[uniffi::export] -impl OnchainReceiveResponse { - /// Get the bech32 (native segwit) address - pub fn bech32(&self) -> String { - self.bech32.clone() - } - - /// Get the taproot (P2TR) address - pub fn p2tr(&self) -> String { - self.p2tr.clone() - } + pub bech32: String, + pub p2tr: String, } impl From for OnchainReceiveResponse { @@ -256,42 +223,13 @@ impl From for OnchainReceiveResponse { } } -#[allow(unused)] -#[derive(uniffi::Object)] +#[derive(uniffi::Record)] pub struct SendResponse { - status: PayStatus, - preimage: Vec, - amount_msat: u64, - amount_sent_msat: u64, - parts: u32, -} - -#[uniffi::export] -impl SendResponse { - /// Get the payment status - pub fn status(&self) -> PayStatus { - self.status.clone() - } - - /// Get the payment preimage - pub fn preimage(&self) -> Vec { - self.preimage.clone() - } - - /// Get the amount in millisatoshis - pub fn amount_msat(&self) -> u64 { - self.amount_msat - } - - /// Get the amount sent in millisatoshis - pub fn amount_sent_msat(&self) -> u64 { - self.amount_sent_msat - } - - /// Get the number of parts used - pub fn parts(&self) -> u32 { - self.parts - } + pub status: PayStatus, + pub preimage: Vec, + pub amount_msat: u64, + pub amount_sent_msat: u64, + pub parts: u32, } impl From for SendResponse { @@ -306,18 +244,9 @@ impl From for SendResponse { } } -#[allow(unused)] -#[derive(uniffi::Object)] +#[derive(uniffi::Record)] pub struct ReceiveResponse { - bolt11: String, -} - -#[uniffi::export] -impl ReceiveResponse { - /// Get the BOLT11 invoice string - pub fn bolt11(&self) -> String { - self.bolt11.clone() - } + pub bolt11: String, } #[derive(uniffi::Enum, Clone)]