Skip to content

Commit

Permalink
seals: add WTxoSeal
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jan 29, 2025
1 parent 12ff9e6 commit 1487568
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 53 deletions.
2 changes: 2 additions & 0 deletions seals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ extern crate commit_verify;
extern crate serde;

mod txout;
mod wtxout;

pub use txout::{
mmb, mpc, Anchor, AnchorError, AnchorMergeError, Noise, TxoSeal, TxoSealError, TxoSealExt,
};
pub use wtxout::{WOutpoint, WTxoSeal};
38 changes: 2 additions & 36 deletions seals/src/txout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ use core::error::Error;
use core::fmt::Debug;

use amplify::{ByteArray, Bytes, Bytes32};
use bc::{Outpoint, Tx, Txid, Vout};
use commit_verify::{
CommitId, ConvolveVerifyError, DigestExt, EmbedVerifyError, ReservedBytes, Sha256, StrictHash,
};
use bc::{Outpoint, Tx, Txid};
use commit_verify::{CommitId, ConvolveVerifyError, EmbedVerifyError, ReservedBytes};
use dbc::opret::{OpretError, OpretProof};
use dbc::tapret::TapretProof;
use single_use_seals::{ClientSideWitness, PublishedWitness, SealWitness, SingleUseSeal};
Expand Down Expand Up @@ -233,44 +231,12 @@ impl StrictDumb for TxoSealExt {
#[display("{primary}/{secondary}")]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = dbc::LIB_NAME_BPCORE)]
#[derive(CommitEncode)]
#[commit_encode(strategy = strict, id = StrictHash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TxoSeal {
pub primary: Outpoint,
pub secondary: TxoSealExt,
}

impl TxoSeal {
/// Creates a new witness output-based seal definition without fallback.
///
/// # Arguments
///
/// `nonce` is a deterministic incremental number, preventing from creating the same seal if the
/// same output is used.
pub fn vout_no_fallback(vout: Vout, noise_engine: Sha256, nonce: u64) -> Self {
Self::no_fallback(Outpoint::new(Txid::from([0xFFu8; 32]), vout), noise_engine, nonce)
}

/// Creates a new witness output-based seal definition without fallback.
///
/// # Arguments
///
/// `nonce` is a deterministic incremental number, preventing from creating the same seal if the
/// same output is used.
pub fn no_fallback(outpoint: Outpoint, mut noise_engine: Sha256, nonce: u64) -> Self {
noise_engine.input_raw(&nonce.to_be_bytes());
noise_engine.input_raw(outpoint.txid.as_ref());
noise_engine.input_raw(&outpoint.vout.to_u32().to_be_bytes());
let mut noise = [0xFFu8; 40];
noise[..32].copy_from_slice(&noise_engine.finish());
Self {
primary: outpoint,
secondary: TxoSealExt::Noise(Noise(noise.into())),
}
}
}

impl SingleUseSeal for TxoSeal {
type Message = mmb::Message;
type PubWitness = Tx;
Expand Down
99 changes: 99 additions & 0 deletions seals/src/wtxout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Bitcoin protocol single-use-seals library.
//
// SPDX-License-Identifier: Apache-2.0
//
// Written in 2019-2024 by
// Dr Maxim Orlovsky <[email protected]>
//
// Copyright (C) 2019-2024 LNP/BP Standards Association. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Witness-output enabled TxO-seals allow constructing graphs of seals, useful in protocols like
//! RGB.
use amplify::Bytes;
use bc::{Outpoint, Vout};
use commit_verify::{DigestExt, Sha256, StrictHash};
use strict_encoding::StrictSum;

use crate::{Noise, TxoSealExt};

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = dbc::LIB_NAME_BPCORE, tags = custom, dumb = Self::Wout(strict_dumb!()))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(untagged))]
pub enum WOutpoint {
#[display("~:{0}")]
#[strict_type(tag = 0)]
Wout(Vout),

#[display(inner)]
#[strict_type(tag = 1)]
Extern(Outpoint),
}

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display)]
#[display("{primary}/{secondary}")]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]

Check warning on line 48 in seals/src/wtxout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/wtxout.rs#L48

Added line #L48 was not covered by tests
#[strict_type(lib = dbc::LIB_NAME_BPCORE)]
#[derive(CommitEncode)]
#[commit_encode(strategy = strict, id = StrictHash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct WTxoSeal {
pub primary: WOutpoint,
pub secondary: TxoSealExt,
}

