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 authored and andreacorbellini committed Sep 12, 2023
1 parent 7bcebca commit f9c7265
Show file tree
Hide file tree
Showing 3 changed files with 52 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.

3 changes: 3 additions & 0 deletions ironfish-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ rand = "0.8.5"
tiny-bip39 = "0.8"
xxhash-rust = { version = "0.8.5", features = ["xxh3"] }

[dev-dependencies]
hex-literal = "0.4"

[build-dependencies]
hex = "0.4"
reqwest = { optional = true, version = "0.11", features = ["blocking"] }
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 f9c7265

Please sign in to comment.