Skip to content

Commit c5f8a15

Browse files
committed
Add sign_transaction, deprecate signTransaction
1 parent fbaf1ad commit c5f8a15

File tree

8 files changed

+48
-15
lines changed

8 files changed

+48
-15
lines changed

docs/contracts.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ Methods
885885
:meth:`~web3.eth.Eth.send_transaction`.
886886

887887
Additionally, the dictionary may be used for offline transaction signing using
888-
:meth:`~web3.eth.account.Account.signTransaction`.
888+
:meth:`~web3.eth.account.Account.sign_transaction`.
889889

890890
.. code-block:: python
891891

docs/examples.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ Just remember that you have to sign all transactions locally, as infura does not
649649
transaction = contract.functions.function_Name(params).buildTransaction()
650650
transaction.update({ 'gas' : appropriate_gas_amount })
651651
transaction.update({ 'nonce' : w3.eth.get_transaction_count('Your_Wallet_Address') })
652-
signed_tx = w3.eth.account.signTransaction(transaction, private_key)
652+
signed_tx = w3.eth.account.sign_transaction(transaction, private_key)
653653
654654
P.S : the two updates are done to the transaction dictionary, since a raw transaction might not contain gas & nonce amounts, so you have to add them manually.
655655

docs/overview.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Making Transactions
158158

159159
The most common use cases will be satisfied with
160160
:meth:`send_transaction <web3.eth.Eth.send_transaction>` or the combination of
161-
:meth:`signTransaction <web3.eth.Eth.signTransaction>` and
161+
:meth:`sign_transaction <web3.eth.Eth.sign_transaction>` and
162162
:meth:`sendRawTransaction <web3.eth.Eth.sendRawTransaction>`.
163163

164164
.. note::
@@ -171,7 +171,7 @@ API
171171
^^^
172172

173173
- :meth:`web3.eth.send_transaction() <web3.eth.Eth.send_transaction>`
174-
- :meth:`web3.eth.signTransaction() <web3.eth.Eth.signTransaction>`
174+
- :meth:`web3.eth.sign_transaction() <web3.eth.Eth.sign_transaction>`
175175
- :meth:`web3.eth.sendRawTransaction() <web3.eth.Eth.sendRawTransaction>`
176176
- :meth:`web3.eth.replaceTransaction() <web3.eth.Eth.replaceTransaction>`
177177
- :meth:`web3.eth.modifyTransaction() <web3.eth.Eth.modifyTransaction>`

docs/web3.eth.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,9 +737,9 @@ The following methods are available on the ``web3.eth`` namespace.
737737
.. py:method:: Eth.sendTransaction(transaction)
738738
739739
.. warning:: Deprecated: This property is deprecated in favor of
740-
:attr:`~web3.eth.Eth.send_transaction`
740+
:attr:`~web3.eth.Eth.send_transaction()`
741741

742-
.. py:method:: Eth.signTransaction(transaction)
742+
.. py:method:: Eth.sign_transaction(transaction)
743743
744744
* Delegates to ``eth_signTransaction`` RPC Method.
745745

@@ -748,7 +748,7 @@ The following methods are available on the ``web3.eth`` namespace.
748748

