Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RGB "decentralized geneses": contract unowned extensions & valencies implementation #112

Merged
merged 13 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions src/bp/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,14 +710,14 @@ impl From<ChainParams> for Chain {
match params {
p if p == Chain::Mainnet.chain_params() => Chain::Mainnet,
p if p == Chain::Testnet3.chain_params() => Chain::Testnet3,
p if p == Chain::Signet.chain_params() => Chain::Signet,
p if p == Chain::LiquidV1.chain_params() => Chain::LiquidV1,
p if p == Chain::Regtest(p.genesis_hash).chain_params() => {
Chain::Regtest(p.genesis_hash)
}
p if p == Chain::Signet.chain_params() => Chain::Signet,
p if p == Chain::SignetCustom(p.genesis_hash).chain_params() => {
Chain::SignetCustom(p.genesis_hash)
}
p if p == Chain::LiquidV1.chain_params() => Chain::LiquidV1,
p => Chain::Other(p),
}
}
Expand Down Expand Up @@ -1107,6 +1107,67 @@ mod test {
assert_ne!(Chain::Signet, Chain::SignetCustom(BlockHash::hash(b"")));
}

#[test]
fn test_chain_encode() {
const DATA_CHAINPARAMS_MAINNET: [u8; 142] = [
0x6f, 0xe2, 0x8c, 0xa, 0xb6, 0xf1, 0xb3, 0x72, 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63,
0xf7, 0x4f, 0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x8, 0x9c, 0x68, 0xd6, 0x19, 0x0, 0x0,
0x0, 0x0, 0x0, 0x7, 0x0, 0x62, 0x69, 0x74, 0x63, 0x6f, 0x69, 0x6e, 0xf9, 0xbe, 0xb4,
0xd9, 0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80, 0xa4, 0x77, 0xa0, 0x63, 0xaf, 0x32,
0xb2, 0xbb, 0xc9, 0x7c, 0x9f, 0xf9, 0xf0, 0x1f, 0x2c, 0x42, 0x25, 0xe9, 0x73, 0x98,
0x81, 0x8, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0,
];
const DATA_CHAINPARAMS_LIQUID: [u8; 162] = [
0x14, 0x66, 0x27, 0x58, 0x36, 0x22, 0xd, 0xb2, 0x94, 0x4c, 0xa0, 0x59, 0xa3, 0xa1, 0xe,
0xf6, 0xfd, 0x2e, 0xa6, 0x84, 0xb0, 0x68, 0x8d, 0x2c, 0x37, 0x92, 0x96, 0x88, 0x8a,
0x20, 0x60, 0x3, 0x8, 0x0, 0x6c, 0x69, 0x71, 0x75, 0x69, 0x64, 0x76, 0x31, 0xf9, 0xbe,
0xb4, 0xd9, 0x8, 0x0, 0x6c, 0x69, 0x71, 0x75, 0x69, 0x64, 0x76, 0x31, 0x2, 0x0, 0x65,
0x78, 0x82, 0x1b, 0x81, 0x1b, 0x1, 0x0, 0x0, 0x0, 0x40, 0x42, 0xf, 0x0, 0x1, 0x22, 0x2,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x4c, 0x42, 0x54, 0x43, 0xe, 0x0, 0x4c, 0x69,
0x71, 0x75, 0x69, 0x64, 0x20, 0x42, 0x69, 0x74, 0x63, 0x6f, 0x69, 0x6e, 0xe, 0x0, 0x4c,
0x69, 0x71, 0x75, 0x69, 0x64, 0x20, 0x73, 0x61, 0x74, 0x6f, 0x73, 0x68, 0x69, 0x0,
0xe1, 0xf5, 0x5, 0x0, 0x0, 0x0, 0x0, 0x14, 0x66, 0x27, 0x58, 0x36, 0x22, 0xd, 0xb2,
0x94, 0x4c, 0xa0, 0x59, 0xa3, 0xa1, 0xe, 0xf6, 0xfd, 0x2e, 0xa6, 0x84, 0xb0, 0x68,
0x8d, 0x2c, 0x37, 0x92, 0x96, 0x88, 0x8a, 0x20, 0x60, 0x3, 0x0, 0x0, 0x0,
];

test_encode!(
(DATA_CHAINPARAMS_MAINNET, Chain),
(DATA_CHAINPARAMS_TESTNET, Chain),
(DATA_CHAINPARAMS_SIGNET, Chain),
(DATA_CHAINPARAMS_LIQUID, Chain)
);
}

