Skip to content

Commit ad85d35

Browse files
committed
Add web3.eth.signTransaction test to integration tests
1 parent e4f46cf commit ad85d35

File tree

7 files changed

+40
-0
lines changed

7 files changed

+40
-0
lines changed

tests/integration/go_ethereum/common.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
Web3ModuleTest,
99
)
1010

11+
GETH_EXPECTED = b'\xf8j\x80\x85\x040\xe24\x00\x82R\x08\x94\xdcTM\x1a\xa8\x8f\xf8\xbb\xd2\xf2\xae\xc7T\xb1\xf1\xe9\x9e\x18\x12\xfd\x01\x80\x86\xee\xca\xc4f\xe1\x16\xa0\xbb7^\x1f\xf0\x03(P\x07|\x053Q\xd3M\xf1\x83\xe9\xdcp\xdc\x02\xb4\xe7`\x85\xcd\x84\xdb\xb4\xd0\xaa\xa07\x8cl\xd7\xa6R\x01\xfaW\x0e\x0f\xc1_$\xdf`\x8dO\x18\x1dC\xbc\x87\x8fud\xd2R*W\xfd4' # noqa: E501
12+
1113

1214
class GoEthereumTest(Web3ModuleTest):
1315
def _check_web3_clientVersion(self, client_version):
@@ -59,6 +61,9 @@ def test_eth_estimateGas_with_block(self,
5961
web3, unlocked_account_dual_type
6062
)
6163

64+
def test_eth_signTransaction(self, web3, unlocked_account):
65+
super().test_eth_signTransaction(web3, unlocked_account, GETH_EXPECTED)
66+
6267

6368
class GoEthereumVersionModuleTest(VersionModuleTest):
6469
pass

tests/integration/test_ethereum_tester.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ def func_wrapper(self, eth_tester, *args, **kwargs):
216216

217217
class TestEthereumTesterEthModule(EthModuleTest):
218218
test_eth_sign = not_implemented(EthModuleTest.test_eth_sign, ValueError)
219+
test_eth_signTransaction = not_implemented(EthModuleTest.test_eth_signTransaction, ValueError)
219220

220221
@disable_auto_mine
221222
def test_eth_getTransactionReceipt_unmined(self, eth_tester, web3, unlocked_account):

web3/_utils/module_testing/eth_module.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,23 @@ def test_eth_sign(self, web3, unlocked_account_dual_type):
196196
)
197197
assert new_signature != signature
198198

199+
def test_eth_signTransaction(self, web3, unlocked_account, geth_expected=None):
200+
txn_params = {
201+
'from': unlocked_account,
202+
'to': unlocked_account,
203+
'value': 1,
204+
'gas': 21000,
205+
'gasPrice': web3.eth.gasPrice,
206+
'nonce': 0,
207+
}
208+
COINBASE_PK = '0x58d23b55bc9cdce1f18c2500f40ff4ab7245df9a89505e9b1fa4851f623d241d'
209+
result = web3.eth.signTransaction(txn_params)
210+
actual = web3.eth.account.signTransaction(txn_params, COINBASE_PK)
211+
if geth_expected:
212+
assert result['raw'] == geth_expected
213+
else:
214+
assert result['raw'] == actual.rawTransaction
215+
199216
def test_eth_sendTransaction_addr_checksum_required(self, web3, unlocked_account):
200217
non_checksum_addr = unlocked_account.lower()
201218
txn_params = {

web3/_utils/rpc_abi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
'eth_newFilter': FILTER_PARAMS_ABIS,
5151
'eth_sendRawTransaction': ['bytes'],
5252
'eth_sendTransaction': TRANSACTION_PARAMS_ABIS,
53+
'eth_signTransaction': TRANSACTION_PARAMS_ABIS,
5354
'eth_sign': ['address', 'bytes'],
5455
# personal
5556
'personal_sendTransaction': TRANSACTION_PARAMS_ABIS,

web3/eth.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ def sign(self, account, data=None, hexstr=None, text=None):
311311
"eth_sign", [account, message_hex],
312312
)
313313

314+
def signTransaction(self, transaction):
315+
return self.web3.manager.request_blocking(
316+
"eth_signTransaction", [transaction],
317+
)
318+
314319
@apply_to_return_value(HexBytes)
315320
def call(self, transaction, block_identifier=None):
316321
# TODO: move to middleware

web3/middleware/pythonic.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ def to_hexbytes(num_bytes, val, variable_length=False):
109109
transaction_formatter = apply_formatters_to_dict(TRANSACTION_FORMATTERS)
110110

111111

112+
SIGNED_TX_FORMATTER = {
113+
'raw': HexBytes,
114+
'tx': transaction_formatter,
115+
}
116+
117+
118+
signed_tx_formatter = apply_formatters_to_dict(SIGNED_TX_FORMATTER)
119+
120+
112121
WHISPER_LOG_FORMATTERS = {
113122
'sig': to_hexbytes(130),
114123
'topic': to_hexbytes(8),
@@ -344,6 +353,7 @@ def to_hexbytes(num_bytes, val, variable_length=False):
344353
),
345354
'eth_sendRawTransaction': to_hexbytes(32),
346355
'eth_sendTransaction': to_hexbytes(32),
356+
'eth_signTransaction': apply_formatter_if(is_not_null, signed_tx_formatter),
347357
'eth_sign': HexBytes,
348358
'eth_syncing': apply_formatter_if(is_not_false, syncing_formatter),
349359
# personal

web3/providers/eth_tester/defaults.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def personal_send_transaction(eth_tester, params):
201201
)),
202202
'getCode': call_eth_tester('get_code'),
203203
'sign': not_implemented,
204+
'signTransaction': not_implemented,
204205
'sendTransaction': call_eth_tester('send_transaction'),
205206
'sendRawTransaction': call_eth_tester('send_raw_transaction'),
206207
'call': call_eth_tester('call'), # TODO: untested

0 commit comments

Comments
 (0)