Skip to content

Commit 2f27a63

Browse files
committed
Port sign_message_by_address
1 parent d657481 commit 2f27a63

File tree

5 files changed

+51
-29
lines changed

5 files changed

+51
-29
lines changed

chia/_tests/cmds/cmd_test_utils.py

Lines changed: 8 additions & 6 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

@@ -46,6 +46,8 @@
4646
NFTGetInfo,
4747
NFTGetInfoResponse,
4848
SendTransactionMultiResponse,
49+
SignMessageByAddress,
50+
SignMessageByAddressResponse,
4951
WalletInfoResponse,
5052
)
5153
from chia.wallet.wallet_rpc_client import WalletRpcClient
@@ -149,12 +151,12 @@ async def get_cat_name(self, wallet_id: int) -> str:
149151
self.add_to_log("get_cat_name", (wallet_id,))
150152
return "test" + str(wallet_id)
151153

152-
async def sign_message_by_address(self, address: str, message: str) -> tuple[str, str, str]:
153-
self.add_to_log("sign_message_by_address", (address, message))
154-
pubkey = bytes([3] * 48).hex()
155-
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(bytes([3] * 48))
157+
signature = G2Element.from_bytes(bytes([6] * 576))
156158
signing_mode = SigningMode.CHIP_0002.value
157-
return pubkey, signature, signing_mode
159+
return SignMessageByAddressResponse(pubkey, signature, signing_mode)
158160

159161
async def sign_message_by_id(self, id: str, message: str) -> tuple[str, str, str]:
160162
self.add_to_log("sign_message_by_id", (id, message))

chia/cmds/wallet_funcs.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
NFTTransferNFT,
7474
RoyaltyAsset,
7575
SendTransactionResponse,
76+
SignMessageByAddress,
7677
VCAddProofs,
7778
VCGet,
7879
VCGetList,
@@ -1626,9 +1627,12 @@ async def sign_message(
16261627
if address is None:
16271628
print("Address is required for XCH address type.")
16281629
return
1629-
pubkey, signature, signing_mode = await wallet_client.sign_message_by_address(
1630-
address.original_address, message
1630+
response = await wallet_client.sign_message_by_address(
1631+
SignMessageByAddress(address.original_address, message)
16311632
)
1633+
pubkey = str(response.pubkey)
1634+
signature = str(response.signature)
1635+
signing_mode = response.signing_mode
16321636
elif addr_type == AddressType.DID:
16331637
if did_id is None:
16341638
print("DID id is required for DID address type.")

chia/wallet/wallet_request_types.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,23 @@ class VerifySignatureResponse(Streamable):
376376
error: Optional[str] = None
377377

378378

379+
@streamable
380+
@dataclass(frozen=True)
381+
class SignMessageByAddress(Streamable):
382+
address: str
383+
message: str
384+
is_hex: bool = False
385+
safe_mode: bool = True
386+
387+
388+
@streamable
389+
@dataclass(frozen=True)
390+
class SignMessageByAddressResponse(Streamable):
391+
pubkey: G1Element
392+
signature: G2Element
393+
signing_mode: str
394+
395+
379396
@streamable
380397
@dataclass(frozen=True)
381398
class GetTransactionMemo(Streamable):

chia/wallet/wallet_rpc_api.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@
243243
PWStatus,
244244
PWStatusResponse,
245245
SetWalletResyncOnStartup,
246+
SignMessageByAddress,
247+
SignMessageByAddressResponse,
246248
SplitCoins,
247249
SplitCoinsResponse,
248250
SubmitTransactions,
@@ -2021,35 +2023,29 @@ async def verify_signature(self, request: VerifySignature) -> VerifySignatureRes
20212023
else:
20222024
return VerifySignatureResponse(isValid=False, error="Signature is invalid.")
20232025

2024-
async def sign_message_by_address(self, request: dict[str, Any]) -> EndpointResult:
2026+
@marshal
2027+
async def sign_message_by_address(self, request: SignMessageByAddress) -> SignMessageByAddressResponse:
20252028
"""
20262029
Given a derived P2 address, sign the message by its private key.
20272030
:param request:
20282031
:return:
20292032
"""
2030-
puzzle_hash: bytes32 = decode_puzzle_hash(request["address"])
2031-
is_hex: bool = request.get("is_hex", False)
2032-
if isinstance(is_hex, str):
2033-
is_hex = True if is_hex.lower() == "true" else False
2034-
safe_mode: bool = request.get("safe_mode", True)
2035-
if isinstance(safe_mode, str):
2036-
safe_mode = True if safe_mode.lower() == "true" else False
2033+
puzzle_hash: bytes32 = decode_puzzle_hash(request.address)
20372034
mode: SigningMode = SigningMode.CHIP_0002
2038-
if is_hex and safe_mode:
2035+
if request.is_hex and request.safe_mode:
20392036
mode = SigningMode.CHIP_0002_HEX_INPUT
2040-
elif not is_hex and not safe_mode:
2037+
elif not request.is_hex and not request.safe_mode:
20412038
mode = SigningMode.BLS_MESSAGE_AUGMENTATION_UTF8_INPUT
2042-
elif is_hex and not safe_mode:
2039+
elif request.is_hex and not request.safe_mode:
20432040
mode = SigningMode.BLS_MESSAGE_AUGMENTATION_HEX_INPUT
20442041
pubkey, signature = await self.service.wallet_state_manager.main_wallet.sign_message(
2045-
request["message"], puzzle_hash, mode
2042+
request.message, puzzle_hash, mode
2043+
)
2044+
return SignMessageByAddressResponse(
2045+
pubkey=pubkey,
2046+
signature=signature,
2047+
signing_mode=mode.value,
20462048
)
2047-
return {
2048-
"success": True,
2049-
"pubkey": str(pubkey),
2050-
"signature": str(signature),
2051-
"signing_mode": mode.value,
2052-
}
20532049

20542050
async def sign_message_by_id(self, request: dict[str, Any]) -> EndpointResult:
20552051
"""

chia/wallet/wallet_rpc_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@
160160
SendTransactionMultiResponse,
161161
SendTransactionResponse,
162162
SetWalletResyncOnStartup,
163+
SignMessageByAddress,
164+
SignMessageByAddressResponse,
163165
SplitCoins,
164166
SplitCoinsResponse,
165167
SubmitTransactions,
@@ -1144,9 +1146,10 @@ async def send_notification(
11441146
)
11451147
return TransactionRecord.from_json_dict(response["tx"])
11461148

1147-
async def sign_message_by_address(self, address: str, message: str) -> tuple[str, str, str]:
1148-
response = await self.fetch("sign_message_by_address", {"address": address, "message": message})
1149-
return response["pubkey"], response["signature"], response["signing_mode"]
1149+
async def sign_message_by_address(self, request: SignMessageByAddress) -> SignMessageByAddressResponse:
1150+
return SignMessageByAddressResponse.from_json_dict(
1151+
await self.fetch("sign_message_by_address", request.to_json_dict())
1152+
)
11501153

11511154
async def sign_message_by_id(
11521155
self, id: str, message: str, is_hex: bool = False, safe_mode: bool = True

0 commit comments

Comments
 (0)