refactor: bindings keyset response

This commit is contained in:
thesimplekid
2023-12-27 21:15:08 +00:00
parent 2508f4ed36
commit dcc35092f4
10 changed files with 35 additions and 37 deletions

View File

@@ -145,7 +145,7 @@ interface MintKeySet {
};
interface KeysResponse {
constructor(Keys keys);
constructor(sequence<KeySet> keysets);
};
interface KeySetResponse {

View File

@@ -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,
@@ -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(),
},
}
}

View File

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

View File

@@ -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)
}
}

View File

@@ -148,7 +148,7 @@ interface MintKeySet {
};
interface KeysResponse {
constructor(Keys keys);
constructor(sequence<KeySet> keysets);
};
interface KeySetResponse {

View File

@@ -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;
@@ -68,7 +68,10 @@ impl JsMint {
.clone()
.into();
Ok(KeysResponse { keys: keyset.keys }.into())
Ok(KeysResponse {
keysets: vec![keyset],
}
.into())
}
/// Get Keysets

View File

@@ -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()),
}
}
@@ -59,9 +61,8 @@ 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> {
@@ -69,7 +70,7 @@ impl JsWallet {
Ok(self
.inner
.mint_token(*amount.deref(), &hash, memo, unit)
.mint_token(*amount.deref(), memo, unit)
.await
.map_err(into_err)?
.into())
@@ -77,15 +78,9 @@ impl JsWallet {
/// 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

View File

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

View File

@@ -5,6 +5,7 @@ use std::collections::{BTreeMap, HashMap};
use serde::{Deserialize, Serialize};
use super::KeySet;
use crate::error::Error;
use crate::Amount;
@@ -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
@@ -170,7 +170,7 @@ impl<'de> serde::de::Deserialize<'de> for KeysResponse {
deserializer.deserialize_map(KeysVisitor)
}
}
*/
pub mod mint {
use std::collections::BTreeMap;

View File

@@ -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};
@@ -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)?,
@@ -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>
@@ -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 {
@@ -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();