Skip to content

Commit 8463909

Browse files
authored
[CHIA-3598] Port easy wallet endpoints to @marshal (#19941)
* Port `get_transaction_count` * Port `get_next_address` * Port `delete_unconfirmed_transactions` * Port `get_current_derivation_index` * Really disliking this framework FYI * Port `extend_derivation_index` * Fix the mock * Port `delete_notifications` * Fix the test check * Store, not RPC * Port `verify_signature` * Port `sign_message_by_address` * Fix tests * Port `sign_message_by_id` * fix tests * fix test * test coverage * comments by @altendky
1 parent bcdc0c6 commit 8463909

15 files changed

+436
-295
lines changed

chia/_tests/cmds/cmd_test_utils.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pathlib import Path
88
from typing import Any, Optional, cast
99

10-
from chia_rs import BlockRecord, Coin, G2Element
10+
from chia_rs import BlockRecord, Coin, G1Element, G2Element
1111
from chia_rs.sized_bytes import bytes32
1212
from chia_rs.sized_ints import uint8, uint16, uint32, uint64
1313

@@ -44,6 +44,10 @@
4444
NFTGetInfo,
4545
NFTGetInfoResponse,
4646
SendTransactionMultiResponse,
47+
SignMessageByAddress,
48+
SignMessageByAddressResponse,
49+
SignMessageByID,
50+
SignMessageByIDResponse,
4751
WalletInfoResponse,
4852
)
4953
from chia.wallet.wallet_rpc_client import WalletRpcClient
@@ -147,19 +151,37 @@ async def get_cat_name(self, wallet_id: int) -> str:
147151
self.add_to_log("get_cat_name", (wallet_id,))
148152
return "test" + str(wallet_id)
149153

150-
async def sign_message_by_address(self, address: str, message: str) -> tuple[str, str, str]:
151-
self.add_to_log("sign_message_by_address", (address, message))
152-
pubkey = bytes([3] * 48).hex()
153-
signature = bytes([6] * 576).hex()
154+
async def sign_message_by_address(self, request: SignMessageByAddress) -> SignMessageByAddressResponse:
155+
self.add_to_log("sign_message_by_address", (request.address, request.message))
156+
pubkey = G1Element.from_bytes(
157+
bytes.fromhex(
158+
"b5acf3599bc5fa5da1c00f6cc3d5bcf1560def67778b7f50a8c373a83f78761505b6250ab776e38a292e26628009aec4"
159+
)
160+
)
161+
signature = G2Element.from_bytes(
162+
bytes.fromhex(
163+
"c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
164+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
165+
)
166+
)
154167
signing_mode = SigningMode.CHIP_0002.value
155-
return pubkey, signature, signing_mode
168+
return SignMessageByAddressResponse(pubkey, signature, signing_mode)
156169

157-
async def sign_message_by_id(self, id: str, message: str) -> tuple[str, str, str]:
158-
self.add_to_log("sign_message_by_id", (id, message))
159-
pubkey = bytes([4] * 48).hex()
160-
signature = bytes([7] * 576).hex()
170+
async def sign_message_by_id(self, request: SignMessageByID) -> SignMessageByIDResponse:
171+
self.add_to_log("sign_message_by_id", (request.id, request.message))
172+
pubkey = G1Element.from_bytes(
173+
bytes.fromhex(
174+
"a9e652cb551d5978a9ee4b7aa52a4e826078a54b08a3d903c38611cb8a804a9a29c926e4f8549314a079e04ecde10cc1"
175+
)
176+
)
177+
signature = G2Element.from_bytes(
178+
bytes.fromhex(
179+
"c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
180+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
181+
)
182+
)
161183
signing_mode = SigningMode.CHIP_0002.value
162-
return pubkey, signature, signing_mode
184+
return SignMessageByIDResponse(pubkey, signature, bytes32.zeros, signing_mode)
163185

