1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

#![allow(bare_trait_objects)]

#[allow(clippy::large_enum_variant)]
#[allow(clippy::derive_partial_eq_without_eq)]
pub mod types {
    include!(concat!(env!("OUT_DIR"), "/jsonrpc.rs"));

    pub use crate::constants::*;
}

pub mod constants;

fn is_default<T: Default + PartialEq>(t: &T) -> bool {
    t == &T::default()
}

#[cfg(test)]
mod tests {
    use crate::{is_default, types as jsonrpc, types::Amount};
    use diem_crypto::HashValue;
    use diem_json_rpc_types::views::{
        AmountView, BytesView, PreburnWithMetadataView, ScriptView, TransactionDataView,
    };
    use diem_types::account_address::AccountAddress;
    use serde_json::json;

    #[test]
    fn test_is_default() {
        let amount = Amount {
            amount: 0,
            currency: "CODE".to_string(),
        };
        assert!(is_default(&amount.amount));
        assert!(!is_default(&amount.currency));

        assert_eq!(
            serde_json::json!({
                "currency": "CODE"
            }),
            serde_json::to_value(&amount).unwrap()
        );

        let des_amount: Amount = serde_json::from_value(serde_json::json!({
            "currency": "CODE"
        }))
        .unwrap();
        assert_eq!(amount, des_amount);
    }

    #[test]
    fn test_serialize_preburn_with_metadata_view() {
        let view = PreburnWithMetadataView {
            preburn: AmountView {
                amount: 1,
                currency: "XUS".to_string(),
            },
            metadata: None,
        };
        let value = serde_json::to_value(&view).unwrap();
        assert_eq!(
            value,
            json!({
                "preburn": {
                    "amount": 1,
                    "currency": "XUS"
                }
            })
        );

        let preburn: jsonrpc::PreburnWithMetadata = serde_json::from_value(value).unwrap();
        assert_eq!(preburn.metadata, "");
    }

    #[test]
    fn test_serialize_transaction_data_view() {
        let bytes = BytesView::from(vec![]);
        let hash = HashValue::random();
        let view = TransactionDataView::UserTransaction {
            sender: AccountAddress::from_hex_literal("0xdd").unwrap(),
            signature_scheme: "Ed25519".to_string(),
            signature: bytes.clone(),
            public_key: bytes.clone(),
            secondary_signers: None,
            secondary_signature_schemes: None,
            secondary_signatures: None,
            secondary_public_keys: None,
            sequence_number: 10,
            chain_id: 4,
            max_gas_amount: 100,
            gas_unit_price: 10,
            gas_currency: "XUS".to_string(),
            expiration_timestamp_secs: 60,
            script_hash: hash,
            script_bytes: bytes,
            script: ScriptView::unknown(),
        };

        let value = serde_json::to_value(&view).unwrap();
        assert_eq!(
            value,
            json!({
                "type": "user",
                "sender": "000000000000000000000000000000dd",
                "signature_scheme": "Ed25519",
                "signature": "",
                "public_key": "",
                "sequence_number": 10,
                "chain_id": 4,
                "max_gas_amount": 100,
                "gas_unit_price": 10,
                "gas_currency": "XUS",
                "expiration_timestamp_secs": 60,
                "script_hash": hash,
                "script_bytes": "",
                "script": {
                    "type": "unknown"
                },
            })
        );

        let txn_data: jsonrpc::TransactionData = serde_json::from_value(value).unwrap();
        assert_eq!(txn_data.secondary_signers, Vec::<String>::new());
        assert_eq!(txn_data.secondary_signature_schemes, Vec::<String>::new());
        assert_eq!(txn_data.secondary_signatures, Vec::<String>::new());
        assert_eq!(txn_data.secondary_public_keys, Vec::<String>::new());
    }

    #[test]
    fn test_serialize_multi_agent_transaction_data_view() {
        let bytes = BytesView::from(vec![42]);
        let hash = HashValue::random();
        let view = TransactionDataView::UserTransaction {
            sender: AccountAddress::from_hex_literal("0xdd").unwrap(),
            signature_scheme: "Ed25519".to_string(),
            signature: bytes.clone(),
            public_key: bytes.clone(),
            secondary_signers: Some(vec![
                AccountAddress::from_hex_literal("0xaa").unwrap(),
                AccountAddress::from_hex_literal("0xbb").unwrap(),
            ]),
            secondary_signature_schemes: Some(vec![
                "Ed25519".to_string(),
                "MultiEd25519".to_string(),
            ]),
            secondary_signatures: Some(vec![BytesView::from(vec![42]), BytesView::from(vec![43])]),
            secondary_public_keys: Some(vec![BytesView::from(vec![44]), BytesView::from(vec![45])]),
            sequence_number: 10,
            chain_id: 4,
            max_gas_amount: 100,
            gas_unit_price: 10,
            gas_currency: "XUS".to_string(),
            expiration_timestamp_secs: 60,
            script_hash: hash,
            script_bytes: bytes.clone(),
            script: ScriptView::unknown(),
        };

        let value = serde_json::to_value(&view).unwrap();
        assert_eq!(
            value,
            json!({
                "type": "user",
                "sender": "000000000000000000000000000000dd",
                "signature_scheme": "Ed25519",
                "signature": "2a",
                "public_key": "2a",
                "secondary_signers": [
                    "000000000000000000000000000000aa",
                    "000000000000000000000000000000bb"
                ],
                "secondary_signature_schemes": ["Ed25519", "MultiEd25519"],
                "secondary_signatures": [ "2a", "2b" ],
                "secondary_public_keys": [ "2c", "2d" ],
                "sequence_number": 10,
                "chain_id": 4,
                "max_gas_amount": 100,
                "gas_unit_price": 10,
                "gas_currency": "XUS",
                "expiration_timestamp_secs": 60,
                "script_hash": hash,
                "script_bytes": bytes,
                "script": {
                    "type": "unknown"
                },
            })
        );

        let txn_data: jsonrpc::TransactionData = serde_json::from_value(value).unwrap();
        assert_eq!(
            txn_data.secondary_signers,
            vec![
                "000000000000000000000000000000aa",
                "000000000000000000000000000000bb"
            ]
        );
        assert_eq!(
            txn_data.secondary_signature_schemes,
            vec!["Ed25519", "MultiEd25519"]
        );
        assert_eq!(txn_data.secondary_signatures, vec!["2a", "2b"]);
        assert_eq!(txn_data.secondary_public_keys, vec!["2c", "2d"]);
    }
}