Skip to content

Commit

Permalink
chain net in genesis + resolver check
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedberg committed Jan 31, 2025
1 parent 657ab35 commit cefaa1b
Show file tree
Hide file tree
Showing 16 changed files with 536 additions and 296 deletions.
92 changes: 92 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ aluvm = { version = "~0.11.0-beta.9", features = ["std", "ascii-armor"] }
commit_verify = { version = "~0.11.0-beta.9", features = ["rand", "derive"] }
single_use_seals = "~0.11.0-beta.9"
bp-core = { version = "~0.11.0-beta.9" }
bp-std = { version = "~0.11.0-beta.9.1" }
secp256k1 = { version = "0.30.0", features = ["global-context"] }
mime = "~0.3.17"
serde_crate = { package = "serde", version = "1", features = ["derive"], optional = true }
Expand Down
14 changes: 6 additions & 8 deletions src/operation/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ use commit_verify::{
use strict_encoding::StrictDumb;

use crate::{
impl_serde_baid64, Assign, AssignmentType, Assignments, BundleId, ConcealedAttach,
impl_serde_baid64, Assign, AssignmentType, Assignments, BundleId, ChainNet, ConcealedAttach,
ConcealedData, ConcealedState, ConcealedValue, ConfidentialState, DataState, ExposedSeal,
ExposedState, Extension, ExtensionType, Ffv, Genesis, GlobalState, GlobalStateType, Layer1,
Operation, Redeemed, SchemaId, SecretSeal, Transition, TransitionBundle, TransitionType,
TypedAssigns, LIB_NAME_RGB_COMMIT,
ExposedState, Extension, ExtensionType, Ffv, Genesis, GlobalState, GlobalStateType, Operation,
Redeemed, SchemaId, SecretSeal, Transition, TransitionBundle, TransitionType, TypedAssigns,
LIB_NAME_RGB_COMMIT,
};

/// Unique contract identifier equivalent to the contract genesis commitment
Expand Down Expand Up @@ -236,8 +236,7 @@ pub struct BaseCommitment {
pub schema_id: SchemaId,
pub timestamp: i64,
pub issuer: StrictHash,
pub layer1: Layer1,
pub testnet: bool,
pub chain_net: ChainNet,
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
Expand Down Expand Up @@ -279,8 +278,7 @@ impl Genesis {
flags: self.flags,
schema_id: self.schema_id,
timestamp: self.timestamp,
layer1: self.layer1,
testnet: self.testnet,
chain_net: self.chain_net,
issuer: self.issuer.commit_id(),
};
OpCommitment {
Expand Down
126 changes: 126 additions & 0 deletions src/operation/layer1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::str::FromStr;

use bp::BlockHash;
use bpstd::{AddressNetwork, Network};
use strict_encoding::{StrictDecode, StrictEncode, StrictType};

use crate::LIB_NAME_RGB_COMMIT;
Expand All @@ -40,3 +44,125 @@ pub enum Layer1 {
Bitcoin = 0,
Liquid = 1,
}

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[display(inner)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_COMMIT, tags = repr, into_u8, try_from_u8)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
#[repr(u8)]
#[derive(Default)]
pub enum ChainNet {
BitcoinMainnet = 0,
BitcoinTestnet3 = 1,
#[default]
BitcoinTestnet4 = 2,
BitcoinSignet = 3,
BitcoinRegtest = 4,
LiquidMainnet = 5,
LiquidTestnet = 6,
}

impl ChainNet {
pub fn prefix(&self) -> &str {
match self {
ChainNet::BitcoinMainnet => "bc",
ChainNet::BitcoinTestnet3 => "tb3",
ChainNet::BitcoinTestnet4 => "tb4",
ChainNet::BitcoinRegtest => "bcrt",
ChainNet::BitcoinSignet => "sb",
ChainNet::LiquidMainnet => "lq",
ChainNet::LiquidTestnet => "tl",
}
}

pub fn layer1(&self) -> Layer1 {
match self {
ChainNet::BitcoinMainnet
| ChainNet::BitcoinTestnet3
| ChainNet::BitcoinTestnet4
| ChainNet::BitcoinSignet
| ChainNet::BitcoinRegtest => Layer1::Bitcoin,
ChainNet::LiquidMainnet | ChainNet::LiquidTestnet => Layer1::Liquid,
}
}

pub fn address_network(&self) -> AddressNetwork {
match self {
ChainNet::BitcoinMainnet => AddressNetwork::Mainnet,
ChainNet::BitcoinTestnet3 | ChainNet::BitcoinTestnet4 | ChainNet::BitcoinSignet => {
AddressNetwork::Testnet
}
ChainNet::BitcoinRegtest => AddressNetwork::Regtest,
ChainNet::LiquidMainnet => AddressNetwork::Mainnet,
ChainNet::LiquidTestnet => AddressNetwork::Testnet,
}
}

pub fn genesis_block_hash(&self) -> BlockHash {
BlockHash::from_str(match self {
ChainNet::BitcoinMainnet => {
"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
}
ChainNet::BitcoinTestnet3 => {
"000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"
}
ChainNet::BitcoinTestnet4 => {
"00000000da84f2bafbbc53dee25a72ae507ff4914b867c565be350b0da8bf043"
}
ChainNet::BitcoinSignet => {
"00000008819873e925422c1ff0f99f7cc9bbb232af63a077a480a3633bee1ef6"
}
ChainNet::BitcoinRegtest => {
"0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"
}
ChainNet::LiquidMainnet => {
"4f4eac81e5f9f04f5d2a17b03e6726e6a1af69d9c3f00d820f1c82fcb6000000"
}
ChainNet::LiquidTestnet => {
"f9f21a7636b35c12f080ff73fc8bb16bb7c3ceafdc2eb1b673f0ea7a40c00000"
}
})
.unwrap()
}
}

impl From<Network> for ChainNet {
fn from(value: Network) -> Self {
match value {
Network::Mainnet => ChainNet::BitcoinMainnet,
Network::Regtest => ChainNet::BitcoinRegtest,
Network::Signet => ChainNet::BitcoinSignet,
Network::Testnet3 => ChainNet::BitcoinTestnet3,
Network::Testnet4 => ChainNet::BitcoinTestnet4,
}
}
}

#[derive(Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum ChainNetParseError {
/// invalid chain-network pair {0}.
Invalid(String),
}

impl FromStr for ChainNet {
type Err = ChainNetParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase() {
x if ChainNet::BitcoinMainnet.prefix() == x => Ok(ChainNet::BitcoinMainnet),
x if ChainNet::BitcoinRegtest.prefix() == x => Ok(ChainNet::BitcoinRegtest),
x if ChainNet::BitcoinSignet.prefix() == x => Ok(ChainNet::BitcoinSignet),
x if ChainNet::BitcoinTestnet3.prefix() == x => Ok(ChainNet::BitcoinTestnet3),
x if ChainNet::BitcoinTestnet4.prefix() == x => Ok(ChainNet::BitcoinTestnet4),
x if ChainNet::LiquidMainnet.prefix() == x => Ok(ChainNet::LiquidMainnet),
x if ChainNet::LiquidTestnet.prefix() == x => Ok(ChainNet::LiquidTestnet),
_ => Err(ChainNetParseError::Invalid(s.to_owned())),
}
}
}
2 changes: 1 addition & 1 deletion src/operation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub use commit::{
pub use data::{ConcealedData, DataState, RevealedData, VoidState};
pub use fungible::{ConcealedValue, FungibleState, RevealedValue};
pub use global::{GlobalState, GlobalValues};
pub use layer1::Layer1;
pub use layer1::{ChainNet, Layer1};
pub use meta::{MetaValue, Metadata, MetadataError};
pub use operations::{
Extension, Genesis, Identity, Input, Inputs, Operation, Opout, OpoutParseError, Redeemed,
Expand Down
9 changes: 4 additions & 5 deletions src/operation/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ use strict_encoding::{RString, StrictDeserialize, StrictEncode, StrictSerialize}

use crate::schema::{self, ExtensionType, OpFullType, OpType, SchemaId, TransitionType};
use crate::{
Assign, AssignmentIndex, AssignmentType, Assignments, AssignmentsRef, ConcealedAttach,
ConcealedData, ConcealedValue, ContractId, DiscloseHash, ExposedState, Ffv, GenesisSeal,
GlobalState, GraphSeal, Layer1, Metadata, OpDisclose, OpId, SecretSeal, TypedAssigns,
Assign, AssignmentIndex, AssignmentType, Assignments, AssignmentsRef, ChainNet,
ConcealedAttach, ConcealedData, ConcealedValue, ContractId, DiscloseHash, ExposedState, Ffv,
GenesisSeal, GlobalState, GraphSeal, Metadata, OpDisclose, OpId, SecretSeal, TypedAssigns,
VoidState, LIB_NAME_RGB_COMMIT,
};

Expand Down Expand Up @@ -343,8 +343,7 @@ pub struct Genesis {
pub flags: ReservedBytes<1, 0>,
pub timestamp: i64,
pub issuer: Identity,
pub layer1: Layer1,
pub testnet: bool,
pub chain_net: ChainNet,
pub metadata: Metadata,
pub globals: GlobalState,
pub assignments: Assignments<GenesisSeal>,
Expand Down
4 changes: 2 additions & 2 deletions src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ use crate::{

/// Strict types id for the library providing data types for RGB consensus.
pub const LIB_ID_RGB_COMMIT: &str =
"stl:FjjU0$$o-dgf5dmK-ohaEobc-Y6Vz3rp-TqLj3kS-8F7NMQQ#capsule-arthur-quest";
"stl:n4BoS9Kd-oZ1mUgb-6Hqg9hY-q$JXa84-YoWed1a-!6AZCTM#raymond-open-organic";
/// Strict types id for the library providing data types for RGB consensus.
pub const LIB_ID_RGB_LOGIC: &str =
"stl:iLZ2rLhr-oAp7rHh-cX2LO3T-7evttnL-KT6MX!V-m64WEyQ#janet-mineral-phone";
"stl:HffUFU0Z-oNyZXNs-O8u1dRc-Q4Z5mOo-3bqPppu-A0f5iTo#permit-helena-lorenzo";

fn _rgb_commit_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_RGB_COMMIT), tiny_bset! {
Expand Down
Loading

0 comments on commit cefaa1b

Please sign in to comment.