Skip to content

Commit

Permalink
Merge pull request #183 from RGB-WG/layer1
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky authored Oct 30, 2023
2 parents 1a7fbe4 + c0be2d9 commit 13b8ec1
Show file tree
Hide file tree
Showing 15 changed files with 695 additions and 335 deletions.
28 changes: 18 additions & 10 deletions src/contract/assignments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use super::ExposedState;
use crate::contract::seal::GenesisSeal;
use crate::{
AssignmentType, ExposedSeal, GraphSeal, RevealedAttach, RevealedData, RevealedValue,
SecretSeal, StateType, VoidState, LIB_NAME_RGB,
SealDefinition, SecretSeal, StateType, VoidState, LIB_NAME_RGB,
};

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Display, Error)]
Expand Down Expand Up @@ -76,12 +76,15 @@ pub enum Assign<State: ExposedState, Seal: ExposedSeal> {
state: State::Confidential,
},
#[strict_type(tag = 0x03)]
Revealed { seal: Seal, state: State },
Revealed {
seal: SealDefinition<Seal>,
state: State,
},
#[strict_type(tag = 0x02)]
ConfidentialSeal { seal: SecretSeal, state: State },
#[strict_type(tag = 0x01)]
ConfidentialState {
seal: Seal,
seal: SealDefinition<Seal>,
state: State::Confidential,
},
}
Expand Down Expand Up @@ -118,9 +121,11 @@ impl<State: ExposedState, Seal: ExposedSeal> Hash for Assign<State, Seal> {
}

