refactor: nut11 blinded messages

This commit is contained in:
thesimplekid
2024-02-28 23:29:02 +00:00
parent 9462b6262f
commit c37749dea8
6 changed files with 48 additions and 50 deletions

View File

@@ -172,15 +172,11 @@ impl Client for HttpClient {
) -> Result<SwapResponse, Error> {
let url = join_url(mint_url, &["v1", "swap"])?;
println!("{}", serde_json::to_string(&swap_request).unwrap());
let res = minreq::post(url).with_json(&swap_request)?.send()?;
let value = res.json::<Value>()?;
println!("{}", value);
let response: Result<SwapResponse, serde_json::Error> =
serde_json::from_value(value.clone());
println!("{:?}", response);
match response {
Ok(res) => Ok(res),

View File

@@ -234,7 +234,6 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
}
let keysets = self.client.get_mint_keysets(mint_url.try_into()?).await?;
println!("{:?}", keysets);
self.localstore
.add_mint_keysets(
@@ -385,16 +384,12 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
pre_swap.pre_mint_secrets.secrets(),
&keys,
)?;
// println!("{:?}", p);
let mint_proofs = proofs.entry(token.mint).or_default();
mint_proofs.extend(p);
}
//println!("{:?}", proofs);
for (mint, p) in proofs {
println!("{:?}", serde_json::to_string(&p));
println!("{:?}", mint);
self.add_mint(mint.clone()).await?;
self.localstore.add_proofs(mint, p).await?;
}
@@ -780,7 +775,6 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
let mut change_proofs = vec![];
for proof in post_swap_proofs {
println!("post swap proof: {:?}", proof);
let conditions: Result<cashu::nuts::nut10::Secret, _> = (&proof.secret).try_into();
if conditions.is_ok() {
send_proofs.push(proof);
@@ -849,14 +843,12 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
{
let conditions: Result<P2PKConditions, _> = secret.try_into();
if let Ok(conditions) = conditions {
println!("{:?}", conditions);
let pubkeys = conditions.pubkeys;
for pubkey in pubkeys {
if let Some(signing) = pubkey_secret_key.get(&pubkey.to_string()) {
proof.sign_p2pk_proof(signing.clone()).unwrap();
proof.verify_p2pk().unwrap();
println!("v");
}
}

View File

@@ -17,8 +17,8 @@ pub mod nut11;
#[cfg(feature = "wallet")]
pub use nut00::wallet::{PreMint, PreMintSecrets, Token};
#[cfg(not(feature = "nut11"))]
pub use nut00::Proof;
pub use nut00::{BlindedMessage, BlindedSignature, CurrencyUnit, PaymentMethod};
pub use nut00::{BlindedMessage, Proof};
pub use nut00::{BlindedSignature, CurrencyUnit, PaymentMethod};
pub use nut01::{Keys, KeysResponse, PublicKey, SecretKey};
pub use nut02::mint::KeySet as MintKeySet;
pub use nut02::{Id, KeySet, KeySetInfo, KeysetResponse};
@@ -40,6 +40,8 @@ pub use nut08::{MeltBolt11Request, MeltBolt11Response};
#[cfg(feature = "nut10")]
pub use nut10::{Kind, Secret as Nut10Secret, SecretData};
#[cfg(feature = "nut11")]
pub use nut11::{P2PKConditions, Proof, SigFlag, Signatures, SigningKey, VerifyingKey};
pub use nut11::{
BlindedMessage, P2PKConditions, Proof, SigFlag, Signatures, SigningKey, VerifyingKey,
};
pub type Proofs = Vec<Proof>;

View File

@@ -8,11 +8,7 @@ use std::str::FromStr;
use serde::{Deserialize, Serialize};
use super::{Id, Proofs, PublicKey};
#[cfg(feature = "nut11")]
use super::{Signatures, SigningKey};
use crate::error::Error;
#[cfg(feature = "nut11")]
use crate::nuts::nut11::{witness_deserialize, witness_serialize};
use crate::secret::Secret;
use crate::url::UncheckedUrl;
use crate::Amount;
@@ -28,13 +24,6 @@ pub struct BlindedMessage {
/// encrypted secret message (B_)
#[serde(rename = "B_")]
pub b: PublicKey,
/// Witness
#[cfg(feature = "nut11")]
#[serde(default)]
#[serde(skip_serializing_if = "Signatures::is_empty")]
#[serde(serialize_with = "witness_serialize")]
#[serde(deserialize_with = "witness_deserialize")]
pub witness: Signatures,
}
impl BlindedMessage {
@@ -43,24 +32,8 @@ impl BlindedMessage {
amount,
keyset_id,
b,
#[cfg(feature = "nut11")]
witness: Signatures::default(),
}
}
#[cfg(feature = "nut11")]
pub fn sign_p2pk_blinded_message(&mut self, secret_key: SigningKey) -> Result<(), Error> {
let msg_to_sign = hex::decode(self.b.to_string())?;
println!("{:?}", msg_to_sign);
let signature = secret_key.sign(&msg_to_sign);
self.witness
.signatures
.push(hex::encode(signature.to_bytes()));
Ok(())
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, hash::Hash)]

View File

@@ -99,6 +99,48 @@ impl PartialOrd for Proof {
}
}
/// Blinded Message [NUT-00]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlindedMessage {
/// Amount
pub amount: Amount,
/// Keyset Id
#[serde(rename = "id")]
pub keyset_id: Id,
/// encrypted secret message (B_)
#[serde(rename = "B_")]
pub b: PublicKey,
/// Witness
#[serde(default)]
#[serde(skip_serializing_if = "Signatures::is_empty")]
#[serde(serialize_with = "witness_serialize")]
#[serde(deserialize_with = "witness_deserialize")]
pub witness: Signatures,
}
impl BlindedMessage {
pub fn new(amount: Amount, keyset_id: Id, b: PublicKey) -> Self {
Self {
amount,
keyset_id,
b,
witness: Signatures::default(),
}
}
#[cfg(feature = "nut11")]
pub fn sign_p2pk_blinded_message(&mut self, secret_key: SigningKey) -> Result<(), Error> {
let msg_to_sign = hex::decode(self.b.to_string())?;
let signature = secret_key.sign(&msg_to_sign);
self.witness
.signatures
.push(hex::encode(signature.to_bytes()));
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct P2PKConditions {
#[serde(skip_serializing_if = "Option::is_none")]
@@ -277,12 +319,6 @@ impl Proof {
let mut valid_sigs = 0;
println!("{:?}", self.secret.to_string());
println!(
"sec bytes: {:?}",
self.secret.to_string().into_bytes().len()
);
let msg = &self.secret.to_bytes().unwrap();
for signature in &self.witness.signatures {
@@ -301,7 +337,6 @@ impl Proof {
}
if valid_sigs.ge(&spending_conditions.num_sigs.unwrap_or(1)) {
println!("valid sigs: {}", valid_sigs);
return Ok(());
}

View File

@@ -69,7 +69,7 @@ impl Secret {
serde_json::from_str(&self.0);
match secret {
Ok(_) => Ok(self.0.clone().replace('\\', "").into_bytes()),
Ok(_) => Ok(self.0.clone().into_bytes()),
Err(_) => Ok(hex::decode(&self.0)?),
}
}