bindings/cashu-sdk improve: use secret type

This commit is contained in:
thesimplekid
2023-09-09 09:20:55 +01:00
parent f860fc298a
commit a72a1e654d
5 changed files with 31 additions and 25 deletions

View File

@@ -26,6 +26,11 @@ interface Amount {
};
interface Secret {
constructor();
sequence<u8> as_bytes();
};
interface PublicKey {
[Throws=CashuError, Name=from_hex]
constructor(string hex);
@@ -46,9 +51,9 @@ interface BlindedMessage {
};
interface Proof {
constructor(Amount amount, string secret, PublicKey c, string? id);
constructor(Amount amount, Secret secret, PublicKey c, string? id);
Amount amount();
string secret();
Secret secret();
PublicKey c();
string? id();
};
@@ -61,9 +66,9 @@ interface BlindedSignature {
};
interface MintProof {
constructor(Amount? amount, string secret, PublicKey? c, string? id);
constructor(Amount? amount, Secret secret, PublicKey? c, string? id);
Amount? amount();
string secret();
Secret secret();
PublicKey? c();
string? id();
@@ -94,7 +99,7 @@ interface BlindedMessages {
[Throws=CashuError, Name=blank]
constructor(Amount fee_reserve);
sequence<BlindedMessage> blinded_messages();
sequence<string> secrets();
sequence<Secret> secrets();
sequence<SecretKey> rs();
sequence<Amount> amounts();
};
@@ -314,7 +319,7 @@ interface Wallet {
interface Mint {
constructor(string secret, string derivation_path, record<string, MintKeySet> inactive_keysets, sequence<string> spent_secrets, u8 max_order);
constructor(string secret, string derivation_path, record<string, MintKeySet> inactive_keysets, sequence<Secret> spent_secrets, u8 max_order);
KeySet active_keyset_pubkeys();
KeySetResponse keysets();
MintKeySet active_keyset();
@@ -324,7 +329,7 @@ interface Mint {
[Throws=CashuSdkError]
SplitResponse proccess_split_request(SplitRequest split_request);
[Throws=CashuSdkError]
string verify_proof(Proof proof);
void verify_proof(Proof proof);
[Throws=CashuSdkError]
void verify_melt_request(MeltRequest melt_request);
[Throws=CashuSdkError]

View File

@@ -10,8 +10,8 @@ mod ffi {
CheckFeesRequest, CheckFeesResponse, CheckSpendableRequest, CheckSpendableResponse,
InvoiceStatus, KeyPair, KeySet, KeySetResponse, Keys, MeltRequest, MeltResponse, MintInfo,
MintKeySet, MintProof, MintProofs, MintRequest, MintVersion, Nut05MeltRequest,
Nut05MeltResponse, PostMintResponse, Proof, PublicKey, RequestMintResponse, SecretKey,
SplitRequest, SplitResponse, Token,
Nut05MeltResponse, PostMintResponse, Proof, PublicKey, RequestMintResponse, Secret,
SecretKey, SplitRequest, SplitResponse, Token,
};
pub use crate::client::Client;

View File

@@ -9,7 +9,8 @@ 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,
MeltResponse, MintKeySet, MintRequest, PostMintResponse, Proof, Secret, SplitRequest,
SplitResponse,
};
pub struct Mint {
@@ -21,10 +22,13 @@ impl Mint {
secret: String,
derivation_path: String,
inactive_keysets: HashMap<String, Arc<MintKeySet>>,
spent_secrets: Vec<String>,
spent_secrets: Vec<Arc<Secret>>,
max_order: u8,
) -> Self {
let spent_secrets = spent_secrets.into_iter().collect();
let spent_secrets = spent_secrets
.into_iter()
.map(|s| s.as_ref().deref().clone())
.collect();
let inactive_keysets = inactive_keysets
.into_iter()
@@ -89,7 +93,7 @@ impl Mint {
))
}
pub fn verify_proof(&self, proof: Arc<Proof>) -> Result<String> {
pub fn verify_proof(&self, proof: Arc<Proof>) -> Result<()> {
Ok(self
.inner
.read()

View File

@@ -14,14 +14,15 @@ use cashu::nuts::nut07::CheckSpendableResponse;
use cashu::nuts::nut08::MeltRequest;
use cashu::nuts::nut08::MeltResponse;
use cashu::nuts::*;
use cashu::secret::Secret;
use cashu::Amount;
pub struct Mint {
// pub pubkey: PublicKey,
pub active_keyset: nut02::mint::KeySet,
pub inactive_keysets: HashMap<String, nut02::mint::KeySet>,
pub spent_secrets: HashSet<String>,
pub pending_secrets: HashSet<String>,
pub spent_secrets: HashSet<Secret>,
pub pending_secrets: HashSet<Secret>,
}
impl Mint {
@@ -29,7 +30,7 @@ impl Mint {
secret: &str,
derivation_path: &str,
inactive_keysets: HashMap<String, nut02::mint::KeySet>,
spent_secrets: HashSet<String>,
spent_secrets: HashSet<Secret>,
max_order: u8,
) -> Self {
Self {
@@ -126,7 +127,7 @@ impl Mint {
let proof_count = split_request.proofs.len();
let secrets: HashSet<String> = split_request.proofs.into_iter().map(|p| p.secret).collect();
let secrets: HashSet<Secret> = split_request.proofs.into_iter().map(|p| p.secret).collect();
// Check that there are no duplicate proofs in request
if secrets.len().ne(&proof_count) {
@@ -190,7 +191,7 @@ impl Mint {
};
verify_message(
keypair.secret_key.to_owned().into(),
keypair.secret_key.clone().into(),
proof.c.clone().into(),
&proof.secret,
)?;
@@ -225,11 +226,7 @@ impl Mint {
return Err(Error::Amount);
}
let secrets: HashSet<&str> = melt_request
.proofs
.iter()
.map(|p| p.secret.as_str())
.collect();
let secrets: HashSet<&Secret> = melt_request.proofs.iter().map(|p| &p.secret).collect();
// Ensure proofs are unique and not being double spent
if melt_request.proofs.len().ne(&secrets.len()) {

View File

@@ -128,7 +128,7 @@ pub fn sign_message(
pub fn verify_message(
a: SecretKey,
unblinded_message: k256::PublicKey,
msg: Secret,
msg: &Secret,
) -> Result<(), error::mint::Error> {
// Y
let y = hash_to_curve(msg.as_bytes());
@@ -273,6 +273,6 @@ mod tests {
// C
let c = unblind_message(signed.into(), blinded.1, bob_pub.into()).unwrap();
assert!(verify_message(bob_sec, c.into(), x).is_ok());
assert!(verify_message(bob_sec, c.into(), &x).is_ok());
}
}