#[test]
fn test_chain_genesis_hashes() {
assert_eq!(
Expand Down
50 changes: 37 additions & 13 deletions src/rgb/bech32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

use ::bech32::{self, FromBase32, ToBase32};
use ::core::fmt::{Display, Formatter};
use ::core::str::{pattern::Pattern, FromStr};
use ::core::str::FromStr;

use crate::rgb::{seal, Anchor, ContractId, Disclosure, Genesis, Schema, SchemaId, Transition};
use crate::rgb::{
seal, Anchor, ContractId, Disclosure, Extension, Genesis, Schema, SchemaId, Transition,
};
use crate::strict_encoding::{self, strict_decode, strict_encode};

#[derive(Clone, Debug)]
Expand All @@ -25,22 +27,25 @@ pub enum Bech32 {
Schema(Schema),
SchemaId(SchemaId),
Genesis(Genesis),
Extension(Extension),
Transition(Transition),
Anchor(Anchor),
Disclosure(Disclosure),
Other(String, Vec<u8>),
}

impl Bech32 {
pub const HRP: &'static str = "rgb";
pub const HRP_CONTRACT_ID: &'static str = "rgb";
pub const HRP_SCHEMA_ID: &'static str = "sch";

pub const HRP_OUTPOINT: &'static str = "txo";
pub const HRP_ID: &'static str = "rgb";
pub const HRP_SCHEMA: &'static str = "schema_data";
pub const HRP_SCHEMA_ID: &'static str = "schema";

pub const HRP_SCHEMA: &'static str = "schema";
pub const HRP_GENESIS: &'static str = "genesis";
pub const HRP_TRANSITION: &'static str = "rgb_ts";
pub const HRP_ANCHOR: &'static str = "rgb_anc";
pub const HRP_DISCLOSURE: &'static str = "rgb_disclosure";
pub const HRP_EXTENSION: &'static str = "statex";
pub const HRP_TRANSITION: &'static str = "transition";
pub const HRP_ANCHOR: &'static str = "anchor";
pub const HRP_DISCLOSURE: &'static str = "disclosure";
}

pub trait ToBech32 {
Expand Down Expand Up @@ -73,12 +78,19 @@ impl ToBech32 for SchemaId {
Bech32::SchemaId(self.clone())
}
}

impl ToBech32 for Genesis {
fn to_bech32(&self) -> Bech32 {
Bech32::Genesis(self.clone())
}
}

impl ToBech32 for Extension {
fn to_bech32(&self) -> Bech32 {
Bech32::Extension(self.clone())
}
}

impl ToBech32 for Transition {
fn to_bech32(&self) -> Bech32 {
Bech32::Transition(self.clone())
Expand Down Expand Up @@ -125,15 +137,15 @@ impl FromStr for Bech32 {

Ok(match hrp {
x if x == Self::HRP_OUTPOINT => Self::Outpoint(strict_decode(&data)?),
x if x == Self::HRP_ID => Self::ContractId(strict_decode(&data)?),
x if x == Self::HRP_CONTRACT_ID => Self::ContractId(strict_decode(&data)?),
x if x == Self::HRP_SCHEMA => Self::Schema(strict_decode(&data)?),
x if x == Self::HRP_SCHEMA_ID => Self::SchemaId(strict_decode(&data)?),
x if x == Self::HRP_GENESIS => Self::Genesis(strict_decode(&data)?),
x if x == Self::HRP_EXTENSION => Self::Extension(strict_decode(&data)?),
x if x == Self::HRP_TRANSITION => Self::Transition(strict_decode(&data)?),
x if x == Self::HRP_ANCHOR => Self::Anchor(strict_decode(&data)?),
x if x == Self::HRP_DISCLOSURE => Self::Disclosure(strict_decode(&data)?),
other if Self::HRP.is_prefix_of(&other) => Self::Other(other, data),
other => Err(Error::WrongHrp(other))?,
other => Self::Other(other, data),
})
}
}
Expand All @@ -142,10 +154,11 @@ impl Display for Bech32 {
fn fmt(&self, f: &mut Formatter<'_>) -> ::core::fmt::Result {
let (hrp, data) = match self {
Self::Outpoint(obj) => (Self::HRP_OUTPOINT, strict_encode(obj)),
Self::ContractId(obj) => (Self::HRP_ID, strict_encode(obj)),
Self::ContractId(obj) => (Self::HRP_CONTRACT_ID, strict_encode(obj)),
Self::Schema(obj) => (Self::HRP_SCHEMA, strict_encode(obj)),
Self::SchemaId(obj) => (Self::HRP_SCHEMA_ID, strict_encode(obj)),
Self::Genesis(obj) => (Self::HRP_GENESIS, strict_encode(obj)),
Self::Extension(obj) => (Self::HRP_EXTENSION, strict_encode(obj)),
Self::Transition(obj) => (Self::HRP_TRANSITION, strict_encode(obj)),
Self::Anchor(obj) => (Self::HRP_ANCHOR, strict_encode(obj)),
Self::Disclosure(obj) => (Self::HRP_DISCLOSURE, strict_encode(obj)),
Expand Down Expand Up @@ -179,6 +192,17 @@ impl FromStr for Genesis {
}
}

impl FromStr for Extension {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match Bech32::from_str(s)? {
Bech32::Extension(obj) => Ok(obj),
_ => Err(Error::WrongType),
}
}
}

impl FromStr for Transition {
type Err = Error;

Expand Down
2 changes: 1 addition & 1 deletion src/rgb/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use assignments::{
};
pub use conceal::AutoConceal;
pub use field::{FieldData, Metadata};
pub use nodes::{ContractId, Genesis, Node, NodeId, Transition};
pub use nodes::{ContractId, Extension, Genesis, Node, NodeId, Transition};
pub use seal::SealDefinition;

use secp256k1zkp::Secp256k1 as Secp256k1zkp;
Expand Down
Loading