-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathsnip_12_json.rs
44 lines (41 loc) · 1.24 KB
/
snip_12_json.rs
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
use starknet::{core::types::TypedData, macros::felt};
fn main() {
let raw = r#"{
"types": {
"StarknetDomain": [
{ "name": "name", "type": "shortstring" },
{ "name": "version", "type": "shortstring" },
{ "name": "chainId", "type": "shortstring" },
{ "name": "revision", "type": "shortstring" }
],
"Example Message": [
{ "name": "Name", "type": "string" },
{ "name": "Some Array", "type": "u128*" },
{ "name": "Some Object", "type": "My Object" }
],
"My Object": [
{ "name": "Some Selector", "type": "selector" },
{ "name": "Some Contract Address", "type": "ContractAddress" }
]
},
"primaryType": "Example Message",
"domain": {
"name": "Starknet Example",
"version": "1",
"chainId": "SN_MAIN",
"revision": "1"
},
"message": {
"Name": "some name",
"Some Array": [1, 2, 3, 4],
"Some Object": {
"Some Selector": "transfer",
"Some Contract Address": "0x0123"
}
}
}"#;
let typed_data = serde_json::from_str::<TypedData>(raw).unwrap();
println!("SNIP-12 revision: {}", typed_data.revision());
let message_hash = typed_data.message_hash(felt!("0x1234")).unwrap();
println!("SNIP-12 hash: {:#064x}", message_hash);
}