mirror of
https://github.com/aljazceru/cdk.git
synced 2026-02-05 13:16:00 +01:00
refactor: use async mutex
This commit is contained in:
@@ -59,7 +59,7 @@ impl Token {
|
||||
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());
|
||||
let unit = unit.map(|u| CurrencyUnitSdk::from_str(&u).unwrap_or_default());
|
||||
|
||||
Ok(Self {
|
||||
inner: TokenSdk::new(mint, proofs, memo, unit)?,
|
||||
@@ -95,7 +95,7 @@ impl Token {
|
||||
|
||||
impl fmt::Display for Token {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.inner.to_string())
|
||||
write!(f, "{}", self.inner)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ impl MintQuoteBolt11Request {
|
||||
pub fn new(amount: Arc<Amount>, unit: String) -> Self {
|
||||
Self {
|
||||
inner: MintQuoteBolt11RequestSdk {
|
||||
amount: amount.as_ref().deref().clone(),
|
||||
amount: *amount.as_ref().deref(),
|
||||
unit: CurrencyUnit::from_str(&unit).unwrap(),
|
||||
},
|
||||
}
|
||||
@@ -37,7 +37,7 @@ impl MintQuoteBolt11Request {
|
||||
}
|
||||
|
||||
pub fn unit(&self) -> Arc<CurrencyUnit> {
|
||||
Arc::new(self.inner.clone().unit.into())
|
||||
Arc::new(self.inner.clone().unit)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,6 @@ impl Bolt11Invoice {
|
||||
pub fn amount(&self) -> Option<Arc<Amount>> {
|
||||
self.inner
|
||||
.amount_milli_satoshis()
|
||||
.map(|a| Arc::new(Amount::from(a / 1000).into()))
|
||||
.map(|a| Arc::new(Amount::from(a / 1000)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,10 +37,10 @@ impl MeltQuote {
|
||||
Self {
|
||||
inner: MeltQuoteSdk {
|
||||
id,
|
||||
amount: amount.as_ref().deref().clone(),
|
||||
amount: *amount.as_ref().deref(),
|
||||
unit: CurrencyUnit::from_str(&unit).unwrap(),
|
||||
request: request.as_ref().deref().clone(),
|
||||
fee_reserve: fee_reserve.as_ref().deref().clone(),
|
||||
fee_reserve: *fee_reserve.as_ref().deref(),
|
||||
paid,
|
||||
expiry,
|
||||
},
|
||||
|
||||
@@ -36,7 +36,7 @@ impl MintQuote {
|
||||
Self {
|
||||
inner: MintQuoteSdk {
|
||||
id,
|
||||
amount: amount.as_ref().deref().clone(),
|
||||
amount: *amount.as_ref().deref(),
|
||||
unit: CurrencyUnit::from_str(&unit).unwrap(),
|
||||
request: request.as_ref().deref().clone(),
|
||||
paid,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::ops::Deref;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::sync::Arc;
|
||||
|
||||
use cashu_ffi::{
|
||||
BlindedSignature, Bolt11Invoice, CurrencyUnit, MeltQuote, MintQuote, PreMintSecrets, Proof,
|
||||
@@ -11,6 +11,7 @@ use cashu_sdk::url::UncheckedUrl;
|
||||
use cashu_sdk::wallet::Wallet as WalletSdk;
|
||||
use once_cell::sync::Lazy;
|
||||
use tokio::runtime::Runtime;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::types::{Melted, SendProofs};
|
||||
@@ -19,7 +20,7 @@ use crate::{Amount, Keys};
|
||||
static RUNTIME: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("Can't start Tokio runtime"));
|
||||
|
||||
pub struct Wallet {
|
||||
inner: RwLock<WalletSdk<HttpClient>>,
|
||||
inner: Mutex<WalletSdk<HttpClient>>,
|
||||
}
|
||||
|
||||
impl Wallet {
|
||||
@@ -51,8 +52,8 @@ impl Wallet {
|
||||
pub fn check_proofs_spent(&self, proofs: Vec<Arc<Proof>>) -> Result<Arc<ProofsStatus>> {
|
||||
let proofs = RUNTIME.block_on(async {
|
||||
self.inner
|
||||
.read()
|
||||
.unwrap()
|
||||
.lock()
|
||||
.await
|
||||
.check_proofs_spent(proofs.iter().map(|p| p.as_ref().deref().clone()).collect())
|
||||
.await
|
||||
})?;
|
||||
@@ -68,8 +69,8 @@ impl Wallet {
|
||||
) -> Result<Arc<Token>> {
|
||||
let token = RUNTIME.block_on(async {
|
||||
self.inner
|
||||
.write()
|
||||
.unwrap()
|
||||
.lock()
|
||||
.await
|
||||
.mint_token(*amount.as_ref().deref(), memo, unit.map(|u| u.into()))
|
||||
.await
|
||||
})?;
|
||||
@@ -80,8 +81,8 @@ impl Wallet {
|
||||
pub fn mint_quote(&self, amount: Arc<Amount>, unit: CurrencyUnit) -> Result<Arc<MintQuote>> {
|
||||
let quote = RUNTIME.block_on(async {
|
||||
self.inner
|
||||
.write()
|
||||
.unwrap()
|
||||
.lock()
|
||||
.await
|
||||
.mint_quote(*amount.as_ref().deref(), unit.into())
|
||||
.await
|
||||
})?;
|
||||
@@ -90,14 +91,14 @@ impl Wallet {
|
||||
}
|
||||
|
||||
pub fn mint(&self, quote: String) -> Result<Vec<Arc<Proof>>> {
|
||||
let proofs = RUNTIME.block_on(async { self.inner.write().unwrap().mint("e).await })?;
|
||||
let proofs = RUNTIME.block_on(async { self.inner.lock().await.mint("e).await })?;
|
||||
|
||||
Ok(proofs.into_iter().map(|p| Arc::new(p.into())).collect())
|
||||
}
|
||||
|
||||
pub fn receive(&self, encoded_token: String) -> Result<Vec<Arc<Proof>>> {
|
||||
let proofs = RUNTIME
|
||||
.block_on(async { self.inner.write().unwrap().receive(&encoded_token).await })?;
|
||||
let proofs =
|
||||
RUNTIME.block_on(async { self.inner.lock().await.receive(&encoded_token).await })?;
|
||||
|
||||
Ok(proofs.into_iter().map(|p| Arc::new(p.into())).collect())
|
||||
}
|
||||
@@ -107,24 +108,20 @@ impl Wallet {
|
||||
blinded_messages: Arc<PreMintSecrets>,
|
||||
promises: Vec<Arc<BlindedSignature>>,
|
||||
) -> Result<Vec<Arc<Proof>>> {
|
||||
Ok(self
|
||||
.inner
|
||||
.read()
|
||||
.unwrap()
|
||||
.process_split_response(
|
||||
let proofs = RUNTIME.block_on(async {
|
||||
self.inner.lock().await.process_split_response(
|
||||
blinded_messages.as_ref().deref().clone(),
|
||||
promises.iter().map(|p| p.as_ref().into()).collect(),
|
||||
)?
|
||||
.into_iter()
|
||||
.map(|p| Arc::new(p.into()))
|
||||
.collect())
|
||||
)
|
||||
})?;
|
||||
Ok(proofs.into_iter().map(|p| Arc::new(p.into())).collect())
|
||||
}
|
||||
|
||||
pub fn send(&self, amount: Arc<Amount>, proofs: Vec<Arc<Proof>>) -> Result<Arc<SendProofs>> {
|
||||
let send_proofs = RUNTIME.block_on(async {
|
||||
self.inner
|
||||
.read()
|
||||
.unwrap()
|
||||
.lock()
|
||||
.await
|
||||
.send(
|
||||
*amount.as_ref().deref(),
|
||||
proofs.iter().map(|p| p.as_ref().deref().clone()).collect(),
|
||||
@@ -142,8 +139,8 @@ impl Wallet {
|
||||
) -> Result<Arc<MeltQuote>> {
|
||||
let melt_quote = RUNTIME.block_on(async {
|
||||
self.inner
|
||||
.write()
|
||||
.unwrap()
|
||||
.lock()
|
||||
.await
|
||||
.melt_quote(unit.into(), request.as_ref().deref().clone())
|
||||
.await
|
||||
})?;
|
||||
@@ -154,8 +151,8 @@ impl Wallet {
|
||||
pub fn melt(&self, quote_id: String, proofs: Vec<Arc<Proof>>) -> Result<Arc<Melted>> {
|
||||
let melted = RUNTIME.block_on(async {
|
||||
self.inner
|
||||
.write()
|
||||
.unwrap()
|
||||
.lock()
|
||||
.await
|
||||
.melt(
|
||||
"e_id,
|
||||
proofs.iter().map(|p| p.as_ref().deref().clone()).collect(),
|
||||
@@ -172,10 +169,12 @@ impl Wallet {
|
||||
unit: Option<CurrencyUnit>,
|
||||
memo: Option<String>,
|
||||
) -> Result<String> {
|
||||
Ok(self.inner.read().unwrap().proofs_to_token(
|
||||
proofs.iter().map(|p| p.as_ref().deref().clone()).collect(),
|
||||
memo,
|
||||
unit.map(|u| u.into()),
|
||||
)?)
|
||||
Ok(RUNTIME.block_on(async {
|
||||
self.inner.lock().await.proofs_to_token(
|
||||
proofs.iter().map(|p| p.as_ref().deref().clone()).collect(),
|
||||
memo,
|
||||
unit.map(|u| u.into()),
|
||||
)
|
||||
})?)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,8 +65,7 @@ impl JsMint {
|
||||
.inner
|
||||
.keyset(&keyset_id)
|
||||
.ok_or(JsError::new("Unknown Keyset"))?
|
||||
.clone()
|
||||
.into();
|
||||
.clone();
|
||||
|
||||
Ok(KeysResponse {
|
||||
keysets: vec![keyset],
|
||||
|
||||
Reference in New Issue
Block a user