Skip to content

Cross-chain secret seals. Better and more stable cross-chain APIs #199

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

Merged
merged 11 commits into from
Jan 6, 2024
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
42 changes: 21 additions & 21 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rgb-core"
version = "0.11.0-beta.3"
version = "0.11.0-beta.4"
authors = ["Dr Maxim Orlovsky <[email protected]>"]
description = "RGB Core Library: confidential & scalable smart contracts on Bitcoin & Lightning (consensus layer)"
repository = "https://github.com/RGB-WG/rgb-core"
Expand All @@ -23,12 +23,12 @@ required-features = ["stl"]

[dependencies]
amplify = { version = "~4.5.0", features = ["rand"] }
strict_encoding = "~2.6.1"
strict_encoding = "~2.6.2"
strict_types = "~1.6.3"
aluvm = { version = "~0.11.0-beta.2", features = ["std"] }
commit_verify = { version = "~0.11.0-beta.2", features = ["rand", "derive"] }
single_use_seals = "~0.11.0-beta.2"
bp-core = { version = "~0.11.0-beta.2" }
commit_verify = { version = "~0.11.0-beta.3", features = ["rand", "derive"] }
single_use_seals = "~0.11.0-beta.3"
bp-core = { version = "~0.11.0-beta.3" }
secp256k1-zkp = { version = "0.9.2", features = ["rand", "rand-std", "global-context"] } # TODO: Update version before the release
baid58 = "~0.4.4"
mime = "~0.3.17"
Expand Down
50 changes: 8 additions & 42 deletions src/contract/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use bp::Txid;
use commit_verify::mpc;
use strict_encoding::StrictDumb;

use crate::{BundleId, ContractId, TransitionBundle, WitnessId, WitnessOrd, LIB_NAME_RGB};
use crate::{BundleId, ContractId, TransitionBundle, WitnessId, WitnessOrd, XChain, LIB_NAME_RGB};

#[derive(Clone, Eq, PartialEq, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
Expand All @@ -49,48 +49,14 @@ impl AnchoredBundle {
pub fn bundle_id(&self) -> BundleId { self.bundle.bundle_id() }
}

#[derive(Clone, Eq, PartialEq, 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")
)]
pub enum XAnchor<P: mpc::Proof + StrictDumb = mpc::MerkleProof> {
#[strict_type(tag = 0x00)]
Bitcoin(AnchorSet<P>),

#[strict_type(tag = 0x01)]
Liquid(AnchorSet<P>),
}
pub type XAnchor<P = mpc::MerkleProof> = XChain<AnchorSet<P>>;

impl<P: mpc::Proof + StrictDumb> XAnchor<P> {
#[inline]
pub fn layer1(&self) -> Layer1 {
match self {
XAnchor::Bitcoin(_) => Layer1::Bitcoin,
XAnchor::Liquid(_) => Layer1::Liquid,
}
}
pub fn witness_id(&self) -> Option<WitnessId> { self.maybe_map_ref(|set| set.txid()) }

#[inline]
pub fn witness_id(&self) -> WitnessId {
match self {
XAnchor::Bitcoin(anchor) => WitnessId::Bitcoin(anchor.txid_unchecked()),
XAnchor::Liquid(anchor) => WitnessId::Liquid(anchor.txid_unchecked()),
}
}

fn map<Q: mpc::Proof + StrictDumb, E>(
self,
f: impl FnOnce(AnchorSet<P>) -> Result<AnchorSet<Q>, E>,
) -> Result<XAnchor<Q>, E> {
match self {
XAnchor::Bitcoin(anchor) => f(anchor).map(XAnchor::Bitcoin),
XAnchor::Liquid(anchor) => f(anchor).map(XAnchor::Liquid),
}
}
pub fn witness_id_unchecked(&self) -> WitnessId { self.map_ref(|set| set.txid_unchecked()) }
}

impl XAnchor<mpc::MerkleBlock> {
Expand All @@ -111,7 +77,7 @@ impl XAnchor<mpc::MerkleBlock> {
self,
contract_id: ContractId,
) -> Result<XAnchor<mpc::MerkleProof>, mpc::LeafNotKnown> {
self.map(|a| a.into_merkle_proof(contract_id))
self.try_map(|a| a.into_merkle_proof(contract_id))
}
}

