From 3f250282f3056846cc9da01bc9621165777fa999 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Tue, 25 Jul 2023 16:20:40 +0200 Subject: [PATCH 1/2] seals: add chunking support to SecretSeal utxob-string representation --- Cargo.lock | 4 ++-- seals/Cargo.toml | 2 +- seals/src/txout/blind.rs | 28 +++++++++++++++++++--------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2808e492..5ec456c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,9 +104,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "baid58" -version = "0.4.1" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "052064cc0caa02b62c88f06a7237304fb297873c78b6e95addecc3c5ddfce4ae" +checksum = "bc0585242d87ed976e05db6ae86a0f771f140104a4b6c91b4c3e43b9b2357486" dependencies = [ "base58", "blake3", diff --git a/seals/Cargo.toml b/seals/Cargo.toml index 702fb5be..f0808068 100644 --- a/seals/Cargo.toml +++ b/seals/Cargo.toml @@ -21,7 +21,7 @@ amplify = { workspace = true } single_use_seals = { workspace = true } commit_verify = { workspace = true } strict_encoding = { workspace = true } -baid58 = "0.4.1" +baid58 = "0.4.4" bp-primitives = { version = "0.10.6", path = "../primitives" } bp-dbc = { version = "0.10.6", path = "../dbc" } rand = "0.8.5" diff --git a/seals/src/txout/blind.rs b/seals/src/txout/blind.rs index d124606d..257437e9 100644 --- a/seals/src/txout/blind.rs +++ b/seals/src/txout/blind.rs @@ -26,7 +26,7 @@ use std::hash::Hash; use std::str::FromStr; use amplify::{hex, Bytes32, Wrapper}; -use baid58::{Baid58ParseError, FromBaid58, ToBaid58}; +use baid58::{Baid58ParseError, Chunking, FromBaid58, ToBaid58, CHUNKING_32CHECKSUM}; use bc::{Outpoint, Txid, Vout}; use commit_verify::{CommitVerify, Conceal}; use dbc::tapret::Lnpbp12; @@ -334,13 +334,12 @@ where Self: TxoSeal } /// Blind version of transaction outpoint-based single-use-seal -#[derive(Wrapper, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display, From)] +#[derive(Wrapper, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, From)] #[wrapper(Index, RangeOps, BorrowSlice, Hex)] #[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] #[strict_type(lib = dbc::LIB_NAME_BPCORE)] #[derive(CommitEncode)] #[commit_encode(strategy = strict)] -#[display(Self::to_baid58_string)] #[cfg_attr( feature = "serde", derive(Serialize, Deserialize), @@ -354,17 +353,25 @@ pub struct SecretSeal( impl ToBaid58<32> for SecretSeal { const HRI: &'static str = "utxob"; + const CHUNKING: Option = CHUNKING_32CHECKSUM; fn to_baid58_payload(&self) -> [u8; 32] { self.0.into_inner() } + fn to_baid58_string(&self) -> String { self.to_string() } } impl FromBaid58<32> for SecretSeal {} impl FromStr for SecretSeal { type Err = Baid58ParseError; - fn from_str(s: &str) -> Result { SecretSeal::from_baid58_str(s) } + fn from_str(s: &str) -> Result { + SecretSeal::from_baid58_maybe_chunked_str(s, ':', ' ') + } } -impl SecretSeal { - /// Returns Baid58 string representation of the secret seal. Equal to - /// `Display` - pub fn to_baid58_string(&self) -> String { format!("{:0^}", self.to_baid58()) } +impl Display for SecretSeal { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + if f.alternate() { + write!(f, "{::^}", self.to_baid58()) + } else { + write!(f, "{::^.3}", self.to_baid58()) + } + } } impl From for SecretSeal { @@ -409,11 +416,14 @@ mod test { } .to_concealed_seal(); - let baid58 = "utxob02eFrirURjqLnqR74AKRfdnc9MDpvSRjmZGmFPrw7nvuTe1wy83"; + let baid58 = "utxob:2eFrirU-RjqLnqR74-AKRfdnc9M-DpvSRjmZG-mFPrw7nvu-Te1wy83"; assert_eq!(baid58, seal.to_string()); + assert_eq!(baid58.replace('-', ""), format!("{seal:#}")); assert_eq!(seal.to_string(), seal.to_baid58_string()); let reconstructed = SecretSeal::from_str(baid58).unwrap(); assert_eq!(reconstructed, seal); + let reconstructed = SecretSeal::from_str(&baid58.replace('-', "")).unwrap(); + assert_eq!(reconstructed, seal); } #[test] From 67d5c6960a924da5a6436db0a926ec40141a49ba Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Tue, 25 Jul 2023 16:42:54 +0200 Subject: [PATCH 2/2] dbc: use Base85 representation for Tapret commitments --- Cargo.lock | 8 +++++- dbc/Cargo.toml | 2 +- dbc/src/tapret/tapscript.rs | 49 +++++++++++++++++++++---------------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5ec456c5..61276b37 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -126,6 +126,12 @@ version = "0.21.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "604178f6c5c21f02dc555784810edfb88d34ac2c73b2eae109655649ee73ce3d" +[[package]] +name = "base85" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88b68cde48c539cf9db81c5598e215dc32d0401a167f899cf26e35f61815eb9e" + [[package]] name = "bitflags" version = "1.3.2" @@ -175,7 +181,7 @@ name = "bp-dbc" version = "0.10.6" dependencies = [ "amplify", - "baid58", + "base85", "bp-primitives", "commit_verify", "secp256k1", diff --git a/dbc/Cargo.toml b/dbc/Cargo.toml index 97fdf3a1..a99c073d 100644 --- a/dbc/Cargo.toml +++ b/dbc/Cargo.toml @@ -18,7 +18,7 @@ path = "src/lib.rs" [dependencies] amplify = { workspace = true } -baid58 = "0.4.1" +base85 = "1.1.1" strict_encoding = { workspace = true } commit_verify = { workspace = true, features = ["rand"] } bp-primitives = { version = "0.10.6", path = "../primitives" } diff --git a/dbc/src/tapret/tapscript.rs b/dbc/src/tapret/tapscript.rs index e3e54461..830e33a7 100644 --- a/dbc/src/tapret/tapscript.rs +++ b/dbc/src/tapret/tapscript.rs @@ -19,14 +19,14 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::fmt::{self, Display, Formatter}; use std::io; use std::str::FromStr; use amplify::confinement::Confined; -use baid58::{Baid58ParseError, FromBaid58, ToBaid58}; use bc::{TapCode, TapScript}; use commit_verify::{mpc, CommitEncode, CommitVerify}; -use strict_encoding::{StrictDeserialize, StrictSerialize}; +use strict_encoding::{DecodeError, DeserializeError, StrictDeserialize, StrictSerialize}; use super::Lnpbp12; use crate::LIB_NAME_BPCORE; @@ -39,8 +39,7 @@ pub const TAPRET_SCRIPT_COMMITMENT_PREFIX: [u8; 31] = [ ]; /// Information about tapret commitment. -#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)] -#[display(Self::to_baid58_string)] +#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)] #[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)] #[strict_type(lib = LIB_NAME_BPCORE)] #[derive(CommitEncode)] @@ -66,23 +65,32 @@ impl From<[u8; 33]> for TapretCommitment { } } -impl ToBaid58<33> for TapretCommitment { - const HRI: &'static str = "tapret"; - fn to_baid58_payload(&self) -> [u8; 33] { - let mut data = io::Cursor::new([0u8; 33]); - self.commit_encode(&mut data); - data.into_inner() +impl TapretCommitment { + /// Returns serialized representation of the commitment data. + pub fn to_vec(&self) -> Vec { + self.to_strict_serialized::<33>() + .expect("exact size match") + .into_inner() } } -impl FromBaid58<33> for TapretCommitment {} -impl TapretCommitment { - fn to_baid58_string(&self) -> String { format!("{:0^}", self.to_baid58()) } +impl Display for TapretCommitment { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let s = base85::encode(&self.to_vec()); + f.write_str(&s) + } } - impl FromStr for TapretCommitment { - type Err = Baid58ParseError; - fn from_str(s: &str) -> Result { Self::from_baid58_str(s) } + type Err = DeserializeError; + fn from_str(s: &str) -> Result { + let data = base85::decode(s).ok_or_else(|| { + DecodeError::DataIntegrityError(format!( + "invalid Base85 encoding of tapret data \"{s}\"" + )) + })?; + let data = Confined::try_from(data).map_err(DecodeError::from)?; + Self::from_strict_serialized::<33>(data) + } } impl TapretCommitment { @@ -110,12 +118,14 @@ impl CommitVerify for TapScript { #[cfg(test)] mod test { use amplify::RawArray; + use commit_verify::{Digest, Sha256}; use super::*; pub fn commitment() -> TapretCommitment { + let msg = Sha256::digest("test data"); TapretCommitment { - mpc: mpc::Commitment::from_raw_array([0x6Cu8; 32]), + mpc: mpc::Commitment::from_raw_array(msg), nonce: 8, } } @@ -137,11 +147,8 @@ mod test { #[test] pub fn tapret_commitment_baid58() { let commitment = commitment(); - let encoded = commitment.to_baid58(); - let decoded = TapretCommitment::from_baid58(encoded).unwrap(); let s = commitment.to_string(); - assert_eq!(s, "tapret04dm9azJKdXhE27U4MHX8GsmibZEJ6WBMNmeKXGLPJDNaLPKNm9R"); + assert_eq!(s, "k#7JerF92P=PEN7cf&`GWfS*?rIEdfEup1%zausI2m"); assert_eq!(Ok(commitment.clone()), TapretCommitment::from_str(&s)); - assert_eq!(decoded, commitment); } }