From d3aaae6bc1125184d291f51d4e495a4fbf999fa9 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Wed, 11 Dec 2024 11:16:46 +0100 Subject: [PATCH] seals: add TxoSeal constructors --- consensus/src/tx.rs | 1 + seals/src/txout.rs | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/consensus/src/tx.rs b/consensus/src/tx.rs index 4be298cc..d3590734 100644 --- a/consensus/src/tx.rs +++ b/consensus/src/tx.rs @@ -36,6 +36,7 @@ use crate::{ }; #[derive(Wrapper, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, From)] +#[wrapper(AsSlice)] #[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] #[strict_type(lib = LIB_NAME_BITCOIN)] #[cfg_attr( diff --git a/seals/src/txout.rs b/seals/src/txout.rs index aed66f6c..abd32847 100644 --- a/seals/src/txout.rs +++ b/seals/src/txout.rs @@ -28,8 +28,8 @@ use std::cmp::Ordering; use amplify::confinement::TinyOrdMap; use amplify::{ByteArray, Bytes, Bytes32}; -use bc::{Outpoint, Tx, Txid}; -use commit_verify::{mpc, CommitId, ReservedBytes, StrictHash}; +use bc::{Outpoint, Tx, Txid, Vout}; +use commit_verify::{mpc, CommitId, Digest, DigestExt, ReservedBytes, Sha256, StrictHash}; use single_use_seals::{ClientSideWitness, PublishedWitness, SealWitness, SingleUseSeal}; use strict_encoding::StrictDumb; @@ -180,6 +180,26 @@ impl Ord for TxoSeal { } } +impl TxoSeal { + pub fn vout_no_fallback(vout: Vout, noise_seed: impl AsRef<[u8]>) -> Self { + Self::no_fallback(Outpoint::new(Txid::from([0xFFu8; 32]), vout), noise_seed) + } + + pub fn no_fallback(outpoint: Outpoint, noise_seed: impl AsRef<[u8]>) -> Self { + let mut engine = Sha256::new(); + engine.input_raw(noise_seed.as_ref()); + engine.input_raw(outpoint.txid.as_ref()); + engine.input_raw(&outpoint.vout.to_u32().to_be_bytes()); + let mut noise = [0xFFu8; 40]; + noise[..32].copy_from_slice(&engine.finish()); + Self { + primary: outpoint, + secondary: TxoSealExt::Noise(Noise(noise.into())), + _phantom: PhantomData, + } + } +} + impl SingleUseSeal for TxoSeal { type Message = Bytes32; type PubWitness = Tx;