Skip to content

Commit

Permalink
Fixing serde to use Bech32 encoding for ContractId and SchemaId types
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Dec 15, 2020
1 parent e9e7233 commit ea219e8
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Change Log
==========

v0.2.1
------
- Fixing serde to use Bech32 encoding for ContractId and SchemaId types

v0.2.0
------
No changes since RC2
Expand Down
3 changes: 2 additions & 1 deletion Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lnpbp"
version = "0.2.0"
version = "0.2.1"
license = "MIT"
authors = ["Dr. Maxim Orlovsky <orlovsky@pandoracore.com>"]
description = "LNP/BP Core Library implementing LNPBP specifications & standards"
Expand Down Expand Up @@ -89,6 +89,9 @@ num-derive = "~0.3.0"
async-trait = { version = "~0.1.30", optional = true }
torut = { version = "~0.1.6", features = ["v2", "v3"] }

[dev-dependencies]
serde_json = "~1.0.60"

[target.'cfg(target_os="android")'.dependencies]
cc = { version = "=1.0.41" }
# TODO: Find a soliton to a problem with breaking change in one of tokio
Expand Down
62 changes: 59 additions & 3 deletions src/rgb/bech32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,85 +31,141 @@ use crate::strict_encoding::{
/// Bech32 representation of generic RGB data, that can be generated from
/// some string basing on Bech32 HRP value.
#[derive(Clone, Debug, From)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", untagged)
)]
pub enum Bech32 {
/// Pedersen commitment
///
/// HRP: `pedersen`
#[from]
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
PedersenCommitment(secp256k1zkp::pedersen::Commitment),

/// Bulletproofs
///
/// HRP: `bulletproof`
#[from]
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
Bulletproof(secp256k1zkp::pedersen::RangeProof),

/// Curve25519 public key
///
/// HRP: `curve25519pk`
#[from]
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
Curve25519Pk(ed25519_dalek::PublicKey),

/// Ed25519 signature
///
/// HRP: `ed25519sign`
#[from]
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
Ed25519Sign(ed25519_dalek::Signature),

/// Blinded UTXO for assigning RGB state to.
///
/// HRP: `utxob`
#[from]
// TODO: (new) Remove it once invoice implementation will be completed
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
BlindedUtxo(seal::Confidential),

/// RGB Schema ID (hash of the schema data).
///
/// HRP: `sch`
#[from]
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
SchemaId(SchemaId),

/// RGB Schema raw data (hash of the genesis).
///
/// HRP: `schema`
#[from]
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
Schema(Schema),

/// RGB Contract ID (hash of the genesis).
///
/// HRP: `rgb`
#[from]
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
ContractId(ContractId),

/// RGB Contract genesis raw data
///
/// HRP: `genesis`
#[from]
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
Genesis(Genesis),

/// Raw data of state transition under some RGB contract
///
/// HRP: `transition`
#[from]
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
Transition(Transition),

/// Raw data of state extension under some RGB contract
///
/// HRP: `statex`
#[from]
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
Extension(Extension),

/// Anchor data for some dterministic bitcoin commitment
///
/// HRP: `anchor`
#[from]
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
Anchor(Anchor),

/// Disclosure data revealing some specific confidential information about
/// RGB contract
///
/// HRP: `disclosure`
#[from]
#[serde(
serialize_with = "to_bech32_str",
deserialize_with = "from_bech32_str"
)]
Disclosure(Disclosure),

