binding/cashu improve: use Id type

This commit is contained in:
thesimplekid
2023-09-09 12:46:30 +01:00
parent 6cde1e7b22
commit 9343f95f2b
7 changed files with 85 additions and 35 deletions

View File

@@ -27,6 +27,10 @@ interface Secret {
sequence<u8> 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<string> keyset_ids);
sequence<string> keyset_ids();
constructor(sequence<Id> keyset_ids);
sequence<Id> keyset_ids();
};
interface RequestMintResponse {

View File

@@ -48,3 +48,11 @@ impl From<ParseOrSemanticError> for CashuError {
}
}
}
impl From<cashu::nuts::nut02::Error> for CashuError {
fn from(err: cashu::nuts::nut02::Error) -> Self {
Self::Generic {
err: "Nut 2 error".to_string(),
}
}
}

View File

@@ -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::{

View File

@@ -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<Amount>, c: Arc<PublicKey>) -> Self {
pub fn new(id: Arc<Id>, amount: Arc<Amount>, c: Arc<PublicKey>) -> 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<Id> {
Arc::new(self.inner.id.clone().into())
}
pub fn amount(&self) -> Arc<Amount> {

View File

@@ -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<Amount>,
secret: Arc<Secret>,
c: Arc<PublicKey>,
id: Option<String>,
id: Option<Arc<Id>>,
) -> 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<String> {
self.inner.id.clone()
pub fn id(&self) -> Option<Arc<Id>> {
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<Arc<Amount>>,
secret: Arc<Secret>,
c: Option<Arc<PublicKey>>,
id: Option<String>,
id: Option<Arc<Id>>,
) -> 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<String> {
self.inner.id.clone()
pub fn id(&self) -> Option<Arc<Id>> {
self.inner.id.clone().map(|id| Arc::new(id.into()))
}
}

View File

@@ -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<Self> {
Ok(Self {
inner: IdSdk::try_from_base64(&id)?,
})
}
}
impl From<IdSdk> for Id {
fn from(inner: IdSdk) -> Id {
Id { inner }
}
}
impl From<Id> 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<Keys>) -> Self {
pub fn new(id: Arc<Id>, keys: Arc<Keys>) -> 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<Id> {
Arc::new(self.inner.id.clone().into())
}
pub fn keys(&self) -> Arc<Keys> {
@@ -48,15 +79,20 @@ pub struct KeySetResponse {
}
impl KeySetResponse {
pub fn new(keyset_ids: Vec<String>) -> Self {
let keysets = HashSet::from_iter(keyset_ids);
pub fn new(keyset_ids: Vec<Arc<Id>>) -> Self {
let keysets = keyset_ids.into_iter().map(|id| id.inner).collect();
Self {
inner: Response { keysets },
}
}
pub fn keyset_ids(&self) -> Vec<String> {
self.inner.clone().keysets.into_iter().collect()
pub fn keyset_ids(&self) -> Vec<Arc<Id>> {
self.inner
.clone()
.keysets
.into_iter()
.map(|id| Arc::new(id.into()))
.collect()
}
}

View File

@@ -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;