mirror of
https://github.com/aljazceru/cdk.git
synced 2026-02-18 19:45:45 +01:00
refactor: rename split to swap
This commit is contained in:
@@ -5,7 +5,7 @@ use async_trait::async_trait;
|
||||
use cashu::nuts::MintInfo;
|
||||
use cashu::nuts::{
|
||||
BlindedMessage, Keys, MeltBolt11Request, MeltBolt11Response, MintBolt11Request,
|
||||
MintBolt11Response, PreMintSecrets, Proof, SplitRequest, SplitResponse, *,
|
||||
MintBolt11Response, PreMintSecrets, Proof, SwapRequest, SwapResponse, *,
|
||||
};
|
||||
#[cfg(feature = "nut07")]
|
||||
use cashu::nuts::{CheckSpendableRequest, CheckSpendableResponse};
|
||||
@@ -129,8 +129,8 @@ impl Client for HttpClient {
|
||||
async fn post_split(
|
||||
&self,
|
||||
mint_url: Url,
|
||||
split_request: SplitRequest,
|
||||
) -> Result<SplitResponse, Error> {
|
||||
split_request: SwapRequest,
|
||||
) -> Result<SwapResponse, Error> {
|
||||
let url = join_url(mint_url, "split")?;
|
||||
|
||||
let res = Request::post(url.as_str())
|
||||
@@ -143,8 +143,7 @@ impl Client for HttpClient {
|
||||
.await
|
||||
.map_err(|err| Error::Gloo(err.to_string()))?;
|
||||
|
||||
let response: Result<SplitResponse, serde_json::Error> =
|
||||
serde_json::from_value(res.clone());
|
||||
let response: Result<SwapResponse, serde_json::Error> = serde_json::from_value(res.clone());
|
||||
|
||||
match response {
|
||||
Ok(res) => Ok(res),
|
||||
|
||||
@@ -7,7 +7,7 @@ use async_trait::async_trait;
|
||||
use cashu::nuts::MintInfo;
|
||||
use cashu::nuts::{
|
||||
BlindedMessage, Keys, MeltBolt11Request, MeltBolt11Response, MintBolt11Request,
|
||||
MintBolt11Response, PreMintSecrets, Proof, SplitRequest, SplitResponse, *,
|
||||
MintBolt11Response, PreMintSecrets, Proof, SwapRequest, SwapResponse, *,
|
||||
};
|
||||
#[cfg(feature = "nut07")]
|
||||
use cashu::nuts::{CheckSpendableRequest, CheckSpendableResponse};
|
||||
@@ -108,21 +108,19 @@ impl Client for HttpClient {
|
||||
async fn post_split(
|
||||
&self,
|
||||
mint_url: Url,
|
||||
split_request: SplitRequest,
|
||||
) -> Result<SplitResponse, Error> {
|
||||
let url = join_url(mint_url, "split")?;
|
||||
split_request: SwapRequest,
|
||||
) -> Result<SwapResponse, Error> {
|
||||
// TODO: Add to endpoint
|
||||
let url = join_url(mint_url, "swap")?;
|
||||
|
||||
let res = minreq::post(url).with_json(&split_request)?.send()?;
|
||||
|
||||
println!("{:?}", res);
|
||||
|
||||
let response: Result<SplitResponse, serde_json::Error> =
|
||||
let response: Result<SwapResponse, serde_json::Error> =
|
||||
serde_json::from_value(res.json::<Value>()?.clone());
|
||||
|
||||
match response {
|
||||
Ok(res) if res.promises.is_some() => Ok(res),
|
||||
_ => Err(Error::from_json(&res.json::<Value>()?.to_string())?),
|
||||
}
|
||||
Ok(response?)
|
||||
}
|
||||
|
||||
/// Spendable check [NUT-07]
|
||||
|
||||
@@ -9,7 +9,7 @@ use cashu::nuts::CheckSpendableResponse;
|
||||
use cashu::nuts::MintInfo;
|
||||
use cashu::nuts::{
|
||||
BlindedMessage, Keys, KeysetResponse, MeltBolt11Response, MintBolt11Response, PreMintSecrets,
|
||||
Proof, SplitRequest, SplitResponse,
|
||||
Proof, SwapRequest, SwapResponse,
|
||||
};
|
||||
use cashu::utils;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -111,8 +111,8 @@ pub trait Client {
|
||||
async fn post_split(
|
||||
&self,
|
||||
mint_url: Url,
|
||||
split_request: SplitRequest,
|
||||
) -> Result<SplitResponse, Error>;
|
||||
split_request: SwapRequest,
|
||||
) -> Result<SwapResponse, Error>;
|
||||
|
||||
#[cfg(feature = "nut07")]
|
||||
async fn post_check_spendable(
|
||||
|
||||
@@ -3,8 +3,8 @@ use std::collections::{HashMap, HashSet};
|
||||
use cashu::dhke::{sign_message, verify_message};
|
||||
pub use cashu::error::mint::Error;
|
||||
use cashu::nuts::{
|
||||
BlindedMessage, BlindedSignature, MeltBolt11Request, MeltBolt11Response, Proof, SplitRequest,
|
||||
SplitResponse, *,
|
||||
BlindedMessage, BlindedSignature, MeltBolt11Request, MeltBolt11Response, Proof, SwapRequest,
|
||||
SwapResponse, *,
|
||||
};
|
||||
#[cfg(feature = "nut07")]
|
||||
use cashu::nuts::{CheckSpendableRequest, CheckSpendableResponse};
|
||||
@@ -153,8 +153,8 @@ impl Mint {
|
||||
|
||||
pub fn process_split_request(
|
||||
&mut self,
|
||||
split_request: SplitRequest,
|
||||
) -> Result<SplitResponse, Error> {
|
||||
split_request: SwapRequest,
|
||||
) -> Result<SwapResponse, Error> {
|
||||
let proofs_total = split_request.input_amount();
|
||||
|
||||
let output_total = split_request.output_amount();
|
||||
@@ -190,7 +190,7 @@ impl Mint {
|
||||
.map(|b| self.blind_sign(b).unwrap())
|
||||
.collect();
|
||||
|
||||
Ok(SplitResponse::new(promises))
|
||||
Ok(SwapResponse::new(promises))
|
||||
}
|
||||
|
||||
fn verify_proof(&self, proof: &Proof) -> Result<(), Error> {
|
||||
|
||||
@@ -5,7 +5,7 @@ use cashu::dhke::{construct_proofs, unblind_message};
|
||||
#[cfg(feature = "nut07")]
|
||||
use cashu::nuts::nut00::mint;
|
||||
use cashu::nuts::{
|
||||
BlindedSignature, CurrencyUnit, Keys, PreMintSecrets, PreSplit, Proof, Proofs, SplitRequest,
|
||||
BlindedSignature, CurrencyUnit, Keys, PreMintSecrets, PreSwap, Proof, Proofs, SwapRequest,
|
||||
Token,
|
||||
};
|
||||
#[cfg(feature = "nut07")]
|
||||
@@ -138,32 +138,27 @@ impl<C: Client> Wallet<C> {
|
||||
// Sum amount of all proofs
|
||||
let _amount: Amount = token.proofs.iter().map(|p| p.amount).sum();
|
||||
|
||||
let pre_split = self.create_split(None, token.proofs)?;
|
||||
let pre_swap = self.create_split(None, token.proofs)?;
|
||||
|
||||
let split_response = self
|
||||
let swap_response = self
|
||||
.client
|
||||
.post_split(self.mint_url.clone().try_into()?, pre_split.split_request)
|
||||
.post_split(self.mint_url.clone().try_into()?, pre_swap.split_request)
|
||||
.await?;
|
||||
|
||||
if let Some(promises) = &split_response.promises {
|
||||
// Proof to keep
|
||||
let p = construct_proofs(
|
||||
promises.to_owned(),
|
||||
pre_split.pre_mint_secrets.rs(),
|
||||
pre_split.pre_mint_secrets.secrets(),
|
||||
&keys,
|
||||
)?;
|
||||
proofs.push(p);
|
||||
} else {
|
||||
warn!("Response missing promises");
|
||||
return Err(Error::Custom("Split response missing promises".to_string()));
|
||||
}
|
||||
// Proof to keep
|
||||
let p = construct_proofs(
|
||||
swap_response.signatures,
|
||||
pre_swap.pre_mint_secrets.rs(),
|
||||
pre_swap.pre_mint_secrets.secrets(),
|
||||
&keys,
|
||||
)?;
|
||||
proofs.push(p);
|
||||
}
|
||||
Ok(proofs.iter().flatten().cloned().collect())
|
||||
}
|
||||
|
||||
/// Create Split Payload
|
||||
fn create_split(&self, amount: Option<Amount>, proofs: Proofs) -> Result<PreSplit, Error> {
|
||||
fn create_split(&self, amount: Option<Amount>, proofs: Proofs) -> Result<PreSwap, Error> {
|
||||
// Since split is used to get the needed combination of tokens for a specific
|
||||
// amount first blinded messages are created for the amount
|
||||
|
||||
@@ -184,9 +179,9 @@ impl<C: Client> Wallet<C> {
|
||||
PreMintSecrets::random((&self.mint_keys).into(), value)?
|
||||
};
|
||||
|
||||
let split_request = SplitRequest::new(proofs, pre_mint_secrets.blinded_messages());
|
||||
let split_request = SwapRequest::new(proofs, pre_mint_secrets.blinded_messages());
|
||||
|
||||
Ok(PreSplit {
|
||||
Ok(PreSwap {
|
||||
pre_mint_secrets,
|
||||
split_request,
|
||||
})
|
||||
@@ -231,35 +226,31 @@ impl<C: Client> Wallet<C> {
|
||||
return Err(Error::InsufficientFunds);
|
||||
}
|
||||
|
||||
let pre_split = self.create_split(Some(amount), proofs)?;
|
||||
let pre_swap = self.create_split(Some(amount), proofs)?;
|
||||
|
||||
let split_response = self
|
||||
let swap_response = self
|
||||
.client
|
||||
.post_split(self.mint_url.clone().try_into()?, pre_split.split_request)
|
||||
.post_split(self.mint_url.clone().try_into()?, pre_swap.split_request)
|
||||
.await?;
|
||||
|
||||
let mut keep_proofs = Proofs::new();
|
||||
let mut send_proofs = Proofs::new();
|
||||
|
||||
if let Some(promises) = split_response.promises {
|
||||
let mut proofs = construct_proofs(
|
||||
promises,
|
||||
pre_split.pre_mint_secrets.rs(),
|
||||
pre_split.pre_mint_secrets.secrets(),
|
||||
&self.mint_keys,
|
||||
)?;
|
||||
let mut proofs = construct_proofs(
|
||||
swap_response.signatures,
|
||||
pre_swap.pre_mint_secrets.rs(),
|
||||
pre_swap.pre_mint_secrets.secrets(),
|
||||
&self.mint_keys,
|
||||
)?;
|
||||
|
||||
proofs.reverse();
|
||||
proofs.reverse();
|
||||
|
||||
for proof in proofs {
|
||||
if (proof.amount + send_proofs.iter().map(|p| p.amount).sum()).gt(&amount) {
|
||||
keep_proofs.push(proof);
|
||||
} else {
|
||||
send_proofs.push(proof);
|
||||
}
|
||||
for proof in proofs {
|
||||
if (proof.amount + send_proofs.iter().map(|p| p.amount).sum()).gt(&amount) {
|
||||
keep_proofs.push(proof);
|
||||
} else {
|
||||
send_proofs.push(proof);
|
||||
}
|
||||
} else {
|
||||
return Err(Error::Custom("Invalid split response".to_string()));
|
||||
}
|
||||
|
||||
// println!("Send Proofs: {:#?}", send_proofs);
|
||||
|
||||
@@ -18,8 +18,8 @@ pub use nut01::{Keys, KeysResponse, PublicKey, SecretKey};
|
||||
pub use nut02::mint::KeySet as MintKeySet;
|
||||
pub use nut02::{Id, KeySet, KeySetInfo, KeysetResponse};
|
||||
#[cfg(feature = "wallet")]
|
||||
pub use nut03::PreSplit;
|
||||
pub use nut03::{SplitRequest, SplitResponse};
|
||||
pub use nut03::PreSwap;
|
||||
pub use nut03::{SwapRequest, SwapResponse};
|
||||
pub use nut04::{
|
||||
MintBolt11Request, MintBolt11Response, MintQuoteBolt11Request, MintQuoteBolt11Response,
|
||||
};
|
||||
|
||||
@@ -12,21 +12,21 @@ pub use crate::Bolt11Invoice;
|
||||
|
||||
#[cfg(feature = "wallet")]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
pub struct PreSplit {
|
||||
pub struct PreSwap {
|
||||
pub pre_mint_secrets: PreMintSecrets,
|
||||
pub split_request: SplitRequest,
|
||||
pub split_request: SwapRequest,
|
||||
}
|
||||
|
||||
/// Split Request [NUT-06]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct SplitRequest {
|
||||
pub struct SwapRequest {
|
||||
/// Proofs that are to be spent in `Split`
|
||||
pub inputs: Proofs,
|
||||
/// Blinded Messages for Mint to sign
|
||||
pub outputs: Vec<BlindedMessage>,
|
||||
}
|
||||
|
||||
impl SplitRequest {
|
||||
impl SwapRequest {
|
||||
pub fn new(inputs: Proofs, outputs: Vec<BlindedMessage>) -> Self {
|
||||
Self { inputs, outputs }
|
||||
}
|
||||
@@ -44,24 +44,22 @@ impl SplitRequest {
|
||||
|
||||
/// Split Response [NUT-06]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct SplitResponse {
|
||||
pub struct SwapResponse {
|
||||
/// Promises
|
||||
pub promises: Option<Vec<BlindedSignature>>,
|
||||
pub signatures: Vec<BlindedSignature>,
|
||||
}
|
||||
|
||||
impl SplitResponse {
|
||||
pub fn new(promises: Vec<BlindedSignature>) -> SplitResponse {
|
||||
SplitResponse {
|
||||
promises: Some(promises),
|
||||
impl SwapResponse {
|
||||
pub fn new(promises: Vec<BlindedSignature>) -> SwapResponse {
|
||||
SwapResponse {
|
||||
signatures: promises,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn promises_amount(&self) -> Option<Amount> {
|
||||
self.promises.as_ref().map(|promises| {
|
||||
promises
|
||||
.iter()
|
||||
.map(|BlindedSignature { amount, .. }| *amount)
|
||||
.sum()
|
||||
})
|
||||
pub fn promises_amount(&self) -> Amount {
|
||||
self.signatures
|
||||
.iter()
|
||||
.map(|BlindedSignature { amount, .. }| *amount)
|
||||
.sum()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user