Skip to content

Commit

Permalink
Fix error on source mdia (#429)
Browse files Browse the repository at this point in the history
* Fix error on source mdia

* Fix errors

* Fix tests

* Fixing error import

* New fix import

* Update src/structs.rs

Co-authored-by: Armando CD <[email protected]>

* Fix naming conventions

* Fix test problem

* Fixing error not controlled

---------

Co-authored-by: Armando CD <[email protected]>
  • Loading branch information
josediegorobles and crisdut authored Dec 11, 2023
1 parent 929febc commit 8421459
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 47 deletions.
10 changes: 5 additions & 5 deletions lib/web/rgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IssueResponse> =>
JSON.parse(await BMC.issue_contract(nostrHexSk, request));
JSON.parse(await BMC.full_issue_contract(nostrHexSk, request));

export const createInvoice = async (
nostrHexSk: string,
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/bitcoin/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
35 changes: 35 additions & 0 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MediaRequest>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[serde(rename_all = "camelCase")]
#[derive(Validate)]
Expand Down
44 changes: 40 additions & 4 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions tests/rgb/web/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use bitmask_core::{
info,
structs::{
ContractsResponse, DecryptedWalletData, IssueRequest, NextAddressResponse,
ContractsResponse, DecryptedWalletData, FullIssueRequest, NextAddressResponse,
NextUtxoResponse, SecretString, WatcherRequest, WatcherResponse,
},
web::{
Expand All @@ -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,
},
Expand Down Expand Up @@ -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(),
Expand All @@ -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;
Expand Down
24 changes: 9 additions & 15 deletions tests/rgb/web/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
},
Expand Down Expand Up @@ -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(),
Expand All @@ -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)");
Expand Down Expand Up @@ -364,28 +364,22 @@ 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(),
precision,
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);
}
12 changes: 6 additions & 6 deletions tests/rgb/web/swaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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(),
Expand All @@ -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)");
Expand Down
18 changes: 9 additions & 9 deletions tests/rgb/web/transfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
},
Expand Down Expand Up @@ -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(),
Expand All @@ -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)");
Expand Down Expand Up @@ -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(),
Expand All @@ -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)");
Expand Down

0 comments on commit 8421459

Please sign in to comment.