Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 105 additions & 1 deletion rpc/src/v1/types/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use rustc_serialize::hex::FromHexError;
use super::super::errors::ConversionError;
use super::{AssetMintOutput, AssetTransferInput, AssetTransferOutput, OrderOnTransfer};

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum Action {
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -650,3 +650,107 @@ impl From<Action> for Result<ActionType, ConversionError> {
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::{from_str, to_string};

#[test]
fn serialize_metadata_with_single_quotations() {
let mint = ActionWithId::MintAsset {
network_id: "ab".into(),
shard_id: 0,
metadata: "string with 'a single quotation'".to_string(),
approver: None,
administrator: None,
allowed_script_hashes: vec![],

output: AssetMintOutput {
lock_script_hash: Default::default(),
parameters: vec![],
supply: Some(1.into()),
}
.into(),

approvals: vec![],
id: Default::default(),
};
let s = to_string(&mint).unwrap();
let expected = r#"{"type":"mintAsset","networkId":"ab","shardId":0,"metadata":"string with 'a single quotation'","approver":null,"administrator":null,"allowedScriptHashes":[],"output":{"lockScriptHash":"0x0000000000000000000000000000000000000000","parameters":[],"supply":"0x1"},"approvals":[],"id":"0x0000000000000000000000000000000000000000000000000000000000000000"}"#;
assert_eq!(&s, expected);
}

#[test]
fn parse_metadata_with_single_quotations() {
let input = r#"{"type":"mintAsset","networkId":"ab","shardId":0,"metadata":"string with 'a single quotation'","approver":null,"administrator":null,"allowedScriptHashes":[],"output":{"lockScriptHash":"0x0000000000000000000000000000000000000000","parameters":[],"supply":"0x1"},"approvals":[]}"#;
let mint = from_str(input).unwrap();
let expected = Action::MintAsset {
network_id: "ab".into(),
shard_id: 0,
metadata: "string with 'a single quotation'".to_string(),
approver: None,
administrator: None,
allowed_script_hashes: vec![],

output: AssetMintOutput {
lock_script_hash: Default::default(),
parameters: vec![],
supply: Some(1.into()),
}
.into(),

approvals: vec![],
};
assert_eq!(expected, mint);
}

#[test]
fn parse_metadata_with_apostrophe() {
let input = r#"{"type":"mintAsset","networkId":"ab","shardId":0,"metadata":"string with 'an apostrophe’","approver":null,"administrator":null,"allowedScriptHashes":[],"output":{"lockScriptHash":"0x0000000000000000000000000000000000000000","parameters":[],"supply":"0x1"},"approvals":[]}"#;
let mint = from_str(input).unwrap();
let expected = Action::MintAsset {
network_id: "ab".into(),
shard_id: 0,
metadata: "string with 'an apostrophe’".to_string(),
approver: None,
administrator: None,
allowed_script_hashes: vec![],

output: AssetMintOutput {
lock_script_hash: Default::default(),
parameters: vec![],
supply: Some(1.into()),
}
.into(),

approvals: vec![],
};
assert_eq!(expected, mint);
}

#[test]
fn serialize_metadata_with_apostrophe() {
let mint = ActionWithId::MintAsset {
network_id: "ab".into(),
shard_id: 0,
metadata: "string with 'an apostrophe’".to_string(),
approver: None,
administrator: None,
allowed_script_hashes: vec![],

output: AssetMintOutput {
lock_script_hash: Default::default(),
parameters: vec![],
supply: Some(1.into()),
}
.into(),

approvals: vec![],
id: Default::default(),
};
let s = to_string(&mint).unwrap();
let expected = r#"{"type":"mintAsset","networkId":"ab","shardId":0,"metadata":"string with 'an apostrophe’","approver":null,"administrator":null,"allowedScriptHashes":[],"output":{"lockScriptHash":"0x0000000000000000000000000000000000000000","parameters":[],"supply":"0x1"},"approvals":[],"id":"0x0000000000000000000000000000000000000000000000000000000000000000"}"#;
assert_eq!(&s, expected);
}
}
4 changes: 2 additions & 2 deletions rpc/src/v1/types/asset_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use cjson::uint::Uint;
use ctypes::transaction::{AssetOutPoint as AssetOutPointType, AssetTransferInput as AssetTransferInputType, Timelock};
use primitives::{Bytes, H256};

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AssetOutPoint {
pub tracker: H256,
Expand Down Expand Up @@ -49,7 +49,7 @@ impl From<AssetOutPoint> for AssetOutPointType {
}
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AssetTransferInput {
pub prev_out: AssetOutPoint,
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/v1/types/asset_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use ctypes::transaction::{AssetMintOutput as AssetMintOutputType, AssetTransferO
use primitives::{H160, H256};
use rustc_serialize::hex::{FromHex, FromHexError, ToHex};

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AssetTransferOutput {
pub lock_script_hash: H160,
Expand Down Expand Up @@ -53,7 +53,7 @@ impl From<AssetTransferOutput> for Result<AssetTransferOutputType, FromHexError>
}
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AssetMintOutput {
pub lock_script_hash: H160,
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/v1/types/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use primitives::{Bytes, H160, H256};

use super::AssetOutPoint;

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Order {
pub asset_type_from: H256,
Expand Down Expand Up @@ -75,7 +75,7 @@ impl From<Order> for OrderType {
}
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderOnTransfer {
pub order: Order,
Expand Down
35 changes: 35 additions & 0 deletions types/src/transaction/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,41 @@ mod tests {
});
}

#[test]
fn encode_and_decode_mint_with_single_quotation() {
rlp_encode_and_decode_test!(Action::MintAsset {
network_id: "tc".into(),
shard_id: 3,
metadata: "metadata has a single quotation(')".to_string(),
output: Box::new(AssetMintOutput {
lock_script_hash: H160::random(),
parameters: vec![vec![1, 2, 3], vec![4, 5, 6], vec![0, 7]],
supply: Some(10000),
}),
approver: None,
administrator: None,
allowed_script_hashes: vec![],
approvals: vec![Signature::random()],
});
}

#[test]
fn encode_and_decode_mint_with_apostrophe() {
rlp_encode_and_decode_test!(Action::MintAsset {
network_id: "tc".into(),
shard_id: 3,
metadata: "metadata has an apostrophe(’)".to_string(),
output: Box::new(AssetMintOutput {
lock_script_hash: H160::random(),
parameters: vec![vec![1, 2, 3], vec![4, 5, 6], vec![0, 7]],
supply: Some(10000),
}),
approver: None,
administrator: None,
allowed_script_hashes: vec![],
approvals: vec![Signature::random()],
});
}

#[test]
fn encode_and_decode_transfer_asset() {
Expand Down