164186
async def cat_asset_id_to_name(self, asset_id: bytes32) -> Optional[tuple[Optional[uint32], str]]:
165187
"""
@@ -250,14 +272,6 @@ async def get_spendable_coins(
250272
unconfirmed_additions = [Coin(bytes32([7] * 32), bytes32([8] * 32), uint64(1234580000))]
251273
return confirmed_records, unconfirmed_removals, unconfirmed_additions
252274

253-
async def get_next_address(self, wallet_id: int, new_address: bool) -> str:
254-
self.add_to_log("get_next_address", (wallet_id, new_address))
255-
addr = encode_puzzle_hash(bytes32([self.wallet_index] * 32), "xch")
256-
self.wallet_index += 1
257-
if self.wallet_index > 254:
258-
self.wallet_index = 1
259-
return addr
260-
261275
async def send_transaction_multi(
262276
self,
263277
wallet_id: int,

chia/_tests/cmds/wallet/test_did.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ def test_did_sign_message(capsys: object, get_test_cli_clients: tuple[TestRpcCli
116116
# these are various things that should be in the output
117117
assert_list = [
118118
f"Message: {message.hex()}",
119-
f"Public Key: {bytes([4] * 48).hex()}",
120-
f"Signature: {bytes([7] * 576).hex()}",
119+
"Public Key: a9e652cb551d5978a9ee4b7aa52a4e826078a54b08a3d903c38611cb8a804a9a29c926e4f8549314a079e04ecde10cc1",
120+
"Signature: c0" + "00" * (42 - 1),
121121
f"Signing Mode: {SigningMode.CHIP_0002.value}",
122122
]
123123
run_cli_command_and_assert(capsys, root_dir, [*command_args, f"-i{did_id}"], assert_list)

chia/_tests/cmds/wallet/test_nft.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ def test_nft_sign_message(capsys: object, get_test_cli_clients: tuple[TestRpcCli
6464

6565
inst_rpc_client = TestWalletRpcClient()
6666
test_rpc_clients.wallet_rpc_client = inst_rpc_client
67-
did_id = encode_puzzle_hash(get_bytes32(1), "nft")
67+
nft_id = encode_puzzle_hash(get_bytes32(1), "nft")
6868
message = b"hello nft world!!"
69-
command_args = ["wallet", "did", "sign_message", FINGERPRINT_ARG, f"-m{message.hex()}"]
69+
command_args = ["wallet", "nft", "sign_message", FINGERPRINT_ARG, f"-m{message.hex()}"]
7070
# these are various things that should be in the output
7171
assert_list = [
7272
f"Message: {message.hex()}",
73-
f"Public Key: {bytes([4] * 48).hex()}",
74-
f"Signature: {bytes([7] * 576).hex()}",
73+
"Public Key: a9e652cb551d5978a9ee4b7aa52a4e826078a54b08a3d903c38611cb8a804a9a29c926e4f8549314a079e04ecde10cc1",
74+
"Signature: c0" + "00" * (42 - 1),
7575
f"Signing Mode: {SigningMode.CHIP_0002.value}",
7676
]
77-
run_cli_command_and_assert(capsys, root_dir, [*command_args, f"-i{did_id}"], assert_list)
77+
run_cli_command_and_assert(capsys, root_dir, [*command_args, f"-i{nft_id}"], assert_list)
7878
expected_calls: logType = {
79-
"sign_message_by_id": [(did_id, message.hex())], # xch std
79+
"sign_message_by_id": [(nft_id, message.hex())], # xch std
8080
}
8181
test_rpc_clients.wallet_rpc_client.check_log(expected_calls)
8282

chia/_tests/cmds/wallet/test_notifications.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from pathlib import Path
4-
from typing import Optional, cast
4+
from typing import cast
55

66
from chia_rs.sized_bytes import bytes32
77
from chia_rs.sized_ints import uint32, uint64
@@ -12,7 +12,7 @@
1212
from chia.wallet.conditions import ConditionValidTimes
1313
from chia.wallet.notification_store import Notification
1414
from chia.wallet.transaction_record import TransactionRecord
15-
from chia.wallet.wallet_request_types import GetNotifications, GetNotificationsResponse
15+
from chia.wallet.wallet_request_types import DeleteNotifications, GetNotifications, GetNotificationsResponse
1616

1717
test_condition_valid_times: ConditionValidTimes = ConditionValidTimes(min_time=uint64(100), max_time=uint64(150))
1818

@@ -111,15 +111,31 @@ def test_notifications_delete(capsys: object, get_test_cli_clients: tuple[TestRp
111111

112112
# set RPC Client
113113
class NotificationsDeleteRpcClient(TestWalletRpcClient):
114-
async def delete_notifications(self, ids: Optional[list[bytes32]] = None) -> bool:
115-
self.add_to_log("delete_notifications", (ids,))
116-
return True
114+
async def delete_notifications(self, request: DeleteNotifications) -> None:
115+
self.add_to_log("delete_notifications", (request.ids,))
117116

118117
inst_rpc_client = NotificationsDeleteRpcClient()
119118
test_rpc_clients.wallet_rpc_client = inst_rpc_client
119+
# Try all first
120120
command_args = ["wallet", "notifications", "delete", FINGERPRINT_ARG, "--all"]
121121
# these are various things that should be in the output
122-
assert_list = ["Success: True"]
122+
assert_list = ["Success!"]
123123
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)
124124
expected_calls: logType = {"delete_notifications": [(None,)]}
125125
test_rpc_clients.wallet_rpc_client.check_log(expected_calls)
126+
# Next try specifying IDs
127+
command_args = [
128+
"wallet",
129+
"notifications",
130+
"delete",
131+
FINGERPRINT_ARG,
132+
"--id",
133+
bytes32.zeros.hex(),
134+
"--id",
135+
bytes32.zeros.hex(),
136+
]
137+
# these are various things that should be in the output
138+
assert_list = ["Success!"]
139+
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)
140+
expected_calls = {"delete_notifications": [([bytes32.zeros, bytes32.zeros],)]}
141+
test_rpc_clients.wallet_rpc_client.check_log(expected_calls)

chia/_tests/cmds/wallet/test_wallet.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@
4545
CancelOfferResponse,
4646
CATSpendResponse,
4747
CreateOfferForIDsResponse,
48+
DeleteUnconfirmedTransactions,
49+
ExtendDerivationIndex,
50+
ExtendDerivationIndexResponse,
4851
FungibleAsset,
52+
GetCurrentDerivationIndexResponse,
4953
GetHeightInfoResponse,
54+
GetNextAddress,
55+
GetNextAddressResponse,
5056
GetTransaction,
5157
GetTransactions,
5258
GetTransactionsResponse,
@@ -493,11 +499,11 @@ def test_get_address(capsys: object, get_test_cli_clients: tuple[TestRpcClients,
493499

494500
# set RPC Client
495501
class GetAddressWalletRpcClient(TestWalletRpcClient):
496-
async def get_next_address(self, wallet_id: int, new_address: bool) -> str:
497-
self.add_to_log("get_next_address", (wallet_id, new_address))
498-
if new_address:
499-
return encode_puzzle_hash(get_bytes32(3), "xch")
500-
return encode_puzzle_hash(get_bytes32(4), "xch")
502+
async def get_next_address(self, request: GetNextAddress) -> GetNextAddressResponse:
503+
self.add_to_log("get_next_address", (request.wallet_id, request.new_address))
504+
if request.new_address:
505+
return GetNextAddressResponse(request.wallet_id, encode_puzzle_hash(get_bytes32(3), "xch"))
506+
return GetNextAddressResponse(request.wallet_id, encode_puzzle_hash(get_bytes32(4), "xch"))
501507

502508
inst_rpc_client = GetAddressWalletRpcClient()
503509
test_rpc_clients.wallet_rpc_client = inst_rpc_client
@@ -569,8 +575,8 @@ def test_del_unconfirmed_tx(capsys: object, get_test_cli_clients: tuple[TestRpcC
569575

570576
# set RPC Client
571577
class UnconfirmedTxRpcClient(TestWalletRpcClient):
572-
async def delete_unconfirmed_transactions(self, wallet_id: int) -> None:
573-
self.add_to_log("delete_unconfirmed_transactions", (wallet_id,))
578+
async def delete_unconfirmed_transactions(self, request: DeleteUnconfirmedTransactions) -> None:
579+
self.add_to_log("delete_unconfirmed_transactions", (request.wallet_id,))
574580

575581
inst_rpc_client = UnconfirmedTxRpcClient()
576582
test_rpc_clients.wallet_rpc_client = inst_rpc_client
@@ -594,9 +600,9 @@ def test_get_derivation_index(capsys: object, get_test_cli_clients: tuple[TestRp
594600

595601
# set RPC Client
596602
class GetDerivationIndexRpcClient(TestWalletRpcClient):
597-
async def get_current_derivation_index(self) -> str:
603+
async def get_current_derivation_index(self) -> GetCurrentDerivationIndexResponse:
598604
self.add_to_log("get_current_derivation_index", ())
599-
return str(520)
605+
return GetCurrentDerivationIndexResponse(uint32(520))
600606

601607
inst_rpc_client = GetDerivationIndexRpcClient()
602608
test_rpc_clients.wallet_rpc_client = inst_rpc_client
@@ -625,8 +631,9 @@ def test_sign_message(capsys: object, get_test_cli_clients: tuple[TestRpcClients
625631
# these are various things that should be in the output
626632
assert_list = [
627633
f"Message: {message.hex()}",
628-
f"Public Key: {bytes([3] * 48).hex()}",
629-
f"Signature: {bytes([6] * 576).hex()}",
634+
"Public Key: b5acf3599bc5fa5da1c00f6cc3d5bcf1560def67778b7f50a8c373a83f78761505b6250ab776e38a292e26628009aec4",
635+
"Signature: c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
636+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
630637
f"Signing Mode: {SigningMode.CHIP_0002.value}",
631638
]
632639
run_cli_command_and_assert(capsys, root_dir, [*command_args, f"-a{xch_addr}"], assert_list)
@@ -641,9 +648,9 @@ def test_update_derivation_index(capsys: object, get_test_cli_clients: tuple[Tes
641648

642649
# set RPC Client
643650
class UpdateDerivationIndexRpcClient(TestWalletRpcClient):
644-
async def extend_derivation_index(self, index: int) -> str:
645-
self.add_to_log("extend_derivation_index", (index,))
646-
return str(index)
651+
async def extend_derivation_index(self, request: ExtendDerivationIndex) -> ExtendDerivationIndexResponse:
652+
self.add_to_log("extend_derivation_index", (request.index,))
653+
return ExtendDerivationIndexResponse(request.index)
647654

648655
inst_rpc_client = UpdateDerivationIndexRpcClient()
649656
test_rpc_clients.wallet_rpc_client = inst_rpc_client

chia/_tests/pools/test_pool_rpc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from chia.wallet.util.wallet_types import WalletType
4242
from chia.wallet.wallet_node import WalletNode
4343
from chia.wallet.wallet_request_types import (
44+
DeleteUnconfirmedTransactions,
4445
GetTransactions,
4546
GetWalletBalance,
4647
GetWallets,
@@ -463,7 +464,7 @@ async def pw_created(check_wallet_id: int) -> bool:
463464
def mempool_empty() -> bool:
464465
return full_node_api.full_node.mempool_manager.mempool.size() == 0
465466

466-
await client.delete_unconfirmed_transactions(1)
467+
await client.delete_unconfirmed_transactions(DeleteUnconfirmedTransactions(uint32(1)))
467468
await full_node_api.process_all_wallet_transactions(wallet=wallet)
468469
await full_node_api.wait_for_wallet_synced(wallet_node=wallet_node, timeout=20)
469470

chia/_tests/wallet/did_wallet/test_did.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from chia.types.peer_info import PeerInfo
2424
from chia.types.signing_mode import CHIP_0002_SIGN_MESSAGE_PREFIX
2525
from chia.util.bech32m import decode_puzzle_hash, encode_puzzle_hash
26+
from chia.util.byte_types import hexstr_to_bytes
2627
from chia.wallet.did_wallet.did_wallet import DIDWallet
2728
from chia.wallet.singleton import (
2829
create_singleton_puzzle,
@@ -1091,9 +1092,9 @@ async def test_did_sign_message(wallet_environments: WalletTestFramework):
10911092
)
10921093
puzzle: Program = Program.to((CHIP_0002_SIGN_MESSAGE_PREFIX, message))
10931094
assert AugSchemeMPL.verify(
1094-
G1Element.from_bytes(bytes.fromhex(response["pubkey"])),
1095+
G1Element.from_bytes(hexstr_to_bytes(response["pubkey"])),
10951096
puzzle.get_tree_hash(),
1096-
G2Element.from_bytes(bytes.fromhex(response["signature"])),
1097+
G2Element.from_bytes(hexstr_to_bytes(response["signature"])),
10971098
)
10981099
# Test hex string
10991100
message = "0123456789ABCDEF"
@@ -1107,9 +1108,9 @@ async def test_did_sign_message(wallet_environments: WalletTestFramework):
11071108
puzzle = Program.to((CHIP_0002_SIGN_MESSAGE_PREFIX, bytes.fromhex(message)))
11081109

11091110
assert AugSchemeMPL.verify(
1110-
G1Element.from_bytes(bytes.fromhex(response["pubkey"])),
1111+
G1Element.from_bytes(hexstr_to_bytes(response["pubkey"])),
11111112
puzzle.get_tree_hash(),
1112-
G2Element.from_bytes(bytes.fromhex(response["signature"])),
1113+
G2Element.from_bytes(hexstr_to_bytes(response["signature"])),
11131114
)
11141115

11151116
# Test BLS sign string
@@ -1119,15 +1120,15 @@ async def test_did_sign_message(wallet_environments: WalletTestFramework):
11191120
{
11201121
"id": encode_puzzle_hash(did_wallet_1.did_info.origin_coin.name(), AddressType.DID.value),
11211122
"message": message,
1122-
"is_hex": "False",
1123-
"safe_mode": "False",
1123+
"is_hex": False,
1124+
"safe_mode": False,
11241125
}
11251126
)
11261127

11271128
assert AugSchemeMPL.verify(
1128-
G1Element.from_bytes(bytes.fromhex(response["pubkey"])),
1129+
G1Element.from_bytes(hexstr_to_bytes(response["pubkey"])),
11291130
bytes(message, "utf-8"),
1130-
G2Element.from_bytes(bytes.fromhex(response["signature"])),
1131+
G2Element.from_bytes(hexstr_to_bytes(response["signature"])),
11311132
)
11321133
# Test BLS sign hex
11331134
message = "0123456789ABCDEF"
@@ -1142,9 +1143,9 @@ async def test_did_sign_message(wallet_environments: WalletTestFramework):
11421143
)
11431144

11441145
assert AugSchemeMPL.verify(
1145-
G1Element.from_bytes(bytes.fromhex(response["pubkey"])),
1146-
bytes.fromhex(message),
1147-
G2Element.from_bytes(bytes.fromhex(response["signature"])),
1146+
G1Element.from_bytes(hexstr_to_bytes(response["pubkey"])),
1147+
hexstr_to_bytes(message),
1148+
G2Element.from_bytes(hexstr_to_bytes(response["signature"])),
11481149
)
11491150

11501151

0 commit comments

Comments
 (0)