impl WTxoSeal {
/// Creates a new witness output-based seal definition without fallback.
///
/// # Arguments
///
/// `nonce` is a deterministic incremental number, preventing from creating the same seal if the
/// same output is used.
pub fn vout_no_fallback(vout: Vout, noise_engine: Sha256, nonce: u64) -> Self {
Self::with(WOutpoint::Wout(vout), noise_engine, nonce)
}

Check warning on line 67 in seals/src/wtxout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/wtxout.rs#L65-L67

Added lines #L65 - L67 were not covered by tests

/// Creates a new witness output-based seal definition without fallback.
///
/// # Arguments
///
/// `nonce` is a deterministic incremental number, preventing from creating the same seal if the
/// same output is used.
pub fn no_fallback(outpoint: Outpoint, noise_engine: Sha256, nonce: u64) -> Self {
Self::with(WOutpoint::Extern(outpoint), noise_engine, nonce)
}

Check warning on line 77 in seals/src/wtxout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/wtxout.rs#L75-L77

Added lines #L75 - L77 were not covered by tests

pub fn with(outpoint: WOutpoint, mut noise_engine: Sha256, nonce: u64) -> Self {
noise_engine.input_raw(&nonce.to_be_bytes());
match outpoint {
WOutpoint::Wout(wout) => {
noise_engine.input_raw(&[WOutpoint::ALL_VARIANTS[0].0]);
noise_engine.input_raw(&wout.to_u32().to_be_bytes());
}
WOutpoint::Extern(outpoint) => {
noise_engine.input_raw(&[WOutpoint::ALL_VARIANTS[1].0]);
noise_engine.input_raw(outpoint.txid.as_ref());
noise_engine.input_raw(&outpoint.vout.to_u32().to_be_bytes());
}

Check warning on line 90 in seals/src/wtxout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/wtxout.rs#L79-L90

Added lines #L79 - L90 were not covered by tests
}
let mut noise = [0xFFu8; 40];
noise[..32].copy_from_slice(&noise_engine.finish());
Self {
primary: outpoint,
secondary: TxoSealExt::Noise(Noise::from(Bytes::from(noise))),
}
}

Check warning on line 98 in seals/src/wtxout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/wtxout.rs#L92-L98

Added lines #L92 - L98 were not covered by tests
}
10 changes: 5 additions & 5 deletions src/bin/bpcore-stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ use bc::stl::{bp_consensus_stl, bp_tx_stl};
use bp::stl::bp_core_stl;
use commit_verify::stl::commit_verify_stl;
use commit_verify::CommitmentLayout;
use seals::TxoSeal;
use seals::WTxoSeal;
use strict_encoding::libname;
use strict_types::stl::std_stl;
use strict_types::{parse_args, SystemBuilder};

