From 557a271eac49e0ce693ca410c840308c28ac97aa Mon Sep 17 00:00:00 2001 From: Armando Dutra Date: Fri, 12 Jan 2024 03:00:49 -0300 Subject: [PATCH] fix: avoid attachment id duplication --- src/proxy.rs | 9 ++++++--- src/rgb.rs | 2 +- tests/lightning.rs | 2 +- tests/rgb/web/proxy.rs | 20 ++++++++++++++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/proxy.rs b/src/proxy.rs index 5afa6bec..ee239c3b 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -95,7 +95,9 @@ mod server { let (id, source) = match encode { MediaEncode::Base64 => { let source = base64::encode(&content); - let id = blake3::hash(&content).to_hex().to_string(); + let id = blake3::hash(blake3::hash(&content).as_bytes()) + .to_hex() + .to_string(); (id, source) } MediaEncode::Sha2 => { @@ -105,7 +107,7 @@ mod server { } MediaEncode::Blake3 => { let source = blake3::hash(&content).to_hex().to_string(); - let id = source.clone(); + let id = source.to_string(); (id, source) } }; @@ -113,7 +115,7 @@ mod server { let metadata = MediaMetadata::new(&id, &media.ty, &media.uri, &source); // Store File - let attachment_id = blake3::hash(source.as_bytes()).to_hex().to_lowercase(); + let attachment_id = id; let file_req = RgbProxyMediaFileReq { params: RgbProxyMedia { attachment_id: attachment_id.clone(), @@ -134,6 +136,7 @@ mod server { file_name: metadata_id.clone(), bytes: metadata_content, }; + proxy_media_store(file_req).await?; Ok(metadata) diff --git a/src/rgb.rs b/src/rgb.rs index eea3a0bb..2cb4bebd 100644 --- a/src/rgb.rs +++ b/src/rgb.rs @@ -1987,7 +1987,7 @@ pub async fn finish_auction_offers( iface, buyer_invoice, .. - }) = all_bids.clone().get(0) + }) = all_bids.clone().first() { let all_invoices = all_bids .clone() diff --git a/tests/lightning.rs b/tests/lightning.rs index a72690ae..1bbfcf2e 100644 --- a/tests/lightning.rs +++ b/tests/lightning.rs @@ -109,7 +109,7 @@ pub async fn get_balance_test() -> Result<()> { thread::sleep(time::Duration::from_secs(1)); if let AuthResponse::Result { refresh: _, token } = response { let accounts = get_balance(&token).await?; - let btc_account = accounts.get(0).unwrap(); + let btc_account = accounts.first().unwrap(); assert_eq!(btc_account.balance, "0"); } diff --git a/tests/rgb/web/proxy.rs b/tests/rgb/web/proxy.rs index d5a25ef3..7131037b 100644 --- a/tests/rgb/web/proxy.rs +++ b/tests/rgb/web/proxy.rs @@ -383,3 +383,23 @@ async fn create_uda_with_medias() { let issue_resp: JsValue = resolve(full_issue_contract(issuer_sk.to_string(), issue_req)).await; let issuer_resp: IssueResponse = json_parse(&issue_resp); } + +#[wasm_bindgen_test] +#[allow(unused_assignments)] +async fn import_attachments_into_proxy() { + set_panic_hook(); + info!("Import Attachments (Static Files Media) on the RGB Proxy"); + let media_item = MediaItemRequest { + ty: "image/svg+xml".to_string(), + uri: "https://www.torproject.org/static/images/yec-2023/main-illo.svg".to_string(), + }; + let import_media_req = MediaRequest { + preview: Some(media_item.clone()), + media: Some(media_item.clone()), + attachments: vec![media_item.clone()], + }; + + let import_media_req = serde_wasm_bindgen::to_value(&import_media_req).expect(""); + let import_media_resp: JsValue = resolve(import_uda_data(import_media_req)).await; + let import_media_resp: MediaResponse = json_parse(&import_media_resp); +}