Skip to content

Commit 736d208

Browse files
authored
Merge pull request #110 from RGB-WG/v0.11
Update to use automatically-generated asset tags
2 parents afe2b9f + 4dd7662 commit 736d208

File tree

3 files changed

+19
-36
lines changed

3 files changed

+19
-36
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/interface/builder.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ pub enum BuilderError {
6464
/// state `{0}` provided to the builder has invalid name.
6565
InvalidState(AssignmentType),
6666

67-
/// can't add asset of type `{0}`: you need to register the type with asset
68-
/// type firtst using `add_asset_tag` method.
69-
AssetTagUnknown(AssignmentType),
67+
/// asset tag for the state `{0}` must be added before any fungible state of
68+
/// the same type.
69+
AssetTagSet(AssignmentType),
7070

7171
/// interface doesn't specifies default operation name, thus an explicit
7272
/// operation type must be provided with `set_operation_type` method.
@@ -431,6 +431,10 @@ impl<Seal: ExposedSeal> OperationBuilder<Seal> {
431431
.assignments_type(&name, ty)
432432
.ok_or(BuilderError::AssignmentNotFound(name))?;
433433

434+
if self.fungible.contains_key(&type_id) {
435+
return Err(BuilderError::AssetTagSet(type_id));
436+
}
437+
434438
self.asset_tags.insert(type_id, asset_tag)?;
435439
Ok(self)
436440
}
@@ -481,10 +485,17 @@ impl<Seal: ExposedSeal> OperationBuilder<Seal> {
481485
.assignments_type(&name, ty)
482486
.ok_or(BuilderError::AssignmentNotFound(name))?;
483487

484-
let tag = *self
485-
.asset_tags
486-
.get(&type_id)
487-
.ok_or(BuilderError::AssetTagUnknown(type_id))?;
488+
let tag = match self.asset_tags.get(&type_id) {
489+
Some(asset_tag) => *asset_tag,
490+
None => {
491+
let asset_tag = AssetTag::new_random(
492+
format!("{}/{}", self.schema.schema_id(), self.iface.iface_id()),
493+
type_id,
494+
);
495+
self.asset_tags.insert(type_id, asset_tag)?;
496+
asset_tag
497+
}
498+
};
488499

489500
let state = RevealedValue::new_random_blinding(value, tag);
490501

src/interface/mod.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub mod rgb21;
3232
pub mod rgb25;
3333
mod suppl;
3434

35-
pub use asset_tag_ext::AssetTagExt;
3635
pub use builder::{BuilderError, ContractBuilder, TransitionBuilder};
3736
pub use contract::{
3837
AllocationWitness, ContractIface, FilterExclude, FilterIncludeAll, FungibleAllocation,
@@ -63,30 +62,3 @@ pub enum VerNo {
6362
#[display("v1")]
6463
V1 = 0,
6564
}
66-
67-
// TODO: Move to RGB Core
68-
mod asset_tag_ext {
69-
use std::time::SystemTime;
70-
71-
use amplify::confinement::U8;
72-
use bp::secp256k1::rand::{thread_rng, RngCore};
73-
use commit_verify::{DigestExt, Sha256};
74-
use rgb::{AssetTag, AssignmentType};
75-
76-
pub trait AssetTagExt: Sized {
77-
fn new_random(contract_domain: impl AsRef<str>, assignment_type: AssignmentType) -> Self;
78-
}
79-
80-
impl AssetTagExt for AssetTag {
81-
fn new_random(contract_domain: impl AsRef<str>, assignment_type: AssignmentType) -> Self {
82-
let rand = thread_rng().next_u64();
83-
let timestamp = SystemTime::now().elapsed().expect("system time error");
84-
let mut hasher = Sha256::default();
85-
hasher.input_with_len::<U8>(contract_domain.as_ref().as_bytes());
86-
hasher.input_raw(&assignment_type.to_le_bytes());
87-
hasher.input_raw(&timestamp.as_nanos().to_le_bytes());
88-
hasher.input_raw(&rand.to_le_bytes());
89-
AssetTag::from(hasher.finish())
90-
}
91-
}
92-
}

0 commit comments

Comments
 (0)