From 6f392ab81da403dc1d52414cc8a5b868fe4af991 Mon Sep 17 00:00:00 2001 From: Larry Klosowski Date: Mon, 30 Nov 2026 00:57:59 -0800 Subject: [PATCH 1/6] test: RED a plain value send to a contract address is refused _sign_and_send with empty calldata and a recipient that holds code (the WrappedSALT and EntryPoint entries of the live 40204 book) must refuse with "This address is a contract. Use its deposit function instead." before signing; a plain account is sent as before; a call with calldata is not checked; unreadable code is refused. Red: CONTRACT_RECIPIENT does not exist yet. Signed-off-by: Larry Klosowski Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_019cgBjEUXiPasw1yPyGXkXC --- tests/test_plain_transfer_recipient.py | 87 ++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 tests/test_plain_transfer_recipient.py diff --git a/tests/test_plain_transfer_recipient.py b/tests/test_plain_transfer_recipient.py new file mode 100644 index 0000000..c4e7210 --- /dev/null +++ b/tests/test_plain_transfer_recipient.py @@ -0,0 +1,87 @@ +"""A plain value transfer (no calldata) to an address that holds code is refused before anything +is signed: the caller is pointed at the contract's own deposit function. The RPC answers here are +a scripted test fixture (patch.object on _rpc_call).""" +from unittest.mock import MagicMock, patch + +import pytest + +from citrate_sdk.client import CONTRACT_RECIPIENT, CitrateClient +from citrate_sdk.errors import CitrateError + +# The live 40204 book (citrate-chain contracts/addresses/40204.json, r1007): both are contracts. +WRAPPED_SALT = "0xaa918302b94a4b0e75e01e019cc6b819b4f7c906" +ENTRY_POINT = "0x97d5391a647429233e202f99231743c53a648f3c" +PLAIN = "0x1111111111111111111111111111111111111111" + + +def _client(): + c = CitrateClient(rpc_url="http://example", allow_insecure_http=True) + km = MagicMock() + km.get_address.return_value = "0x9858effd232b4033e47d90003d41ec34ecaeda94" + km.sign_transaction.return_value = "0xsigned" + c.key_manager = km + return c + + +def _rpc(methods, code_fails=False): + def answer(method, params=None): + methods.append(method) + if method == "eth_getCode": + if code_fails: + raise CitrateError("unavailable") + return "0x6080604052" if params[0].lower() in (WRAPPED_SALT, ENTRY_POINT) else "0x" + if method == "eth_getTransactionCount": + return "0x0" + if method == "eth_sendRawTransaction": + return "0x" + "22" * 32 + raise AssertionError(f"unexpected {method}") + + return answer + + +@pytest.mark.parametrize("to", [WRAPPED_SALT, ENTRY_POINT]) +def test_value_to_wrapped_salt_and_entry_point_is_refused(to): + c = _client() + methods = [] + with patch.object(c, "_rpc_call", side_effect=_rpc(methods)), patch.object(c, "verify_network"), patch.object( + c, "_eip155_chain_id", return_value=40204 + ): + with pytest.raises(CitrateError) as e: + c._sign_and_send(to, "0x", 10**18, 21_000) + assert str(e.value) == CONTRACT_RECIPIENT + assert CONTRACT_RECIPIENT == "This address is a contract. Use its deposit function instead." + assert "eth_sendRawTransaction" not in methods + c.key_manager.sign_transaction.assert_not_called() + + +def test_value_to_a_plain_account_is_sent_as_before(): + c = _client() + methods = [] + with patch.object(c, "_rpc_call", side_effect=_rpc(methods)), patch.object(c, "verify_network"), patch.object( + c, "_eip155_chain_id", return_value=40204 + ): + tx_hash = c._sign_and_send(PLAIN, "0x", 5, 21_000) + assert tx_hash.startswith("0x") + assert methods[0] == "eth_getCode" and methods[-1] == "eth_sendRawTransaction" + + +def test_a_call_with_calldata_is_not_checked(): + c = _client() + methods = [] + with patch.object(c, "_rpc_call", side_effect=_rpc(methods)), patch.object(c, "verify_network"), patch.object( + c, "_eip155_chain_id", return_value=40204 + ): + c._sign_and_send(WRAPPED_SALT, "0xd0e30db0", 5, 60_000) + assert "eth_getCode" not in methods + + +def test_unreadable_recipient_code_is_refused(): + c = _client() + methods = [] + with patch.object(c, "_rpc_call", side_effect=_rpc(methods, code_fails=True)), patch.object( + c, "verify_network" + ), patch.object(c, "_eip155_chain_id", return_value=40204): + with pytest.raises(CitrateError) as e: + c._sign_and_send(PLAIN, "0x", 5, 21_000) + assert "could not check the recipient" in str(e.value) + c.key_manager.sign_transaction.assert_not_called() From a34121eafe1fc52dc8d15fda3c24487a55af1181 Mon Sep 17 00:00:00 2001 From: Larry Klosowski Date: Mon, 30 Nov 2026 01:08:39 -0800 Subject: [PATCH 2/6] fix: a plain value send to an address that holds code is refused _sign_and_send now reads eth_getCode for the recipient of a plain value transfer (no calldata, value > 0) before signing: code present raises "This address is a contract. Use its deposit function instead." (exported as CONTRACT_RECIPIENT); code that cannot be read is refused. Calls with calldata and sends to plain accounts are unchanged. Signed-off-by: Larry Klosowski Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_019cgBjEUXiPasw1yPyGXkXC --- citrate_sdk/client.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/citrate_sdk/client.py b/citrate_sdk/client.py index cc6196f..546d618 100644 --- a/citrate_sdk/client.py +++ b/citrate_sdk/client.py @@ -43,6 +43,10 @@ def _event_topic(signature: str) -> str: return Web3.keccak(text=signature).hex().lower() +#: Shown when a plain value transfer (no calldata) names an address that holds code. +CONTRACT_RECIPIENT = "This address is a contract. Use its deposit function instead." + + class CitrateClient: """ Main client for interacting with Citrate blockchain. @@ -504,6 +508,21 @@ def _send_transaction( encoded = data if isinstance(data, str) else json.dumps(data) return self._sign_and_send(to_address, "0x" + encoded.encode().hex(), value, gas_limit) + def _check_plain_transfer_recipient(self, to_address: str) -> None: + """Refuse a plain value transfer (no calldata) to an address that holds code. + + Value for a contract goes through that contract's own function. Data source: + ``eth_getCode`` on ``rpc_url``; code that cannot be read is refused too. + """ + try: + code = self._rpc_call("eth_getCode", [to_address, "latest"]) + except Exception as e: # noqa: BLE001 - any failure fails closed + raise CitrateError(f"could not check the recipient: {e}") from e + if not isinstance(code, str) or not code.startswith("0x"): + raise CitrateError("could not check the recipient: eth_getCode returned no hex") + if code not in ("0x", "0x0"): + raise CitrateError(CONTRACT_RECIPIENT) + def _sign_and_send( self, to_address: str | None, data_hex: str, value: int, gas_limit: int ) -> str: @@ -518,6 +537,10 @@ def _sign_and_send( # r1006: refuse another network (same chain id, different genesis) before signing. self.verify_network() + # A plain value transfer to a contract is refused before anything is signed. + if to_address is not None and data_hex in ("", "0x") and value > 0: + self._check_plain_transfer_recipient(to_address) + # Get account info from_address = self.key_manager.get_address() nonce = self.get_nonce(from_address) From 5436f0abd66078bd8bff43774ff0886b68f84854 Mon Sep 17 00:00:00 2001 From: Larry Klosowski Date: Mon, 30 Nov 2026 01:11:20 -0800 Subject: [PATCH 3/6] test: type the recorded RPC method lists (mypy) Signed-off-by: Larry Klosowski Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_019cgBjEUXiPasw1yPyGXkXC --- tests/test_plain_transfer_recipient.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_plain_transfer_recipient.py b/tests/test_plain_transfer_recipient.py index c4e7210..99c3cfc 100644 --- a/tests/test_plain_transfer_recipient.py +++ b/tests/test_plain_transfer_recipient.py @@ -42,7 +42,7 @@ def answer(method, params=None): @pytest.mark.parametrize("to", [WRAPPED_SALT, ENTRY_POINT]) def test_value_to_wrapped_salt_and_entry_point_is_refused(to): c = _client() - methods = [] + methods: list[str] = [] with patch.object(c, "_rpc_call", side_effect=_rpc(methods)), patch.object(c, "verify_network"), patch.object( c, "_eip155_chain_id", return_value=40204 ): @@ -56,7 +56,7 @@ def test_value_to_wrapped_salt_and_entry_point_is_refused(to): def test_value_to_a_plain_account_is_sent_as_before(): c = _client() - methods = [] + methods: list[str] = [] with patch.object(c, "_rpc_call", side_effect=_rpc(methods)), patch.object(c, "verify_network"), patch.object( c, "_eip155_chain_id", return_value=40204 ): @@ -67,7 +67,7 @@ def test_value_to_a_plain_account_is_sent_as_before(): def test_a_call_with_calldata_is_not_checked(): c = _client() - methods = [] + methods: list[str] = [] with patch.object(c, "_rpc_call", side_effect=_rpc(methods)), patch.object(c, "verify_network"), patch.object( c, "_eip155_chain_id", return_value=40204 ): @@ -77,7 +77,7 @@ def test_a_call_with_calldata_is_not_checked(): def test_unreadable_recipient_code_is_refused(): c = _client() - methods = [] + methods: list[str] = [] with patch.object(c, "_rpc_call", side_effect=_rpc(methods, code_fails=True)), patch.object( c, "verify_network" ), patch.object(c, "_eip155_chain_id", return_value=40204): From e3bf33e822bbab1ee7b85afbc38506f3e262b784 Mon Sep 17 00:00:00 2001 From: Larry Klosowski Date: Mon, 30 Nov 2026 01:37:29 -0800 Subject: [PATCH 4/6] test: RED a plain value send to a member's smart wallet is allowed (R5 B1) A CitrateWalletFactory wallet is Solady's ERC1967 proxy whose implementation slot holds the book's CitrateWallet (receive() is empty), so a plain transfer credits it. The same proxy code for any other implementation, other code, or an unreadable slot is still refused. Pins the proxy runtime, its codehash and the ERC1967 slot as the definition Core shares. Message copy says why (R5 N2). Signed-off-by: Larry Klosowski Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_019cgBjEUXiPasw1yPyGXkXC --- tests/test_plain_transfer_recipient.py | 80 +++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/tests/test_plain_transfer_recipient.py b/tests/test_plain_transfer_recipient.py index 99c3cfc..821e5fd 100644 --- a/tests/test_plain_transfer_recipient.py +++ b/tests/test_plain_transfer_recipient.py @@ -5,13 +5,27 @@ import pytest +from eth_utils import keccak + +from citrate_sdk._generated import contract as _contract from citrate_sdk.client import CONTRACT_RECIPIENT, CitrateClient from citrate_sdk.errors import CitrateError +from citrate_sdk.identity.wallet import ( + CITRATE_WALLET_PROXY_CODEHASH, + CITRATE_WALLET_PROXY_RUNTIME, + ERC1967_IMPLEMENTATION_SLOT, +) # The live 40204 book (citrate-chain contracts/addresses/40204.json, r1007): both are contracts. WRAPPED_SALT = "0xaa918302b94a4b0e75e01e019cc6b819b4f7c906" ENTRY_POINT = "0x97d5391a647429233e202f99231743c53a648f3c" PLAIN = "0x1111111111111111111111111111111111111111" +# A CitrateWalletFactory smart wallet: Solady's ERC1967 proxy whose implementation slot holds the +# book's CitrateWallet (receive() is empty, so a plain transfer credits the wallet). +SMART_WALLET = "0x2222222222222222222222222222222222222222" +# The same proxy code pointing at some other implementation. +OTHER_PROXY = "0x3333333333333333333333333333333333333333" +OTHER_IMPL = "0x4444444444444444444444444444444444444444" def _client(): @@ -23,13 +37,25 @@ def _client(): return c -def _rpc(methods, code_fails=False): +def _slot(addr): + return "0x" + "00" * 12 + addr[2:].lower() + + +def _rpc(methods, code_fails=False, slot_fails=False): def answer(method, params=None): methods.append(method) if method == "eth_getCode": if code_fails: raise CitrateError("unavailable") + if params[0].lower() in (SMART_WALLET, OTHER_PROXY): + return CITRATE_WALLET_PROXY_RUNTIME return "0x6080604052" if params[0].lower() in (WRAPPED_SALT, ENTRY_POINT) else "0x" + if method == "eth_getStorageAt": + if slot_fails: + raise CitrateError("unavailable") + assert params[1] == ERC1967_IMPLEMENTATION_SLOT + impl = _contract.aa_stack()["CitrateWallet"] if params[0].lower() == SMART_WALLET else OTHER_IMPL + return _slot(impl) if method == "eth_getTransactionCount": return "0x0" if method == "eth_sendRawTransaction": @@ -49,7 +75,11 @@ def test_value_to_wrapped_salt_and_entry_point_is_refused(to): with pytest.raises(CitrateError) as e: c._sign_and_send(to, "0x", 10**18, 21_000) assert str(e.value) == CONTRACT_RECIPIENT - assert CONTRACT_RECIPIENT == "This address is a contract. Use its deposit function instead." + assert CONTRACT_RECIPIENT == ( + "This address is a contract, so a plain transfer to it may never be credited to anyone. " + "Use the contract's own deposit function instead." + ) + assert "\u2014" not in CONTRACT_RECIPIENT assert "eth_sendRawTransaction" not in methods c.key_manager.sign_transaction.assert_not_called() @@ -85,3 +115,49 @@ def test_unreadable_recipient_code_is_refused(): c._sign_and_send(PLAIN, "0x", 5, 21_000) assert "could not check the recipient" in str(e.value) c.key_manager.sign_transaction.assert_not_called() + + +# R5 B1 (2026-11-30): a member's smart wallet is a contract that accepts a plain transfer. + + +def _send(to, **kw): + c = _client() + methods: list[str] = [] + with patch.object(c, "_rpc_call", side_effect=_rpc(methods, **kw)), patch.object( + c, "verify_network" + ), patch.object(c, "_eip155_chain_id", return_value=40204): + try: + return c, methods, c._sign_and_send(to, "0x", 10**18, 21_000), None + except CitrateError as e: + return c, methods, None, e + + +def test_value_to_a_citrate_wallet_is_sent(): + c, methods, tx_hash, err = _send(SMART_WALLET) + assert err is None and tx_hash is not None + assert methods[:2] == ["eth_getCode", "eth_getStorageAt"] + assert methods[-1] == "eth_sendRawTransaction" + + +def test_the_same_proxy_code_for_another_implementation_is_refused(): + c, methods, _, err = _send(OTHER_PROXY) + assert err is not None and str(err) == CONTRACT_RECIPIENT + c.key_manager.sign_transaction.assert_not_called() + + +def test_an_unreadable_implementation_slot_is_refused(): + c, methods, _, err = _send(SMART_WALLET, slot_fails=True) + assert err is not None and "could not check the recipient" in str(err) + c.key_manager.sign_transaction.assert_not_called() + + +def test_the_wallet_proxy_definition_is_the_factory_one(): + # One definition for Core and the SDKs: Solady's ERC1967 proxy runtime (LibClone, the code + # CitrateWalletFactory deploys; the same bytes the wallet address prediction hashes) and its + # codehash, pinned here and in Core's transfer guard. + assert CITRATE_WALLET_PROXY_RUNTIME == ( + "0x363d3d373d3d363d7f360894a13ba1a3210667c828492db98dca3e2076" + "cc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3" + ) + assert CITRATE_WALLET_PROXY_CODEHASH == "0x" + keccak(bytes.fromhex(CITRATE_WALLET_PROXY_RUNTIME[2:])).hex() + assert ERC1967_IMPLEMENTATION_SLOT == "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc" From 5db277812f2b8776474f2d4264d5c168af4e29a5 Mon Sep 17 00:00:00 2001 From: Larry Klosowski Date: Mon, 30 Nov 2026 01:48:45 -0800 Subject: [PATCH 5/6] fix: a plain value send to a member's smart wallet is allowed (R5 B1) The recipient check allows exactly one kind of code: CitrateWalletFactory's ERC1967 proxy runtime (Solady LibClone, the same bytes the address prediction hashes) whose ERC1967 implementation slot holds the book's CitrateWallet. Any other code, the same proxy for another implementation, or a slot that cannot be read is refused. The runtime, its codehash and the slot are exported from identity.wallet as the shared definition Core pins too. Message copy says why. Signed-off-by: Larry Klosowski Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_019cgBjEUXiPasw1yPyGXkXC --- citrate_sdk/client.py | 33 ++++++++++++++++++++++---- citrate_sdk/identity/wallet.py | 9 +++++++ tests/test_plain_transfer_recipient.py | 1 - 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/citrate_sdk/client.py b/citrate_sdk/client.py index 546d618..ebf3a6a 100644 --- a/citrate_sdk/client.py +++ b/citrate_sdk/client.py @@ -43,8 +43,12 @@ def _event_topic(signature: str) -> str: return Web3.keccak(text=signature).hex().lower() -#: Shown when a plain value transfer (no calldata) names an address that holds code. -CONTRACT_RECIPIENT = "This address is a contract. Use its deposit function instead." +#: Shown when a plain value transfer (no calldata) names an address that holds code (other than a +#: member's smart wallet). +CONTRACT_RECIPIENT = ( + "This address is a contract, so a plain transfer to it may never be credited to anyone. " + "Use the contract's own deposit function instead." +) class CitrateClient: @@ -511,16 +515,35 @@ def _send_transaction( def _check_plain_transfer_recipient(self, to_address: str) -> None: """Refuse a plain value transfer (no calldata) to an address that holds code. - Value for a contract goes through that contract's own function. Data source: - ``eth_getCode`` on ``rpc_url``; code that cannot be read is refused too. + Value for a contract goes through that contract's own function. The one exception is a + member's smart wallet: CitrateWalletFactory's ERC1967 proxy code whose implementation slot + holds the book's ``CitrateWallet`` (its ``receive()`` is empty, so the wallet is credited). + Data source: ``eth_getCode`` and ``eth_getStorageAt`` on ``rpc_url``; anything that cannot + be read is refused. """ + from .identity.wallet import ( + CITRATE_WALLET_PROXY_RUNTIME, + ERC1967_IMPLEMENTATION_SLOT, + ) + try: code = self._rpc_call("eth_getCode", [to_address, "latest"]) except Exception as e: # noqa: BLE001 - any failure fails closed raise CitrateError(f"could not check the recipient: {e}") from e if not isinstance(code, str) or not code.startswith("0x"): raise CitrateError("could not check the recipient: eth_getCode returned no hex") - if code not in ("0x", "0x0"): + if code in ("0x", "0x0"): + return + if code.lower() != CITRATE_WALLET_PROXY_RUNTIME: + raise CitrateError(CONTRACT_RECIPIENT) + try: + slot = self._rpc_call("eth_getStorageAt", [to_address, ERC1967_IMPLEMENTATION_SLOT, "latest"]) + except Exception as e: # noqa: BLE001 - any failure fails closed + raise CitrateError(f"could not check the recipient: {e}") from e + if not isinstance(slot, str) or not slot.startswith("0x") or len(slot) != 66: + raise CitrateError("could not check the recipient: eth_getStorageAt returned no word") + wallet_impl = str(_contract.aa_stack()["CitrateWallet"]).lower() + if slot.lower() != "0x" + "00" * 12 + wallet_impl[2:]: raise CitrateError(CONTRACT_RECIPIENT) def _sign_and_send( diff --git a/citrate_sdk/identity/wallet.py b/citrate_sdk/identity/wallet.py index e6ebf22..97da27e 100644 --- a/citrate_sdk/identity/wallet.py +++ b/citrate_sdk/identity/wallet.py @@ -26,6 +26,15 @@ _TAIL = bytes.fromhex("cc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3") _ZERO = "0x" + "00" * 20 +#: The runtime code CitrateWalletFactory deploys for every wallet: Solady's ERC1967 proxy +#: (LibClone ``createDeterministicERC1967``), the tail of the init code hashed above. It reads its +#: implementation from the ERC1967 slot, so the code is the same for every wallet. +CITRATE_WALLET_PROXY_RUNTIME = "0x" + (_BODY[3:] + _TAIL).hex() +#: keccak256 of that runtime; Core's transfer guard pins the same value. +CITRATE_WALLET_PROXY_CODEHASH = "0x" + keccak(_BODY[3:] + _TAIL).hex() +#: ERC1967 implementation slot (``bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1)``). +ERC1967_IMPLEMENTATION_SLOT = "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc" + class WalletPredictionError(Exception): pass diff --git a/tests/test_plain_transfer_recipient.py b/tests/test_plain_transfer_recipient.py index 821e5fd..44ad54e 100644 --- a/tests/test_plain_transfer_recipient.py +++ b/tests/test_plain_transfer_recipient.py @@ -4,7 +4,6 @@ from unittest.mock import MagicMock, patch import pytest - from eth_utils import keccak from citrate_sdk._generated import contract as _contract From 48fc6e25cea6a6bda8c7cda7bf1407b1c4cf9d0b Mon Sep 17 00:00:00 2001 From: Larry Klosowski Date: Mon, 30 Nov 2026 01:50:13 -0800 Subject: [PATCH 6/6] test: pin the wallet proxy codehash to Solady's ERC1967_CODE_HASH Signed-off-by: Larry Klosowski Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_019cgBjEUXiPasw1yPyGXkXC --- tests/test_plain_transfer_recipient.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_plain_transfer_recipient.py b/tests/test_plain_transfer_recipient.py index 44ad54e..d18dba8 100644 --- a/tests/test_plain_transfer_recipient.py +++ b/tests/test_plain_transfer_recipient.py @@ -159,4 +159,6 @@ def test_the_wallet_proxy_definition_is_the_factory_one(): "cc3735a920a3ca505d382bbc545af43d6000803e6038573d6000fd5b3d6000f3" ) assert CITRATE_WALLET_PROXY_CODEHASH == "0x" + keccak(bytes.fromhex(CITRATE_WALLET_PROXY_RUNTIME[2:])).hex() + # Solady LibClone.ERC1967_CODE_HASH at citrate-chain's pinned lib/kernel/lib/solady. + assert CITRATE_WALLET_PROXY_CODEHASH == "0xaaa52c8cc8a0e3fd27ce756cc6b4e70c51423e9b597b11f32d3e49f8b1fc890d" assert ERC1967_IMPLEMENTATION_SLOT == "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc"