Skip to content

Commit

Permalink
Merge pull request #36 from RGB-WG/v0.11
Browse files Browse the repository at this point in the history
Support for asset tags
  • Loading branch information
dr-orlovsky authored Nov 18, 2023
2 parents b75c516 + bfcb57f commit 52c047a
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 48 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ jobs:
fail-fast: false
matrix:
feature:
- cli
- electrum
- esplora
- log
- serde
steps:
- uses: actions/checkout@v3
- name: Install rust stable
Expand Down
31 changes: 14 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,8 @@ serde = ["serde_crate", "serde_yaml", "bp-std/serde", "bp-wallet/serde",]

[package.metadata.docs.rs]
features = [ "all" ]

[patch.crates-io]
aluvm = { git = "https://github.com/AluVM/rust-aluvm", branch = "v0.11" }
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "v0.11" }
rgb-std = { git = "https://github.com/RGB-WG/rgb-std", branch = "v0.11" }
4 changes: 2 additions & 2 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(clippy::needless_update)] // Caused by the From derivation macro

use bp_util::DescriptorOpts;
use bpstd::XpubDerivable;
use rgb_rt::{DescriptorRgb, Runtime, RuntimeError, TapretKey};
Expand Down Expand Up @@ -53,8 +55,6 @@ impl DescriptorOpts for DescrRgbOpts {
#[command(author, version, about)]
pub struct RgbArgs {
#[clap(flatten)]
#[from]
#[wrap]
pub inner: bp_util::Args<Command, DescrRgbOpts>,
}

Expand Down
47 changes: 40 additions & 7 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashSet;
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;
Expand All @@ -27,13 +28,13 @@ use amplify::confinement::U16;
use bp_util::{Config, Exec};
use bpstd::Txid;
use rgb_rt::{DescriptorRgb, RuntimeError};
use rgbinvoice::{RgbInvoice, RgbTransport};
use rgbinvoice::{InvoiceState, RgbInvoice, RgbTransport};
use rgbstd::containers::{Bindle, Transfer, UniversalBindle};
use rgbstd::contract::{ContractId, GenesisSeal, GraphSeal, StateType};
use rgbstd::interface::{ContractBuilder, FilterExclude, SchemaIfaces, TypedState};
use rgbstd::interface::{AssetTagExt, ContractBuilder, FilterExclude, SchemaIfaces};
use rgbstd::persistence::{Inventory, Stash};
use rgbstd::schema::SchemaId;
use rgbstd::SealDefinition;
use rgbstd::{AssetTag, AssignmentType, SealDefinition};
use seals::txout::{CloseMethod, ExplicitSeal, TxPtr};
use strict_types::encoding::{FieldName, TypeName};
use strict_types::StrictVal;
Expand Down Expand Up @@ -293,7 +294,7 @@ impl Exec for RgbArgs {
.map_err(|err| err.to_string())?;
if let Some(file) = file {
// TODO: handle armored flag
bindle.save(&file)?;
bindle.save(file)?;
eprintln!("Contract {contract} exported to '{}'", file.display());
} else {
println!("{bindle}");
Expand Down Expand Up @@ -375,6 +376,11 @@ impl Exec for RgbArgs {
let code = code
.as_mapping()
.expect("invalid YAML root-level structure");

let contract_domain = code
.get("domain")
.map(|val| val.as_str().expect("domain name must be a string"));

if let Some(globals) = code.get("globals") {
for (name, val) in globals
.as_mapping()
Expand Down Expand Up @@ -419,6 +425,7 @@ impl Exec for RgbArgs {
}
}

let mut asset_tags: HashSet<AssignmentType> = empty!();
if let Some(assignments) = code.get("assignments") {
for (name, val) in assignments
.as_mapping()
Expand Down Expand Up @@ -462,6 +469,24 @@ impl Exec for RgbArgs {
match state_schema.state_type() {
StateType::Void => todo!(),
StateType::Fungible => {
let assignment_type =
iface_impl.assignments_type(&field_name).expect(
"unknown assignment name, which is absent from the \
interface implementation",
);
if !asset_tags.contains(&assignment_type) {
let asset_tag = AssetTag::new_random(
contract_domain.expect(
"when issuing contracts using fungible state it is \
required to provide a contract domain",
),
assignment_type,
);
asset_tags.insert(assignment_type);
builder = builder
.add_asset_tag(field_name.clone(), asset_tag)
.expect("unable to register asset tag");
}
let amount = assign
.get("amount")
.expect("owned state must be a fungible amount")
Expand All @@ -482,7 +507,13 @@ impl Exec for RgbArgs {
let mut resolver = PanickingResolver;
let validated_contract = contract
.validate(&mut resolver, self.general.network.is_testnet())
.map_err(|_| RuntimeError::IncompleteContract)?;
.map_err(|consignment| {
RuntimeError::IncompleteContract(
consignment
.into_validation_status()
.expect("just validated"),
)
})?;
runtime
.import_contract(validated_contract, &mut resolver)
.expect("failure importing issued contract");
Expand All @@ -507,14 +538,15 @@ impl Exec for RgbArgs {
operation: None,
assignment: None,
beneficiary: seal.to_concealed_seal().into(),
owned_state: TypedState::Amount(*value),
owned_state: InvoiceState::Amount(*value),
network: None,
expiry: None,
unknown_query: none!(),
};
runtime.store_seal_secret(SealDefinition::Bitcoin(seal))?;
println!("{invoice}");
}
#[allow(unused_variables)]
Command::Transfer {
method,
psbt_file,
Expand Down Expand Up @@ -564,7 +596,7 @@ impl Exec for RgbArgs {
Command::Dump { root_dir } => {
let runtime = self.rgb_runtime()?;

fs::remove_dir_all(&root_dir).ok();
fs::remove_dir_all(root_dir).ok();
fs::create_dir_all(format!("{root_dir}/stash/schemata"))?;
fs::create_dir_all(format!("{root_dir}/stash/ifaces"))?;
fs::create_dir_all(format!("{root_dir}/stash/geneses"))?;
Expand Down Expand Up @@ -676,6 +708,7 @@ impl Exec for RgbArgs {
runtime.accept_transfer(transfer, &mut resolver, *force)?;
eprintln!("Transfer accepted into the stash");
}
#[allow(unused_variables)]
Command::SetHost { method, psbt_file } => {
todo!();
/*
Expand Down
1 change: 1 addition & 0 deletions examples/rgb20-demo.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
interface: RGB20
domain: examples.lnp-bp.org/rgb-wallet/RGB20

globals:
spec:
Expand Down
7 changes: 3 additions & 4 deletions invoice/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
use std::str::FromStr;

use bpstd::Network;
use rgbstd::interface::TypedState;
use rgbstd::stl::Precision;
use rgbstd::ContractId;

use super::{Beneficiary, RgbInvoice, RgbTransport, TransportParseError};
use super::{Beneficiary, InvoiceState, RgbInvoice, RgbTransport, TransportParseError};

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct RgbInvoiceBuilder(RgbInvoice);
Expand All @@ -41,7 +40,7 @@ impl RgbInvoiceBuilder {
operation: None,
assignment: None,
beneficiary: beneficiary.into(),
owned_state: TypedState::Void,
owned_state: InvoiceState::Void,
network: None,
expiry: None,
unknown_query: none!(),
Expand Down Expand Up @@ -81,7 +80,7 @@ impl RgbInvoiceBuilder {
}

pub fn set_amount_raw(mut self, amount: u64) -> Self {
self.0.owned_state = TypedState::Amount(amount);
self.0.owned_state = InvoiceState::Amount(amount);
self
}

Expand Down
7 changes: 3 additions & 4 deletions invoice/src/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

use bpstd::{Address, Network};
use indexmap::IndexMap;
use rgbstd::interface::TypedState;
use rgbstd::{AttachId, ContractId, SecretSeal};
use strict_encoding::{FieldName, TypeName};

Expand All @@ -38,8 +37,8 @@ pub enum RgbTransport {
pub enum InvoiceState {
#[display("")]
Void,
#[display("{0}.{1}")]
Fungible(u64, u64),
#[display("{0}")]
Amount(u64),
#[display("...")] // TODO
Data(Vec<u8> /* StrictVal */),
#[display(inner)]
Expand All @@ -63,7 +62,7 @@ pub struct RgbInvoice {
pub operation: Option<TypeName>,
pub assignment: Option<FieldName>,
pub beneficiary: Beneficiary,
pub owned_state: TypedState,
pub owned_state: InvoiceState,
pub network: Option<Network>,
/// UTC unix timestamp
pub expiry: Option<i64>,
Expand Down
7 changes: 3 additions & 4 deletions invoice/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ use fluent_uri::enc::EStr;
use fluent_uri::Uri;
use indexmap::IndexMap;
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use rgbstd::interface::TypedState;
use rgbstd::{ContractId, SecretSeal};
use strict_encoding::{InvalidIdent, TypeName};

use super::{Beneficiary, RgbInvoice, RgbTransport};
use super::{Beneficiary, InvoiceState, RgbInvoice, RgbTransport};

const OMITTED: char = '~';
const EXPIRY: &str = "expiry";
Expand Down Expand Up @@ -294,8 +293,8 @@ impl FromStr for RgbInvoice {
let mut assignment = path[next_path_index].split('+');
// TODO: support other state types
let (beneficiary_str, value) = match (assignment.next(), assignment.next()) {
(Some(a), Some(b)) => (b, TypedState::Amount(a.parse::<u64>()?)),
(Some(b), None) => (b, TypedState::Void),
(Some(a), Some(b)) => (b, InvoiceState::Amount(a.parse::<u64>()?)),
(Some(b), None) => (b, InvoiceState::Void),
_ => return Err(InvoiceParseError::Invalid),
};

Expand Down
6 changes: 3 additions & 3 deletions src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub enum DescriptorRgb<S: DeriveSet = XpubDerivable> {
}

impl<S: DeriveSet> Default for DescriptorRgb<S> {
fn default() -> Self { DescriptorRgb::None }
fn default() -> Self { Self::None }
}

impl<S: DeriveSet> Derive<DerivedScript> for DescriptorRgb<S> {
Expand Down Expand Up @@ -171,9 +171,9 @@ where Self: Derive<DerivedScript>

fn xpubs(&self) -> Self::XpubIter<'_> { todo!() }

fn compr_keyset(&self, terminal: Terminal) -> IndexMap<CompressedPk, KeyOrigin> { todo!() }
fn compr_keyset(&self, _terminal: Terminal) -> IndexMap<CompressedPk, KeyOrigin> { todo!() }

fn xonly_keyset(&self, terminal: Terminal) -> IndexMap<XOnlyPk, TapDerivation> { todo!() }
fn xonly_keyset(&self, _terminal: Terminal) -> IndexMap<XOnlyPk, TapDerivation> { todo!() }
}

impl From<DescriptorStd> for DescriptorRgb {
Expand Down
Loading

0 comments on commit 52c047a

Please sign in to comment.