From 84214596c40d6bb9e47e5463d3586ad369ba37d8 Mon Sep 17 00:00:00 2001 From: Jose D Robles Date: Mon, 11 Dec 2023 18:55:55 +0100 Subject: [PATCH] Fix error on source mdia (#429) * Fix error on source mdia * Fix errors * Fix tests * Fixing error import * New fix import * Update src/structs.rs Co-authored-by: Armando CD * Fix naming conventions * Fix test problem * Fixing error not controlled --------- Co-authored-by: Armando CD --- lib/web/rgb.ts | 10 ++++----- src/bitcoin/payment.rs | 6 +++--- src/structs.rs | 35 ++++++++++++++++++++++++++++++ src/web.rs | 44 ++++++++++++++++++++++++++++++++++---- tests/rgb/web/contracts.rs | 10 ++++----- tests/rgb/web/proxy.rs | 24 ++++++++------------- tests/rgb/web/swaps.rs | 12 +++++------ tests/rgb/web/transfers.rs | 18 ++++++++-------- 8 files changed, 112 insertions(+), 47 deletions(-) diff --git a/lib/web/rgb.ts b/lib/web/rgb.ts index 1b915656..71f5189f 100644 --- a/lib/web/rgb.ts +++ b/lib/web/rgb.ts @@ -3,11 +3,11 @@ import * as BMC from "./bitmask_core"; -export const issueContract = async ( +export const fullIssueContract = async ( nostrHexSk: string, - request: IssueRequest + request: FullIssueRequest ): Promise => - JSON.parse(await BMC.issue_contract(nostrHexSk, request)); + JSON.parse(await BMC.full_issue_contract(nostrHexSk, request)); export const createInvoice = async ( nostrHexSk: string, @@ -250,7 +250,7 @@ export interface IssueMetadata { collectible?: NewCollectible[]; } -export interface IssueRequest { +export interface FullIssueRequest { /// The ticker of the asset ticker: string; /// Name of the asset @@ -266,7 +266,7 @@ export interface IssueRequest { /// The name of the iface (ex: RGB20) iface: string; /// contract metadata (only RGB21/UDA) - meta?: IssueMediaRequest; + meta?: MediaRequest; } export interface NewCollectible { diff --git a/src/bitcoin/payment.rs b/src/bitcoin/payment.rs index ea8ac4d4..7e46dcec 100644 --- a/src/bitcoin/payment.rs +++ b/src/bitcoin/payment.rs @@ -93,9 +93,9 @@ pub async fn create_payjoin( .into_iter() .enumerate() .find(|(_, txo)| { - invoices.iter().all(|invoice| { - txo.script_pubkey != invoice.address.script_pubkey() - }) + invoices + .iter() + .all(|invoice| txo.script_pubkey != invoice.address.script_pubkey()) }) .map(|(i, _)| i); diff --git a/src/structs.rs b/src/structs.rs index f4842e19..3be0efd3 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -112,6 +112,41 @@ pub struct IssueAssetRequest { pub request: IssueRequest, } +#[derive(Serialize, Deserialize, Debug, Clone, Default)] +#[serde(rename_all = "camelCase")] +#[derive(Validate)] +#[garde(context(RGBContext))] +pub struct FullIssueRequest { + /// The ticker of the asset + #[garde(ascii)] + #[garde(length(min = 1, max = 8))] + pub ticker: String, + /// Name of the asset + #[garde(ascii)] + #[garde(length(min = 1, max = 40))] + pub name: String, + /// Description of the asset + #[garde(ascii)] + #[garde(length(min = u8::MIN.into(), max = u8::MAX.into()))] + pub description: String, + /// Amount of the asset + #[garde(range(min = u64::MIN, max = u64::MAX))] + pub supply: u64, + /// Precision of the asset + #[garde(range(min = u8::MIN, max = u8::MAX))] + pub precision: u8, + /// Seal of the initial owner + #[garde(ascii)] + #[garde(custom(verify_tapret_seal))] + pub seal: String, + /// The name of the iface (ex: RGB20) + #[garde(alphanumeric)] + pub iface: String, + /// contract metadata (only RGB21/UDA) + #[garde(skip)] + pub meta: Option, +} + #[derive(Serialize, Deserialize, Debug, Clone, Default)] #[serde(rename_all = "camelCase")] #[derive(Validate)] diff --git a/src/web.rs b/src/web.rs index ecea196e..de2d03fd 100644 --- a/src/web.rs +++ b/src/web.rs @@ -7,10 +7,10 @@ use wasm_bindgen_futures::{future_to_promise, JsFuture}; use crate::rgb::structs::ContractAmount; use crate::structs::{ - AcceptRequest, FullRgbTransferRequest, ImportRequest, InvoiceRequest, IssueRequest, - MediaRequest, PsbtRequest, PublishPsbtRequest, ReIssueRequest, RgbBidRequest, RgbOfferRequest, - RgbRemoveTransferRequest, RgbSaveTransferRequest, RgbSwapRequest, RgbTransferRequest, - SecretString, SignPsbtRequest, WatcherRequest, + AcceptRequest, FullIssueRequest, FullRgbTransferRequest, ImportRequest, InvoiceRequest, + IssueMediaRequest, IssueRequest, MediaRequest, PsbtRequest, PublishPsbtRequest, ReIssueRequest, + RgbBidRequest, RgbOfferRequest, RgbRemoveTransferRequest, RgbSaveTransferRequest, + RgbSwapRequest, RgbTransferRequest, SecretString, SignPsbtRequest, WatcherRequest, }; pub fn set_panic_hook() { @@ -391,6 +391,42 @@ pub mod rgb { }) } + #[allow(clippy::too_many_arguments)] + #[wasm_bindgen] + pub fn full_issue_contract(nostr_hex_sk: String, request: JsValue) -> Promise { + set_panic_hook(); + + future_to_promise(async move { + let pre_req: FullIssueRequest = serde_wasm_bindgen::from_value(request).unwrap(); + let media = match pre_req.meta { + Some(media) => { + let media = crate::rgb::import_uda_data(media).await; + match media { + Ok(media) => Some(IssueMediaRequest::from(media)), + Err(err) => return Err(JsValue::from_string(err.to_string())), + } + } + None => None, + }; + let req = IssueRequest { + ticker: pre_req.ticker, + name: pre_req.name, + description: pre_req.description, + supply: pre_req.supply, + precision: pre_req.precision, + seal: pre_req.seal, + iface: pre_req.iface, + meta: media, + }; + match crate::rgb::issue_contract(&nostr_hex_sk, req).await { + Ok(result) => Ok(JsValue::from_string( + serde_json::to_string(&result).unwrap(), + )), + Err(err) => Err(JsValue::from_string(err.to_string())), + } + }) + } + #[allow(clippy::too_many_arguments)] #[wasm_bindgen] pub fn reissue_contract(nostr_hex_sk: String, request: JsValue) -> Promise { diff --git a/tests/rgb/web/contracts.rs b/tests/rgb/web/contracts.rs index c66f763f..2da08775 100644 --- a/tests/rgb/web/contracts.rs +++ b/tests/rgb/web/contracts.rs @@ -5,7 +5,7 @@ use bitmask_core::{ info, structs::{ - ContractsResponse, DecryptedWalletData, IssueRequest, NextAddressResponse, + ContractsResponse, DecryptedWalletData, FullIssueRequest, NextAddressResponse, NextUtxoResponse, SecretString, WatcherRequest, WatcherResponse, }, web::{ @@ -14,8 +14,8 @@ use bitmask_core::{ }, json_parse, resolve, rgb::{ - create_watcher, import_contract, issue_contract, list_contracts, watcher_next_address, - watcher_next_utxo, + create_watcher, full_issue_contract, import_contract, list_contracts, + watcher_next_address, watcher_next_utxo, }, set_panic_hook, }, @@ -93,7 +93,7 @@ async fn allow_issue_and_list_contracts() { let supply = 5; let issue_utxo = next_utxo; let issue_seal = format!("tapret1st:{issue_utxo}"); - let issue_req = IssueRequest { + let issue_req = FullIssueRequest { ticker: "DIBA".to_string(), name: "DIBA".to_string(), description: "DIBA".to_string(), @@ -105,7 +105,7 @@ async fn allow_issue_and_list_contracts() { }; let issue_req = serde_wasm_bindgen::to_value(&issue_req).expect(""); - let issue_resp: JsValue = resolve(issue_contract(sk.to_string(), issue_req)).await; + let issue_resp: JsValue = resolve(full_issue_contract(sk.to_string(), issue_req)).await; info!("List Contracts"); let list_contracts_resp: JsValue = resolve(list_contracts(sk.to_string())).await; diff --git a/tests/rgb/web/proxy.rs b/tests/rgb/web/proxy.rs index 4ebe2baa..d5a25ef3 100644 --- a/tests/rgb/web/proxy.rs +++ b/tests/rgb/web/proxy.rs @@ -12,8 +12,8 @@ use bitmask_core::{ rgb::{prefetch::prefetch_resolver_txs, resolvers::ExplorerResolver}, structs::{ AssetType, BatchRgbTransferResponse, ContractResponse, ContractsResponse, - DecryptedWalletData, FullRgbTransferRequest, FundVaultDetails, ImportRequest, - InvoiceRequest, InvoiceResponse, IssueMediaRequest, IssueRequest, IssueResponse, + DecryptedWalletData, FullIssueRequest, FullRgbTransferRequest, FundVaultDetails, + ImportRequest, InvoiceRequest, InvoiceResponse, IssueMediaRequest, IssueResponse, MediaItemRequest, MediaRequest, MediaResponse, NextAddressResponse, NextUtxoResponse, PsbtFeeRequest, PublishedPsbtResponse, RgbSaveTransferRequest, RgbTransferRequest, RgbTransferResponse, RgbTransferStatusResponse, SecretString, SignPsbtRequest, WalletData, @@ -26,8 +26,8 @@ use bitmask_core::{ }, json_parse, resolve, rgb::{ - create_watcher, full_transfer_asset, get_consignment, get_contract, - import_consignments, import_contract, import_uda_data, issue_contract, list_contracts, + create_watcher, full_issue_contract, full_transfer_asset, get_consignment, + get_contract, import_consignments, import_contract, import_uda_data, list_contracts, psbt_sign_and_publish_file, rgb_create_invoice, save_transfer, verify_transfers, watcher_next_address, watcher_next_utxo, }, @@ -158,7 +158,7 @@ async fn import_and_get_consig_from_proxy() { let precision = 2; let issue_utxo = issuer_next_utxo.utxo.unwrap().outpoint.to_string(); let issue_seal = format!("tapret1st:{issue_utxo}"); - let issue_req = IssueRequest { + let issue_req = FullIssueRequest { ticker: "DIBA".to_string(), name: "DIBA".to_string(), description: "DIBA".to_string(), @@ -170,7 +170,7 @@ async fn import_and_get_consig_from_proxy() { }; let issue_req = serde_wasm_bindgen::to_value(&issue_req).expect(""); - let issue_resp: JsValue = resolve(issue_contract(issuer_sk.to_string(), issue_req)).await; + let issue_resp: JsValue = resolve(full_issue_contract(issuer_sk.to_string(), issue_req)).await; let issuer_resp: IssueResponse = json_parse(&issue_resp); info!("Import Contract (Owner)"); @@ -364,17 +364,11 @@ async fn create_uda_with_medias() { attachments: vec![], }; - let import_media_req = serde_wasm_bindgen::to_value(&import_media_req).expect(""); - let import_media_resp = resolve(import_uda_data(import_media_req)).await; - let issuer_resp: MediaResponse = json_parse(&import_media_resp); - - let media_req = IssueMediaRequest::from(issuer_resp); - let supply = 1; let precision = 0; let issue_utxo = issuer_next_utxo.utxo.unwrap().outpoint.to_string(); let issue_seal = format!("tapret1st:{issue_utxo}"); - let issue_req = IssueRequest { + let issue_req = FullIssueRequest { ticker: "DIBA".to_string(), name: "DIBA".to_string(), description: "DIBA".to_string(), @@ -382,10 +376,10 @@ async fn create_uda_with_medias() { supply, seal: issue_seal.to_owned(), iface: iface.to_string(), - meta: Some(media_req), + meta: Some(import_media_req), }; let issue_req = serde_wasm_bindgen::to_value(&issue_req).expect(""); - let issue_resp: JsValue = resolve(issue_contract(issuer_sk.to_string(), issue_req)).await; + let issue_resp: JsValue = resolve(full_issue_contract(issuer_sk.to_string(), issue_req)).await; let issuer_resp: IssueResponse = json_parse(&issue_resp); } diff --git a/tests/rgb/web/swaps.rs b/tests/rgb/web/swaps.rs index 8667a70c..e071da86 100644 --- a/tests/rgb/web/swaps.rs +++ b/tests/rgb/web/swaps.rs @@ -12,8 +12,8 @@ use bitmask_core::{ rgb::{prefetch::prefetch_resolver_txs, resolvers::ExplorerResolver}, structs::{ AssetType, BatchRgbTransferResponse, ContractResponse, ContractsResponse, - DecryptedWalletData, FullRgbTransferRequest, FundVaultDetails, ImportRequest, - InvoiceRequest, InvoiceResponse, IssueRequest, IssueResponse, NextAddressResponse, + DecryptedWalletData, FullIssueRequest, FullRgbTransferRequest, FundVaultDetails, + ImportRequest, InvoiceRequest, InvoiceResponse, IssueResponse, NextAddressResponse, NextUtxoResponse, PsbtFeeRequest, PublishedPsbtResponse, RgbBidRequest, RgbBidResponse, RgbOfferRequest, RgbOfferResponse, RgbSaveTransferRequest, RgbSwapRequest, RgbSwapResponse, RgbTransferRequest, RgbTransferResponse, RgbTransferStatusResponse, SecretString, @@ -26,8 +26,8 @@ use bitmask_core::{ }, json_parse, resolve, rgb::{ - create_bid, create_offer, create_swap, create_watcher, full_transfer_asset, - get_contract, import_contract, issue_contract, list_contracts, my_bids, my_offers, + create_bid, create_offer, create_swap, create_watcher, full_issue_contract, + full_transfer_asset, get_contract, import_contract, list_contracts, my_bids, my_offers, my_orders, psbt_sign_and_publish_file, psbt_sign_file, public_offers, rgb_create_invoice, save_transfer, verify_transfers, watcher_next_address, watcher_next_utxo, @@ -180,7 +180,7 @@ async fn create_transfer_swap_flow() { let precision = 2; let issue_utxo = issuer_next_utxo.utxo.unwrap().outpoint.to_string(); let issue_seal = format!("tapret1st:{issue_utxo}"); - let issue_req = IssueRequest { + let issue_req = FullIssueRequest { ticker: "DIBA".to_string(), name: "DIBA".to_string(), description: "DIBA".to_string(), @@ -192,7 +192,7 @@ async fn create_transfer_swap_flow() { }; let issue_req = serde_wasm_bindgen::to_value(&issue_req).expect(""); - let issue_resp: JsValue = resolve(issue_contract(issuer_sk.to_string(), issue_req)).await; + let issue_resp: JsValue = resolve(full_issue_contract(issuer_sk.to_string(), issue_req)).await; let issuer_resp: IssueResponse = json_parse(&issue_resp); info!("Import Contract (Owner)"); diff --git a/tests/rgb/web/transfers.rs b/tests/rgb/web/transfers.rs index 1abd2780..c7e28021 100644 --- a/tests/rgb/web/transfers.rs +++ b/tests/rgb/web/transfers.rs @@ -15,8 +15,8 @@ use bitmask_core::{ rgb::{prefetch::prefetch_resolver_txs, resolvers::ExplorerResolver}, structs::{ AssetType, BatchRgbTransferResponse, ContractResponse, ContractsResponse, - DecryptedWalletData, FullRgbTransferRequest, FundVaultDetails, ImportRequest, - InvoiceRequest, InvoiceResponse, IssueRequest, IssueResponse, NextAddressResponse, + DecryptedWalletData, FullIssueRequest, FullRgbTransferRequest, FundVaultDetails, + ImportRequest, InvoiceRequest, InvoiceResponse, IssueResponse, NextAddressResponse, NextUtxoResponse, PsbtFeeRequest, PublishedPsbtResponse, RgbSaveTransferRequest, RgbTransferRequest, RgbTransferResponse, RgbTransferStatusResponse, SecretString, SignPsbtRequest, WalletData, WatcherRequest, WatcherResponse, @@ -28,9 +28,9 @@ use bitmask_core::{ }, json_parse, resolve, rgb::{ - create_watcher, full_transfer_asset, get_contract, import_contract, issue_contract, - list_contracts, psbt_sign_and_publish_file, rgb_create_invoice, save_transfer, - verify_transfers, watcher_next_address, watcher_next_utxo, + create_watcher, full_issue_contract, full_transfer_asset, get_contract, + import_contract, list_contracts, psbt_sign_and_publish_file, rgb_create_invoice, + save_transfer, verify_transfers, watcher_next_address, watcher_next_utxo, }, set_panic_hook, }, @@ -155,7 +155,7 @@ async fn create_transfer_with_fee_value() { let precision = 2; let issue_utxo = issuer_next_utxo.utxo.unwrap().outpoint.to_string(); let issue_seal = format!("tapret1st:{issue_utxo}"); - let issue_req = IssueRequest { + let issue_req = FullIssueRequest { ticker: "DIBA".to_string(), name: "DIBA".to_string(), description: "DIBA".to_string(), @@ -167,7 +167,7 @@ async fn create_transfer_with_fee_value() { }; let issue_req = serde_wasm_bindgen::to_value(&issue_req).expect(""); - let issue_resp: JsValue = resolve(issue_contract(issuer_sk.to_string(), issue_req)).await; + let issue_resp: JsValue = resolve(full_issue_contract(issuer_sk.to_string(), issue_req)).await; let issuer_resp: IssueResponse = json_parse(&issue_resp); info!("Import Contract (Owner)"); @@ -460,7 +460,7 @@ async fn create_transfer_with_fee_rate() { let precision = 2; let issue_utxo = issuer_next_utxo.utxo.unwrap().outpoint.to_string(); let issue_seal = format!("tapret1st:{issue_utxo}"); - let issue_req = IssueRequest { + let issue_req = FullIssueRequest { ticker: "DIBA".to_string(), name: "DIBA".to_string(), description: "DIBA".to_string(), @@ -472,7 +472,7 @@ async fn create_transfer_with_fee_rate() { }; let issue_req = serde_wasm_bindgen::to_value(&issue_req).expect(""); - let issue_resp: JsValue = resolve(issue_contract(issuer_sk.to_string(), issue_req)).await; + let issue_resp: JsValue = resolve(full_issue_contract(issuer_sk.to_string(), issue_req)).await; let issuer_resp: IssueResponse = json_parse(&issue_resp); info!("Import Contract (Owner)");