Skip to content

Commit 2b74b2e

Browse files
authored
upload files for mint (#541)
1 parent d12aa91 commit 2b74b2e

File tree

7 files changed

+82
-18
lines changed

7 files changed

+82
-18
lines changed

crates/bcr-ebill-api/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ bcr-ebill-transport = { path = "../bcr-ebill-transport" }
3131
tokio.workspace = true
3232
tokio_with_wasm.workspace = true
3333
secp256k1.workspace = true
34-
bcr-wdc-webapi = { git = "https://github.com/BitcreditProtocol/wildcat", rev = "4437c3b809105df69ed459a9698cac8deb8d9210" }
35-
bcr-wdc-quote-client = { git = "https://github.com/BitcreditProtocol/wildcat", rev = "4437c3b809105df69ed459a9698cac8deb8d9210" }
36-
bcr-wdc-key-client = { git = "https://github.com/BitcreditProtocol/wildcat", rev = "4437c3b809105df69ed459a9698cac8deb8d9210" }
37-
bcr-wdc-swap-client = { git = "https://github.com/BitcreditProtocol/wildcat", rev = "4437c3b809105df69ed459a9698cac8deb8d9210" }
34+
bcr-wdc-webapi = { git = "https://github.com/BitcreditProtocol/wildcat", rev = "210ee9fccfb93e00247c46dbfcd4cc55287" }
35+
bcr-wdc-quote-client = { git = "https://github.com/BitcreditProtocol/wildcat", rev = "210ee9fccfb93e00247c46dbfcd4cc55287" }
36+
bcr-wdc-key-client = { git = "https://github.com/BitcreditProtocol/wildcat", rev = "210ee9fccfb93e00247c46dbfcd4cc55287" }
37+
bcr-wdc-swap-client = { git = "https://github.com/BitcreditProtocol/wildcat", rev = "210ee9fccfb93e00247c46dbfcd4cc55287" }
3838
cashu = { version = "0.9", default-features = false }
3939
rand = { version = "0.8" }
4040
hex = { version = "0.4" }
41-
41+
url = {version = "2.5"}
4242

4343
[target.'cfg(target_arch = "wasm32")'.dependencies]
4444
reqwest = { workspace = true, features = ["json"] }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl FileStorageClient {
6161
}
6262
}
6363

