Skip to content

Commit

Permalink
Add (de)serialization for Asset
Browse files Browse the repository at this point in the history
  • Loading branch information
andiflabs committed Sep 7, 2023
1 parent e39f3b4 commit 955fc9c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions ironfish-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 41 additions & 2 deletions ironfish-rust/src/assets/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 955fc9c

Please sign in to comment.