From 60b1afbd95760609bb692518daca96fdaba11982 Mon Sep 17 00:00:00 2001 From: Bhargavasomu Date: Mon, 8 Apr 2019 21:15:42 +0530 Subject: [PATCH] Add support for eth_signTypedData RPC call --- web3/_utils/module_testing/eth_module.py | 70 ++++++++++++++++++++++ web3/_utils/rpc_abi.py | 1 + web3/eth.py | 5 ++ web3/middleware/exception_retry_request.py | 1 + web3/middleware/pythonic.py | 1 + 5 files changed, 78 insertions(+) diff --git a/web3/_utils/module_testing/eth_module.py b/web3/_utils/module_testing/eth_module.py index dc96dd01be..16e1e97b5a 100644 --- a/web3/_utils/module_testing/eth_module.py +++ b/web3/_utils/module_testing/eth_module.py @@ -202,6 +202,76 @@ def test_eth_sign(self, web3, unlocked_account_dual_type): ) assert new_signature != signature + def test_eth_signTypedData(self, web3, unlocked_account_dual_type): + validJSONMessage = ''' + { + "types": { + "EIP712Domain": [ + {"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": "Ether Mail", + "version": "1", + "chainId": 1, + "verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" + }, + "message": { + "from": { + "name": "Cow", + "wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" + }, + "to": { + "name": "Bob", + "wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" + }, + "contents": "Hello, Bob!" + } + } + ''' + signature = web3.eth.signTypedData( + unlocked_account_dual_type, + validJSONMessage + ) + assert is_bytes(signature) + assert len(signature) == 32 + 32 + 1 + + # # test other formats + # hexsign = web3.eth.sign( + # unlocked_account_dual_type, + # hexstr='0x4d6573736167652074c3b6207369676e2e204c6f6e676572207468616e206861736821' + # ) + # assert hexsign == signature + # + # intsign = web3.eth.sign( + # unlocked_account_dual_type, + # 0x4d6573736167652074c3b6207369676e2e204c6f6e676572207468616e206861736821 + # ) + # assert intsign == signature + # + # bytessign = web3.eth.sign( + # unlocked_account_dual_type, b'Message t\xc3\xb6 sign. Longer than hash!' + # ) + # assert bytessign == signature + # + # new_signature = web3.eth.sign( + # unlocked_account_dual_type, text='different message is different' + # ) + # assert new_signature != signature + def test_eth_signTransaction(self, web3, unlocked_account): txn_params = { 'from': unlocked_account, diff --git a/web3/_utils/rpc_abi.py b/web3/_utils/rpc_abi.py index 880a57e69e..8dc523d129 100644 --- a/web3/_utils/rpc_abi.py +++ b/web3/_utils/rpc_abi.py @@ -52,6 +52,7 @@ 'eth_sendTransaction': TRANSACTION_PARAMS_ABIS, 'eth_signTransaction': TRANSACTION_PARAMS_ABIS, 'eth_sign': ['address', 'bytes'], + 'eth_signTypedData': ['address', None], 'eth_submitHashrate': ['uint', 'bytes32'], 'eth_submitWork': ['bytes8', 'bytes32', 'bytes32'], # personal diff --git a/web3/eth.py b/web3/eth.py index b8f442aaa3..79f874e478 100644 --- a/web3/eth.py +++ b/web3/eth.py @@ -320,6 +320,11 @@ def signTransaction(self, transaction): "eth_signTransaction", [transaction], ) + def signTypedData(self, account, jsonMessage): + return self.web3.manager.request_blocking( + "eth_signTypedData", [account, jsonMessage], + ) + @apply_to_return_value(HexBytes) def call(self, transaction, block_identifier=None): # TODO: move to middleware diff --git a/web3/middleware/exception_retry_request.py b/web3/middleware/exception_retry_request.py index 56210648f5..8ec90b5365 100644 --- a/web3/middleware/exception_retry_request.py +++ b/web3/middleware/exception_retry_request.py @@ -47,6 +47,7 @@ 'eth_getCompilers', 'eth_getWork', 'eth_sign', + 'eth_signTypedData', 'eth_sendRawTransaction', 'personal_importRawKey', 'personal_newAccount', diff --git a/web3/middleware/pythonic.py b/web3/middleware/pythonic.py index 7296c0a2d0..5ad2efb3cf 100644 --- a/web3/middleware/pythonic.py +++ b/web3/middleware/pythonic.py @@ -355,6 +355,7 @@ def to_hexbytes(num_bytes, val, variable_length=False): 'eth_sendTransaction': to_hexbytes(32), 'eth_signTransaction': apply_formatter_if(is_not_null, signed_tx_formatter), 'eth_sign': HexBytes, + 'eth_signTypedData': HexBytes, 'eth_syncing': apply_formatter_if(is_not_false, syncing_formatter), # personal 'personal_importRawKey': to_checksum_address,