Skip to content

Commit

Permalink
Add sign_transaction, deprecate signTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
kclowes committed Feb 18, 2021
1 parent fbaf1ad commit a728caf
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ Methods
:meth:`~web3.eth.Eth.send_transaction`.

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

.. code-block:: python
Expand Down
2 changes: 1 addition & 1 deletion docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ Just remember that you have to sign all transactions locally, as infura does not
transaction = contract.functions.function_Name(params).buildTransaction()
transaction.update({ 'gas' : appropriate_gas_amount })
transaction.update({ 'nonce' : w3.eth.get_transaction_count('Your_Wallet_Address') })
signed_tx = w3.eth.account.signTransaction(transaction, private_key)
signed_tx = w3.eth.account.sign_transaction(transaction, private_key)
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.

Expand Down
4 changes: 2 additions & 2 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Making Transactions

The most common use cases will be satisfied with
:meth:`send_transaction <web3.eth.Eth.send_transaction>` or the combination of
:meth:`signTransaction <web3.eth.Eth.signTransaction>` and
:meth:`sign_transaction <web3.eth.Eth.sign_transaction>` and
:meth:`sendRawTransaction <web3.eth.Eth.sendRawTransaction>`.

.. note::
Expand All @@ -171,7 +171,7 @@ API
^^^

- :meth:`web3.eth.send_transaction() <web3.eth.Eth.send_transaction>`
- :meth:`web3.eth.signTransaction() <web3.eth.Eth.signTransaction>`
- :meth:`web3.eth.sign_transaction() <web3.eth.Eth.sign_transaction>`
- :meth:`web3.eth.sendRawTransaction() <web3.eth.Eth.sendRawTransaction>`
- :meth:`web3.eth.replaceTransaction() <web3.eth.Eth.replaceTransaction>`
- :meth:`web3.eth.modifyTransaction() <web3.eth.Eth.modifyTransaction>`
Expand Down
11 changes: 8 additions & 3 deletions docs/web3.eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,9 @@ The following methods are available on the ``web3.eth`` namespace.
.. py:method:: Eth.sendTransaction(transaction)
.. warning:: Deprecated: This property is deprecated in favor of
:attr:`~web3.eth.Eth.send_transaction`
:attr:`~web3.eth.Eth.send_transaction()`

.. py:method:: Eth.signTransaction(transaction)
.. py:method:: Eth.sign_transaction(transaction)
* Delegates to ``eth_signTransaction`` RPC Method.

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

