bindings/sdk: Add Mint

This commit is contained in:
thesimplekid
2023-09-02 12:44:04 +01:00
parent 2edce622ae
commit 19f6e24dfe
9 changed files with 217 additions and 2 deletions

View File

@@ -37,6 +37,12 @@ impl KeySet {
} }
} }
impl From<cashu::nuts::nut02::KeySet> for KeySet {
fn from(inner: cashu::nuts::nut02::KeySet) -> KeySet {
KeySet { inner }
}
}
pub struct KeySetResponse { pub struct KeySetResponse {
inner: Response, inner: Response,
} }

View File

@@ -20,3 +20,9 @@ impl MintKeySet {
} }
} }
} }
impl From<cashu::nuts::nut02::mint::KeySet> for MintKeySet {
fn from(inner: cashu::nuts::nut02::mint::KeySet) -> MintKeySet {
MintKeySet { inner }
}
}

View File

@@ -8,6 +8,13 @@ pub struct MintRequest {
inner: MintRequestSdk, inner: MintRequestSdk,
} }
impl Deref for MintRequest {
type Target = MintRequestSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl MintRequest { impl MintRequest {
pub fn new(outputs: Vec<Arc<BlindedMessage>>) -> Self { pub fn new(outputs: Vec<Arc<BlindedMessage>>) -> Self {
Self { Self {
@@ -34,10 +41,23 @@ impl MintRequest {
} }
} }
impl From<cashu::nuts::nut04::MintRequest> for MintRequest {
fn from(inner: cashu::nuts::nut04::MintRequest) -> MintRequest {
MintRequest { inner }
}
}
pub struct PostMintResponse { pub struct PostMintResponse {
inner: PostMintResponseSdk, inner: PostMintResponseSdk,
} }
impl Deref for PostMintResponse {
type Target = PostMintResponseSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl PostMintResponse { impl PostMintResponse {
pub fn new(promises: Vec<Arc<BlindedSignature>>) -> Self { pub fn new(promises: Vec<Arc<BlindedSignature>>) -> Self {
Self { Self {

View File

@@ -1,4 +1,4 @@
use std::sync::Arc; use std::{ops::Deref, sync::Arc};
use cashu::nuts::nut07::{ use cashu::nuts::nut07::{
CheckSpendableRequest as CheckSpendableRequestSdk, CheckSpendableRequest as CheckSpendableRequestSdk,
@@ -11,6 +11,13 @@ pub struct CheckSpendableRequest {
inner: CheckSpendableRequestSdk, inner: CheckSpendableRequestSdk,
} }
impl Deref for CheckSpendableRequest {
type Target = CheckSpendableRequestSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl CheckSpendableRequest { impl CheckSpendableRequest {
pub fn new(proofs: Vec<Arc<Proof>>) -> Self { pub fn new(proofs: Vec<Arc<Proof>>) -> Self {
Self { Self {

View File

@@ -1,3 +1,4 @@
use std::ops::Deref;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
@@ -11,6 +12,13 @@ pub struct MeltRequest {
inner: MeltRequestSdk, inner: MeltRequestSdk,
} }
impl Deref for MeltRequest {
type Target = MeltRequestSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl MeltRequest { impl MeltRequest {
pub fn new( pub fn new(
proofs: Vec<Arc<Proof>>, proofs: Vec<Arc<Proof>>,

View File

@@ -313,3 +313,24 @@ interface Wallet {
[Throws=CashuSdkError] [Throws=CashuSdkError]
string proof_to_token(sequence<Proof> proof, string? memo); string proof_to_token(sequence<Proof> proof, string? memo);
}; };
interface Mint {
constructor(string secret, string derivation_path, record<string, MintKeySet> inactive_keysets, sequence<string> spent_secrets, u8 max_order);
KeySet active_keyset_pubkeys();
KeySetResponse keysets();
MintKeySet active_keyset();
KeySet? keyset(string id);
[Throws=CashuSdkError]
PostMintResponse process_mint_request(MintRequest mint_request);
[Throws=CashuSdkError]
SplitResponse proccess_split_request(SplitRequest split_request);
[Throws=CashuSdkError]
string verify_proof(Proof proof);
[Throws=CashuSdkError]
void verify_melt_request(MeltRequest melt_request);
[Throws=CashuSdkError]
MeltResponse process_melt_request(MeltRequest melt_request, string preimage, Amount totoal_spent);
};

View File

@@ -30,3 +30,11 @@ impl From<cashu_sdk::wallet::Error> for CashuSdkError {
} }
} }
} }
impl From<cashu_sdk::mint::Error> for CashuSdkError {
fn from(err: cashu_sdk::mint::Error) -> CashuSdkError {
Self::Generic {
err: err.to_string(),
}
}
}

View File

@@ -0,0 +1,139 @@
use std::{
collections::HashMap,
ops::Deref,
sync::{Arc, RwLock},
};
use cashu_sdk::mint::Mint as MintSdk;
use crate::error::Result;
use cashu_ffi::{
Amount, CheckSpendableRequest, CheckSpendableResponse, KeySet, KeySetResponse, MeltRequest,
MeltResponse, MintKeySet, MintRequest, PostMintResponse, Proof, SplitRequest, SplitResponse,
};
pub struct Mint {
inner: RwLock<MintSdk>,
}
impl Mint {
pub fn new(
secret: String,
derivation_path: String,
inactive_keysets: HashMap<String, Arc<MintKeySet>>,
spent_secrets: Vec<String>,
max_order: u8,
) -> Self {
let spent_secrets = spent_secrets.into_iter().collect();
let inactive_keysets = inactive_keysets
.into_iter()
.map(|(k, v)| (k, v.as_ref().deref().clone()))
.collect();
Self {
inner: MintSdk::new(
&secret,
&derivation_path,
inactive_keysets,
spent_secrets,
max_order,
)
.into(),
}
}
pub fn active_keyset_pubkeys(&self) -> Arc<KeySet> {
Arc::new(self.inner.read().unwrap().active_keyset_pubkeys().into())
}
pub fn keysets(&self) -> Arc<KeySetResponse> {
Arc::new(self.inner.read().unwrap().keysets().into())
}
pub fn active_keyset(&self) -> Arc<MintKeySet> {
Arc::new(self.inner.read().unwrap().active_keyset.clone().into())
}
pub fn keyset(&self, id: String) -> Option<Arc<KeySet>> {
self.inner
.read()
.unwrap()
.keyset(&id)
.map(|k| Arc::new(k.into()))
}
pub fn process_mint_request(
&self,
mint_request: Arc<MintRequest>,
) -> Result<Arc<PostMintResponse>> {
Ok(Arc::new(
self.inner
.write()
.unwrap()
.process_mint_request(mint_request.as_ref().deref().clone())?
.into(),
))
}
pub fn proccess_split_request(
&self,
split_request: Arc<SplitRequest>,
) -> Result<Arc<SplitResponse>> {
Ok(Arc::new(
self.inner
.write()
.unwrap()
.process_split_request(split_request.as_ref().deref().clone())?
.into(),
))
}
pub fn verify_proof(&self, proof: Arc<Proof>) -> Result<String> {
Ok(self
.inner
.read()
.unwrap()
.verify_proof(proof.as_ref().deref())?)
}
pub fn check_spendable(
&self,
check_spendable: Arc<CheckSpendableRequest>,
) -> Result<Arc<CheckSpendableResponse>> {
Ok(Arc::new(
self.inner
.read()
.unwrap()
.check_spendable(check_spendable.as_ref().deref())?
.into(),
))
}
pub fn verify_melt_request(&self, melt_request: Arc<MeltRequest>) -> Result<()> {
Ok(self
.inner
.write()
.unwrap()
.verify_melt_request(melt_request.as_ref().deref())?)
}
pub fn process_melt_request(
&self,
melt_request: Arc<MeltRequest>,
preimage: String,
total_spent: Arc<Amount>,
) -> Result<Arc<MeltResponse>> {
Ok(Arc::new(
self.inner
.write()
.unwrap()
.process_melt_request(
melt_request.as_ref().deref(),
&preimage,
*total_spent.as_ref().deref(),
)?
.into(),
))
}
}

View File

@@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};
use cashu::dhke::sign_message; use cashu::dhke::sign_message;
use cashu::dhke::verify_message; use cashu::dhke::verify_message;
use cashu::error::mint::Error; pub use cashu::error::mint::Error;
use cashu::nuts::nut00::BlindedMessage; use cashu::nuts::nut00::BlindedMessage;
use cashu::nuts::nut00::BlindedSignature; use cashu::nuts::nut00::BlindedSignature;
use cashu::nuts::nut00::Proof; use cashu::nuts::nut00::Proof;