Skip to content

Commit 60b1afb

Browse files
committed
Add support for eth_signTypedData RPC call
1 parent cba9538 commit 60b1afb

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

web3/_utils/module_testing/eth_module.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,76 @@ def test_eth_sign(self, web3, unlocked_account_dual_type):
202202
)
203203
assert new_signature != signature
204204

205+
def test_eth_signTypedData(self, web3, unlocked_account_dual_type):
206+
validJSONMessage = '''
207+
{
208+
"types": {
209+
"EIP712Domain": [
210+
{"name": "name", "type": "string"},
211+
{"name": "version", "type": "string"},
212+
{"name": "chainId", "type": "uint256"},
213+
{"name": "verifyingContract", "type": "address"}
214+
],
215+
"Person": [
216+
{"name": "name", "type": "string"},
217+
{"name": "wallet", "type": "address"}
218+
],
219+
"Mail": [
220+
{"name": "from", "type": "Person"},
221+
{"name": "to", "type": "Person"},
222+
{"name": "contents", "type": "string"}
223+
]
224+
},
225+
"primaryType": "Mail",
226+
"domain": {
227+
"name": "Ether Mail",
228+
"version": "1",
229+
"chainId": 1,
230+
"verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
231+
},
232+
"message": {
233+
"from": {
234+
"name": "Cow",
235+
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
236+
},
237+
"to": {
238+
"name": "Bob",
239+
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
240+
},
241+
"contents": "Hello, Bob!"
242+
}
243+
}
244+
'''
245+
signature = web3.eth.signTypedData(
246+
unlocked_account_dual_type,
247+
validJSONMessage
248+
)
249+
assert is_bytes(signature)
250+
assert len(signature) == 32 + 32 + 1
251+
252+
# # test other formats
253+
# hexsign = web3.eth.sign(
254+
# unlocked_account_dual_type,
255+
# hexstr='0x4d6573736167652074c3b6207369676e2e204c6f6e676572207468616e206861736821'
256+
# )
257+
# assert hexsign == signature
258+
#
259+
# intsign = web3.eth.sign(
260+
# unlocked_account_dual_type,
261+
# 0x4d6573736167652074c3b6207369676e2e204c6f6e676572207468616e206861736821
262+
# )
263+
# assert intsign == signature
264+
#
265+
# bytessign = web3.eth.sign(
266+
# unlocked_account_dual_type, b'Message t\xc3\xb6 sign. Longer than hash!'
267+
# )
268+
# assert bytessign == signature
269+
#
270+
# new_signature = web3.eth.sign(
271+
# unlocked_account_dual_type, text='different message is different'
272+
# )
273+
# assert new_signature != signature
274+
205275
def test_eth_signTransaction(self, web3, unlocked_account):
206276
txn_params = {
207277
'from': unlocked_account,

web3/_utils/rpc_abi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
'eth_sendTransaction': TRANSACTION_PARAMS_ABIS,
5353
'eth_signTransaction': TRANSACTION_PARAMS_ABIS,
5454
'eth_sign': ['address', 'bytes'],
55+
'eth_signTypedData': ['address', None],
5556
'eth_submitHashrate': ['uint', 'bytes32'],
5657
'eth_submitWork': ['bytes8', 'bytes32', 'bytes32'],
5758
# personal

web3/eth.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ def signTransaction(self, transaction):
320320
"eth_signTransaction", [transaction],
321321
)
322322

323+
def signTypedData(self, account, jsonMessage):
324+
return self.web3.manager.request_blocking(
325+
"eth_signTypedData", [account, jsonMessage],
326+
)
327+
323328
@apply_to_return_value(HexBytes)
324329
def call(self, transaction, block_identifier=None):
325330
# TODO: move to middleware

web3/middleware/exception_retry_request.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
'eth_getCompilers',
4848
'eth_getWork',
4949
'eth_sign',
50+
'eth_signTypedData',
5051
'eth_sendRawTransaction',
5152
'personal_importRawKey',
5253
'personal_newAccount',

web3/middleware/pythonic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ def to_hexbytes(num_bytes, val, variable_length=False):
355355
'eth_sendTransaction': to_hexbytes(32),
356356
'eth_signTransaction': apply_formatter_if(is_not_null, signed_tx_formatter),
357357
'eth_sign': HexBytes,
358+
'eth_signTypedData': HexBytes,
358359
'eth_syncing': apply_formatter_if(is_not_false, syncing_formatter),
359360
# personal
360361
'personal_importRawKey': to_checksum_address,

0 commit comments

Comments
 (0)