749749
.. code-block:: python
750750
751-
>>> signed_txn = w3.eth.signTransaction(dict(
751+
>>> signed_txn = w3.eth.sign_transaction(dict(
752752
nonce=w3.eth.get_transaction_count(w3.eth.coinbase),
753753
gasPrice=w3.eth.gas_price,
754754
gas=100000,
@@ -760,6 +760,11 @@ The following methods are available on the ``web3.eth`` namespace.
760760
b"\xf8d\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\x1b\xa0\x11\r\x8f\xee\x1d\xe5=\xf0\x87\x0en\xb5\x99\xed;\xf6\x8f\xb3\xf1\xe6,\x82\xdf\xe5\x97lF|\x97%;\x15\xa04P\xb7=*\xef \t\xf0&\xbc\xbf\tz%z\xe7\xa3~\xb5\xd3\xb7=\xc0v\n\xef\xad+\x98\xe3'" # noqa: E501
761761
762762
763+
.. py:method:: Eth.signTransaction(transaction)
764+
765+
.. warning:: Deprecated: This property is deprecated in favor of
766+
:attr:`~web3.eth.Eth.sign_transaction()`
767+
763768
.. py:method:: Eth.sendRawTransaction(raw_transaction)
764769
765770
* Delegates to ``eth_sendRawTransaction`` RPC Method

newsfragments/1879.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``web3.eth.sign_transaction``, deprecate ``web3.eth.signTransaction``

tests/integration/test_ethereum_tester.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,13 @@ class TestEthereumTesterEthModule(EthModuleTest):
243243
EthModuleTest.test_eth_sign_ens_names, ValueError
244244
)
245245
test_eth_signTypedData = not_implemented(EthModuleTest.test_eth_signTypedData, ValueError)
246-
test_eth_signTransaction = not_implemented(EthModuleTest.test_eth_signTransaction, ValueError)
247-
test_eth_signTransaction_ens_names = not_implemented(
248-
EthModuleTest.test_eth_signTransaction_ens_names, ValueError
246+
test_eth_signTransaction_deprecated = not_implemented(
247+
EthModuleTest.test_eth_signTransaction_deprecated,
248+
ValueError
249+
)
250+
test_eth_sign_transaction = not_implemented(EthModuleTest.test_eth_sign_transaction, ValueError)
251+
test_eth_sign_transaction_ens_names = not_implemented(
252+
EthModuleTest.test_eth_sign_transaction_ens_names, ValueError
249253
)
250254
test_eth_submitHashrate = not_implemented(EthModuleTest.test_eth_submitHashrate, ValueError)
251255
test_eth_submitWork = not_implemented(EthModuleTest.test_eth_submitWork, ValueError)

web3/_utils/module_testing/eth_module.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ def test_invalid_eth_signTypedData(
517517
json.loads(invalid_typed_message)
518518
)
519519

520-
def test_eth_signTransaction(self, web3: "Web3", unlocked_account: ChecksumAddress) -> None:
520+
def test_eth_sign_transaction(self, web3: "Web3", unlocked_account: ChecksumAddress) -> None:
521521
txn_params: TxParams = {
522522
'from': unlocked_account,
523523
'to': unlocked_account,
@@ -526,7 +526,7 @@ def test_eth_signTransaction(self, web3: "Web3", unlocked_account: ChecksumAddre
526526
'gasPrice': web3.eth.gas_price,
527527
'nonce': Nonce(0),
528528
}
529-
result = web3.eth.signTransaction(txn_params)
529+
result = web3.eth.sign_transaction(txn_params)
530530
signatory_account = web3.eth.account.recover_transaction(result['raw'])
531531
assert unlocked_account == signatory_account
532532
assert result['tx']['to'] == txn_params['to']
@@ -535,7 +535,29 @@ def test_eth_signTransaction(self, web3: "Web3", unlocked_account: ChecksumAddre
535535
assert result['tx']['gasPrice'] == txn_params['gasPrice']
536536
assert result['tx']['nonce'] == txn_params['nonce']
537537

538-
def test_eth_signTransaction_ens_names(
538+
def test_eth_signTransaction_deprecated(self,
539+
web3: "Web3",
540+
unlocked_account: ChecksumAddress) -> None:
541+
txn_params: TxParams = {
542+
'from': unlocked_account,
543+
'to': unlocked_account,
544+
'value': Wei(1),
545+
'gas': Wei(21000),
546+
'gasPrice': web3.eth.gas_price,
547+
'nonce': Nonce(0),
548+
}
549+
with pytest.warns(DeprecationWarning,
550+
match='signTransaction is deprecated in favor of sign_transaction'):
551+
result = web3.eth.signTransaction(txn_params)
552+
signatory_account = web3.eth.account.recover_transaction(result['raw'])
553+
assert unlocked_account == signatory_account
554+
assert result['tx']['to'] == txn_params['to']
555+
assert result['tx']['value'] == txn_params['value']
556+
assert result['tx']['gas'] == txn_params['gas']
557+
assert result['tx']['gasPrice'] == txn_params['gasPrice']
558+
assert result['tx']['nonce'] == txn_params['nonce']
559+
560+
def test_eth_sign_transaction_ens_names(
539561
self, web3: "Web3", unlocked_account: ChecksumAddress
540562
) -> None:
541563
with ens_addresses(web3, {'unlocked-account.eth': unlocked_account}):
@@ -547,7 +569,7 @@ def test_eth_signTransaction_ens_names(
547569
'gasPrice': web3.eth.gas_price,
548570
'nonce': Nonce(0),
549571
}
550-
result = web3.eth.signTransaction(txn_params)
572+
result = web3.eth.sign_transaction(txn_params)
551573
signatory_account = web3.eth.account.recover_transaction(result['raw'])
552574
assert unlocked_account == signatory_account
553575
assert result['tx']['to'] == unlocked_account

web3/eth.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ def sign_munger(
493493
mungers=[sign_munger],
494494
)
495495

496-
signTransaction: Method[Callable[[TxParams], SignedTx]] = Method(
496+
sign_transaction: Method[Callable[[TxParams], SignedTx]] = Method(
497497
RPC.eth_signTransaction,
498498
mungers=[default_root_munger],
499499
)
@@ -667,3 +667,4 @@ def setGasPriceStrategy(self, gas_price_strategy: GasPriceStrategy) -> None:
667667
getUncleByBlock = DeprecatedMethod(get_uncle_by_block, 'getUncleByBlock', 'get_uncle_by_block')
668668
getUncleCount = DeprecatedMethod(get_uncle_count, 'getUncleCount', 'get_uncle_count')
669669
sendTransaction = DeprecatedMethod(send_transaction, 'sendTransaction', 'send_transaction')
670+
signTransaction = DeprecatedMethod(sign_transaction, 'signTransaction', 'sign_transaction')

0 commit comments

Comments
 (0)