From 3301199609537c260d25e26981ec9deab6dd65f4 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Fri, 22 Dec 2023 09:50:40 +0000 Subject: [PATCH] refactor: some cashu-sdk-ffi to v1 --- bindings/cashu-ffi/src/cashu.udl | 67 ++++---- bindings/cashu-ffi/src/nuts/nut00/token.rs | 10 ++ bindings/cashu-ffi/src/nuts/nut04/mod.rs | 6 + bindings/cashu-sdk-ffi/src/cashu_sdk.udl | 178 +++++++++++---------- bindings/cashu-sdk-ffi/src/lib.rs | 13 +- bindings/cashu-sdk-ffi/src/mint.rs | 24 +-- bindings/cashu-sdk-ffi/src/wallet.rs | 39 ++--- 7 files changed, 179 insertions(+), 158 deletions(-) diff --git a/bindings/cashu-ffi/src/cashu.udl b/bindings/cashu-ffi/src/cashu.udl index c81b2a970..216e4c711 100644 --- a/bindings/cashu-ffi/src/cashu.udl +++ b/bindings/cashu-ffi/src/cashu.udl @@ -33,10 +33,6 @@ interface Secret { // NUT00 -interface Id { - [Throws=CashuError] - constructor(string id); -}; interface PublicKey { [Throws=CashuError, Name=from_hex] @@ -112,6 +108,14 @@ interface PreMintSecrets { sequence amounts(); }; + +// NUT-02 + +interface Id { + [Throws=CashuError] + constructor(string id); +}; + interface KeyPair { [Name=from_secret_key] constructor(SecretKey secret_key); @@ -137,7 +141,6 @@ interface MintKeySet { constructor(string secret, string unit, string derivation_path, u8 max_order); }; - interface KeysResponse { constructor(Keys keys); }; @@ -147,14 +150,28 @@ interface KeySetResponse { sequence keysets(); }; -// --- NUT-04 +// NUT-03 + +interface SwapRequest { + constructor(sequence proofs, sequence outputs); + sequence proofs(); + sequence outputs(); + Amount proofs_amount(); + Amount output_amount(); +}; + +interface SwapResponse { + constructor(sequence promises); + sequence signatures(); +}; + +// NUT-04 interface MintQuoteBolt11Request { constructor(Amount amount, string unit); Amount amount(); }; - interface MintQuoteBolt11Response { constructor(string quote, string request, boolean paid, u64 expiry); string quote(); @@ -175,8 +192,6 @@ interface MintBolt11Response { sequence signatures(); }; -// --- - // NUT-05 interface MeltQuoteBolt11Response { @@ -208,20 +223,21 @@ interface MeltBolt11Response { boolean paid(); }; -// ---- -interface SwapRequest { - constructor(sequence proofs, sequence outputs); - sequence proofs(); - sequence outputs(); - Amount proofs_amount(); - Amount output_amount(); -}; +// NUT-06 -interface SwapResponse { - constructor(sequence promises); - sequence signatures(); +interface MintInfo { + constructor(string? name, PublicKey? pubkey, MintVersion? version, string? description, string? description_long, sequence>? contact, string nuts, string? motd); + string? name(); + PublicKey? pubkey(); + MintVersion? version(); + string? description(); + string? description_long(); + sequence>? contact(); + string? motd(); }; +// NUT-07 + interface CheckSpendableRequest { constructor(sequence proofs); sequence proofs(); @@ -239,17 +255,6 @@ interface MintVersion { string version(); }; -interface MintInfo { - constructor(string? name, PublicKey? pubkey, MintVersion? version, string? description, string? description_long, sequence>? contact, string nuts, string? motd); - string? name(); - PublicKey? pubkey(); - MintVersion? version(); - string? description(); - string? description_long(); - sequence>? contact(); - string? motd(); -}; - interface KeySetInfo { constructor(Id id, string unit); diff --git a/bindings/cashu-ffi/src/nuts/nut00/token.rs b/bindings/cashu-ffi/src/nuts/nut00/token.rs index acef05326..905349fc5 100644 --- a/bindings/cashu-ffi/src/nuts/nut00/token.rs +++ b/bindings/cashu-ffi/src/nuts/nut00/token.rs @@ -25,6 +25,16 @@ impl From<&CurrencyUnit> for CurrencyUnitSdk { } } +impl From for CurrencyUnitSdk { + fn from(unit: CurrencyUnit) -> CurrencyUnitSdk { + match unit { + CurrencyUnit::Sat() => CurrencyUnitSdk::Sat, + CurrencyUnit::Usd() => CurrencyUnitSdk::Usd, + CurrencyUnit::Custom { unit } => CurrencyUnitSdk::Custom(unit.clone()), + } + } +} + impl From for CurrencyUnit { fn from(unit: CurrencyUnitSdk) -> CurrencyUnit { match unit { diff --git a/bindings/cashu-ffi/src/nuts/nut04/mod.rs b/bindings/cashu-ffi/src/nuts/nut04/mod.rs index 9851bdbaa..4aa1d0ac8 100644 --- a/bindings/cashu-ffi/src/nuts/nut04/mod.rs +++ b/bindings/cashu-ffi/src/nuts/nut04/mod.rs @@ -160,3 +160,9 @@ impl MintBolt11Response { .collect() } } + +impl From for MintBolt11Response { + fn from(inner: MintBolt11ResponseSdk) -> MintBolt11Response { + MintBolt11Response { inner } + } +} diff --git a/bindings/cashu-sdk-ffi/src/cashu_sdk.udl b/bindings/cashu-sdk-ffi/src/cashu_sdk.udl index 47ab48ca8..a584092f7 100644 --- a/bindings/cashu-sdk-ffi/src/cashu_sdk.udl +++ b/bindings/cashu-sdk-ffi/src/cashu_sdk.udl @@ -8,12 +8,12 @@ interface CashuError { }; -// Types - +// Types [Enum] interface CurrencyUnit { Sat(); + Usd(); Custom(string unit); }; @@ -25,26 +25,17 @@ interface Bolt11Invoice { }; interface Amount { - u64 to_sat(); - u64 to_msat(); - [Name = from_sat] - constructor(u64 sat); - [Name = from_msat] - constructor(u64 msat); + constructor(u64 amount); sequence split(); }; - interface Secret { constructor(); sequence as_bytes(); }; +// NUT00 -interface Id { - [Throws=CashuError] - constructor(string id); -}; interface PublicKey { [Throws=CashuError, Name=from_hex] @@ -62,6 +53,7 @@ interface SecretKey { interface BlindedMessage { constructor(Id keyset_id, Amount amount, PublicKey b); Amount amount(); + Id keyset_id(); PublicKey b(); }; @@ -70,12 +62,12 @@ interface Proof { Amount amount(); Secret secret(); PublicKey c(); - Id id(); - }; + Id keyset_id(); +}; interface BlindedSignature { - constructor(Id id, Amount amount, PublicKey c); - Id id(); + constructor(Id keyset_id, Amount amount, PublicKey c); + Id keyset_id(); Amount amount(); PublicKey c(); }; @@ -85,7 +77,7 @@ interface MintProof { Amount? amount(); Secret secret(); PublicKey? c(); - Id? id(); + Id? keyset_id(); }; @@ -98,10 +90,10 @@ interface MintProofs { interface Token { [Throws=CashuError] - constructor(string mint, sequence token, string? memo, CurrencyUnit? unit); + constructor(string mint, sequence token, string? memo, string? unit); sequence token(); string? memo(); - CurrencyUnit? unit(); + string? unit(); string to_string(); [Throws=CashuError, Name=from_string] constructor(string token); @@ -119,6 +111,14 @@ interface PreMintSecrets { sequence amounts(); }; + +// NUT-02 + +interface Id { + [Throws=CashuError] + constructor(string id); +}; + interface KeyPair { [Name=from_secret_key] constructor(SecretKey secret_key); @@ -139,13 +139,11 @@ interface KeySet { Keys keys(); }; - interface MintKeySet { [Name=generate] constructor(string secret, string unit, string derivation_path, u8 max_order); }; - interface KeysResponse { constructor(Keys keys); }; @@ -155,6 +153,48 @@ interface KeySetResponse { sequence keysets(); }; +// NUT-03 + +interface SwapRequest { + constructor(sequence proofs, sequence outputs); + sequence proofs(); + sequence outputs(); + Amount proofs_amount(); + Amount output_amount(); +}; + +interface SwapResponse { + constructor(sequence promises); + sequence signatures(); +}; + +// NUT-04 + +interface MintQuoteBolt11Request { + constructor(Amount amount, string unit); + Amount amount(); +}; + +interface MintQuoteBolt11Response { + constructor(string quote, string request, boolean paid, u64 expiry); + string quote(); + string request(); + boolean paid(); + u64 expiry(); +}; + + +interface MintBolt11Request { + constructor(string quote, sequence outputs); + string quote(); + sequence outputs(); +}; + +interface MintBolt11Response { + constructor(sequence signatures); + sequence signatures(); +}; + // NUT-05 interface MeltQuoteBolt11Response { @@ -171,49 +211,46 @@ interface MeltQuoteBolt11Request { [Throws=CashuError] constructor(string request, string unit); string request(); - string unit(); }; interface MeltBolt11Request { [Throws=CashuError] - constructor(sequence inputs, string quote); + constructor(string quote, sequence inputs, sequence? outputs); sequence inputs(); string quote(); }; -interface RequestMintResponse { - [Throws=CashuError] - constructor(string invoice, string hash); - string invoice(); - string hash(); -}; - -interface MintRequest { - constructor(sequence outputs); - sequence outputs(); - Amount total_amount(); +interface MeltBolt11Response { + constructor(boolean paid, string? payment_preimage, sequence? change); + string? payment_preimage(); + boolean paid(); }; -interface PostMintResponse { - constructor(sequence promises); - sequence promises(); -}; +// NUT-06 -interface SplitRequest { - constructor(sequence proofs, sequence outputs); - sequence proofs(); - sequence outputs(); - Amount proofs_amount(); - Amount output_amount(); +interface MintInfo { + constructor(string? name, PublicKey? pubkey, MintVersion? version, string? description, string? description_long, sequence>? contact, string nuts, string? motd); + string? name(); + PublicKey? pubkey(); + MintVersion? version(); + string? description(); + string? description_long(); + sequence>? contact(); + string? motd(); }; -interface SplitResponse { - constructor(sequence promises); - sequence promises(); - Amount? promises_amount(); +// NUT-07 +interface CheckSpendableRequest { + constructor(sequence proofs); + sequence proofs(); }; +interface CheckSpendableResponse { + constructor(sequence spendable, sequence pending); + sequence spendable(); + sequence pending(); +}; interface MintVersion { constructor(string name, string version); @@ -221,16 +258,8 @@ interface MintVersion { string version(); }; -interface MintInfo { - constructor(string? name, PublicKey? pubkey, MintVersion? version, string? description, string? description_long, sequence>? contact, sequence nuts, string? motd); - string? name(); - PublicKey? pubkey(); - MintVersion? version(); - string? description(); - string? description_long(); - sequence>? contact(); - sequence nuts(); - string? motd(); +interface KeySetInfo { + constructor(Id id, string unit); }; enum InvoiceStatus { @@ -240,17 +269,6 @@ enum InvoiceStatus { "InFlight" }; -interface ProofsStatus { - constructor(sequence spendable, sequence spent); - sequence spendable(); - sequence spent(); -}; - - -interface KeySetInfo { - constructor(Id id, string unit); - -}; // Cashu Sdk @@ -281,23 +299,19 @@ interface Wallet { // [Throws=CashuSdkError] // ProofsStatus check_proofs_spent(sequence proofs); [Throws=CashuSdkError] - RequestMintResponse request_mint(Amount amount); - [Throws=CashuSdkError] - Token mint_token(Amount amount, string hash, string? unit, string? memo); + Token mint_token(Amount amount, string hash,CurrencyUnit? unit, string? memo); [Throws=CashuSdkError] sequence mint(Amount amount, string hash); - [Throws=CashuSdkError] - Amount check_fee(Bolt11Invoice invoice); [Throws=CashuSdkError] sequence receive(string encoded_token); [Throws=CashuSdkError] - sequence process_split_response(PreMintSecrets blinded_messages, sequence promises); + sequence process_swap_response(PreMintSecrets blinded_messages, sequence promises); [Throws=CashuSdkError] SendProofs send(Amount amount, sequence proofs); [Throws=CashuSdkError] - Melted melt(Bolt11Invoice invoice, sequence proofs, Amount fee_reserve); + Melted melt(string quote, sequence proofs, Amount fee_reserve); [Throws=CashuSdkError] - string proof_to_token(sequence proof, string? unit, string? memo); + string proofs_to_token(sequence proof, CurrencyUnit? unit, string? memo); }; @@ -308,11 +322,5 @@ interface Mint { KeySetResponse keysets(); KeySet? keyset(Id id); [Throws=CashuSdkError] - PostMintResponse process_mint_request(MintRequest mint_request); - [Throws=CashuSdkError] - SplitResponse process_split_request(SplitRequest split_request); - [Throws=CashuSdkError] - void verify_melt_request(MeltRequest melt_request); - [Throws=CashuSdkError] - MeltResponse process_melt_request(MeltRequest melt_request, string preimage, Amount totoal_spent); + SwapResponse process_swap_request(SwapRequest swap_request); }; diff --git a/bindings/cashu-sdk-ffi/src/lib.rs b/bindings/cashu-sdk-ffi/src/lib.rs index 2b3a7056e..2935e5afa 100644 --- a/bindings/cashu-sdk-ffi/src/lib.rs +++ b/bindings/cashu-sdk-ffi/src/lib.rs @@ -5,12 +5,13 @@ mod wallet; mod ffi { pub use cashu_ffi::{ - Amount, BlindedMessage, BlindedSignature, Bolt11Invoice, CashuError, CheckFeesRequest, - CheckFeesResponse, CheckSpendableRequest, CheckSpendableResponse, Id, InvoiceStatus, - KeyPair, KeySet, KeySetInfo, KeySetResponse, Keys, KeysResponse, MeltRequest, MeltResponse, - MintInfo, MintKeySet, MintProof, MintProofs, MintRequest, MintVersion, Nut05MeltRequest, - Nut05MeltResponse, PostMintResponse, PreMintSecrets, Proof, PublicKey, RequestMintResponse, - Secret, SecretKey, SplitRequest, SplitResponse, Token, + Amount, BlindedMessage, BlindedSignature, Bolt11Invoice, CashuError, CheckSpendableRequest, + CheckSpendableResponse, CurrencyUnit, Id, InvoiceStatus, KeyPair, KeySet, KeySetInfo, + KeySetResponse, Keys, KeysResponse, MeltBolt11Request, MeltBolt11Response, + MeltQuoteBolt11Request, MeltQuoteBolt11Response, MintBolt11Request, MintBolt11Response, + MintInfo, MintKeySet, MintProof, MintProofs, MintQuoteBolt11Request, + MintQuoteBolt11Response, MintVersion, Nut05MeltBolt11Request, Nut05MeltBolt11Response, + PreMintSecrets, Proof, PublicKey, Secret, SecretKey, SwapRequest, SwapResponse, Token, }; pub use crate::error::CashuSdkError; diff --git a/bindings/cashu-sdk-ffi/src/mint.rs b/bindings/cashu-sdk-ffi/src/mint.rs index 62c2eddee..4740250b1 100644 --- a/bindings/cashu-sdk-ffi/src/mint.rs +++ b/bindings/cashu-sdk-ffi/src/mint.rs @@ -3,8 +3,8 @@ use std::sync::{Arc, RwLock}; use cashu_ffi::{ Amount, CheckSpendableRequest, CheckSpendableResponse, Id, KeySet, KeySetResponse, - KeysResponse, MeltRequest, MeltResponse, MintRequest, PostMintResponse, Secret, SplitRequest, - SplitResponse, + KeysResponse, MeltBolt11Request, MeltBolt11Response, MintBolt11Request, MintBolt11Response, + Secret, SwapRequest, SwapResponse, }; use cashu_sdk::mint::Mint as MintSdk; @@ -38,6 +38,8 @@ impl Mint { &secret, keysets, spent_secrets, + // TODO: quotes + vec![], *min_fee_reserve.as_ref().deref(), percent_fee_reserve, ) @@ -67,8 +69,8 @@ impl Mint { pub fn process_mint_request( &self, - mint_request: Arc, - ) -> Result> { + mint_request: Arc, + ) -> Result> { Ok(Arc::new( self.inner .write() @@ -78,15 +80,15 @@ impl Mint { )) } - pub fn process_split_request( + pub fn process_swap_request( &self, - split_request: Arc, - ) -> Result> { + split_request: Arc, + ) -> Result> { Ok(Arc::new( self.inner .write() .unwrap() - .process_split_request(split_request.as_ref().deref().clone())? + .process_swap_request(split_request.as_ref().deref().clone())? .into(), )) } @@ -104,7 +106,7 @@ impl Mint { )) } - pub fn verify_melt_request(&self, melt_request: Arc) -> Result<()> { + pub fn verify_melt_request(&self, melt_request: Arc) -> Result<()> { Ok(self .inner .write() @@ -114,10 +116,10 @@ impl Mint { pub fn process_melt_request( &self, - melt_request: Arc, + melt_request: Arc, preimage: String, total_spent: Arc, - ) -> Result> { + ) -> Result> { Ok(Arc::new( self.inner .write() diff --git a/bindings/cashu-sdk-ffi/src/wallet.rs b/bindings/cashu-sdk-ffi/src/wallet.rs index a25538f04..4d402f3ba 100644 --- a/bindings/cashu-sdk-ffi/src/wallet.rs +++ b/bindings/cashu-sdk-ffi/src/wallet.rs @@ -1,9 +1,7 @@ use std::ops::Deref; use std::sync::Arc; -use cashu_ffi::{ - BlindedSignature, Bolt11Invoice, PreMintSecrets, Proof, RequestMintResponse, Token, -}; +use cashu_ffi::{BlindedSignature, CurrencyUnit, PreMintSecrets, Proof, Token}; use cashu_sdk::client::minreq_client::HttpClient; use cashu_sdk::types::ProofsStatus; use cashu_sdk::url::UncheckedUrl; @@ -43,23 +41,21 @@ impl Wallet { Ok(Arc::new(proofs)) } - pub fn request_mint(&self, amount: Arc) -> Result> { - let mint_response = RUNTIME - .block_on(async { self.inner.request_mint(*amount.as_ref().deref()).await })? - .into(); - Ok(Arc::new(mint_response)) - } - pub fn mint_token( &self, amount: Arc, hash: String, - unit: Option, + unit: Option, memo: Option, ) -> Result> { let token = RUNTIME.block_on(async { self.inner - .mint_token(*amount.as_ref().deref(), &hash, unit, memo) + .mint_token( + *amount.as_ref().deref(), + &hash, + memo, + unit.map(|u| u.into()), + ) .await })?; @@ -73,20 +69,13 @@ impl Wallet { Ok(proofs.into_iter().map(|p| Arc::new(p.into())).collect()) } - pub fn check_fee(&self, invoice: Arc) -> Result> { - let amount = RUNTIME - .block_on(async { self.inner.check_fee(invoice.as_ref().deref().clone()).await })?; - - Ok(Arc::new(amount.into())) - } - pub fn receive(&self, encoded_token: String) -> Result>> { let proofs = RUNTIME.block_on(async { self.inner.receive(&encoded_token).await })?; Ok(proofs.into_iter().map(|p| Arc::new(p.into())).collect()) } - pub fn process_split_response( + pub fn process_swap_response( &self, blinded_messages: Arc, promises: Vec>, @@ -117,14 +106,14 @@ impl Wallet { pub fn melt( &self, - invoice: Arc, + quote: String, proofs: Vec>, fee_reserve: Arc, ) -> Result> { let melted = RUNTIME.block_on(async { self.inner .melt( - invoice.as_ref().deref().clone(), + quote, proofs.iter().map(|p| p.as_ref().deref().clone()).collect(), *fee_reserve.as_ref().deref(), ) @@ -134,16 +123,16 @@ impl Wallet { Ok(Arc::new(melted.into())) } - pub fn proof_to_token( + pub fn proofs_to_token( &self, proofs: Vec>, - unit: Option, + unit: Option, memo: Option, ) -> Result { Ok(self.inner.proofs_to_token( proofs.iter().map(|p| p.as_ref().deref().clone()).collect(), - unit, memo, + unit.map(|u| u.into()), )?) } }