diff --git a/bindings/cashu-ffi/src/cashu.udl b/bindings/cashu-ffi/src/cashu.udl index e128c0e4..52a34de9 100644 --- a/bindings/cashu-ffi/src/cashu.udl +++ b/bindings/cashu-ffi/src/cashu.udl @@ -27,6 +27,10 @@ interface Secret { sequence as_bytes(); }; +interface Id { + [Throws=CashuError] + constructor(string id); +}; interface PublicKey { [Throws=CashuError, Name=from_hex] @@ -48,26 +52,26 @@ interface BlindedMessage { }; interface Proof { - constructor(Amount amount, Secret secret, PublicKey c, string? id); + constructor(Amount amount, Secret secret, PublicKey c, Id? id); Amount amount(); Secret secret(); PublicKey c(); - string? id(); + Id? id(); }; interface BlindedSignature { - constructor(string id, Amount amount, PublicKey c); - string id(); + constructor(Id id, Amount amount, PublicKey c); + Id id(); Amount amount(); PublicKey c(); }; interface MintProof { - constructor(Amount? amount, Secret secret, PublicKey? c, string? id); + constructor(Amount? amount, Secret secret, PublicKey? c, Id? id); Amount? amount(); Secret secret(); PublicKey? c(); - string? id(); + Id? id(); }; @@ -116,8 +120,8 @@ interface Keys { }; interface KeySet { - constructor(string id, Keys keys); - string id(); + constructor(Id id, Keys keys); + Id id(); Keys keys(); }; @@ -127,8 +131,8 @@ interface MintKeySet { }; interface KeySetResponse { - constructor(sequence keyset_ids); - sequence keyset_ids(); + constructor(sequence keyset_ids); + sequence keyset_ids(); }; interface RequestMintResponse { diff --git a/bindings/cashu-ffi/src/error.rs b/bindings/cashu-ffi/src/error.rs index 1f3c4461..c545a111 100644 --- a/bindings/cashu-ffi/src/error.rs +++ b/bindings/cashu-ffi/src/error.rs @@ -48,3 +48,11 @@ impl From for CashuError { } } } + +impl From for CashuError { + fn from(err: cashu::nuts::nut02::Error) -> Self { + Self::Generic { + err: "Nut 2 error".to_string(), + } + } +} diff --git a/bindings/cashu-ffi/src/lib.rs b/bindings/cashu-ffi/src/lib.rs index 247755de..926b74fc 100644 --- a/bindings/cashu-ffi/src/lib.rs +++ b/bindings/cashu-ffi/src/lib.rs @@ -14,7 +14,7 @@ mod ffi { pub use crate::nuts::nut01::keys::Keys; pub use crate::nuts::nut01::public_key::PublicKey; pub use crate::nuts::nut01::secret_key::SecretKey; - pub use crate::nuts::nut02::{KeySet, KeySetResponse, MintKeySet}; + pub use crate::nuts::nut02::{Id, KeySet, KeySetResponse, MintKeySet}; pub use crate::nuts::nut03::RequestMintResponse; pub use crate::nuts::nut04::{MintRequest, PostMintResponse}; pub use crate::nuts::nut05::{ diff --git a/bindings/cashu-ffi/src/nuts/nut00/blinded_signature.rs b/bindings/cashu-ffi/src/nuts/nut00/blinded_signature.rs index 6667a2a4..ea195a3b 100644 --- a/bindings/cashu-ffi/src/nuts/nut00/blinded_signature.rs +++ b/bindings/cashu-ffi/src/nuts/nut00/blinded_signature.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use cashu::nuts::nut00::BlindedSignature as BlindedSignatureSdk; use crate::Amount; +use crate::Id; use crate::PublicKey; pub struct BlindedSignature { @@ -11,18 +12,18 @@ pub struct BlindedSignature { } impl BlindedSignature { - pub fn new(id: String, amount: Arc, c: Arc) -> Self { + pub fn new(id: Arc, amount: Arc, c: Arc) -> Self { Self { inner: BlindedSignatureSdk { - id, + id: *id.as_ref().deref(), amount: *amount.as_ref().deref(), c: c.as_ref().into(), }, } } - pub fn id(&self) -> String { - self.inner.id.clone() + pub fn id(&self) -> Arc { + Arc::new(self.inner.id.clone().into()) } pub fn amount(&self) -> Arc { diff --git a/bindings/cashu-ffi/src/nuts/nut00/proof.rs b/bindings/cashu-ffi/src/nuts/nut00/proof.rs index 2a3ab6a1..da10eb45 100644 --- a/bindings/cashu-ffi/src/nuts/nut00/proof.rs +++ b/bindings/cashu-ffi/src/nuts/nut00/proof.rs @@ -2,7 +2,7 @@ use std::{ops::Deref, sync::Arc}; use cashu::nuts::nut00::Proof as ProofSdk; -use crate::{types::Secret, Amount, PublicKey}; +use crate::{types::Secret, Amount, Id, PublicKey}; pub struct Proof { inner: ProofSdk, @@ -20,14 +20,14 @@ impl Proof { amount: Arc, secret: Arc, c: Arc, - id: Option, + id: Option>, ) -> Self { Self { inner: ProofSdk { amount: *amount.as_ref().deref(), secret: secret.as_ref().deref().clone(), c: c.as_ref().deref().clone(), - id, + id: id.map(|id| id.as_ref().deref().clone()), }, } } @@ -44,8 +44,8 @@ impl Proof { Arc::new(self.inner.c.clone().into()) } - pub fn id(&self) -> Option { - self.inner.id.clone() + pub fn id(&self) -> Option> { + self.inner.id.clone().map(|id| Arc::new(id.into())) } } @@ -55,7 +55,7 @@ impl From<&Proof> for ProofSdk { amount: *proof.amount().as_ref().deref(), secret: proof.secret().as_ref().deref().clone(), c: proof.c().deref().into(), - id: proof.id(), + id: proof.id().map(|id| id.as_ref().deref().clone()), } } } @@ -74,6 +74,7 @@ pub mod mint { use crate::types::Secret; use crate::Amount; + use crate::Id; use crate::PublicKey; pub struct Proof { @@ -92,14 +93,14 @@ pub mod mint { amount: Option>, secret: Arc, c: Option>, - id: Option, + id: Option>, ) -> Self { Self { inner: ProofSdk { amount: amount.map(|a| *a.as_ref().deref()), secret: secret.as_ref().deref().clone(), c: c.map(|c| c.as_ref().into()), - id, + id: id.map(|id| id.as_ref().deref().clone()), }, } } @@ -116,8 +117,8 @@ pub mod mint { self.inner.c.clone().map(|c| Arc::new(c.into())) } - pub fn id(&self) -> Option { - self.inner.id.clone() + pub fn id(&self) -> Option> { + self.inner.id.clone().map(|id| Arc::new(id.into())) } } diff --git a/bindings/cashu-ffi/src/nuts/nut02/key_set.rs b/bindings/cashu-ffi/src/nuts/nut02/key_set.rs index 239a61e2..1b96a947 100644 --- a/bindings/cashu-ffi/src/nuts/nut02/key_set.rs +++ b/bindings/cashu-ffi/src/nuts/nut02/key_set.rs @@ -1,12 +1,43 @@ -use std::collections::HashSet; use std::ops::Deref; use std::sync::Arc; +use cashu::nuts::nut02::Id as IdSdk; use cashu::nuts::nut02::KeySet as KeySetSdk; use cashu::nuts::nut02::Response; +use crate::error::Result; use crate::nuts::nut01::keys::Keys; +pub struct Id { + inner: IdSdk, +} + +impl Deref for Id { + type Target = IdSdk; + fn deref(&self) -> &Self::Target { + &self.inner + } +} +impl Id { + pub fn new(id: String) -> Result { + Ok(Self { + inner: IdSdk::try_from_base64(&id)?, + }) + } +} + +impl From for Id { + fn from(inner: IdSdk) -> Id { + Id { inner } + } +} + +impl From for IdSdk { + fn from(id: Id) -> IdSdk { + id.inner + } +} + pub struct KeySet { inner: KeySetSdk, } @@ -19,17 +50,17 @@ impl Deref for KeySet { } impl KeySet { - pub fn new(id: String, keys: Arc) -> Self { + pub fn new(id: Arc, keys: Arc) -> Self { Self { inner: KeySetSdk { - id, + id: id.as_ref().deref().clone(), keys: keys.as_ref().deref().clone(), }, } } - pub fn id(&self) -> String { - self.inner.id.clone() + pub fn id(&self) -> Arc { + Arc::new(self.inner.id.clone().into()) } pub fn keys(&self) -> Arc { @@ -48,15 +79,20 @@ pub struct KeySetResponse { } impl KeySetResponse { - pub fn new(keyset_ids: Vec) -> Self { - let keysets = HashSet::from_iter(keyset_ids); + pub fn new(keyset_ids: Vec>) -> Self { + let keysets = keyset_ids.into_iter().map(|id| id.inner).collect(); Self { inner: Response { keysets }, } } - pub fn keyset_ids(&self) -> Vec { - self.inner.clone().keysets.into_iter().collect() + pub fn keyset_ids(&self) -> Vec> { + self.inner + .clone() + .keysets + .into_iter() + .map(|id| Arc::new(id.into())) + .collect() } } diff --git a/bindings/cashu-ffi/src/nuts/nut02/mod.rs b/bindings/cashu-ffi/src/nuts/nut02/mod.rs index a03ce11a..7836f6e2 100644 --- a/bindings/cashu-ffi/src/nuts/nut02/mod.rs +++ b/bindings/cashu-ffi/src/nuts/nut02/mod.rs @@ -1,5 +1,5 @@ pub mod key_set; pub mod mint_keyset; -pub use key_set::{KeySet, KeySetResponse}; +pub use key_set::{Id, KeySet, KeySetResponse}; pub use mint_keyset::MintKeySet;