64-
fn to_url(relay_url: &str, to_join: &str) -> Result<Url> {
64+
pub fn to_url(relay_url: &str, to_join: &str) -> Result<Url> {
6565
let mut url = reqwest::Url::parse(relay_url)
6666
.and_then(|url| url.join(to_join))
6767
.map_err(|_| Error::InvalidRelayUrl)?;

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use bcr_wdc_quote_client::QuoteClient;
1313
use bcr_wdc_swap_client::SwapClient;
1414
use bcr_wdc_webapi::quotes::{BillInfo, ResolveOffer, StatusReply};
1515
use cashu::{ProofsMethods, State, nut00 as cdk00, nut01 as cdk01, nut02 as cdk02};
16+
use reqwest::Url;
1617
use thiserror::Error;
1718

1819
/// Generic result type
@@ -100,6 +101,7 @@ pub trait MintClientApi: ServiceTraitBounds {
100101
requester_keys: &BcrKeys,
101102
bill: &BitcreditBill,
102103
endorsees: &[BillParticipant],
104+
files: &[Url],
103105
) -> Result<String>;
104106
/// Look up a quote for a mint
105107
async fn lookup_quote_for_mint(
@@ -257,6 +259,7 @@ impl MintClientApi for MintClient {
257259
requester_keys: &BcrKeys,
258260
bill: &BitcreditBill,
259261
endorsees: &[BillParticipant],
262+
files: &[Url],
260263
) -> Result<String> {
261264
let bill_info = BillInfo {
262265
id: bill.id.clone(),
@@ -270,6 +273,7 @@ impl MintClientApi for MintClient {
270273
sum: bill.sum,
271274
maturity_date: util::date::date_string_to_rfc3339(&bill.maturity_date)
272275
.map_err(|_| Error::InvalidDate)?,
276+
file_urls: files.to_owned(),
273277
};
274278
let public_key = cdk01::PublicKey::from_hex(requester_keys.get_public_key())
275279
.map_err(|_| Error::PubKey)?;
@@ -423,10 +427,10 @@ impl From<StatusReply> for QuoteStatusReply {
423427
expiration_date,
424428
discounted,
425429
},
426-
StatusReply::Accepted { keyset_id } => QuoteStatusReply::Accepted { keyset_id },
427-
StatusReply::Rejected { tstamp } => QuoteStatusReply::Rejected { tstamp },
430+
StatusReply::Accepted { keyset_id, .. } => QuoteStatusReply::Accepted { keyset_id },
431+
StatusReply::Rejected { tstamp, .. } => QuoteStatusReply::Rejected { tstamp },
428432
StatusReply::Canceled { tstamp } => QuoteStatusReply::Cancelled { tstamp },
429-
StatusReply::OfferExpired { tstamp } => QuoteStatusReply::Expired { tstamp },
433+
StatusReply::OfferExpired { tstamp, .. } => QuoteStatusReply::Expired { tstamp },
430434
}
431435
}
432436
}

crates/bcr-ebill-api/src/service/bill_service/issue.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ use bcr_ebill_transport::BillChainEvent;
1717
use log::{debug, error, info};
1818

1919
impl BillService {
20-
async fn encrypt_and_save_uploaded_file(
20+
pub(super) async fn encrypt_and_save_uploaded_file(
2121
&self,
2222
file_name: &str,
2323
file_bytes: &[u8],
2424
bill_id: &str,
25-
bill_public_key: &str,
25+
public_key: &str,
2626
relay_url: &str,
2727
) -> Result<File> {
2828
let file_hash = util::sha256_hash(file_bytes);
29-
let encrypted = util::crypto::encrypt_ecies(file_bytes, bill_public_key)?;
29+
let encrypted = util::crypto::encrypt_ecies(file_bytes, public_key)?;
3030
let nostr_hash = self.file_upload_client.upload(relay_url, encrypted).await?;
3131
info!("Saved file {file_name} with hash {file_hash} for bill {bill_id}");
3232
Ok(File {

crates/bcr-ebill-api/src/service/bill_service/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5811,7 +5811,7 @@ pub mod tests {
58115811
.returning(|_, _, _| Ok(vec![]));
58125812
ctx.mint_client
58135813
.expect_enquire_mint_quote()
5814-
.returning(|_, _, _, _| Ok("quote_id".to_owned()));
5814+
.returning(|_, _, _, _, _| Ok("quote_id".to_owned()));
58155815
ctx.mint_store
58165816
.expect_add_request()
58175817
.returning(|_, _, _, _, _| Ok(()));

crates/bcr-ebill-api/src/service/bill_service/service.rs

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::data::{
1414
identity::Identity,
1515
};
1616
use crate::external::bitcoin::BitcoinClientApi;
17-
use crate::external::file_storage::FileStorageClientApi;
17+
use crate::external::file_storage::{self, FileStorageClientApi};
1818
use crate::external::mint::{MintClientApi, QuoteStatusReply, ResolveMintOffer};
1919
use crate::get_config;
2020
use crate::persistence::bill::BillChainStoreApi;
@@ -44,7 +44,7 @@ use bcr_ebill_core::util::currency;
4444
use bcr_ebill_core::{File, ServiceTraitBounds, Validate, ValidationError};
4545
use bcr_ebill_persistence::mint::MintStoreApi;
4646
use bcr_ebill_transport::NotificationServiceApi;
47-
use log::{debug, error, info};
47+
use log::{debug, error, info, warn};
4848
use std::collections::{HashMap, HashSet};
4949
use std::sync::Arc;
5050

@@ -622,6 +622,38 @@ impl BillService {
622622
nostr_relays: relays,
623623
})
624624
}
625+
626+
/// Download the files, decrypt them, encrypt and upload them for the given node id
627+
/// and relay url and return a list of urls to the uploaded files
628+
async fn upload_bill_files_for_node_id(
629+
&self,
630+
bill_id: &str,
631+
bill_private_key: &str,
632+
node_id: &str,
633+
relay_url: &str,
634+
files: &[File],
635+
) -> Result<Vec<url::Url>> {
636+
if files.is_empty() {
637+
return Ok(vec![]);
638+
}
639+
let mut result = Vec::with_capacity(files.len());
640+
for file in files {
641+
let decrypted_file = self
642+
.open_and_decrypt_attached_file(bill_id, file, bill_private_key)
643+
.await?;
644+
let uploaded_file = self
645+
.encrypt_and_save_uploaded_file(
646+
&file.name,
647+
&decrypted_file,
648+
bill_id,
649+
node_id,
650+
relay_url,
651+
)
652+
.await?;
653+
result.push(file_storage::to_url(relay_url, &uploaded_file.nostr_hash)?);
654+
}
655+
Ok(result)
656+
}
625657
}
626658

627659
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
@@ -1411,11 +1443,38 @@ impl BillServiceApi for BillService {
14111443
));
14121444
}
14131445

1446+
// Upload existing files for the mint
1447+
let file_urls_for_mint = match mint_anon_participant.nostr_relays().first() {
1448+
Some(relay_url) => {
1449+
self.upload_bill_files_for_node_id(
1450+
bill_id,
1451+
&bill_keys.private_key,
1452+
&mint_anon_participant.node_id(),
1453+
relay_url,
1454+
&bill.files,
1455+
)
1456+
.await?
1457+
}
1458+
None => {
1459+
warn!(
1460+
"mint {} does not have a nostr relay",
1461+
&mint_anon_participant.node_id()
1462+
);
1463+
vec![]
1464+
}
1465+
};
1466+
14141467
// Send request to mint to mint
14151468
let endorsees = blockchain.get_endorsees_for_bill(&bill_keys);
14161469
let mint_request_id = self
14171470
.mint_client
1418-
.enquire_mint_quote(&mint_cfg.default_mint_url, signer_keys, &bill, &endorsees)
1471+
.enquire_mint_quote(
1472+
&mint_cfg.default_mint_url,
1473+
signer_keys,
1474+
&bill,
1475+
&endorsees,
1476+
&file_urls_for_mint,
1477+
)
14191478
.await?;
14201479

14211480
// Store request to mint

crates/bcr-ebill-wasm/main.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ let config = {
4848
// esplora_base_url: "http://localhost:8094", // local reg test via docker-compose
4949
bitcoin_network: "testnet",
5050
esplora_base_url: "https://esplora.minibill.tech",
51-
// nostr_relays: ["wss://bitcr-cloud-run-05-550030097098.europe-west1.run.app"],
52-
nostr_relays: ["ws://localhost:8080"],
51+
nostr_relays: ["wss://bitcr-cloud-run-05-550030097098.europe-west1.run.app"],
52+
// nostr_relays: ["ws://localhost:8080"],
5353
// if set to true we will drop DMs from nostr that we don't have in contacts
5454
nostr_only_known_contacts: false,
5555
job_runner_initial_delay_seconds: 1,
@@ -183,6 +183,7 @@ let generalApi = apis.generalApi;
183183
let identityApi = apis.identityApi;
184184
let billApi = apis.billApi;
185185
window.billApi = billApi;
186+
window.identityApi = identityApi;
186187
window.generalApi = generalApi;
187188
let notificationTriggerApi = apis.notificationApi;
188189

0 commit comments

Comments
 (0)