From 5823f65bcdd1fa0c0fdc98e419d7b36cc07ec9d9 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 7 Mar 2024 22:37:00 +0100 Subject: [PATCH 1/5] cli: remove unworking inspection formats --- Cargo.lock | 2 +- Cargo.toml | 2 +- cli/src/command.rs | 37 ++----------------------------------- 3 files changed, 4 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a794b5c..443ae18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1580,7 +1580,7 @@ dependencies = [ [[package]] name = "rgb-core" version = "0.11.0-beta.4" -source = "git+https://github.com/RGB-WG/rgb-core?branch=master#07f07818c6b3b3cdd5e3c692ab3fcc755f0f4c8f" +source = "git+https://github.com/RGB-WG/rgb-core?branch=serde_fix#644c1e49e25ee115c2cdcb260a72aaf97443d662" dependencies = [ "aluvm", "amplify", diff --git a/Cargo.toml b/Cargo.toml index 7cb8dce..23b58b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -100,6 +100,6 @@ features = ["all"] [patch.crates-io] bp-util = { git = "https://github.com/BP-WG/bp-wallet", branch = "master" } bp-wallet = { git = "https://github.com/BP-WG/bp-wallet", branch = "master" } -rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "master" } +rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "serde_fix" } rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "master" } aluvm = { git = "https://github.com/AluVM/rust-aluvm", branch = "master" } diff --git a/cli/src/command.rs b/cli/src/command.rs index 916fb18..9261bef 100644 --- a/cli/src/command.rs +++ b/cli/src/command.rs @@ -43,20 +43,6 @@ use strict_types::StrictVal; use crate::RgbArgs; -// TODO: For now, serde implementation doesn't work for consignments due to -// some of the keys which can't be serialized to strings. Once this fixed, -// allow this inspect formats option -#[derive(ValueEnum, Copy, Clone, Eq, PartialEq, Hash, Debug, Display, Default)] -#[display(lowercase)] -pub enum InspectFormat { - #[default] - Yaml, - Toml, - Json, - Debug, - Contractum, -} - #[derive(Subcommand, Clone, PartialEq, Eq, Debug, Display)] #[display(lowercase)] #[allow(clippy::large_enum_variant)] @@ -243,10 +229,6 @@ pub enum Command { /// Inspects any RGB data file #[display("inspect")] Inspect { - /// Format used for data inspection - #[clap(short, long, default_value = "yaml")] - format: InspectFormat, - /// RGB file to inspect file: PathBuf, }, @@ -779,24 +761,9 @@ impl Exec for RgbArgs { } Some(runtime) } - Command::Inspect { file, format } => { + Command::Inspect { file } => { let bindle = UniversalBindle::load_file(file)?; - // TODO: For now, serde implementation doesn't work for consignments due to - // some of the keys which can't be serialized to strings. Once this fixed, - // allow this inspect formats option - let s = match format { - InspectFormat::Yaml => { - serde_yaml::to_string(&bindle).expect("unable to present as YAML") - } - InspectFormat::Toml => { - toml::to_string(&bindle).expect("unable to present as TOML") - } - InspectFormat::Json => { - serde_json::to_string(&bindle).expect("unable to present as JSON") - } - InspectFormat::Debug => format!("{bindle:#?}"), - InspectFormat::Contractum => todo!("contractum representation"), - }; + let s = serde_yaml::to_string(&bindle).expect("unable to present as YAML"); println!("{s}"); None } From c2e514e8773b575943e1186e244fce2ea8ea41a3 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 7 Mar 2024 22:44:56 +0100 Subject: [PATCH 2/5] cli: make stash dump in YAML --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- cli/src/command.rs | 24 ++++++++++++------------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 443ae18..613d946 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1601,7 +1601,7 @@ dependencies = [ [[package]] name = "rgb-invoice" version = "0.11.0-beta.4" -source = "git+https://github.com/RGB-WG/rgb-std?branch=master#64e511ae20756b6062ad371ecf37624d433f959d" +source = "git+https://github.com/RGB-WG/rgb-std?branch=serde#dffaf9c06c098264d22f70fe5a5fd662c7604c2f" dependencies = [ "amplify", "baid58", @@ -1672,7 +1672,7 @@ dependencies = [ [[package]] name = "rgb-std" version = "0.11.0-beta.4" -source = "git+https://github.com/RGB-WG/rgb-std?branch=master#64e511ae20756b6062ad371ecf37624d433f959d" +source = "git+https://github.com/RGB-WG/rgb-std?branch=serde#dffaf9c06c098264d22f70fe5a5fd662c7604c2f" dependencies = [ "amplify", "baid58", diff --git a/Cargo.toml b/Cargo.toml index 23b58b2..e500ede 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -101,5 +101,5 @@ features = ["all"] bp-util = { git = "https://github.com/BP-WG/bp-wallet", branch = "master" } bp-wallet = { git = "https://github.com/BP-WG/bp-wallet", branch = "master" } rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "serde_fix" } -rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "master" } +rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "serde" } aluvm = { git = "https://github.com/AluVM/rust-aluvm", branch = "master" } diff --git a/cli/src/command.rs b/cli/src/command.rs index 9261bef..9bc062f 100644 --- a/cli/src/command.rs +++ b/cli/src/command.rs @@ -823,8 +823,8 @@ impl Exec for RgbArgs { } for id in runtime.witness_ids()? { fs::write( - format!("{root_dir}/stash/anchors/{id}.debug"), - format!("{:#?}", runtime.anchor(id)?), + format!("{root_dir}/stash/anchors/{id}.yaml"), + serde_yaml::to_string(runtime.anchor(id)?)?, )?; } for id in runtime.extension_ids()? { @@ -845,24 +845,24 @@ impl Exec for RgbArgs { // Index fs::write( - format!("{root_dir}/index/op-to-bundle.debug"), - format!("{:#?}", runtime.debug_bundle_op_index()), + format!("{root_dir}/index/op-to-bundle.yaml"), + serde_yaml::to_string(runtime.debug_bundle_op_index())?, )?; fs::write( - format!("{root_dir}/index/bundle-to-anchor.debug"), - format!("{:#?}", runtime.debug_anchor_bundle_index()), + format!("{root_dir}/index/bundle-to-anchor.yaml"), + serde_yaml::to_string(runtime.debug_anchor_bundle_index())?, )?; fs::write( - format!("{root_dir}/index/contracts.debug"), - format!("{:#?}", runtime.debug_contract_index()), + format!("{root_dir}/index/contracts.yaml"), + serde_yaml::to_string(runtime.debug_contract_index())?, )?; fs::write( - format!("{root_dir}/index/terminals.debug"), - format!("{:#?}", runtime.debug_terminal_index()), + format!("{root_dir}/index/terminals.yaml"), + serde_yaml::to_string(runtime.debug_terminal_index())?, )?; fs::write( - format!("{root_dir}/seal-secret.debug"), - format!("{:#?}", runtime.debug_seal_secrets()), + format!("{root_dir}/seal-secret.yaml"), + serde_yaml::to_string(runtime.debug_seal_secrets())?, )?; eprintln!("Dump is successfully generated and saved to '{root_dir}'"); None From 81d98ae638c1a73b991718526397b233c9e87c72 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 7 Mar 2024 23:16:27 +0100 Subject: [PATCH 3/5] cli: support inspecting consignments in directory format --- cli/src/command.rs | 80 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/cli/src/command.rs b/cli/src/command.rs index 9bc062f..0d68dbb 100644 --- a/cli/src/command.rs +++ b/cli/src/command.rs @@ -24,20 +24,25 @@ use std::fs::File; use std::path::PathBuf; use std::str::FromStr; -use amplify::confinement::U16; +use amplify::confinement::{SmallOrdMap, TinyOrdMap, TinyOrdSet, U16}; use bp_util::{BpCommand, Config, Exec}; use bpstd::Sats; use psbt::{Psbt, PsbtVer}; use rgb_rt::{DescriptorRgb, RgbKeychain, RuntimeError, TransferParams}; -use rgbstd::containers::{Bindle, BuilderSeal, Transfer, UniversalBindle}; +use rgbstd::containers::{ + Bindle, BuilderSeal, ContainerVer, ContentId, ContentSigs, Terminal, Transfer, UniversalBindle, +}; use rgbstd::contract::{ContractId, GenesisSeal, GraphSeal, StateType}; -use rgbstd::interface::{AmountChange, ContractBuilder, FilterExclude, IfaceId, SchemaIfaces}; +use rgbstd::interface::{ + AmountChange, ContractBuilder, ContractSuppl, FilterExclude, IfaceId, SchemaIfaces, +}; use rgbstd::invoice::{Beneficiary, RgbInvoice, RgbInvoiceBuilder, XChainNet}; use rgbstd::persistence::{Inventory, Stash}; use rgbstd::schema::SchemaId; use rgbstd::validation::Validity; -use rgbstd::{OutputSeal, XChain, XOutputSeal}; +use rgbstd::{AssetTag, AssignmentType, BundleId, OutputSeal, XChain, XOutputSeal}; use seals::txout::CloseMethod; +use serde_crate::{Deserialize, Serialize}; use strict_types::encoding::{FieldName, TypeName}; use strict_types::StrictVal; @@ -231,6 +236,13 @@ pub enum Command { Inspect { /// RGB file to inspect file: PathBuf, + + /// Path to save the dumped data. If not given, prints PSBT to STDOUT. + path: Option, + + /// Export using directory format for the compound bundles + #[clap(long, requires("path"))] + dir: bool, }, /// Debug-dump all stash and inventory data @@ -761,10 +773,64 @@ impl Exec for RgbArgs { } Some(runtime) } - Command::Inspect { file } => { + Command::Inspect { file, dir, path } => { + #[derive(Clone, Debug)] + #[derive(Serialize, Deserialize)] + #[serde(crate = "serde_crate", rename_all = "camelCase")] + pub struct ConsignmentInspection { + version: ContainerVer, + transfer: bool, + asset_tags: TinyOrdMap, + terminals: SmallOrdMap, + supplements: TinyOrdSet, + signatures: TinyOrdMap, + } + let bindle = UniversalBindle::load_file(file)?; - let s = serde_yaml::to_string(&bindle).expect("unable to present as YAML"); - println!("{s}"); + let contract = match bindle { + UniversalBindle::Contract(contract) if *dir => Some(contract.into_split()), + UniversalBindle::Transfer(transfer) if *dir => { + let (transfer, sigs) = transfer.into_split(); + Some((transfer.into_contract(), sigs)) + } + bindle => { + let s = serde_yaml::to_string(&bindle).expect("unable to present as YAML"); + match path { + None => println!("{s}"), + Some(path) => fs::write(path, s)?, + } + None + } + }; + if let Some((contract, sigs)) = contract { + let mut map = map![ + s!("genesis") => serde_yaml::to_string(&contract.genesis)?, + s!("schema") => serde_yaml::to_string(&contract.schema)?, + s!("bundles") => serde_yaml::to_string(&contract.bundles)?, + s!("extensions") => serde_yaml::to_string(&contract.extensions)?, + s!("sigs") => serde_yaml::to_string(&sigs)? + ]; + for (_, pair) in contract.ifaces { + map.insert( + format!("iface-{}", pair.iface.name), + serde_yaml::to_string(&pair)?, + ); + } + let contract = ConsignmentInspection { + version: contract.version, + transfer: contract.transfer, + asset_tags: contract.asset_tags, + terminals: contract.terminals, + supplements: contract.supplements, + signatures: contract.signatures, + }; + map.insert(s!("consignment-meta"), serde_yaml::to_string(&contract)?); + let path = path.as_ref().expect("required by clap"); + fs::create_dir_all(path)?; + for (file, value) in map { + fs::write(format!("{}/{file}.yaml", path.display()), value)?; + } + } None } Command::Dump { root_dir } => { From db84558651fd6b6ac03de1a910ed5b791c467b3c Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 7 Mar 2024 23:26:43 +0100 Subject: [PATCH 4/5] cli: export scripts and types on inspection as separate files --- cli/src/command.rs | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/cli/src/command.rs b/cli/src/command.rs index 0d68dbb..ac6ff03 100644 --- a/cli/src/command.rs +++ b/cli/src/command.rs @@ -25,6 +25,7 @@ use std::path::PathBuf; use std::str::FromStr; use amplify::confinement::{SmallOrdMap, TinyOrdMap, TinyOrdSet, U16}; +use baid58::ToBaid58; use bp_util::{BpCommand, Config, Exec}; use bpstd::Sats; use psbt::{Psbt, PsbtVer}; @@ -40,6 +41,7 @@ use rgbstd::invoice::{Beneficiary, RgbInvoice, RgbInvoiceBuilder, XChainNet}; use rgbstd::persistence::{Inventory, Stash}; use rgbstd::schema::SchemaId; use rgbstd::validation::Validity; +use rgbstd::vm::RgbIsa; use rgbstd::{AssetTag, AssignmentType, BundleId, OutputSeal, XChain, XOutputSeal}; use seals::txout::CloseMethod; use serde_crate::{Deserialize, Serialize}; @@ -804,17 +806,36 @@ impl Exec for RgbArgs { }; if let Some((contract, sigs)) = contract { let mut map = map![ - s!("genesis") => serde_yaml::to_string(&contract.genesis)?, - s!("schema") => serde_yaml::to_string(&contract.schema)?, - s!("bundles") => serde_yaml::to_string(&contract.bundles)?, - s!("extensions") => serde_yaml::to_string(&contract.extensions)?, - s!("sigs") => serde_yaml::to_string(&sigs)? + s!("genesis.yaml") => serde_yaml::to_string(&contract.genesis)?, + s!("schema.yaml") => serde_yaml::to_string(&contract.schema)?, + s!("bundles.yaml") => serde_yaml::to_string(&contract.bundles)?, + s!("extensions.yaml") => serde_yaml::to_string(&contract.extensions)?, + s!("sigs.yaml") => serde_yaml::to_string(&sigs)?, + s!("schema-types.sty") => contract.schema.types.to_string(), ]; + for (id, lib) in &contract.schema.script.as_alu_script().libs { + let mut buf = Vec::new(); + lib.print_disassemble::(&mut buf)?; + map.insert(format!("{}.aluasm", id.to_baid58().mnemonic()), unsafe { + String::from_utf8_unchecked(buf) + }); + } for (_, pair) in contract.ifaces { map.insert( - format!("iface-{}", pair.iface.name), + format!("iface-{}.yaml", pair.iface.name), serde_yaml::to_string(&pair)?, ); + map.insert( + format!("iface-{}.sty", pair.iface.name), + pair.iface.types.to_string(), + ); + for (id, lib) in &pair.iimpl.script.as_alu_script().libs { + let mut buf = Vec::new(); + lib.print_disassemble::(&mut buf)?; + map.insert(format!("{}.aluasm", id.to_baid58().mnemonic()), unsafe { + String::from_utf8_unchecked(buf) + }); + } } let contract = ConsignmentInspection { version: contract.version, @@ -824,11 +845,11 @@ impl Exec for RgbArgs { supplements: contract.supplements, signatures: contract.signatures, }; - map.insert(s!("consignment-meta"), serde_yaml::to_string(&contract)?); + map.insert(s!("consignment-meta.yaml"), serde_yaml::to_string(&contract)?); let path = path.as_ref().expect("required by clap"); fs::create_dir_all(path)?; for (file, value) in map { - fs::write(format!("{}/{file}.yaml", path.display()), value)?; + fs::write(format!("{}/{file}", path.display()), value)?; } } None From 2132a798115f3c2b43f322c21723ae58067d5164 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 7 Mar 2024 23:36:53 +0100 Subject: [PATCH 5/5] cli: draft command to reconstruct consignment from YAML --- cli/src/command.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/cli/src/command.rs b/cli/src/command.rs index ac6ff03..aa97551 100644 --- a/cli/src/command.rs +++ b/cli/src/command.rs @@ -24,7 +24,7 @@ use std::fs::File; use std::path::PathBuf; use std::str::FromStr; -use amplify::confinement::{SmallOrdMap, TinyOrdMap, TinyOrdSet, U16}; +use amplify::confinement::{SmallOrdMap, TinyOrdMap, TinyOrdSet, U16 as MAX16, U32 as MAX32}; use baid58::ToBaid58; use bp_util::{BpCommand, Config, Exec}; use bpstd::Sats; @@ -45,7 +45,7 @@ use rgbstd::vm::RgbIsa; use rgbstd::{AssetTag, AssignmentType, BundleId, OutputSeal, XChain, XOutputSeal}; use seals::txout::CloseMethod; use serde_crate::{Deserialize, Serialize}; -use strict_types::encoding::{FieldName, TypeName}; +use strict_types::encoding::{FieldName, StrictSerialize, TypeName}; use strict_types::StrictVal; use crate::RgbArgs; @@ -247,6 +247,18 @@ pub enum Command { dir: bool, }, + /// Reconstructs consignment from a YAML file + #[display("reconstruct")] + #[clap(hide = true)] + Reconstruct { + /// RGB file with the consignment YAML data + src: PathBuf, + + /// Path for the resulting consignment file. If not given, prints the + /// consignment to STDOUT. + dst: Option, + }, + /// Debug-dump all stash and inventory data #[display("dump")] Dump { @@ -559,7 +571,7 @@ impl Exec for RgbArgs { .expect("global type doesn't match type definition"); let serialized = types - .strict_serialize_type::(&typed_val) + .strict_serialize_type::(&typed_val) .expect("internal error"); // Workaround for borrow checker: let field_name = @@ -854,6 +866,17 @@ impl Exec for RgbArgs { } None } + Command::Reconstruct { src, dst } => { + let file = File::open(src)?; + let transfer: Bindle = serde_yaml::from_reader(&file)?; + match dst { + None => println!("{transfer}"), + Some(dst) => { + transfer.strict_serialize_to_file::(dst)?; + } + } + None + } Command::Dump { root_dir } => { let runtime = self.rgb_runtime(&config)?;