Skip to content

Commit

Permalink
Add support for eth_signTypedData RPC call
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhargavasomu committed Apr 8, 2019
1 parent cba9538 commit 6c5d71a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
70 changes: 70 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions web3/_utils/rpc_abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ def signTransaction(self, transaction):
"eth_signTransaction", [transaction],
)

def signTypedData(self, account, jsonMessage):
return self.web3.manager.request_blocking(
"account_signTypedData", [account, jsonMessage],
)

@apply_to_return_value(HexBytes)
def call(self, transaction, block_identifier=None):
# TODO: move to middleware
Expand Down
1 change: 1 addition & 0 deletions web3/middleware/exception_retry_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'eth_getCompilers',
'eth_getWork',
'eth_sign',
'eth_signTypedData',
'eth_sendRawTransaction',
'personal_importRawKey',
'personal_newAccount',
Expand Down
1 change: 1 addition & 0 deletions web3/middleware/pythonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6c5d71a

Please sign in to comment.