/// Binary data for unknown Bech32 HRPs
Expand Down Expand Up @@ -626,7 +682,7 @@ impl Display for Disclosure {

/// Serializes type to a Bech32 string.
#[cfg(feature = "serde")]
pub fn to_bech32<T, S>(buffer: &T, serializer: S) -> Result<S::Ok, S::Error>
pub fn to_bech32_str<T, S>(buffer: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: ToBech32,
S: Serializer,
Expand All @@ -636,7 +692,7 @@ where

/// Deserializes a Bech32 to a `Vec<u8>`.
#[cfg(feature = "serde")]
pub fn from_bech32<'de, T, D>(deserializer: D) -> Result<T, D::Error>
pub fn from_bech32_str<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: FromBech32,
D: Deserializer<'de>,
Expand Down
8 changes: 4 additions & 4 deletions src/rgb/contract/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ pub enum Revealed {
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::rgb::bech32::to_bech32",
deserialize_with = "crate::rgb::bech32::from_bech32"
serialize_with = "crate::rgb::bech32::to_bech32_str",
deserialize_with = "crate::rgb::bech32::from_bech32_str"
)
)]
Curve25519Pubkey(ed25519_dalek::PublicKey),
Expand All @@ -100,8 +100,8 @@ pub enum Revealed {
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::rgb::bech32::to_bech32",
deserialize_with = "crate::rgb::bech32::from_bech32"
serialize_with = "crate::rgb::bech32::to_bech32_str",
deserialize_with = "crate::rgb::bech32::from_bech32_str"
)
)]
Ed25519Signature(ed25519_dalek::Signature),
Expand Down
20 changes: 16 additions & 4 deletions src/rgb/contract/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::rgb::schema::{
TransitionType,
};
use crate::rgb::{
schema, seal, Metadata, SchemaId, SimplicityScript, ToBech32,
schema, seal, Bech32, Metadata, SchemaId, SimplicityScript, ToBech32,
};

/// Holds definition of valencies for contract nodes, which is a set of
Expand All @@ -56,7 +56,6 @@ impl sha256t::Tag for NodeIdTag {

/// Unique node (genesis, extensions & state transition) identifier equivalent
/// to the commitment hash
#[cfg_attr(feature = "serde", serde_as(as = "DisplayFromStr"))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
Expand Down Expand Up @@ -85,11 +84,10 @@ impl CommitEncodeWithStrategy for NodeId {
}

/// Unique contract identifier equivalent to the contract genesis commitment
#[cfg_attr(feature = "serde", serde_as(as = "DisplayFromStr"))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
serde(crate = "serde_crate", try_from = "Bech32", into = "Bech32")
)]
#[derive(
Wrapper,
Expand Down Expand Up @@ -1040,6 +1038,20 @@ mod test {
);
}

#[test]
fn test_id_serde() {
let genesis: Genesis = Genesis::strict_decode(&GENESIS[..]).unwrap();
let contract_id = genesis.contract_id();
assert_eq!(
contract_id.to_string(),
"rgb1z586eqcsa5z5zvqep9y3rhqsykqpuycrhhvkc2aey6hdu25yyrgqv0a28q"
);
assert_eq!(
serde_json::to_string(&contract_id).unwrap(),
"\"rgb1z586eqcsa5z5zvqep9y3rhqsykqpuycrhhvkc2aey6hdu25yyrgqv0a28q\""
);
}

#[test]
fn test_genesis_impl() {
let genesis: Genesis = Genesis::strict_decode(&GENESIS[..]).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions src/rgb/contract/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ pub struct Confidential {
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::rgb::bech32::to_bech32",
deserialize_with = "crate::rgb::bech32::from_bech32"
serialize_with = "crate::rgb::bech32::to_bech32_str",
deserialize_with = "crate::rgb::bech32::from_bech32_str"
)
)]
pub commitment: pedersen::Commitment,
#[cfg_attr(
feature = "serde",
serde(
serialize_with = "crate::rgb::bech32::to_bech32",
deserialize_with = "crate::rgb::bech32::from_bech32"
serialize_with = "crate::rgb::bech32::to_bech32_str",
deserialize_with = "crate::rgb::bech32::from_bech32_str"
)
)]
pub bulletproof: pedersen::RangeProof,
Expand Down
5 changes: 2 additions & 3 deletions src/rgb/schema/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::client_side_validation::{
};
use crate::commit_verify::CommitVerify;
use crate::features;
use crate::rgb::ToBech32;
use crate::rgb::{Bech32, ToBech32};

// Here we can use usize since encoding/decoding makes sure that it's u16
pub type FieldType = usize;
Expand All @@ -51,11 +51,10 @@ impl sha256t::Tag for SchemaIdTag {
}

/// Commitment-based schema identifier used for committing to the schema type
#[cfg_attr(feature = "serde", serde_as(as = "DisplayFromStr"))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
serde(crate = "serde_crate", try_from = "Bech32", into = "Bech32")
)]
#[derive(
Wrapper,
Expand Down

0 comments on commit ea219e8

Please sign in to comment.