Skip to content

Commit

Permalink
refactor: bindings keyset response
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Dec 27, 2023
1 parent 2508f4e commit dcc3509
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 37 deletions.
2 changes: 1 addition & 1 deletion bindings/cashu-ffi/src/cashu.udl
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ interface MintKeySet {
};

interface KeysResponse {
constructor(Keys keys);
constructor(sequence<KeySet> keysets);
};

interface KeySetResponse {
Expand Down
6 changes: 3 additions & 3 deletions bindings/cashu-ffi/src/nuts/nut01/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
use cashu::nuts::nut01::{Keys as KeysSdk, KeysResponse as KeysResponseSdk};
use cashu::Amount as AmountSdk;

use crate::{Amount, PublicKey};
use crate::{Amount, KeySet, PublicKey};

pub struct Keys {
inner: KeysSdk,
Expand Down Expand Up @@ -100,10 +100,10 @@ impl From<KeysResponseSdk> for KeysResponse {
}

impl KeysResponse {
pub fn new(keys: Arc<Keys>) -> Self {
pub fn new(keysets: Vec<Arc<KeySet>>) -> Self {
Self {
inner: KeysResponseSdk {
keys: keys.as_ref().deref().clone(),
keysets: keysets.iter().map(|k| k.as_ref().deref().clone()).collect(),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/cashu-ffi/src/nuts/nut06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl MintInfo {
description_long: Option<String>,
contact: Option<Vec<Vec<String>>>,
// TODO: Should be a nuts type
nuts: String,
_nuts: String,
motd: Option<String>,
) -> Self {
let pubkey = pubkey.map(|p| p.as_ref().deref().clone());
Expand Down
4 changes: 2 additions & 2 deletions bindings/cashu-js/src/nuts/nut02/keyset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl JsKeysResponse {

/// Get Keys
#[wasm_bindgen(getter)]
pub fn keys(&self) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.keys).map_err(into_err)
pub fn keysets(&self) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.keysets).map_err(into_err)
}
}
2 changes: 1 addition & 1 deletion bindings/cashu-sdk-ffi/src/cashu_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ interface MintKeySet {
};

interface KeysResponse {
constructor(Keys keys);
constructor(sequence<KeySet> keysets);
};

interface KeySetResponse {
Expand Down
7 changes: 5 additions & 2 deletions bindings/cashu-sdk-js/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::Deref;
use cashu_js::nuts::{JsCheckSpendableRequest, JsCheckSpendableResponse};
use cashu_js::nuts::{
JsId, JsKeySet, JsKeySetsResponse, JsKeysResponse, JsMeltBolt11Request, JsMeltBolt11Response,
JsMintBolt11Request, JsMintBolt11Response, JsSwapRequest, JsSwapResponse,
JsSwapRequest, JsSwapResponse,
};
use cashu_js::JsAmount;
use cashu_sdk::mint::Mint;
Expand Down Expand Up @@ -68,7 +68,10 @@ impl JsMint {
.clone()
.into();

Ok(KeysResponse { keys: keyset.keys }.into())
Ok(KeysResponse {
keysets: vec![keyset],
}
.into())
}

/// Get Keysets
Expand Down
21 changes: 8 additions & 13 deletions bindings/cashu-sdk-js/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ impl From<Wallet<HttpClient>> for JsWallet {

#[wasm_bindgen(js_class = Wallet)]
impl JsWallet {
// TODO: Quotes
#[wasm_bindgen(constructor)]
pub fn new(mint_url: String, mint_keys: JsKeys) -> JsWallet {
let client = HttpClient {};

JsWallet {
inner: Wallet::new(client, mint_url.into(), mint_keys.deref().clone()),
inner: Wallet::new(client, mint_url.into(), vec![], mint_keys.deref().clone()),
}
}

Expand All @@ -59,33 +61,26 @@ impl JsWallet {
/// Mint Token
#[wasm_bindgen(js_name = mintToken)]
pub async fn mint_token(
&self,
&mut self,
amount: JsAmount,
hash: String,
memo: Option<String>,
unit: Option<String>,
) -> Result<JsToken> {
let unit = unit.map(|u| CurrencyUnit::from_str(&u).unwrap_or_default());

Ok(self
.inner
.mint_token(*amount.deref(), &hash, memo, unit)
.mint_token(*amount.deref(), memo, unit)
.await
.map_err(into_err)?
.into())
}

/// Mint
#[wasm_bindgen(js_name = mint)]
pub async fn mint(&self, amount: JsAmount, hash: String) -> Result<JsValue> {
serde_wasm_bindgen::to_value(
&self
.inner
.mint(*amount.deref(), &hash)
.await
.map_err(into_err)?,
)
.map_err(into_err)
pub async fn mint(&mut self, quote: String) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.mint(&quote).await.map_err(into_err)?)
.map_err(into_err)
}

/// Receive
Expand Down
8 changes: 5 additions & 3 deletions crates/cashu-sdk/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ impl Mint {
/// Retrieve the public keys of the active keyset for distribution to
/// wallet clients
pub fn keyset_pubkeys(&self, keyset_id: &Id) -> Option<KeysResponse> {
let keys: Keys = match self.keysets.get(keyset_id) {
Some(keyset) => keyset.keys.clone().into(),
let keyset = match self.keysets.get(keyset_id) {
Some(keyset) => keyset.clone(),
None => {
return None;
}
};

Some(KeysResponse { keys })
Some(KeysResponse {
keysets: vec![keyset.into()],
})
}

/// Return a list of all supported keysets
Expand Down
10 changes: 5 additions & 5 deletions crates/cashu/src/nuts/nut01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::{BTreeMap, HashMap};

use serde::{Deserialize, Serialize};

use super::KeySet;
use crate::error::Error;
use crate::Amount;

Expand Down Expand Up @@ -123,13 +124,12 @@ impl Keys {
}

/// Mint Public Keys [NUT-01]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KeysResponse {
/// set of public keys that the mint generates
#[serde(flatten)]
pub keys: Keys,
pub keysets: Vec<KeySet>,
}

/*
impl<'de> serde::de::Deserialize<'de> for KeysResponse {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down Expand Up @@ -170,7 +170,7 @@ impl<'de> serde::de::Deserialize<'de> for KeysResponse {
deserializer.deserialize_map(KeysVisitor)
}
}

*/
pub mod mint {
use std::collections::BTreeMap;

Expand Down
10 changes: 4 additions & 6 deletions crates/cashu/src/nuts/nut02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// https://github.com/cashubtc/nuts/blob/main/02.md

use core::fmt;
use std::collections::HashSet;
use std::str::FromStr;

use bitcoin::hashes::{sha256, Hash};
Expand Down Expand Up @@ -60,14 +59,11 @@ impl FromStr for Id {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
println!("{}", s);
// Check if the string length is valid
if s.len() != 16 {
return Err(Error::Length);
}

println!("{}", s[2..].as_bytes().len());

Ok(Self {
version: KeySetVersion::Version00,
id: s[2..].as_bytes().try_into().map_err(|_| Error::Length)?,
Expand Down Expand Up @@ -95,7 +91,7 @@ impl<'de> serde::de::Deserialize<'de> for Id {
type Value = Id;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a 12-character Base64 string")
formatter.write_str("Expecting a 14 char hex string")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
Expand Down Expand Up @@ -154,7 +150,7 @@ impl From<&Keys> for Id {
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KeysetResponse {
/// set of public key ids that the mint generates
pub keysets: HashSet<KeySetInfo>,
pub keysets: Vec<KeySetInfo>,
}

impl KeysetResponse {
Expand Down Expand Up @@ -366,6 +362,8 @@ mod test {

#[test]
fn deserialization_and_id_generation() {
let _id = Id::from_str("009a1f293253e41e").unwrap();

let keys: Keys = serde_json::from_str(SHORT_KEYSET).unwrap();

let id: Id = (&keys).into();
Expand Down

0 comments on commit dcc3509

Please sign in to comment.