impl<State: ExposedState, Seal: ExposedSeal> Assign<State, Seal> {
pub fn revealed(seal: Seal, state: State) -> Self { Assign::Revealed { seal, state } }
pub fn revealed(seal: SealDefinition<Seal>, state: State) -> Self {
Assign::Revealed { seal, state }
}

pub fn with_seal_replaced(assignment: &Self, seal: Seal) -> Self {
pub fn with_seal_replaced(assignment: &Self, seal: SealDefinition<Seal>) -> Self {
match assignment {
Assign::Confidential { seal: _, state } |
Assign::ConfidentialState { seal: _, state } => Assign::ConfidentialState {
Expand All @@ -145,7 +150,7 @@ impl<State: ExposedState, Seal: ExposedSeal> Assign<State, Seal> {
}
}

pub fn revealed_seal(&self) -> Option<Seal> {
pub fn revealed_seal(&self) -> Option<SealDefinition<Seal>> {
match self {
Assign::Revealed { seal, .. } | Assign::ConfidentialState { seal, .. } => Some(*seal),
Assign::Confidential { .. } | Assign::ConfidentialSeal { .. } => None,
Expand Down Expand Up @@ -175,21 +180,21 @@ impl<State: ExposedState, Seal: ExposedSeal> Assign<State, Seal> {
}
}

pub fn as_revealed(&self) -> Option<(&Seal, &State)> {
pub fn as_revealed(&self) -> Option<(&SealDefinition<Seal>, &State)> {
match self {
Assign::Revealed { seal, state } => Some((seal, state)),
_ => None,
}
}

pub fn to_revealed(&self) -> Option<(Seal, State)> {
pub fn to_revealed(&self) -> Option<(SealDefinition<Seal>, State)> {
match self {
Assign::Revealed { seal, state } => Some((*seal, state.clone())),
_ => None,
}
}

pub fn into_revealed(self) -> Option<(Seal, State)> {
pub fn into_revealed(self) -> Option<(SealDefinition<Seal>, State)> {
match self {
Assign::Revealed { seal, state } => Some((seal, state)),
_ => None,
Expand Down Expand Up @@ -438,7 +443,10 @@ impl<Seal: ExposedSeal> TypedAssigns<Seal> {
/// If seal definition does not exist, returns [`UnknownDataError`]. If the
/// seal is confidential, returns `Ok(None)`; otherwise returns revealed
/// seal data packed as `Ok(Some(`[`Seal`]`))`
pub fn revealed_seal_at(&self, index: u16) -> Result<Option<Seal>, UnknownDataError> {
pub fn revealed_seal_at(
&self,
index: u16,
) -> Result<Option<SealDefinition<Seal>>, UnknownDataError> {
Ok(match self {
TypedAssigns::Declarative(vec) => vec
.get(index as usize)
Expand Down
38 changes: 29 additions & 9 deletions src/contract/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,38 @@ use std::str::FromStr;

use amplify::confinement::{LargeOrdMap, LargeOrdSet, SmallVec, TinyOrdMap};
use amplify::hex;
use bp::seals::txout::TxoSeal;
use bp::{Outpoint, Txid};
use strict_encoding::{StrictDecode, StrictDumb, StrictEncode};

use crate::contract::contract::WitnessOrd::OffChain;
use crate::{
Assign, AssignmentType, Assignments, AssignmentsRef, ContractId, ExposedSeal, ExposedState,
Extension, Genesis, GlobalStateType, OpId, Operation, RevealedAttach, RevealedData,
RevealedValue, SchemaId, SealWitness, SubSchema, Transition, TypedAssigns, VoidState,
LIB_NAME_RGB,
RevealedValue, SchemaId, SealDefinition, SealWitness, SubSchema, Transition, TypedAssigns,
VoidState, LIB_NAME_RGB,
};

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB, tags = custom)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
#[display(inner)]
#[non_exhaustive]
pub enum Output {
#[strict_type(tag = 0x00)]
Bitcoin(Outpoint),
#[strict_type(tag = 0x01)]
Liquid(Outpoint),
#[strict_type(tag = 0x10)]
Abraxas,
#[strict_type(tag = 0x11, dumb)]
Prime,
}

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB)]
Expand Down Expand Up @@ -144,8 +164,8 @@ impl<State: ExposedState> Ord for OutputAssignment<State> {
}

impl<State: ExposedState> OutputAssignment<State> {
pub fn with_witness<Seal: TxoSeal>(
seal: Seal,
pub fn with_witness<Seal: ExposedSeal>(
seal: SealDefinition<Seal>,
witness_txid: Txid,
state: State,
opid: OpId,
Expand All @@ -160,8 +180,8 @@ impl<State: ExposedState> OutputAssignment<State> {
}
}

pub fn with_genesis<Seal: TxoSeal>(
seal: Seal,
pub fn with_genesis<Seal: ExposedSeal>(
seal: SealDefinition<Seal>,
state: State,
opid: OpId,
ty: AssignmentType,
Expand All @@ -177,8 +197,8 @@ impl<State: ExposedState> OutputAssignment<State> {
}
}

pub fn with_extension<Seal: TxoSeal>(
seal: Seal,
pub fn with_extension<Seal: ExposedSeal>(
seal: SealDefinition<Seal>,
state: State,
opid: OpId,
ty: AssignmentType,
Expand Down
55 changes: 54 additions & 1 deletion src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ mod bundle;
#[allow(clippy::module_inception)]
mod contract;

use std::io::Write;

use amplify::confinement::TinyOrdSet;
pub use assignments::{
Assign, AssignAttach, AssignData, AssignFungible, AssignRights, Assignments, AssignmentsRef,
TypedAssigns,
};
pub use attachment::{AttachId, ConcealedAttach, RevealedAttach};
pub use bundle::{BundleId, BundleItem, TransitionBundle};
use commit_verify::CommitEncode;
pub use contract::{
AttachOutput, ContractHistory, ContractState, DataOutput, FungibleOutput, GlobalOrd, Opout,
OpoutParseError, OutputAssignment, RightsOutput, WitnessAnchor, WitnessHeight, WitnessOrd,
Expand All @@ -52,5 +56,54 @@ pub use operations::{
ContractId, Extension, Genesis, Input, Inputs, OpId, OpRef, Operation, Redeemed, Transition,
Valencies,
};
pub use seal::{ExposedSeal, GenesisSeal, GraphSeal, SealWitness, SecretSeal, TxoSeal};
pub use seal::{
ExposedSeal, GenesisSeal, GraphSeal, SealDefinition, SealWitness, SecretSeal, TxoSeal,
};
pub use state::{ConfidentialState, ExposedState, StateCommitment, StateData, StateType};

use crate::Layer1;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display)]
#[display(lowercase)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = super::LIB_NAME_RGB, tags = repr, into_u8, try_from_u8)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
#[repr(u8)]
#[non_exhaustive]
pub enum AltLayer1 {
#[strict_type(dumb)]
Liquid = 1,
// Abraxas = 0x10,
// Prime = 0x11,
}

impl AltLayer1 {
pub fn layer1(&self) -> Layer1 {
match self {
AltLayer1::Liquid => Layer1::Liquid,
}
}
}

#[derive(Wrapper, Clone, PartialEq, Eq, Hash, Debug, From)]
#[wrapper(Deref)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = super::LIB_NAME_RGB)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct AltLayer1Set(TinyOrdSet<AltLayer1>);

impl CommitEncode for AltLayer1Set {
fn commit_encode(&self, e: &mut impl Write) {
for c in self.iter() {
e.write_all(&[*c as u8]).ok();
}
}
}
5 changes: 3 additions & 2 deletions src/contract/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ use strict_encoding::{StrictDeserialize, StrictEncode, StrictSerialize};

use crate::schema::{self, ExtensionType, OpFullType, OpType, SchemaId, TransitionType};
use crate::{
AssignmentType, Assignments, AssignmentsRef, Ffv, GenesisSeal, GlobalState, GraphSeal, Opout,
ReservedByte, TypedAssigns, LIB_NAME_RGB,
AltLayer1Set, AssignmentType, Assignments, AssignmentsRef, Ffv, GenesisSeal, GlobalState,
GraphSeal, Opout, ReservedByte, TypedAssigns, LIB_NAME_RGB,
};

#[derive(Wrapper, WrapperMut, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default, From)]
Expand Down Expand Up @@ -279,6 +279,7 @@ pub struct Genesis {
pub ffv: Ffv,
pub schema_id: SchemaId,
pub chain: Chain,
pub alt_layer1: AltLayer1Set,
pub metadata: SmallBlob,
pub globals: GlobalState,
pub assignments: Assignments<GenesisSeal>,
Expand Down
86 changes: 83 additions & 3 deletions src/contract/seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ pub use bp::seals::txout::blind::{
ChainBlindSeal as GraphSeal, ParseError, SecretSeal, SingleBlindSeal as GenesisSeal,
};
pub use bp::seals::txout::TxoSeal;
use bp::Txid;
use commit_verify::{CommitEncode, Conceal};
use bp::{Outpoint, Txid};
use commit_verify::{strategies, CommitEncode, Conceal};
use strict_encoding::{StrictDecode, StrictDumb, StrictEncode};

use crate::LIB_NAME_RGB;
use crate::{Layer1, LIB_NAME_RGB};

pub trait ExposedSeal:
Debug
Expand All @@ -52,6 +52,86 @@ impl ExposedSeal for GraphSeal {}

impl ExposedSeal for GenesisSeal {}

/*
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", transparent)
)]
pub struct SealPreimage(Bytes32);
*/

#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB, tags = custom, dumb = Self::Bitcoin(strict_dumb!()))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
#[non_exhaustive]
pub enum SealDefinition<U: ExposedSeal> {
#[strict_type(tag = 0x00)]
Bitcoin(U),
#[strict_type(tag = 0x01)]
Liquid(U),
/*
#[strict_type(tag = 0x10)]
Abraxas(SealPreimage),
#[strict_type(tag = 0x11)]
Prime(SealPreimage),
*/
}

impl<U: ExposedSeal> Conceal for SealDefinition<U> {
type Concealed = SecretSeal;

fn conceal(&self) -> Self::Concealed { todo!() }
}

impl<U: ExposedSeal> commit_verify::CommitStrategy for SealDefinition<U> {
type Strategy = strategies::ConcealStrict;
}

impl SealDefinition<GenesisSeal> {
pub fn transmutate(self) -> SealDefinition<GraphSeal> {
match self {
SealDefinition::Bitcoin(seal) => SealDefinition::Bitcoin(seal.transmutate()),
SealDefinition::Liquid(seal) => SealDefinition::Liquid(seal.transmutate()),
/*
SealDefinition::Abraxas(seal) => SealDefinition::Abraxas(seal),
SealDefinition::Prime(seal) => SealDefinition::Prime(seal),
*/
}
}
}

impl<U: ExposedSeal> SealDefinition<U> {
pub fn layer1(self) -> Layer1 {
match self {
SealDefinition::Bitcoin(_) => Layer1::Bitcoin,
SealDefinition::Liquid(_) => Layer1::Liquid,
}
}

#[inline]
pub fn outpoint(self) -> Option<Outpoint> {
match self {
SealDefinition::Bitcoin(seal) | SealDefinition::Liquid(seal) => seal.outpoint(),
}
}

#[inline]
pub fn outpoint_or(self, txid: Txid) -> Outpoint {
match self {
SealDefinition::Bitcoin(seal) | SealDefinition::Liquid(seal) => seal.outpoint_or(txid),
}
}
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Display)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB, tags = custom, dumb = SealWitness::Genesis)]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ pub mod vm;
pub mod stl;

pub mod prelude {
pub use bp::dbc::{Anchor, AnchorId};
pub use bp::dbc::AnchorId;
pub use contract::*;
pub use schema::*;
pub use validation::AnchoredBundle;
pub use validation::{Anchor, AnchoredBundle, Layer1};

use super::*;
pub use super::{schema, vm};
Expand Down
Loading

0 comments on commit 13b8ec1

Please sign in to comment.