Skip to content

Commit

Permalink
add Null impl for Multisig
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Huseby <dwh@linuxprogrammer.org>
  • Loading branch information
dhuseby committed Apr 3, 2024
1 parent f8ce25e commit 4dbfde8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "multisig"
version = "0.3.2"
version = "0.3.3"
edition = "2021"
authors = ["Dave Huseby <dwh@linuxprogrammer.org>"]
authors = ["Dave Grantham <dwg@linuxprogrammer.org>"]
description = "Multisig self-describing multicodec implementation for digital signatures"
repository = "https://github.com/cryptidtech/multisig.git"
readme = "README.md"
Expand Down
23 changes: 21 additions & 2 deletions src/ms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
use blsful::{inner_types::GroupEncoding, vsss_rs::Share, Signature, SignatureShare};
use multibase::Base;
use multicodec::Codec;
use multitrait::TryDecodeFrom;
use multitrait::{Null, TryDecodeFrom};
use multiutil::{BaseEncoded, CodecInfo, EncodingInfo, Varbytes, Varuint};
use std::{collections::BTreeMap, fmt};

Expand All @@ -23,7 +23,7 @@ pub type EncodedMultisig = BaseEncoded<Multisig>;
pub type Attributes = BTreeMap<AttrId, Vec<u8>>;

/// The multisig structure
#[derive(Clone, PartialEq)]
#[derive(Clone, Default, PartialEq)]
pub struct Multisig {
/// signature codec
pub(crate) codec: Codec,
Expand Down Expand Up @@ -129,6 +129,16 @@ impl<'a> TryDecodeFrom<'a> for Multisig {
}
}

impl Null for Multisig {
fn null() -> Self {
Self::default()
}

fn is_null(&self) -> bool {
*self == Self::null()
}
}

impl fmt::Debug for Multisig {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
Expand Down Expand Up @@ -680,4 +690,13 @@ mod tests {

assert_eq!(ms1, ms3);
}

#[test]
fn test_null() {
let ms1 = Multisig::null();
assert!(ms1.is_null());
let ms2 = Multisig::default();
assert_eq!(ms1, ms2);
assert!(ms2.is_null());
}
}
53 changes: 49 additions & 4 deletions src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod tests {
use crate::{Builder, EncodedMultisig, Multisig};
use multibase::Base;
use multicodec::Codec;
use multitrait::Null;
use serde_test::{assert_tokens, Configure, Token};

#[test]
Expand Down Expand Up @@ -393,10 +394,7 @@ mod tests {
assert_tokens(
&ms.readable(),
&[
Token::Struct {
name: "multisig",
len: 3,
},
Token::Struct { name: "multisig", len: 3, },
Token::BorrowedStr("codec"),
Token::BorrowedStr("bls12_381-g1-sig-share"),
Token::BorrowedStr("message"),
Expand Down Expand Up @@ -454,4 +452,51 @@ mod tests {
let ms2: Multisig = serde_cbor::from_slice(v.as_slice()).unwrap();
assert_eq!(ms1, ms2);
}

#[test]
fn test_null_multisig_serde_compact() {
let ms = Multisig::null();
assert_tokens(
&ms.compact(),
&[
Token::Tuple { len: 4, },
Token::BorrowedBytes(&[0x39]),
Token::BorrowedBytes(&[0x0]),
Token::BorrowedBytes(&[0x0]),
Token::Seq { len: Some(0) },
Token::SeqEnd,
Token::TupleEnd,
],
);
}

#[test]
fn test_null_multisig_serde_readable() {
let ms = Multisig::null();
assert_tokens(
&ms.readable(),
&[
Token::Struct { name: "multisig", len: 3, },
Token::BorrowedStr("codec"),
Token::BorrowedStr("identity"),
Token::BorrowedStr("message"),
Token::BorrowedStr("f00"),
Token::BorrowedStr("attributes"),
Token::Seq { len: Some(0) },
Token::SeqEnd,
Token::StructEnd,
],
);
}

#[test]
fn test_encoded_null_multisig_serde_readable() {
let ms: EncodedMultisig = Multisig::null().into();
assert_tokens(
&ms.readable(),
&[
Token::BorrowedStr("f39000000"),
],
);
}
}

0 comments on commit 4dbfde8

Please sign in to comment.