Skip to content

Commit 60e7c9a

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

File tree

7 files changed

+42
-0
lines changed

7 files changed

+42
-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 = '0xf86a04850430e2340082520894dc544d1aa88ff8bbd2f2aec754b1f1e99e1812fd018086eecac466e115a0b9914e8e1dd060257c6b753035479afa61ee55f9a3220b3366946d4591ee12aaa02ebc5717df48c58eecdc2971ab0baf607c75e0d9a14d95a9b5f7003380201687' # 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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
UNKNOWN_ADDRESS = '0xdEADBEeF00000000000000000000000000000000'
2929
UNKNOWN_HASH = '0xdeadbeef00000000000000000000000000000000000000000000000000000000'
30+
EXPECTED = '0xf86404850430e2340082520894dc544d1aa88ff8bbd2f2aec754b1f1e99e1812fd01801ca001119d1011fc4d00f2a2dc8fa3557c5a36f4022e5a3cabec52626a1993c3ba42a050148defdd5ebbdef5dcdb88485949c3005f1d401b0fad64bccc21617d5456a4' # noqa: E501
3031

3132

3233
class EthModuleTest:
@@ -196,6 +197,25 @@ def test_eth_sign(self, web3, unlocked_account_dual_type):
196197
)
197198
assert new_signature != signature
198199

200+
def test_eth_signTransaction(self, web3, unlocked_account):
201+
txn_params = {
202+
'from': unlocked_account,
203+
'to': unlocked_account,
204+
'value': 1,
205+
'gas': 21000,
206+
'gasPrice': web3.eth.gasPrice,
207+
'nonce': 0,
208+
}
209+
print(unlocked_account)
210+
print('0xdc544d1aa88ff8bbd2f2aec754b1f1e99e1812fd')
211+
COINBASE_PK = '0x58d23b55bc9cdce1f18c2500f40ff4ab7245df9a89505e9b1fa4851f623d241d'
212+
result = web3.eth.signTransaction(txn_params)
213+
actual = web3.eth.account.signTransaction(txn_params, COINBASE_PK)
214+
print(result)
215+
print('---')
216+
print(actual)
217+
assert result['raw'] == actual.rawTransaction
218+
199219
def test_eth_sendTransaction_addr_checksum_required(self, web3, unlocked_account):
200220
non_checksum_addr = unlocked_account.lower()
201221
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ 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+
'tx': transaction_formatter,
114+
}
115+
116+
117+
signed_tx_formatter = apply_formatters_to_dict(SIGNED_TX_FORMATTER)
118+
119+
112120
WHISPER_LOG_FORMATTERS = {
113121
'sig': to_hexbytes(130),
114122
'topic': to_hexbytes(8),
@@ -344,6 +352,7 @@ def to_hexbytes(num_bytes, val, variable_length=False):
344352
),
345353
'eth_sendRawTransaction': to_hexbytes(32),
346354
'eth_sendTransaction': to_hexbytes(32),
355+
'eth_signTransaction': apply_formatter_if(is_not_null, signed_tx_formatter),
347356
'eth_sign': HexBytes,
348357
'eth_syncing': apply_formatter_if(is_not_false, syncing_formatter),
349358
# 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)