Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cashuNUT17NUT18): btc onchain structs #66

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/cdk/src/nuts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub mod nut11;
pub mod nut12;
#[cfg(feature = "nut13")]
pub mod nut13;
pub mod nut17;
pub mod nut18;

#[cfg(feature = "wallet")]
pub use nut00::wallet::{PreMint, PreMintSecrets, Token};
Expand All @@ -24,7 +26,8 @@ pub use nut02::{Id, KeySet, KeySetInfo, KeysetResponse};
pub use nut03::PreSwap;
pub use nut03::{SwapRequest, SwapResponse};
pub use nut04::{
MintBolt11Request, MintBolt11Response, MintQuoteBolt11Request, MintQuoteBolt11Response,
MintBolt11Request, MintBolt11Response, MintMethodSettings, MintQuoteBolt11Request,
MintQuoteBolt11Response,
};
pub use nut05::{
MeltBolt11Request, MeltBolt11Response, MeltQuoteBolt11Request, MeltQuoteBolt11Response,
Expand Down
3 changes: 3 additions & 0 deletions crates/cdk/src/nuts/nut00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl fmt::Display for CurrencyUnit {
pub enum PaymentMethod {
#[default]
Bolt11,
BtcOnChain,
Custom(String),
}

Expand All @@ -121,6 +122,7 @@ impl FromStr for PaymentMethod {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"bolt11" => Ok(Self::Bolt11),
"btconchain" => Ok(Self::BtcOnChain),
_ => Ok(Self::Custom(s.to_string())),
}
}
Expand All @@ -130,6 +132,7 @@ impl fmt::Display for PaymentMethod {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PaymentMethod::Bolt11 => write!(f, "bolt11"),
PaymentMethod::BtcOnChain => write!(f, "btconchain"),
PaymentMethod::Custom(unit) => write!(f, "{}", unit),
}
}
Expand Down
74 changes: 74 additions & 0 deletions crates/cdk/src/nuts/nut17.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! NUT-17: Minting tokens Onchain
//!
//! <https://github.com/cashubtc/nuts/blob/main/17.md>

use serde::{Deserialize, Serialize};

use super::{BlindSignature, BlindedMessage, CurrencyUnit, MintMethodSettings};
use crate::types::MintQuote;
use crate::Amount;

/// Mint quote request [NUT-17]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MintQuoteBtcOnchainRequest {
/// Amount
pub amount: Amount,
/// Unit wallet would like to pay with
pub unit: CurrencyUnit,
}

/// Mint quote response [NUT-17]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MintQuoteBtcOnchainResponse {
/// Quote Id
pub quote: String,
/// Payment request to fulfill
pub address: String,
/// Whether the the request has been paid
pub paid: bool,
/// Unix timestamp until the quote is valid
pub expiry: u64,
}

impl From<MintQuote> for MintQuoteBtcOnchainResponse {
fn from(mint_quote: MintQuote) -> MintQuoteBtcOnchainResponse {
MintQuoteBtcOnchainResponse {
quote: mint_quote.id,
address: mint_quote.request,
paid: mint_quote.paid,
expiry: mint_quote.expiry,
}
}
}

/// Mint request [NUT-17]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MintBtcOnchainRequest {
/// Quote id
pub quote: String,
/// Outputs
pub outputs: Vec<BlindedMessage>,
}

impl MintBtcOnchainRequest {
pub fn total_amount(&self) -> Amount {
self.outputs
.iter()
.map(|BlindedMessage { amount, .. }| *amount)
.sum()
}
}

/// Mint response [NUT-17]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MintBtcOnchainResponse {
/// Blind Signatures
pub signatures: Vec<BlindSignature>,
}

/// Mint Settings
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Settings {
methods: Vec<MintMethodSettings>,
disabled: bool,
}
68 changes: 68 additions & 0 deletions crates/cdk/src/nuts/nut18.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//! NUT-18: Melting Tokens Onchain
//!
//! <https://github.com/cashubtc/nuts/blob/main/18.md>

use serde::{Deserialize, Serialize};

use super::nut05::MeltMethodSettings;
use super::CurrencyUnit;
use crate::nuts::Proofs;
use crate::{Amount, Bolt11Invoice};

/// Melt quote request [NUT-18]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MeltQuoteBtcOnchainRequest {
/// Amount to be paid
pub amount: Amount,
/// Bitcoin onchain address to be paid
pub address: Bolt11Invoice,
/// Unit wallet would like to pay with
pub unit: CurrencyUnit,
}

/// Melt quote response [NUT-18]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MeltQuoteBtcOnchainResponse {
/// Quote Id
pub quote: String,
/// Description
pub description: String,
/// The amount that needs to be provided
pub amount: u64,
/// The fee that is required
pub fee: u64,
/// Whether the the request has been paid
pub paid: bool,
/// Unix timestamp until the quote is valid
pub expiry: u64,
}

/// Melt BTC on chain Request [NUT-18]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MeltBtcOnchianRequest {
/// Quote ID
pub quote: String,
/// Proofs
pub inputs: Proofs,
}

impl MeltBtcOnchianRequest {
pub fn proofs_amount(&self) -> Amount {
self.inputs.iter().map(|proof| proof.amount).sum()
}
}

/// Melt Response [NUT-18]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MeltBtcOnChainResponse {
/// Indicate if payment was successful
pub paid: bool,
// TXID
pub txid: Option<String>,
}

/// Melt Settings
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Settings {
methods: Vec<MeltMethodSettings>,
}
Loading