Skip to content

Commit

Permalink
refactor: v1 MeltRequest
Browse files Browse the repository at this point in the history
TODO: ffi bindings
  • Loading branch information
thesimplekid committed Dec 11, 2023
1 parent 35db3fb commit c706e36
Show file tree
Hide file tree
Showing 22 changed files with 448 additions and 488 deletions.
62 changes: 35 additions & 27 deletions bindings/cashu-ffi/src/cashu.udl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ interface CashuError {
};

// Types

[Enum]
interface CurrencyUnit {
Sat();
Custom(string unit);
};

interface Bolt11Invoice {
[Throws=CashuError]
Expand Down Expand Up @@ -88,11 +94,11 @@ interface MintProofs {

interface Token {
[Throws=CashuError]
constructor(string mint, sequence<Proof> token, string? unit, string? memo);
constructor(string mint, sequence<Proof> token, string? memo, string? unit);
sequence<MintProofs> token();
string? memo();
[Throws=CashuError]
string as_string();
string? unit();
string to_string();
[Throws=CashuError, Name=from_string]
constructor(string token);

Expand Down Expand Up @@ -144,6 +150,32 @@ interface KeySetResponse {
sequence<KeySetInfo> keysets();
};

// NUT-05

interface MeltQuoteBolt11Response {
[Throws=CashuError]
constructor(string quote, u64 amount, u64 fee_reserve, boolean paid, u64 expiry);
string quote();
u64 amount();
u64 fee_reserve();
boolean paid();
u64 expiry();
};

interface MeltQuoteBolt11Request {
[Throws=CashuError]
constructor(string request, string unit);
string request();
string unit();
};

interface MeltBolt11Request {
[Throws=CashuError]
constructor(sequence<Proof> inputs, string quote);
sequence<Proof> inputs();
string quote();
};

interface RequestMintResponse {
[Throws=CashuError]
constructor(string invoice, string hash);
Expand All @@ -162,30 +194,6 @@ interface PostMintResponse {
sequence<BlindedSignature> promises();
};

interface CheckFeesRequest {
[Throws=CashuError]
constructor(string invoice);
string invoice();
};

interface CheckFeesResponse {
constructor(Amount amount);
Amount amount();
};

interface Nut05MeltRequest {
[Throws=CashuError]
constructor(sequence<Proof> proofs, string Invoice);
sequence<Proof> proofs();
string invoice();
};

interface Nut05MeltResponse {
constructor(boolean paid, string? preimage);
boolean paid();
string? preimage();
};

interface SplitRequest {
constructor(sequence<Proof> proofs, sequence<BlindedMessage> outputs);
sequence<Proof> proofs();
Expand Down
8 changes: 4 additions & 4 deletions bindings/cashu-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod ffi {
pub use crate::nuts::nut00::premint_secrets::PreMintSecrets;
pub use crate::nuts::nut00::proof::mint::Proof as MintProof;
pub use crate::nuts::nut00::proof::Proof;
pub use crate::nuts::nut00::token::Token;
pub use crate::nuts::nut00::token::{CurrencyUnit, Token};
pub use crate::nuts::nut01::key_pair::KeyPair;
pub use crate::nuts::nut01::keys::{Keys, KeysResponse};
pub use crate::nuts::nut01::public_key::PublicKey;
Expand All @@ -21,11 +21,11 @@ mod ffi {
pub use crate::nuts::nut03::{RequestMintResponse, SplitRequest, SplitResponse};
pub use crate::nuts::nut04::{MintRequest, PostMintResponse};
pub use crate::nuts::nut05::{
CheckFeesRequest, CheckFeesResponse, MeltRequest as Nut05MeltRequest,
MeltResponse as Nut05MeltResponse,
MeltBolt11Request as Nut05MeltBolt11Request, MeltBolt11Response as Nut05MeltBolt11Response,
MeltQuoteBolt11Request,
};
pub use crate::nuts::nut07::{CheckSpendableRequest, CheckSpendableResponse};
pub use crate::nuts::nut08::{MeltRequest, MeltResponse};
pub use crate::nuts::nut08::{MeltBolt11Request, MeltBolt11Response};
pub use crate::nuts::nut09::{MintInfo, MintVersion};
pub use crate::types::amount::Amount;
pub use crate::types::{Bolt11Invoice, KeySetInfo, Secret};
Expand Down
7 changes: 7 additions & 0 deletions bindings/cashu-ffi/src/nuts/nut00/blinded_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ pub struct BlindedSignature {
inner: BlindedSignatureSdk,
}

impl Deref for BlindedSignature {
type Target = BlindedSignatureSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl BlindedSignature {
pub fn new(id: Arc<Id>, amount: Arc<Amount>, c: Arc<PublicKey>) -> Self {
Self {
Expand Down
45 changes: 41 additions & 4 deletions bindings/cashu-ffi/src/nuts/nut00/token.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;

use cashu::nuts::nut00::wallet::Token as TokenSdk;
use cashu::nuts::CurrencyUnit as CurrencyUnitSdk;
use cashu::url::UncheckedUrl;

use crate::error::Result;
use crate::{MintProofs, Proof};

pub enum CurrencyUnit {
Sat,
Custom { unit: String },
}

impl From<&CurrencyUnit> for CurrencyUnitSdk {
fn from(unit: &CurrencyUnit) -> CurrencyUnitSdk {
match unit {
CurrencyUnit::Sat => CurrencyUnitSdk::Sat,
CurrencyUnit::Custom { unit } => CurrencyUnitSdk::Custom(unit.clone()),
}
}
}

impl From<CurrencyUnitSdk> for CurrencyUnit {
fn from(unit: CurrencyUnitSdk) -> CurrencyUnit {
match unit {
CurrencyUnitSdk::Sat => CurrencyUnit::Sat,
CurrencyUnitSdk::Custom(unit) => CurrencyUnit::Custom { unit: unit.clone() },
}
}
}

pub struct Token {
inner: TokenSdk,
}
Expand All @@ -15,13 +40,16 @@ impl Token {
pub fn new(
mint: String,
proofs: Vec<Arc<Proof>>,
unit: Option<String>,
memo: Option<String>,
unit: Option<String>,
) -> Result<Self> {
let mint = UncheckedUrl::from_str(&mint)?;
let proofs = proofs.into_iter().map(|p| p.as_ref().into()).collect();

let unit = unit.map(|u| CurrencyUnitSdk::from_str(&u).unwrap_or_default().into());

Ok(Self {
inner: TokenSdk::new(mint, proofs, unit, memo)?,
inner: TokenSdk::new(mint, proofs, memo, unit)?,
})
}

Expand All @@ -38,14 +66,23 @@ impl Token {
self.inner.memo.clone()
}

pub fn unit(&self) -> Option<String> {
self.inner
.unit
.clone()
.map(|u| Into::<CurrencyUnitSdk>::into(u).to_string())
}

pub fn from_string(token: String) -> Result<Self> {
Ok(Self {
inner: TokenSdk::from_str(&token)?,
})
}
}

pub fn as_string(&self) -> Result<String> {
Ok(self.inner.to_string())
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.inner.to_string())
}
}

Expand Down
Loading

0 comments on commit c706e36

Please sign in to comment.