cashu-sdk: improve: mint active_keyset_puvkeys returns response type

This commit is contained in:
thesimplekid
2023-09-27 22:30:11 +01:00
parent 2880b5ae8b
commit 135a1cf094
9 changed files with 136 additions and 33 deletions

View File

@@ -130,6 +130,11 @@ interface MintKeySet {
constructor(string secret, string derivation_path, u8 max_order);
};
interface KeysResponse {
constructor(Keys keys);
};
interface KeySetResponse {
constructor(sequence<Id> keyset_ids);
sequence<Id> keyset_ids();

View File

@@ -11,7 +11,7 @@ mod ffi {
pub use crate::nuts::nut00::proof::{mint::Proof as MintProof, Proof};
pub use crate::nuts::nut00::token::Token;
pub use crate::nuts::nut01::key_pair::KeyPair;
pub use crate::nuts::nut01::keys::Keys;
pub use crate::nuts::nut01::keys::{Keys, KeysResponse};
pub use crate::nuts::nut01::public_key::PublicKey;
pub use crate::nuts::nut01::secret_key::SecretKey;
pub use crate::nuts::nut02::{Id, KeySet, KeySetResponse, MintKeySet};

View File

@@ -1,7 +1,7 @@
use std::{collections::HashMap, ops::Deref, sync::Arc};
use crate::{Amount, PublicKey};
use cashu::nuts::nut01::Keys as KeysSdk;
use cashu::nuts::nut01::{Keys as KeysSdk, Response as KeysResponseSdk};
use cashu::Amount as AmountSdk;
pub struct Keys {
@@ -15,6 +15,24 @@ impl Deref for Keys {
}
}
impl From<Keys> for KeysSdk {
fn from(keys: Keys) -> KeysSdk {
keys.inner
}
}
impl From<KeysSdk> for Keys {
fn from(keys: KeysSdk) -> Keys {
let keys = keys
.keys()
.into_iter()
.map(|(amount, pk)| (amount.to_sat().to_string(), Arc::new(pk.into())))
.collect();
Keys::new(keys)
}
}
impl Keys {
pub fn new(keys: HashMap<String, Arc<PublicKey>>) -> Self {
let keys = keys
@@ -55,20 +73,35 @@ impl Keys {
}
}
impl From<Keys> for KeysSdk {
fn from(keys: Keys) -> KeysSdk {
pub struct KeysResponse {
inner: KeysResponseSdk,
}
impl Deref for KeysResponse {
type Target = KeysResponseSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl From<KeysResponse> for KeysResponseSdk {
fn from(keys: KeysResponse) -> KeysResponseSdk {
keys.inner
}
}
impl From<KeysSdk> for Keys {
fn from(keys: KeysSdk) -> Keys {
let keys = keys
.keys()
.into_iter()
.map(|(amount, pk)| (amount.to_sat().to_string(), Arc::new(pk.into())))
.collect();
Keys::new(keys)
impl From<KeysResponseSdk> for KeysResponse {
fn from(keys: KeysResponseSdk) -> KeysResponse {
KeysResponse { inner: keys }
}
}
impl KeysResponse {
pub fn new(keys: Arc<Keys>) -> Self {
Self {
inner: KeysResponseSdk {
keys: keys.as_ref().deref().clone(),
},
}
}
}

View File

@@ -135,6 +135,11 @@ interface MintKeySet {
constructor(string secret, string derivation_path, u8 max_order);
};
interface KeysResponse {
constructor(Keys keys);
};
interface KeySetResponse {
constructor(sequence<Id> keyset_ids);
sequence<Id> keyset_ids();
@@ -326,7 +331,7 @@ interface Wallet {
interface Mint {
[Throws=CashuSdkError]
constructor(string secret, string derivation_path, record<string, MintKeySet> inactive_keysets, sequence<Secret> spent_secrets, u8 max_order, Amount min_fee_reserve, f32 percent_fee_reserve);
KeySet active_keyset_pubkeys();
KeysResponse active_keyset_pubkeys();
KeySetResponse keysets();
MintKeySet active_keyset();
KeySet? keyset(Id id);

View File

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

View File

@@ -5,8 +5,9 @@ use std::{
};
use cashu_ffi::{
Amount, CheckSpendableRequest, CheckSpendableResponse, Id, KeySet, KeySetResponse, MeltRequest,
MeltResponse, MintKeySet, MintRequest, PostMintResponse, Secret, SplitRequest, SplitResponse,
Amount, CheckSpendableRequest, CheckSpendableResponse, Id, KeySet, KeySetResponse,
KeysResponse, MeltRequest, MeltResponse, MintKeySet, MintRequest, PostMintResponse, Secret,
SplitRequest, SplitResponse,
};
use cashu_sdk::mint::Mint as MintSdk;
use cashu_sdk::nuts::nut02::Id as IdSdk;
@@ -54,7 +55,7 @@ impl Mint {
})
}
pub fn active_keyset_pubkeys(&self) -> Arc<KeySet> {
pub fn active_keyset_pubkeys(&self) -> Arc<KeysResponse> {
Arc::new(self.inner.read().unwrap().active_keyset_pubkeys().into())
}

View File

@@ -51,8 +51,10 @@ impl Mint {
/// Retrieve the public keys of the active keyset for distribution to
/// wallet clients
pub fn active_keyset_pubkeys(&self) -> nut02::KeySet {
nut02::KeySet::from(self.active_keyset.clone())
pub fn active_keyset_pubkeys(&self) -> nut01::Response {
nut01::Response {
keys: nut02::KeySet::from(self.active_keyset.clone()).keys,
}
}
/// Return a list of all supported keysets

View File

@@ -81,6 +81,17 @@ impl SecretKey {
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct Keys(BTreeMap<Amount, PublicKey>);
impl From<mint::Keys> for Keys {
fn from(keys: mint::Keys) -> Self {
Self(
keys.0
.iter()
.map(|(amount, keypair)| (*amount, keypair.public_key.clone()))
.collect(),
)
}
}
impl Keys {
pub fn new(keys: BTreeMap<Amount, PublicKey>) -> Self {
Self(keys)
@@ -108,14 +119,51 @@ impl Keys {
}
}
impl From<mint::Keys> for Keys {
fn from(keys: mint::Keys) -> Self {
Self(
keys.0
.iter()
.map(|(amount, keypair)| (*amount, keypair.public_key.clone()))
.collect(),
)
/// Mint Public Keys [NUT-01]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Response {
/// set of public keys that the mint generates
#[serde(flatten)]
pub keys: Keys,
}
impl<'de> serde::de::Deserialize<'de> for Response {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct KeysVisitor;
impl<'de> serde::de::Visitor<'de> for KeysVisitor {
type Value = Response;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("")
}
fn visit_map<M>(self, mut m: M) -> Result<Self::Value, M::Error>
where
M: serde::de::MapAccess<'de>,
{
let mut keys: BTreeMap<Amount, PublicKey> = BTreeMap::new();
while let Some((a, k)) = m.next_entry::<String, String>()? {
let amount = a.parse();
let pub_key = PublicKey::from_hex(k);
if let (Ok(amount), Ok(pubkey)) = (amount, pub_key) {
let amount = Amount::from_sat(amount);
keys.insert(amount, pubkey);
}
// TODO: Should return an error if an amount or key is invalid and not continue
}
Ok(Response { keys: Keys(keys) })
}
}
deserializer.deserialize_map(KeysVisitor)
}
}
@@ -151,7 +199,7 @@ pub mod mint {
#[cfg(test)]
mod tests {
use super::PublicKey;
use super::*;
#[test]
fn pubkey() {
@@ -160,4 +208,13 @@ mod tests {
assert_eq!(pubkey_str, pubkey.to_hex())
}
#[test]
fn key_response() {
let res: String = r#"{"1":"02f71e2d93aa95fc52b938735a24774ad926406c81e9dc9d2aa699fb89281548fd","2":"03b28dd9c19aaf1ec847be31b60c6a5e1a6cb6f87434afcdb0d9348ba0e2bdb150","4":"03ede0e704e223e764a82f73984b0fec0fdbde15ef57b4de95b527f7182af7487e","8":"020fd24fbd552445df70c244be2af77da2b2f634ccfda9e9620b347b5cd50dbdd8","16":"03ef9ef2515df5c0d0851ed9419a24a571ef5e03206d9d2fc6572ac050c5afe1aa","32":"02dbd455474176b30234c178573e874cc79d0c2fc1920cf0e9f133204cf43299c1","64":"0237c1eb11b8a214cca3e0104684227952188039a05cd55c1ad3896a572c70a7c3","128":"02655041771766b94a269f9f1ec1860f2eade55bb472c4db74ac1257ef54aac52b","256":"02b9e0be7b423bded7d60ff7114549de8d2d5b9c099edf8887aff474037e4bc0cf","512":"0320454cc41e646f49e1ac0a62b9667c80dee45545b045575f2a26f01770dc2521","1024":"0267fc1dabac016f46b3d1a650d97b56f3e56540106720f3d24ff7a6e9cd7183e9","2048":"035a9a25251a4da56f49667ca50677470fc6d8e186a875ab7b32aa064eb9e9e948","4096":"02f607a9eed310825c2d2e66d6e64fb237fe21b640b9a66cc7646b2a6480d91457","8192":"033346f7dce2ef71a80c5d657a8930bdd19c7c1708d03829daf43f20eaeda76768","16384":"024fad3b0b60c6b71d848deac173183fae8ddde31bbde531f18ab23473ddff541d","32768":"020d195466819d96d8c7eee9150565b7bd37196c7d12d0e96e389f56be8aebb44b","65536":"038c9bf295a745726c38d14988851d68d201296a802c296faa838000c2f44d25e0","131072":"032ff6491cdeff0bf9b34acd5deceef3cca7682b5f94dbe3068af8bb3b5aa34b81","262144":"02570090f5b6900955fd794d8f22c23fb35fc87fa03069b9b16bea63ea7cda419a","524288":"029d3c751c7d1c3e1d3e4b7791e1e809f6dedf2c28e172a82967d49a14b7c26ce2","1048576":"03b4a41d39cc6f2a8925f694c514e107b87d7ddb8f5ac55c9e4b7895139d0decd8","2097152":"02d4abbce491f87656eb0d2e66ef18eb009f6320169ef12e66703298d5395f2b91","4194304":"0359023fb85f6e6ede0141ab8f4a1277c19ed62b49b8ef5c5e2c8ca7fefe9b2f91","8388608":"0353d3ae1dad05e1b46ab85a366bfcdb7a645e3457f7714003e0fb06f4d75f4d87","16777216":"032d0847606465b97f15aca30c69f5baeeb43bf6188b4679f723119ce6fb9708c5","33554432":"028a673a53e78aa8c992128e21efb3b33fbd54de20afcf81a67e69eaf2bab7e0e9","67108864":"0278b66e140559352bb5aeca854a6466bc439ee206a9f349ed7926aae4335269b7","134217728":"023834651da0737f484a77204c2d06543fb65ad2dd8d095a2be48ca12ebf2664ec","268435456":"032cba9068638965ccc3870c140c72a1b028a820851f36fe59639e7ab3093a8ffd","536870912":"03eae5e4b22dfa5ad77476c925717dc4e005da78142e75b47fb28569d745483af3","1073741824":"02d17d61027602432a8484b65e6d6063ed9157c51ce92099d61ac2820411c59f9f","2147483648":"0236870e39b3a739d5caa04988dce432e3d7988420f04d9b415125af22672e2726"}"#.to_string();
let response: Response = serde_json::from_str(&res).unwrap();
assert_eq!(&serde_json::to_string(&response).unwrap(), &res)
}
}

View File

@@ -148,7 +148,7 @@ impl From<&Keys> for Id {
/// Mint Keysets [NUT-02]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Response {
/// set of public keys that the mint generates
/// set of public key ids that the mint generates
pub keysets: HashSet<Id>,
}