refactor: cashu-ffi some interfaces to v1

This commit is contained in:
thesimplekid
2023-12-21 14:05:40 +00:00
parent dc988d1173
commit 85df35458a
18 changed files with 285 additions and 193 deletions

View File

@@ -10,6 +10,7 @@ interface CashuError {
[Enum]
interface CurrencyUnit {
Sat();
Usd();
Custom(string unit);
};
@@ -21,12 +22,7 @@ interface Bolt11Invoice {
};
interface Amount {
u64 to_sat();
u64 to_msat();
[Name = from_sat]
constructor(u64 sat);
[Name = from_msat]
constructor(u64 msat);
constructor(u64 amount);
sequence<Amount> split();
};
@@ -58,6 +54,7 @@ interface SecretKey {
interface BlindedMessage {
constructor(Id keyset_id, Amount amount, PublicKey b);
Amount amount();
Id keyset_id();
PublicKey b();
};
@@ -66,12 +63,12 @@ interface Proof {
Amount amount();
Secret secret();
PublicKey c();
Id id();
Id keyset_id();
};
interface BlindedSignature {
constructor(Id id, Amount amount, PublicKey c);
Id id();
constructor(Id keyset_id, Amount amount, PublicKey c);
Id keyset_id();
Amount amount();
PublicKey c();
};
@@ -81,7 +78,7 @@ interface MintProof {
Amount? amount();
Secret secret();
PublicKey? c();
Id? id();
Id? keyset_id();
};
@@ -150,6 +147,36 @@ interface KeySetResponse {
sequence<KeySetInfo> keysets();
};
// --- NUT-04
interface MintQuoteBolt11Request {
constructor(Amount amount, string unit);
Amount amount();
};
interface MintQuoteBolt11Response {
constructor(string quote, string request, boolean paid, u64 expiry);
string quote();
string request();
boolean paid();
u64 expiry();
};
interface MintBolt11Request {
constructor(string quote, sequence<BlindedMessage> outputs);
string quote();
sequence<BlindedMessage> outputs();
};
interface MintBolt11Response {
constructor(sequence<BlindedSignature> signatures);
sequence<BlindedSignature> signatures();
};
// ---
// NUT-05
interface MeltQuoteBolt11Response {
@@ -166,35 +193,23 @@ interface MeltQuoteBolt11Request {
[Throws=CashuError]
constructor(string request, string unit);
string request();
string unit();
};
interface MeltBolt11Request {
[Throws=CashuError]
constructor(sequence<Proof> inputs, string quote);
constructor(string quote, sequence<Proof> inputs, sequence<BlindedMessage>? outputs);
sequence<Proof> inputs();
string quote();
};
interface RequestMintResponse {
[Throws=CashuError]
constructor(string invoice, string hash);
string invoice();
string hash();
interface MeltBolt11Response {
constructor(boolean paid, string? payment_preimage, sequence<BlindedSignature>? change);
string? payment_preimage();
boolean paid();
};
interface MintRequest {
constructor(sequence<BlindedMessage> outputs);
sequence<BlindedMessage> outputs();
Amount total_amount();
};
interface PostMintResponse {
constructor(sequence<BlindedSignature> promises);
sequence<BlindedSignature> promises();
};
interface SplitRequest {
// ----
interface SwapRequest {
constructor(sequence<Proof> proofs, sequence<BlindedMessage> outputs);
sequence<Proof> proofs();
sequence<BlindedMessage> outputs();
@@ -202,11 +217,9 @@ interface SplitRequest {
Amount output_amount();
};
interface SplitResponse {
interface SwapResponse {
constructor(sequence<BlindedSignature> promises);
sequence<BlindedSignature> promises();
Amount? promises_amount();
sequence<BlindedSignature> signatures();
};
interface CheckSpendableRequest {
@@ -220,21 +233,6 @@ interface CheckSpendableResponse {
sequence<boolean> pending();
};
interface MeltRequest {
[Throws=CashuError]
constructor(sequence<Proof> proofs, string Invoice, sequence<BlindedMessage>? outputs);
sequence<Proof> proofs();
string invoice();
sequence<BlindedMessage>? outputs();
};
interface MeltResponse {
constructor(boolean paid, string? preimage, sequence<BlindedSignature>? change);
boolean paid();
string? preimage();
sequence<BlindedSignature>? change();
};
interface MintVersion {
constructor(string name, string version);
string name();
@@ -242,14 +240,13 @@ interface MintVersion {
};
interface MintInfo {
constructor(string? name, PublicKey? pubkey, MintVersion? version, string? description, string? description_long, sequence<sequence<string>>? contact, sequence<string> nuts, string? motd);
constructor(string? name, PublicKey? pubkey, MintVersion? version, string? description, string? description_long, sequence<sequence<string>>? contact, string nuts, string? motd);
string? name();
PublicKey? pubkey();
MintVersion? version();
string? description();
string? description_long();
sequence<sequence<string>>? contact();
sequence<string> nuts();
string? motd();
};

View File

@@ -18,15 +18,17 @@ mod ffi {
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};
pub use crate::nuts::nut03::{RequestMintResponse, SplitRequest, SplitResponse};
pub use crate::nuts::nut04::{MintRequest, PostMintResponse};
pub use crate::nuts::nut03::{SwapRequest, SwapResponse};
pub use crate::nuts::nut04::{
MintBolt11Request, MintBolt11Response, MintQuoteBolt11Request, MintQuoteBolt11Response,
};
pub use crate::nuts::nut05::{
MeltBolt11Request as Nut05MeltBolt11Request, MeltBolt11Response as Nut05MeltBolt11Response,
MeltQuoteBolt11Request,
MeltQuoteBolt11Request, MeltQuoteBolt11Response,
};
pub use crate::nuts::nut06::{MintInfo, MintVersion};
pub use crate::nuts::nut07::{CheckSpendableRequest, CheckSpendableResponse};
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};

View File

@@ -4,6 +4,6 @@ pub mod nut02;
pub mod nut03;
pub mod nut04;
pub mod nut05;
pub mod nut06;
pub mod nut07;
pub mod nut08;
pub mod nut09;

View File

@@ -32,6 +32,10 @@ impl BlindedMessage {
Arc::new(self.inner.amount.into())
}
pub fn keyset_id(&self) -> Arc<Id> {
Arc::new(self.inner.keyset_id.into())
}
pub fn b(&self) -> Arc<PublicKey> {
Arc::new(self.inner.b.clone().into())
}

View File

@@ -17,18 +17,18 @@ impl Deref for BlindedSignature {
}
impl BlindedSignature {
pub fn new(id: Arc<Id>, amount: Arc<Amount>, c: Arc<PublicKey>) -> Self {
pub fn new(keyset_id: Arc<Id>, amount: Arc<Amount>, c: Arc<PublicKey>) -> Self {
Self {
inner: BlindedSignatureSdk {
id: *id.as_ref().deref(),
keyset_id: *keyset_id.as_ref().deref(),
amount: *amount.as_ref().deref(),
c: c.as_ref().into(),
},
}
}
pub fn id(&self) -> Arc<Id> {
Arc::new(self.inner.id.into())
pub fn keyset_id(&self) -> Arc<Id> {
Arc::new(self.inner.keyset_id.into())
}
pub fn amount(&self) -> Arc<Amount> {

View File

@@ -18,13 +18,18 @@ impl Deref for Proof {
}
impl Proof {
pub fn new(amount: Arc<Amount>, secret: Arc<Secret>, c: Arc<PublicKey>, id: Arc<Id>) -> Self {
pub fn new(
amount: Arc<Amount>,
secret: Arc<Secret>,
c: Arc<PublicKey>,
keyset_id: Arc<Id>,
) -> Self {
Self {
inner: ProofSdk {
amount: *amount.as_ref().deref(),
secret: secret.as_ref().deref().clone(),
c: c.as_ref().deref().clone(),
id: *id.as_ref().deref(),
keyset_id: *keyset_id.as_ref().deref(),
},
}
}
@@ -41,8 +46,8 @@ impl Proof {
Arc::new(self.inner.c.clone().into())
}
pub fn id(&self) -> Arc<Id> {
Arc::new(self.id.into())
pub fn keyset_id(&self) -> Arc<Id> {
Arc::new(self.keyset_id.into())
}
}
@@ -52,7 +57,7 @@ impl From<&Proof> for ProofSdk {
amount: *proof.amount().as_ref().deref(),
secret: proof.secret().as_ref().deref().clone(),
c: proof.c().deref().into(),
id: proof.id,
keyset_id: proof.keyset_id,
}
}
}
@@ -88,14 +93,14 @@ pub mod mint {
amount: Option<Arc<Amount>>,
secret: Arc<Secret>,
c: Option<Arc<PublicKey>>,
id: Option<Arc<Id>>,
keyset_id: Option<Arc<Id>>,
) -> Self {
Self {
inner: ProofSdk {
amount: amount.map(|a| *a.as_ref().deref()),
secret: secret.as_ref().deref().clone(),
c: c.map(|c| c.as_ref().into()),
id: id.map(|id| *id.as_ref().deref()),
keyset_id: keyset_id.map(|id| *id.as_ref().deref()),
},
}
}
@@ -112,8 +117,8 @@ pub mod mint {
self.inner.c.clone().map(|c| Arc::new(c.into()))
}
pub fn id(&self) -> Option<Arc<Id>> {
self.inner.id.map(|id| Arc::new(id.into()))
pub fn keyset_id(&self) -> Option<Arc<Id>> {
self.inner.keyset_id.map(|id| Arc::new(id.into()))
}
}

View File

@@ -10,14 +10,16 @@ use crate::error::Result;
use crate::{MintProofs, Proof};
pub enum CurrencyUnit {
Sat,
Sat(),
Usd(),
Custom { unit: String },
}
impl From<&CurrencyUnit> for CurrencyUnitSdk {
fn from(unit: &CurrencyUnit) -> CurrencyUnitSdk {
match unit {
CurrencyUnit::Sat => CurrencyUnitSdk::Sat,
CurrencyUnit::Sat() => CurrencyUnitSdk::Sat,
CurrencyUnit::Usd() => CurrencyUnitSdk::Usd,
CurrencyUnit::Custom { unit } => CurrencyUnitSdk::Custom(unit.clone()),
}
}
@@ -26,7 +28,8 @@ impl From<&CurrencyUnit> for CurrencyUnitSdk {
impl From<CurrencyUnitSdk> for CurrencyUnit {
fn from(unit: CurrencyUnitSdk) -> CurrencyUnit {
match unit {
CurrencyUnitSdk::Sat => CurrencyUnit::Sat,
CurrencyUnitSdk::Sat => CurrencyUnit::Sat(),
CurrencyUnitSdk::Usd => CurrencyUnit::Usd(),
CurrencyUnitSdk::Custom(unit) => CurrencyUnit::Custom { unit: unit.clone() },
}
}

View File

@@ -29,7 +29,7 @@ impl From<KeysSdk> for Keys {
let keys = keys
.keys()
.into_iter()
.map(|(amount, pk)| (amount.to_sat().to_string(), Arc::new(pk.into())))
.map(|(amount, pk)| (u64::from(amount).to_string(), Arc::new(pk.into())))
.collect();
Keys::new(keys)
@@ -42,7 +42,7 @@ impl Keys {
.into_iter()
.map(|(amount, pk)| {
(
AmountSdk::from_sat(amount.parse::<u64>().unwrap()),
AmountSdk::from(amount.parse::<u64>().unwrap()),
pk.as_ref().into(),
)
})
@@ -57,7 +57,7 @@ impl Keys {
self.inner
.keys()
.into_iter()
.map(|(amount, pk)| (amount.to_sat().to_string(), Arc::new(pk.into())))
.map(|(amount, pk)| (u64::from(amount).to_string(), Arc::new(pk.into())))
.collect()
}
@@ -71,7 +71,7 @@ impl Keys {
self.inner
.as_hashmap()
.into_iter()
.map(|(amount, pk)| (amount.to_sat().to_string(), pk))
.map(|(amount, pk)| (u64::from(amount).to_string(), pk))
.collect()
}
}

View File

@@ -1,28 +1,28 @@
use std::ops::Deref;
use std::sync::Arc;
use cashu::nuts::{SplitRequest as SplitRequestSdk, SplitResponse as SplitResponseSdk};
use cashu::nuts::{SwapRequest as SwapRequestSdk, SwapResponse as SwapResponseSdk};
use crate::{Amount, BlindedMessage, BlindedSignature, Proof};
pub struct SplitRequest {
inner: SplitRequestSdk,
pub struct SwapRequest {
inner: SwapRequestSdk,
}
impl Deref for SplitRequest {
type Target = SplitRequestSdk;
impl Deref for SwapRequest {
type Target = SwapRequestSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl SplitRequest {
impl SwapRequest {
pub fn new(proofs: Vec<Arc<Proof>>, outputs: Vec<Arc<BlindedMessage>>) -> Self {
let proofs = proofs.into_iter().map(|p| p.as_ref().into()).collect();
let outputs = outputs.into_iter().map(|o| o.as_ref().into()).collect();
Self {
inner: SplitRequestSdk::new(proofs, outputs),
inner: SwapRequestSdk::new(proofs, outputs),
}
}
@@ -53,35 +53,30 @@ impl SplitRequest {
}
}
pub struct SplitResponse {
inner: SplitResponseSdk,
pub struct SwapResponse {
inner: SwapResponseSdk,
}
impl SplitResponse {
pub fn new(promises: Vec<Arc<BlindedSignature>>) -> Self {
let promises = promises.into_iter().map(|p| p.as_ref().into()).collect();
impl SwapResponse {
pub fn new(signatures: Vec<Arc<BlindedSignature>>) -> Self {
let signatures = signatures.into_iter().map(|p| p.as_ref().into()).collect();
Self {
inner: SplitResponseSdk::new(promises),
inner: SwapResponseSdk::new(signatures),
}
}
pub fn promises(&self) -> Vec<Arc<BlindedSignature>> {
pub fn signatures(&self) -> Vec<Arc<BlindedSignature>> {
self.inner
.promises
.signatures
.clone()
.unwrap_or_default()
.into_iter()
.map(|p| Arc::new(p.into()))
.collect()
}
pub fn promises_amount(&self) -> Option<Arc<Amount>> {
self.inner.promises_amount().map(|a| Arc::new(a.into()))
}
}
impl From<cashu::nuts::SplitResponse> for SplitResponse {
fn from(inner: cashu::nuts::SplitResponse) -> SplitResponse {
SplitResponse { inner }
impl From<SwapResponseSdk> for SwapResponse {
fn from(inner: SwapResponseSdk) -> SwapResponse {
SwapResponse { inner }
}
}

View File

@@ -1,33 +1,123 @@
use std::ops::Deref;
use std::str::FromStr;
use std::sync::Arc;
use cashu::nuts::nut04::{MintRequest as MintRequestSdk, PostMintResponse as PostMintResponseSdk};
use cashu::nuts::{
CurrencyUnit, MintBolt11Request as MintBolt11RequestSdk,
MintBolt11Response as MintBolt11ResponseSdk,
MintQuoteBolt11Request as MintQuoteBolt11RequestSdk,
MintQuoteBolt11Response as MintQuoteBolt11ResponseSdk,
};
use crate::{Amount, BlindedMessage, BlindedSignature};
pub struct MintRequest {
inner: MintRequestSdk,
pub struct MintQuoteBolt11Request {
inner: MintQuoteBolt11RequestSdk,
}
impl Deref for MintRequest {
type Target = MintRequestSdk;
impl Deref for MintQuoteBolt11Request {
type Target = MintQuoteBolt11RequestSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl MintRequest {
pub fn new(outputs: Vec<Arc<BlindedMessage>>) -> Self {
impl MintQuoteBolt11Request {
pub fn new(amount: Arc<Amount>, unit: String) -> Self {
Self {
inner: MintRequestSdk {
outputs: outputs
.into_iter()
.map(|o| o.as_ref().deref().clone())
.collect(),
inner: MintQuoteBolt11RequestSdk {
amount: amount.as_ref().deref().clone(),
unit: CurrencyUnit::from_str(&unit).unwrap(),
},
}
}
pub fn amount(&self) -> Arc<Amount> {
Arc::new(self.inner.amount.into())
}
pub fn unit(&self) -> Arc<CurrencyUnit> {
Arc::new(self.inner.clone().unit.into())
}
}
impl From<MintQuoteBolt11RequestSdk> for MintQuoteBolt11Request {
fn from(inner: MintQuoteBolt11RequestSdk) -> MintQuoteBolt11Request {
MintQuoteBolt11Request { inner }
}
}
pub struct MintQuoteBolt11Response {
inner: MintQuoteBolt11ResponseSdk,
}
impl Deref for MintQuoteBolt11Response {
type Target = MintQuoteBolt11ResponseSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl MintQuoteBolt11Response {
pub fn new(quote: String, request: String, paid: bool, expiry: u64) -> Self {
Self {
inner: MintQuoteBolt11ResponseSdk {
quote,
request,
paid,
expiry,
},
}
}
pub fn quote(&self) -> String {
self.quote.clone()
}
pub fn request(&self) -> String {
self.request.clone()
}
pub fn paid(&self) -> bool {
self.paid
}
pub fn expiry(&self) -> u64 {
self.expiry
}
}
impl From<MintQuoteBolt11ResponseSdk> for MintQuoteBolt11Response {
fn from(inner: MintQuoteBolt11ResponseSdk) -> MintQuoteBolt11Response {
MintQuoteBolt11Response { inner }
}
}
pub struct MintBolt11Request {
inner: MintBolt11RequestSdk,
}
impl Deref for MintBolt11Request {
type Target = MintBolt11RequestSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl MintBolt11Request {
pub fn new(quote: String, outputs: Vec<Arc<BlindedMessage>>) -> Self {
Self {
inner: MintBolt11RequestSdk {
quote,
outputs: outputs.iter().map(|o| o.as_ref().deref().clone()).collect(),
},
}
}
pub fn quote(&self) -> String {
self.quote.clone()
}
pub fn outputs(&self) -> Vec<Arc<BlindedMessage>> {
self.inner
.outputs
@@ -36,50 +126,37 @@ impl MintRequest {
.map(|o| Arc::new(o.into()))
.collect()
}
pub fn total_amount(&self) -> Arc<Amount> {
Arc::new(self.inner.total_amount().into())
}
}
impl From<cashu::nuts::nut04::MintRequest> for MintRequest {
fn from(inner: cashu::nuts::nut04::MintRequest) -> MintRequest {
MintRequest { inner }
}
pub struct MintBolt11Response {
inner: MintBolt11ResponseSdk,
}
pub struct PostMintResponse {
inner: PostMintResponseSdk,
}
impl Deref for PostMintResponse {
type Target = PostMintResponseSdk;
impl Deref for MintBolt11Response {
type Target = MintBolt11ResponseSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl PostMintResponse {
pub fn new(promises: Vec<Arc<BlindedSignature>>) -> Self {
impl MintBolt11Response {
pub fn new(signatures: Vec<Arc<BlindedSignature>>) -> Self {
Self {
inner: PostMintResponseSdk {
promises: promises.into_iter().map(|p| p.as_ref().into()).collect(),
inner: MintBolt11ResponseSdk {
signatures: signatures
.into_iter()
.map(|s| s.as_ref().deref().clone())
.collect(),
},
}
}
pub fn promises(&self) -> Vec<Arc<BlindedSignature>> {
pub fn signatures(&self) -> Vec<Arc<BlindedSignature>> {
self.inner
.promises
.signatures
.clone()
.into_iter()
.map(|p| Arc::new(p.into()))
.map(|o| Arc::new(o.into()))
.collect()
}
}
impl From<cashu::nuts::nut04::PostMintResponse> for PostMintResponse {
fn from(inner: cashu::nuts::nut04::PostMintResponse) -> PostMintResponse {
PostMintResponse { inner }
}
}

View File

@@ -110,7 +110,7 @@ impl MeltBolt11Request {
}
pub fn quote(&self) -> String {
self.inner.quote
self.inner.quote.clone()
}
}
@@ -119,11 +119,15 @@ pub struct MeltBolt11Response {
}
impl MeltBolt11Response {
pub fn new(paid: bool, proof: String, change: Option<Vec<Arc<BlindedSignature>>>) -> Self {
pub fn new(
paid: bool,
payment_preimage: Option<String>,
change: Option<Vec<Arc<BlindedSignature>>>,
) -> Self {
Self {
inner: MeltBolt11ResponseSdk {
paid,
proof,
payment_preimage,
change: change.map(|c| c.into_iter().map(|b| b.as_ref().deref().clone()).collect()),
},
}
@@ -133,7 +137,7 @@ impl MeltBolt11Response {
self.inner.paid
}
pub fn proof(&self) -> String {
self.inner.proof.clone()
pub fn payment_preimage(&self) -> Option<String> {
self.inner.payment_preimage.clone()
}
}

View File

@@ -1,7 +1,7 @@
use std::ops::Deref;
use std::sync::Arc;
use cashu::nuts::nut09::{MintInfo as MintInfoSdk, MintVersion as MintVersionSdk};
use cashu::nuts::{MintInfo as MintInfoSdk, MintVersion as MintVersionSdk, Nuts as NutsSdk};
use crate::PublicKey;
@@ -57,7 +57,8 @@ impl MintInfo {
description: Option<String>,
description_long: Option<String>,
contact: Option<Vec<Vec<String>>>,
nuts: Vec<String>,
// TODO: Should be a nuts type
nuts: String,
motd: Option<String>,
) -> Self {
let pubkey = pubkey.map(|p| p.as_ref().deref().clone());
@@ -70,7 +71,7 @@ impl MintInfo {
description,
description_long,
contact,
nuts,
nuts: NutsSdk::default(),
motd,
},
}
@@ -100,8 +101,8 @@ impl MintInfo {
self.inner.contact.clone()
}
pub fn nuts(&self) -> Vec<String> {
self.inner.nuts.clone()
pub fn nuts(&self) -> Arc<Nuts> {
Arc::new(self.inner.nuts.clone().into())
}
pub fn motd(&self) -> Option<String> {
@@ -109,8 +110,25 @@ impl MintInfo {
}
}
impl From<cashu::nuts::nut09::MintInfo> for MintInfo {
fn from(inner: cashu::nuts::nut09::MintInfo) -> MintInfo {
impl From<MintInfoSdk> for MintInfo {
fn from(inner: MintInfoSdk) -> MintInfo {
MintInfo { inner }
}
}
pub struct Nuts {
inner: NutsSdk,
}
impl Deref for Nuts {
type Target = NutsSdk;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl From<NutsSdk> for Nuts {
fn from(inner: NutsSdk) -> Nuts {
Nuts { inner }
}
}

View File

@@ -80,11 +80,15 @@ impl From<MeltBolt11Response> for cashu::nuts::nut08::MeltBolt11Response {
}
impl MeltBolt11Response {
pub fn new(paid: bool, proof: String, change: Option<Vec<Arc<BlindedSignature>>>) -> Self {
pub fn new(
paid: bool,
payment_preimage: Option<String>,
change: Option<Vec<Arc<BlindedSignature>>>,
) -> Self {
Self {
inner: MeltBolt11ResponseSdk {
paid,
proof,
payment_preimage,
change: change
.map(|change| change.into_iter().map(|bs| bs.as_ref().into()).collect()),
},
@@ -95,8 +99,8 @@ impl MeltBolt11Response {
self.inner.paid
}
pub fn proof(&self) -> String {
self.inner.proof.clone()
pub fn payment_preimage(&self) -> Option<String> {
self.inner.payment_preimage.clone()
}
pub fn change(&self) -> Option<Vec<Arc<BlindedSignature>>> {

View File

@@ -15,29 +15,9 @@ impl Deref for Amount {
}
impl Amount {
pub fn new(sats: u64) -> Self {
pub fn new(amount: u64) -> Self {
Self {
inner: AmountSdk::from_sat(sats),
}
}
pub fn to_sat(&self) -> u64 {
self.inner.to_sat()
}
pub fn to_msat(&self) -> u64 {
self.inner.to_msat()
}
pub fn from_sat(sats: u64) -> Self {
Self {
inner: AmountSdk::from_sat(sats),
}
}
pub fn from_msat(msats: u64) -> Self {
Self {
inner: AmountSdk::from_msat(msats),
inner: AmountSdk::from(amount),
}
}
@@ -47,13 +27,10 @@ impl Amount {
/// Split into parts that are powers of two
pub fn split(&self) -> Vec<Arc<Self>> {
let sats = self.inner.to_sat();
(0_u64..64)
.rev()
.filter_map(|bit| {
let part = 1 << bit;
((sats & part) == part).then_some(Arc::new(Self::from_sat(part)))
})
self.inner
.split()
.into_iter()
.map(|a| Arc::new(a.into()))
.collect()
}
}
@@ -69,3 +46,9 @@ impl From<&Amount> for AmountSdk {
amount.inner
}
}
impl From<u64> for Amount {
fn from(amount: u64) -> Amount {
AmountSdk::from(amount).into()
}
}

View File

@@ -38,6 +38,6 @@ impl Bolt11Invoice {
pub fn amount(&self) -> Option<Arc<Amount>> {
self.inner
.amount_milli_satoshis()
.map(|a| Arc::new(Amount::from_msat(a)))
.map(|a| Arc::new(Amount::from(a / 1000).into()))
}
}

View File

@@ -151,21 +151,21 @@ impl Mint {
})
}
pub fn process_split_request(
pub fn process_swap_request(
&mut self,
split_request: SwapRequest,
swap_request: SwapRequest,
) -> Result<SwapResponse, Error> {
let proofs_total = split_request.input_amount();
let proofs_total = swap_request.input_amount();
let output_total = split_request.output_amount();
let output_total = swap_request.output_amount();
if proofs_total != output_total {
return Err(Error::Amount);
}
let proof_count = split_request.inputs.len();
let proof_count = swap_request.inputs.len();
let secrets: HashSet<Secret> = split_request
let secrets: HashSet<Secret> = swap_request
.inputs
.iter()
.map(|p| p.secret.clone())
@@ -176,7 +176,7 @@ impl Mint {
return Err(Error::DuplicateProofs);
}
for proof in &split_request.inputs {
for proof in &swap_request.inputs {
self.verify_proof(proof)?
}
@@ -184,7 +184,7 @@ impl Mint {
self.spent_secrets.insert(secret);
}
let promises: Vec<BlindedSignature> = split_request
let promises: Vec<BlindedSignature> = swap_request
.outputs
.iter()
.map(|b| self.blind_sign(b).unwrap())

View File

@@ -25,7 +25,7 @@ pub use nut04::{
#[cfg(not(feature = "nut08"))]
pub use nut05::{MeltBolt11Request, MeltBolt11Response};
pub use nut05::{MeltQuoteBolt11Request, MeltQuoteBolt11Response};
pub use nut06::{MintInfo, MintVersion};
pub use nut06::{MintInfo, MintVersion, Nuts};
#[cfg(feature = "wallet")]
#[cfg(feature = "nut07")]
pub use nut07::{CheckSpendableRequest, CheckSpendableResponse};

View File

@@ -9,7 +9,7 @@ use crate::Amount;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MintQuoteBolt11Request {
/// Amount
pub amount: u64,
pub amount: Amount,
/// Unit wallet would like to pay with
pub unit: CurrencyUnit,
}