fn main() {
let (format, dir) = parse_args();

let mut lib = bc::stl::bp_tx_stl();
let mut lib = bp_tx_stl();

Check warning on line 37 in src/bin/bpcore-stl.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/bpcore-stl.rs#L37

Added line #L37 was not covered by tests
lib.name = libname!("Tx");
lib.serialize(
format,
Expand Down Expand Up @@ -103,7 +103,7 @@ fn main() {
writeln!(
file,
"{{-
Description: Bitcoin TxO2 blind seals
Description: Bitcoin WTxO blind seals

Check warning on line 106 in src/bin/bpcore-stl.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/bpcore-stl.rs#L106

Added line #L106 was not covered by tests
Author: Dr Maxim Orlovsky <[email protected]>
Copyright (C) 2024 LNP/BP Standards Association. All rights reserved.
License: Apache-2.0
Expand All @@ -113,9 +113,9 @@ Seals vesper lexicon=types+commitments
"
)
.unwrap();
let layout = TxoSeal::commitment_layout();
let layout = WTxoSeal::commitment_layout();

Check warning on line 116 in src/bin/bpcore-stl.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/bpcore-stl.rs#L116

Added line #L116 was not covered by tests
writeln!(file, "{layout}").unwrap();
let tt = sys.type_tree("BPCore.TxoSeal").unwrap();
let tt = sys.type_tree("BPCore.WTxoSeal").unwrap();

Check warning on line 118 in src/bin/bpcore-stl.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/bpcore-stl.rs#L118

Added line #L118 was not covered by tests
writeln!(file, "{tt}").unwrap();

let tt = sys.type_tree("BPCore.Anchor").unwrap();
Expand Down
3 changes: 2 additions & 1 deletion src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use strict_types::{CompileError, LibBuilder, TypeLib};
/// Strict types id for the library providing data types from [`dbc`] and
/// [`seals`] crates.
pub const LIB_ID_BPCORE: &str =
"stl:5nv1kDfd-gGwz25~-eE2nwLv-eLQqAJr-lmsIL5z-eKSOBzQ#super-vatican-clark";
"stl:IvdK_1TS-X9dXRUt-2BstAqb-cd7P6x9-m0BnaF0-2fewUwI#geneva-pulse-philips";

fn _bp_core_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_BPCORE), tiny_bset! {
Expand All @@ -36,6 +36,7 @@ fn _bp_core_stl() -> Result<TypeLib, CompileError> {
commit_verify::stl::commit_verify_stl().to_dependency()
})
.transpile::<seals::TxoSeal>()
.transpile::<seals::WTxoSeal>()
.transpile::<seals::Anchor>()
.transpile::<seals::mpc::Source>()
.compile()
Expand Down
12 changes: 8 additions & 4 deletions stl/[email protected]
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
-----BEGIN STRICT TYPE LIB-----
Id: stl:5nv1kDfd-gGwz25~-eE2nwLv-eLQqAJr-lmsIL5z-eKSOBzQ#super-vatican-clark
Id: stl:IvdK_1TS-X9dXRUt-2BstAqb-cd7P6x9-m0BnaF0-2fewUwI#geneva-pulse-philips
Name: BPCore
Dependencies:
Std#delete-roman-hair,
Bitcoin#signal-color-cipher,
CommitVerify#uranium-alien-extend
Check-SHA256: fd41811e9b276d695288d11fe89825869362e29fbdb820e264c781f8cf6846d3
Check-SHA256: b650a61864b262f7e0c7f3ab492c42e12f647ac82dd0c4d510f50cba5d78849b

20~CnZ*pY=f{E)*4-0TquXIZV=)u>WBLk*fW6RH_XPEi=Ry;9k15<Ql^=uPjBlbC`N(qzPM@Gr{imSMT
SY5T*7C#t%#3&jH2SRCdV{d70{#(;mQ!wWlp{2syrjnjhHcKcJQ)S9nWc+D5OlRfU3`1{iZE18?WpZg|
Expand All @@ -19,7 +19,7 @@ ZEb0ER%LQ&W_bnx17eyujlDm-l!bcJ4l3enMo5^RQ~n#iGkyWd8~8$g22EvjXm4aNm44<OVKiC01qkHf
uRUrZzt;Qv9WjEZdF4fP;8w8;P;zf{Z)0z4Nn|xQsZkZk>V@1=_p5>Oab-~jCR3C`SFec^=zG+gvC{`l
Wpi_3XJt5^Lxv|61vo|<S$`kJ6oIZx{|tq&1{dNqe!iO(;xh_OWpZn5Wk_LjXiAZsp+z#eDth|e9<pix
0Z%tbcQv3gqtC|PsRy3YlM78{a%*g5P;zf?W|Q||cyL4!ji%3ykI<Y<s&nfxrNA!QlZUt8$DItgdJa-$
b7gXNWn@BmbY*if15<Ql0RU!LaM+Gq(Fu_0Ocz)^+@GUUoV7w&pu=F9->y0X3z7m=H4Oj;L2hGcZ*m3&
b7gXNWn@BmbY*if15<Ql0RU!LaM+Gq(Fu_0Ocz)^+@GUUoV7w&pu=F9->y0X3z7m=H4gv=L2hGcZ*m3&
2yJa*P;zf?W&znIM8u_jdVX-OvRFLBkUzBqEU$^0P_gv`vwdM-APoy`aAQz%Z**^CZ)^hoThmulFy|Se
rNZ2%lAcpGODGgmWy)A&{Ao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
9 changes: 8 additions & 1 deletion stl/[email protected]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-
Id: stl:5nv1kDfd-gGwz25~-eE2nwLv-eLQqAJr-lmsIL5z-eKSOBzQ#super-vatican-clark
Id: stl:IvdK_1TS-X9dXRUt-2BstAqb-cd7P6x9-m0BnaF0-2fewUwI#geneva-pulse-philips
Name: BPCore
Version: 0.1.0
Description: Bitcoin client-side-validation library
Expand Down Expand Up @@ -83,4 +83,11 @@ data TxoSeal : primary Bitcoin.Outpoint, secondary TxoSealExt
data TxoSealExt : noise Noise
| fallback Bitcoin.Outpoint

@mnemonic(clock-absorb-side)
data WOutpoint : wout Bitcoin.Vout
| extern Bitcoin.Outpoint

@mnemonic(rapid-arsenal-shirt)
data WTxoSeal : primary WOutpoint, secondary TxoSealExt


14 changes: 8 additions & 6 deletions stl/Seals.vesper
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-
Description: Bitcoin TxO2 blind seals
Description: Bitcoin WTxO blind seals
Author: Dr Maxim Orlovsky <[email protected]>
Copyright (C) 2024 LNP/BP Standards Association. All rights reserved.
License: Apache-2.0
Expand All @@ -8,12 +8,14 @@
Seals vesper lexicon=types+commitments

commitment StrictHash, hasher SHA256, tagged urn:ubideco:strict-types:value-hash#2024-02-10
serialized TxoSeal
serialized WTxoSeal

rec TxoSeal
rec primary, Outpoint
bytes txid, len 32, aka Txid
is vout, U32, aka Vout
rec WTxoSeal
union primary, WOutpoint
is wout, U32, wrapped, aka Vout, tag 0
rec extern, Outpoint, wrapped, tag 1
bytes txid, len 32, aka Txid
is vout, U32, aka Vout
union secondary, TxoSealExt
bytes noise, len 40, wrapped, aka Noise, tag 0
rec fallback, Outpoint, wrapped, tag 1
Expand Down

0 comments on commit 1487568

Please sign in to comment.