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
83 changes: 29 additions & 54 deletions libs/gl-sdk-napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ============================================================================
Expand All @@ -110,15 +85,15 @@ impl Credentials {
#[napi(factory)]
pub fn load(raw: Buffer) -> Result<Credentials> {
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<Buffer> {
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))
}
Expand All @@ -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 })
}
Expand All @@ -157,9 +132,9 @@ impl Scheduler {
pub fn register(&self, signer: &Signer, code: Option<String>) -> Result<Credentials> {
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
Expand All @@ -170,9 +145,9 @@ impl Scheduler {
pub fn recover(&self, signer: &Signer) -> Result<Credentials> {
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 })
}
}

Expand All @@ -185,7 +160,7 @@ impl Signer {
#[napi(constructor)]
pub fn new(phrase: String) -> Result<Self> {
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 })
}
Expand All @@ -197,19 +172,19 @@ impl Signer {
#[napi]
pub fn authenticate(&self, credentials: &Credentials) -> Result<Signer> {
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
/// Returns a handle to control the signer
#[napi]
pub fn start(&self) -> Result<Handle> {
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
Expand Down Expand Up @@ -238,7 +213,7 @@ impl Node {
#[napi(constructor)]
pub fn new(credentials: &Credentials) -> Result<Self> {
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 })
}
Expand Down Expand Up @@ -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,
})
}

Expand All @@ -282,14 +257,14 @@ impl Node {
pub fn send(&self, invoice: String, amount_msat: Option<i64>) -> Result<SendResponse> {
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,
})
}

Expand All @@ -305,24 +280,24 @@ impl Node {
amount_or_all: String,
) -> Result<OnchainSendResponse> {
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,
})
}

/// Generate a new on-chain address
#[napi]
pub fn onchain_receive(&self) -> Result<OnchainReceiveResponse> {
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,
})
}
}
101 changes: 15 additions & 86 deletions libs/gl-sdk/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,11 @@ impl Node {
}
}

#[allow(unused)]
#[derive(uniffi::Object)]
#[derive(uniffi::Record)]
pub struct OnchainSendResponse {
tx: Vec<u8>,
txid: Vec<u8>,
psbt: String,
}

#[uniffi::export]
impl OnchainSendResponse {
/// Get the raw transaction bytes
pub fn tx(&self) -> Vec<u8> {
self.tx.clone()
}

/// Get the transaction ID
pub fn txid(&self) -> Vec<u8> {
self.txid.clone()
}

/// Get the PSBT string
pub fn psbt(&self) -> String {
self.psbt.clone()
}
pub tx: Vec<u8>,
pub txid: Vec<u8>,
pub psbt: String,
}

impl From<clnpb::WithdrawResponse> for OnchainSendResponse {
Expand All @@ -227,24 +208,10 @@ impl From<clnpb::WithdrawResponse> 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<clnpb::NewaddrResponse> for OnchainReceiveResponse {
Expand All @@ -256,42 +223,13 @@ impl From<clnpb::NewaddrResponse> for OnchainReceiveResponse {
}
}

#[allow(unused)]
#[derive(uniffi::Object)]
#[derive(uniffi::Record)]
pub struct SendResponse {
status: PayStatus,
preimage: Vec<u8>,
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<u8> {
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<u8>,
pub amount_msat: u64,
pub amount_sent_msat: u64,
pub parts: u32,
}

impl From<clnpb::PayResponse> for SendResponse {
Expand All @@ -306,18 +244,9 @@ impl From<clnpb::PayResponse> 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)]
Expand Down
Loading