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
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of Tetsy Vapory.

// Tetsy Vapory is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Tetsy Vapory is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Tetsy Vapory.  If not, see <http://www.gnu.org/licenses/>.

//! vip-712 encoding utilities
//!
//! # Specification
//!
//! `encode(domainSeparator : 𝔹²⁵⁶, message : 𝕊) = "\x19\x01" ‖ domainSeparator ‖ hashStruct(message)`
//! - data adheres to 𝕊, a structure defined in the rigorous vip-712
//! - `\x01` is needed to comply with EIP-191
//! - `domainSeparator` and `hashStruct` are defined below
//!
//! ## A) domainSeparator
//!
//! `domainSeparator = hashStruct(vip712Domain)`
//! <br/>
//! <br/>
//! Struct named `VIP712Domain` with the following fields
//!
//! - `name: String`
//! - `version: String`
//! - `chain_id: U256`,
//! - `verifying_contract: H160`
//! - `salt: Option<H256>`
//!
//! ## C) hashStruct
//!
//! `hashStruct(s : 𝕊) = keccak256(typeHash ‖ encodeData(s))`
//! <br/>
//! `typeHash = keccak256(encodeType(typeOf(s)))`
//!
//! ### i) encodeType
//!
//! - `name ‖ "(" ‖ member₁ ‖ "," ‖ member₂ ‖ "," ‖ … ‖ memberₙ ")"`
//! - each member is written as `type ‖ " " ‖ name`
//! - encodings cascade down and are sorted by name
//!
//! ### ii) encodeData
//!
//! - `enc(value₁) ‖ enc(value₂) ‖ … ‖ enc(valueₙ)`
//! - each encoded member is 32-byte long
//!
//!     #### a) atomic
//!
//!     - `boolean`     => `U256`
//!     - `address`     => `H160`
//!     - `uint`        => sign-extended `U256` in big endian order
//!     - `bytes1:31`   => `H@256`
//!
//!     #### b) dynamic
//!
//!     - `bytes`       => `keccak256(bytes)`
//!     - `string`      => `keccak256(string)`
//!
//!     #### c) referenced
//!
//!     - `array`       => `keccak256(encodeData(array))`
//!     - `struct`      => `rec(keccak256(hashStruct(struct)))`
//!
//! ## D) Example
//! ### Query
//! ```json
//! {
//!   "jsonrpc": "2.0",
//!   "method": "vap_signTypedData",
//!   "params": [
//!     "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
//!     {
//!       "types": {
//!         "VIP712Domain": [
//!           {
//!             "name": "name",
//!             "type": "string"
//!           },
//!           {
//!             "name": "version",
//!             "type": "string"
//!           },
//!           {
//!             "name": "chainId",
//!             "type": "uint256"
//!           },
//!           {
//!             "name": "verifyingContract",
//!             "type": "address"
//!           }
//!         ],
//!         "Person": [
//!           {
//!             "name": "name",
//!             "type": "string"
//!           },
//!           {
//!             "name": "wallet",
//!             "type": "address"
//!           }
//!         ],
//!         "Mail": [
//!           {
//!             "name": "from",
//!             "type": "Person"
//!           },
//!           {
//!             "name": "to",
//!             "type": "Person"
//!           },
//!           {
//!             "name": "contents",
//!             "type": "string"
//!           }
//!         ]
//!       },
//!       "primaryType": "Mail",
//!       "domain": {
//!         "name": "Vapor Mail",
//!         "version": "1",
//!         "chainId": 1,
//!         "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
//!       },
//!       "message": {
//!         "from": {
//!           "name": "Cow",
//!           "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
//!         },
//!         "to": {
//!           "name": "Bob",
//!           "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
//!         },
//!         "contents": "Hello, Bob!"
//!       }
//!     }
//!   ],
//!   "id": 1
//! }
//! ```
//
//! ### Response
//! ```json
//! {
//!   "id":1,
//!   "jsonrpc": "2.0",
//!   "result": "0x4355c47d63924e8a72e509b65029052eb6c299d53a04e167c5775fd466751c9d07299936d304c153f6443dfa05f40ff007d72911b6f72307f996231605b915621c"
//! }
//! ```

#![warn(missing_docs)]

#[macro_use]
extern crate validator_derive;
#[macro_use]
extern crate serde_derive;

mod vip712;
mod error;
mod parser;
mod encode;

/// the vip-712 encoding function
pub use crate::encode::hash_structured_data;
/// encoding Error types
pub use crate::error::{ErrorKind, Error};
/// VIP712 struct
pub use crate::vip712::VIP712;