.. code-block:: python
>>> signed_txn = w3.eth.signTransaction(dict(
>>> signed_txn = w3.eth.sign_transaction(dict(
nonce=w3.eth.get_transaction_count(w3.eth.coinbase),
gasPrice=w3.eth.gas_price,
gas=100000,
Expand All @@ -760,6 +760,11 @@ The following methods are available on the ``web3.eth`` namespace.
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
.. py:method:: Eth.signTransaction(transaction)
.. warning:: Deprecated: This property is deprecated in favor of
:attr:`~web3.eth.Eth.sign_transaction()`

.. py:method:: Eth.sendRawTransaction(raw_transaction)
* Delegates to ``eth_sendRawTransaction`` RPC Method
Expand Down
10 changes: 7 additions & 3 deletions tests/integration/test_ethereum_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,13 @@ class TestEthereumTesterEthModule(EthModuleTest):
EthModuleTest.test_eth_sign_ens_names, ValueError
)
test_eth_signTypedData = not_implemented(EthModuleTest.test_eth_signTypedData, ValueError)
test_eth_signTransaction = not_implemented(EthModuleTest.test_eth_signTransaction, ValueError)
test_eth_signTransaction_ens_names = not_implemented(
EthModuleTest.test_eth_signTransaction_ens_names, ValueError
test_eth_signTransaction_deprecated = not_implemented(
EthModuleTest.test_eth_signTransaction_deprecated,
ValueError
)
test_eth_sign_transaction = not_implemented(EthModuleTest.test_eth_sign_transaction, ValueError)
test_eth_sign_transaction_ens_names = not_implemented(
EthModuleTest.test_eth_sign_transaction_ens_names, ValueError
)
test_eth_submitHashrate = not_implemented(EthModuleTest.test_eth_submitHashrate, ValueError)
test_eth_submitWork = not_implemented(EthModuleTest.test_eth_submitWork, ValueError)
Expand Down
30 changes: 26 additions & 4 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def test_invalid_eth_signTypedData(
json.loads(invalid_typed_message)
)

def test_eth_signTransaction(self, web3: "Web3", unlocked_account: ChecksumAddress) -> None:
def test_eth_sign_transaction(self, web3: "Web3", unlocked_account: ChecksumAddress) -> None:
txn_params: TxParams = {
'from': unlocked_account,
'to': unlocked_account,
Expand All @@ -526,7 +526,7 @@ def test_eth_signTransaction(self, web3: "Web3", unlocked_account: ChecksumAddre
'gasPrice': web3.eth.gas_price,
'nonce': Nonce(0),
}
result = web3.eth.signTransaction(txn_params)
result = web3.eth.sign_transaction(txn_params)
signatory_account = web3.eth.account.recover_transaction(result['raw'])
assert unlocked_account == signatory_account
assert result['tx']['to'] == txn_params['to']
Expand All @@ -535,7 +535,29 @@ def test_eth_signTransaction(self, web3: "Web3", unlocked_account: ChecksumAddre
assert result['tx']['gasPrice'] == txn_params['gasPrice']
assert result['tx']['nonce'] == txn_params['nonce']

def test_eth_signTransaction_ens_names(
def test_eth_signTransaction_deprecated(self,
web3: "Web3",
unlocked_account: ChecksumAddress) -> None:
txn_params: TxParams = {
'from': unlocked_account,
'to': unlocked_account,
'value': Wei(1),
'gas': Wei(21000),
'gasPrice': web3.eth.gas_price,
'nonce': Nonce(0),
}
with pytest.warns(DeprecationWarning,
match='signTransaction is deprecated in favor of sign_transaction'):
result = web3.eth.signTransaction(txn_params)
signatory_account = web3.eth.account.recover_transaction(result['raw'])
assert unlocked_account == signatory_account
assert result['tx']['to'] == txn_params['to']
assert result['tx']['value'] == txn_params['value']
assert result['tx']['gas'] == txn_params['gas']
assert result['tx']['gasPrice'] == txn_params['gasPrice']
assert result['tx']['nonce'] == txn_params['nonce']

def test_eth_sign_transaction_ens_names(
self, web3: "Web3", unlocked_account: ChecksumAddress
) -> None:
with ens_addresses(web3, {'unlocked-account.eth': unlocked_account}):
Expand All @@ -547,7 +569,7 @@ def test_eth_signTransaction_ens_names(
'gasPrice': web3.eth.gas_price,
'nonce': Nonce(0),
}
result = web3.eth.signTransaction(txn_params)
result = web3.eth.sign_transaction(txn_params)
signatory_account = web3.eth.account.recover_transaction(result['raw'])
assert unlocked_account == signatory_account
assert result['tx']['to'] == unlocked_account
Expand Down
3 changes: 2 additions & 1 deletion web3/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def sign_munger(
mungers=[sign_munger],
)

signTransaction: Method[Callable[[TxParams], SignedTx]] = Method(
sign_transaction: Method[Callable[[TxParams], SignedTx]] = Method(
RPC.eth_signTransaction,
mungers=[default_root_munger],
)
Expand Down Expand Up @@ -667,3 +667,4 @@ def setGasPriceStrategy(self, gas_price_strategy: GasPriceStrategy) -> None:
getUncleByBlock = DeprecatedMethod(get_uncle_by_block, 'getUncleByBlock', 'get_uncle_by_block')
getUncleCount = DeprecatedMethod(get_uncle_count, 'getUncleCount', 'get_uncle_count')
sendTransaction = DeprecatedMethod(send_transaction, 'sendTransaction', 'send_transaction')
signTransaction = DeprecatedMethod(sign_transaction, 'signTransaction', 'sign_transaction')

0 comments on commit a728caf

Please sign in to comment.