Expand All @@ -129,7 +95,7 @@ impl XAnchor<mpc::MerkleProof> {
contract_id: ContractId,
bundle_id: BundleId,
) -> Result<XAnchor<mpc::MerkleBlock>, mpc::InvalidProof> {
self.map(|a| a.into_merkle_block(contract_id, bundle_id))
self.try_map(|a| a.into_merkle_block(contract_id, bundle_id))
}
}

Expand Down Expand Up @@ -163,11 +129,11 @@ impl<P: mpc::Proof + StrictDumb> AnchorSet<P> {
}
}

pub(crate) fn txid_unchecked(&self) -> Txid {
pub fn txid_unchecked(&self) -> Txid {
match self {
AnchorSet::Tapret(a) => a.txid,
AnchorSet::Opret(a) => a.txid,
AnchorSet::Dual { tapret, .. } => tapret.txid,
AnchorSet::Dual { tapret, opret: _ } => tapret.txid,
}
}

Expand Down
35 changes: 19 additions & 16 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, XSeal, LIB_NAME_RGB,
SecretSeal, StateType, VoidState, XChain, LIB_NAME_RGB,
};

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Display, Error)]
Expand Down Expand Up @@ -72,16 +72,19 @@ pub type AssignAttach<Seal> = Assign<RevealedAttach, Seal>;
pub enum Assign<State: ExposedState, Seal: ExposedSeal> {
#[strict_type(tag = 0x00)]
Confidential {
seal: SecretSeal,
seal: XChain<SecretSeal>,
state: State::Confidential,
},
#[strict_type(tag = 0x03)]
Revealed { seal: XSeal<Seal>, state: State },
Revealed { seal: XChain<Seal>, state: State },
#[strict_type(tag = 0x02)]
ConfidentialSeal { seal: SecretSeal, state: State },
ConfidentialSeal {
seal: XChain<SecretSeal>,
state: State,
},
#[strict_type(tag = 0x01)]
ConfidentialState {
seal: XSeal<Seal>,
seal: XChain<Seal>,
state: State::Confidential,
},
}
Expand Down Expand Up @@ -118,9 +121,9 @@ impl<State: ExposedState, Seal: ExposedSeal> Hash for Assign<State, Seal> {
}

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

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

pub fn to_confidential_seal(&self) -> SecretSeal {
pub fn to_confidential_seal(&self) -> XChain<SecretSeal> {
match self {
Assign::Revealed { seal, .. } | Assign::ConfidentialState { seal, .. } => {
seal.conceal()
Expand All @@ -145,7 +148,7 @@ impl<State: ExposedState, Seal: ExposedSeal> Assign<State, Seal> {
}
}

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

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

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

pub fn into_revealed(self) -> Option<(XSeal<Seal>, State)> {
pub fn into_revealed(self) -> Option<(XChain<Seal>, State)> {
match self {
Assign::Revealed { seal, state } => Some((seal, state)),
_ => None,
Expand Down Expand Up @@ -240,11 +243,11 @@ where Self: Clone
state.commit_encode(e);
}
Assign::ConfidentialState { seal, state } => {
seal.commit_encode(e);
seal.conceal().commit_encode(e);
state.commit_encode(e);
}
Assign::Revealed { seal, state } => {
seal.commit_encode(e);
seal.conceal().commit_encode(e);
state.commit_encode(e);
}
Assign::ConfidentialSeal { seal, state } => {
Expand Down Expand Up @@ -445,7 +448,7 @@ 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<XSeal<Seal>>, UnknownDataError> {
pub fn revealed_seal_at(&self, index: u16) -> Result<Option<XChain<Seal>>, UnknownDataError> {
Ok(match self {
TypedAssigns::Declarative(vec) => vec
.get(index as usize)
Expand All @@ -466,7 +469,7 @@ impl<Seal: ExposedSeal> TypedAssigns<Seal> {
})
}

pub fn to_confidential_seals(&self) -> Vec<SecretSeal> {
pub fn to_confidential_seals(&self) -> Vec<XChain<SecretSeal>> {
match self {
TypedAssigns::Declarative(s) => s
.iter()
Expand Down
Loading