Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async eth.sign #2833

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ Eth
- :meth:`web3.eth.send_transaction() <web3.eth.Eth.send_transaction>`
- :meth:`web3.eth.send_raw_transaction() <web3.eth.Eth.send_raw_transaction>`
- :meth:`web3.eth.wait_for_transaction_receipt() <web3.eth.Eth.wait_for_transaction_receipt>`
- :meth:`web3.eth.sign() <web3.eth.Eth.sign>`

Net
***
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2833.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the ``sign`` method to the ``AsyncEth`` class
48 changes: 48 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,54 @@ async def test_eth_getBlockTransactionCountByNumber_block_with_txn(
assert is_integer(transaction_count)
assert transaction_count >= 1

async def test_async_eth_sign(
e3243eric marked this conversation as resolved.
Show resolved Hide resolved
self, async_w3: "AsyncWeb3", unlocked_account_dual_type: ChecksumAddress
) -> None:
signature = await async_w3.eth.sign(
unlocked_account_dual_type, text="Message tö sign. Longer than hash!"
)
assert is_bytes(signature)
assert len(signature) == 32 + 32 + 1

# test other formats
hexsign = await async_w3.eth.sign(
unlocked_account_dual_type,
hexstr=HexStr(
"0x4d6573736167652074c3b6207369676e2e204c6f6e676572207468616e206861736821" # noqa: E501
),
)
assert hexsign == signature

intsign = await async_w3.eth.sign(
unlocked_account_dual_type,
0x4D6573736167652074C3B6207369676E2E204C6F6E676572207468616E206861736821,
)
assert intsign == signature

bytessign = await async_w3.eth.sign(
unlocked_account_dual_type, b"Message t\xc3\xb6 sign. Longer than hash!"
)
assert bytessign == signature

new_signature = await async_w3.eth.sign(
unlocked_account_dual_type, text="different message is different"
)
assert new_signature != signature

@pytest.mark.asyncio
@pytest.mark.xfail(
reason="Async middleware to convert ENS names to addresses is missing"
)
async def test_async_eth_sign_ens_names(
self, async_w3: "AsyncWeb3", unlocked_account_dual_type: ChecksumAddress
) -> None:
with ens_addresses(async_w3, {"unlocked-acct.eth": unlocked_account_dual_type}):
signature = await async_w3.eth.sign(
ENS("unlocked-acct.eth"), text="Message tö sign. Longer than hash!"
)
assert is_bytes(signature)
assert len(signature) == 32 + 32 + 1

@pytest.mark.asyncio
async def test_async_eth_new_filter(self, async_w3: "AsyncWeb3") -> None:
filter = await async_w3.eth.filter({})
Expand Down
15 changes: 15 additions & 0 deletions web3/eth/async_eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,21 @@ async def get_storage_at(
) -> HexBytes:
return await self._get_storage_at(account, position, block_identifier)

# eth_sign

_sign: Method[Callable[..., Awaitable[HexStr]]] = Method(
RPC.eth_sign, mungers=[BaseEth.sign_munger]
)

async def sign(
self,
account: Union[Address, ChecksumAddress, ENS],
data: Union[int, bytes] = None,
hexstr: HexStr = None,
text: str = None,
) -> HexStr:
return await self._sign(account, data, hexstr, text)

# eth_newFilter, eth_newBlockFilter, eth_newPendingTransactionFilter

filter: Method[
Expand Down
13 changes: 13 additions & 0 deletions web3/eth/base_eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
Empty,
empty,
)
from web3._utils.encoding import (
to_hex,
)
from web3.module import (
Module,
)
Expand Down Expand Up @@ -149,6 +152,16 @@ def call_munger(
else:
return (transaction, block_identifier, state_override)

def sign_munger(
self,
account: Union[Address, ChecksumAddress, ENS],
data: Union[int, bytes] = None,
hexstr: HexStr = None,
text: str = None,
) -> Tuple[Union[Address, ChecksumAddress, ENS], HexStr]:
message_hex = to_hex(data, hexstr=hexstr, text=text)
return (account, message_hex)

def filter_munger(
self,
filter_params: Optional[Union[str, FilterParams]] = None,
Expand Down
17 changes: 3 additions & 14 deletions web3/eth/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
from web3._utils.blocks import (
select_method_for_block_identifier,
)
from web3._utils.encoding import (
to_hex,
)
from web3._utils.fee_utils import (
fee_history_priority_fee,
)
Expand Down Expand Up @@ -566,17 +563,9 @@ def modify_transaction(

# eth_sign

def sign_munger(
self,
account: Union[Address, ChecksumAddress, ENS],
data: Union[int, bytes] = None,
hexstr: HexStr = None,
text: str = None,
) -> Tuple[Union[Address, ChecksumAddress, ENS], HexStr]:
message_hex = to_hex(data, hexstr=hexstr, text=text)
return (account, message_hex)

sign: Method[Callable[..., HexStr]] = Method(RPC.eth_sign, mungers=[sign_munger])
sign: Method[Callable[..., HexStr]] = Method(
RPC.eth_sign, mungers=[BaseEth.sign_munger]
)

# eth_signTransaction

Expand Down