diff --git a/Cargo.lock b/Cargo.lock index 70d843c4ad..b509f70247 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1161,6 +1161,12 @@ dependencies = [ "proc-macro-hack", ] +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + [[package]] name = "hex-literal-impl" version = "0.1.2" @@ -1340,6 +1346,7 @@ dependencies = [ "ff 0.12.1", "group 0.12.1", "hex", + "hex-literal 0.4.1", "ironfish_zkp", "jubjub 0.9.0 (git+https://github.com/iron-fish/jubjub.git?branch=blstrs)", "lazy_static", @@ -1385,7 +1392,7 @@ version = "0.2.0" dependencies = [ "blake2", "byteorder", - "hex-literal", + "hex-literal 0.1.4", "ironfish-phase2", "ironfish_zkp", "pairing 0.23.0", diff --git a/ironfish-rust/Cargo.toml b/ironfish-rust/Cargo.toml index bb804a427e..d67835cac6 100644 --- a/ironfish-rust/Cargo.toml +++ b/ironfish-rust/Cargo.toml @@ -42,6 +42,7 @@ chacha20poly1305 = "0.9.0" crypto_box = { version = "0.8", features = ["std"] } ff = "0.12.0" group = "0.12.0" +hex-literal = "0.4" ironfish_zkp = { version = "0.2.0", path = "../ironfish-zkp" } jubjub = { git = "https://github.com/iron-fish/jubjub.git", branch = "blstrs" } lazy_static = "1.4.0" diff --git a/ironfish-rust/src/assets/asset.rs b/ironfish-rust/src/assets/asset.rs index 0e0b65c211..1d3c11ed4d 100644 --- a/ironfish-rust/src/assets/asset.rs +++ b/ironfish-rust/src/assets/asset.rs @@ -16,7 +16,7 @@ pub const ID_LENGTH: usize = ASSET_ID_LENGTH; /// Describes all the fields necessary for creating and transacting with an /// asset on the Iron Fish network -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, PartialEq, Debug)] pub struct Asset { /// Name of the asset pub(crate) name: [u8; NAME_LENGTH], @@ -140,9 +140,11 @@ impl Asset { #[cfg(test)] mod test { + use hex_literal::hex; + use crate::{util::str_to_array, PublicAddress, SaplingKey}; - use super::Asset; + use super::{Asset, ASSET_LENGTH}; #[test] fn test_asset_new() { @@ -210,4 +212,41 @@ mod test { assert!(asset_res.is_err()); } + + #[test] + fn test_serialization() { + let creator_address = + hex!("51e56d146fae345b78d7226bae7b4e66bdbce207ad074c8782cb47833edbf044"); + let creator = PublicAddress::new(&creator_address).unwrap(); + + let nonce = 0; + let name = str_to_array("name"); + let metadata = str_to_array("{ 'token_identifier': '0x123' }"); + + let asset = Asset::new_with_nonce(creator, name, metadata, nonce).unwrap(); + + let mut buf = Vec::new(); + asset.write(&mut buf).unwrap(); + + assert_eq!( + buf, + hex!( + // creator + "51e56d146fae345b78d7226bae7b4e66bdbce207ad074c8782cb47833edbf044" + // name + "6e616d6500000000000000000000000000000000000000000000000000000000" + // metadata + "7b2027746f6b656e5f6964656e746966696572273a2027307831323327207d00" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + // nonce + "00" + ) + ); + + assert_eq!(buf.len(), ASSET_LENGTH); + + let deserialized = Asset::read(&buf[..]).unwrap(); + assert_eq!(asset, deserialized); + } }