|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
| 3 | +import json |
3 | 4 | import pytest |
4 | 5 |
|
5 | 6 | from eth_abi import ( |
@@ -202,6 +203,107 @@ def test_eth_sign(self, web3, unlocked_account_dual_type): |
202 | 203 | ) |
203 | 204 | assert new_signature != signature |
204 | 205 |
|
| 206 | + def test_eth_signTypedData(self, web3, unlocked_account_dual_type): |
| 207 | + validJSONMessage = ''' |
| 208 | + { |
| 209 | + "types": { |
| 210 | + "EIP712Domain": [ |
| 211 | + {"name": "name", "type": "string"}, |
| 212 | + {"name": "version", "type": "string"}, |
| 213 | + {"name": "chainId", "type": "uint256"}, |
| 214 | + {"name": "verifyingContract", "type": "address"} |
| 215 | + ], |
| 216 | + "Person": [ |
| 217 | + {"name": "name", "type": "string"}, |
| 218 | + {"name": "wallet", "type": "address"} |
| 219 | + ], |
| 220 | + "Mail": [ |
| 221 | + {"name": "from", "type": "Person"}, |
| 222 | + {"name": "to", "type": "Person"}, |
| 223 | + {"name": "contents", "type": "string"} |
| 224 | + ] |
| 225 | + }, |
| 226 | + "primaryType": "Mail", |
| 227 | + "domain": { |
| 228 | + "name": "Ether Mail", |
| 229 | + "version": "1", |
| 230 | + "chainId": "0x01", |
| 231 | + "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" |
| 232 | + }, |
| 233 | + "message": { |
| 234 | + "from": { |
| 235 | + "name": "Cow", |
| 236 | + "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" |
| 237 | + }, |
| 238 | + "to": { |
| 239 | + "name": "Bob", |
| 240 | + "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" |
| 241 | + }, |
| 242 | + "contents": "Hello, Bob!" |
| 243 | + } |
| 244 | + } |
| 245 | + ''' |
| 246 | + signature = HexBytes(web3.eth.signTypedData( |
| 247 | + unlocked_account_dual_type, |
| 248 | + json.loads(validJSONMessage) |
| 249 | + )) |
| 250 | + assert len(signature) == 32 + 32 + 1 |
| 251 | + |
| 252 | + def test_invalid_eth_signTypedData(self, |
| 253 | + web3, |
| 254 | + unlocked_account_dual_type): |
| 255 | + invalid_typed_message = ''' |
| 256 | + { |
| 257 | + "types": { |
| 258 | + "EIP712Domain": [ |
| 259 | + {"name": "name", "type": "string"}, |
| 260 | + {"name": "version", "type": "string"}, |
| 261 | + {"name": "chainId", "type": "uint256"}, |
| 262 | + {"name": "verifyingContract", "type": "address"} |
| 263 | + ], |
| 264 | + "Person": [ |
| 265 | + {"name": "name", "type": "string"}, |
| 266 | + {"name": "wallet", "type": "address"} |
| 267 | + ], |
| 268 | + "Mail": [ |
| 269 | + {"name": "from", "type": "Person"}, |
| 270 | + {"name": "to", "type": "Person[2]"}, |
| 271 | + {"name": "contents", "type": "string"} |
| 272 | + ] |
| 273 | + }, |
| 274 | + "primaryType": "Mail", |
| 275 | + "domain": { |
| 276 | + "name": "Ether Mail", |
| 277 | + "version": "1", |
| 278 | + "chainId": "0x01", |
| 279 | + "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" |
| 280 | + }, |
| 281 | + "message": { |
| 282 | + "from": { |
| 283 | + "name": "Cow", |
| 284 | + "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" |
| 285 | + }, |
| 286 | + "to": [{ |
| 287 | + "name": "Bob", |
| 288 | + "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" |
| 289 | + }], |
| 290 | + "contents": "Hello, Bob!" |
| 291 | + } |
| 292 | + } |
| 293 | + ''' |
| 294 | + # ValueError is caused by the error in the response which is as follows |
| 295 | + # ``Expected 2 items for array type Person[2], got 1 items`` |
| 296 | + with pytest.raises(ValueError) as e: |
| 297 | + web3.eth.signTypedData( |
| 298 | + unlocked_account_dual_type, |
| 299 | + json.loads(invalid_typed_message) |
| 300 | + ) |
| 301 | + try: |
| 302 | + assert "Expected 2 items for array type Person[2], got 1 items" in str(e.value) |
| 303 | + except AssertionError: |
| 304 | + # This error is raised by eth-tester |
| 305 | + assert "Unknown RPC Endpoint: eth_signTypedData" in str(e.value) |
| 306 | + |
205 | 307 | def test_eth_signTransaction(self, web3, unlocked_account): |
206 | 308 | txn_params = { |
207 | 309 | 'from': unlocked_account, |
|
0 commit comments