Skip to content

Commit 4d87c6a

Browse files
authored
switch to bitcrb token, use keyset info (#559)
1 parent 977c7ff commit 4d87c6a

File tree

6 files changed

+40
-39
lines changed

6 files changed

+40
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 0.3.17
22

3+
* Changed minted proofs token format from cashu Token v3 to BitcrB (v4)
34
* Use NodeId, PublicKey, SecretKey and BillId types internally instead of strings (fully breaking)
45
* This breaks all existing databases, since the node ids and bill ids now have the format `prefix|network|pubkey`- example: `bitcrt03f9f94d1fdc2090d46f3524807e3f58618c36988e69577d70d5d4d1e9e9645a4f`
56
* The `prefix` is `bitcr`

crates/bcr-ebill-api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ bcr-wdc-webapi = { git = "https://github.com/BitcreditProtocol/wildcat", tag = "
3535
bcr-wdc-quote-client = { git = "https://github.com/BitcreditProtocol/wildcat", tag = "v0.2.0" }
3636
bcr-wdc-key-client = { git = "https://github.com/BitcreditProtocol/wildcat", tag = "v0.2.0" }
3737
bcr-wdc-swap-client = { git = "https://github.com/BitcreditProtocol/wildcat", tag = "v0.2.0" }
38+
bcr-wallet-lib = { git = "https://github.com/BitcreditProtocol/Wallet-Core", rev = "0a974e79d25fd7018a8165d8575cd06a8da9be59"}
3839
cashu = { version = "0.9", default-features = false }
3940
rand = { version = "0.8" }
4041
hex = { version = "0.4" }

crates/bcr-ebill-api/src/constants.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ pub const VALID_FILE_MIME_TYPES: [&str; 3] = ["image/jpeg", "image/png", "applic
55

66
// When subscribing events we subtract this from the last received event time
77
pub const NOSTR_EVENT_TIME_SLACK: u64 = 3600; // 1 hour
8-
pub const CURRENCY_CRSAT: &str = "crsat";
98
pub const CURRENCY_SAT: &str = "sat";

crates/bcr-ebill-api/src/external/mint.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
use std::str::FromStr;
22

3-
use crate::{constants::CURRENCY_CRSAT, util};
3+
use crate::util;
44
use async_trait::async_trait;
55
use bcr_ebill_core::{
66
PostalAddress, SecretKey, ServiceTraitBounds,
77
bill::BitcreditBill,
88
contact::{BillAnonParticipant, BillIdentParticipant, BillParticipant, ContactType},
99
util::{BcrKeys, date::DateTimeUtc},
1010
};
11+
use bcr_wallet_lib::wallet::TokenOperations;
1112
use bcr_wdc_key_client::KeyClient;
1213
use bcr_wdc_quote_client::QuoteClient;
1314
use bcr_wdc_swap_client::SwapClient;
1415
use bcr_wdc_webapi::quotes::{BillInfo, ResolveOffer, StatusReply};
15-
use cashu::{ProofsMethods, State, nut00 as cdk00, nut01 as cdk01, nut02 as cdk02};
16+
use cashu::{ProofsMethods, State, nut01 as cdk01, nut02 as cdk02};
1617
use reqwest::Url;
1718
use thiserror::Error;
1819

@@ -161,34 +162,28 @@ impl MintClientApi for MintClient {
161162
async fn check_if_proofs_are_spent(&self, mint_url: &str, proofs: &str) -> Result<bool> {
162163
let token_mint_url =
163164
cashu::MintUrl::from_str(mint_url).map_err(|_| Error::InvalidMintUrl)?;
164-
let token = cashu::Token::from_str(proofs).map_err(|_| Error::InvalidToken)?;
165-
166-
if let cashu::Token::TokenV3(token_v3) = token {
167-
if let Some(token_for_mint) = token_v3
168-
.token
169-
.into_iter()
170-
.find(|t| t.mint == token_mint_url)
171-
{
172-
let ys = token_for_mint.proofs.ys().map_err(|_| Error::PubKey)?;
173-
let proof_states =
174-
self.swap_client(mint_url)?
175-
.check_state(ys)
176-
.await
177-
.map_err(|e| {
178-
log::error!("Error checking if proofs are spent at {mint_url}: {e}");
179-
Error::SwapClient
180-
})?;
181-
// all proofs have to be spent
182-
let proofs_spent = proof_states
183-
.iter()
184-
.all(|ps| matches!(ps.state, State::Spent));
185-
Ok(proofs_spent)
186-
} else {
187-
Err(Error::InvalidToken.into())
188-
}
189-
} else {
190-
Err(Error::InvalidToken.into())
165+
let token =
166+
bcr_wallet_lib::wallet::Token::from_str(proofs).map_err(|_| Error::InvalidToken)?;
167+
168+
if token_mint_url != token.mint_url() {
169+
return Err(Error::InvalidToken.into());
191170
}
171+
172+
let ys = token.proofs().ys().map_err(|_| Error::PubKey)?;
173+
174+
let proof_states = self
175+
.swap_client(mint_url)?
176+
.check_state(ys)
177+
.await
178+
.map_err(|e| {
179+
log::error!("Error checking if proofs are spent at {mint_url}: {e}");
180+
Error::SwapClient
181+
})?;
182+
// all proofs have to be spent
183+
let proofs_spent = proof_states
184+
.iter()
185+
.all(|ps| matches!(ps.state, State::Spent));
186+
Ok(proofs_spent)
192187
}
193188

194189
async fn mint(
@@ -206,6 +201,15 @@ impl MintClientApi for MintClient {
206201
let secret_key = cdk01::SecretKey::from_hex(private_key.display_secret().to_string())
207202
.map_err(|_| Error::PrivateKey)?;
208203
let qid = uuid::Uuid::from_str(quote_id).map_err(|_| Error::InvalidMintRequestId)?;
204+
let currency = self
205+
.key_client(mint_url)?
206+
.keyset_info(keyset.id)
207+
.await
208+
.map_err(|e| {
209+
log::error!("Error getting keyset info from {mint_url}: {e}");
210+
Error::Minting
211+
})?
212+
.unit;
209213

210214
// mint
211215
let blinded_signatures = self
@@ -225,14 +229,10 @@ impl MintClientApi for MintClient {
225229
})?;
226230

227231
// generate token from proofs
228-
let token = cdk00::Token::new(
229-
token_mint_url,
230-
proofs,
231-
None,
232-
cashu::CurrencyUnit::Custom(CURRENCY_CRSAT.into()),
233-
);
232+
let token =
233+
bcr_wallet_lib::wallet::Token::new_credit(token_mint_url, currency, None, proofs);
234234

235-
Ok(token.to_v3_string())
235+
Ok(token.to_string())
236236
}
237237

238238
async fn get_keyset_info(&self, mint_url: &str, keyset_id: &str) -> Result<cdk02::KeySet> {

crates/bcr-ebill-core/src/mint/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct MintOffer {
4646
pub expiration_timestamp: u64,
4747
/// The discounted sum the mint offers us
4848
pub discounted_sum: u64,
49-
/// The proofs, encoded as a custom Tokenv3
49+
/// The proofs, encoded as a bitcrB token
5050
pub proofs: Option<String>,
5151
/// Whether the proofs were spent according to the mint
5252
pub proofs_spent: bool,

crates/bcr-ebill-wasm/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ let config = {
5656
job_runner_check_interval_seconds: 600,
5757
// default_mint_url: "http://localhost:4343",
5858
default_mint_url: "https://wildcat-dev-docker.minibill.tech",
59-
default_mint_node_id: "bitcrt02b7102bd5a4a572298f2aa485e86e490881e9ad7906fcae741db141b8ba1e8acc",
59+
default_mint_node_id: "bitcrt0323591791ad4c25eb45a707b10aebe0486eb9ec9485985ab5de12a527810bc42b",
6060
// default_mint_node_id: "bitcrt0372422bfa5c9b60ef2f10ced26e764983b2e675cd7fac374f5f223616530ce3fb", // dev mint
6161
};
6262

0 commit comments

Comments
 (0)