245
245
SetWalletResyncOnStartup ,
246
246
SignMessageByAddress ,
247
247
SignMessageByAddressResponse ,
248
+ SignMessageByID ,
249
+ SignMessageByIDResponse ,
248
250
SplitCoins ,
249
251
SplitCoinsResponse ,
250
252
SubmitTransactions ,
@@ -2047,28 +2049,23 @@ async def sign_message_by_address(self, request: SignMessageByAddress) -> SignMe
2047
2049
signing_mode = mode .value ,
2048
2050
)
2049
2051
2050
- async def sign_message_by_id (self , request : dict [str , Any ]) -> EndpointResult :
2052
+ @marshal
2053
+ async def sign_message_by_id (self , request : SignMessageByID ) -> SignMessageByIDResponse :
2051
2054
"""
2052
2055
Given a NFT/DID ID, sign the message by the P2 private key.
2053
2056
:param request:
2054
2057
:return:
2055
2058
"""
2056
- entity_id : bytes32 = decode_puzzle_hash (request [ "id" ] )
2059
+ entity_id : bytes32 = decode_puzzle_hash (request . id )
2057
2060
selected_wallet : Optional [WalletProtocol [Any ]] = None
2058
- is_hex : bool = request .get ("is_hex" , False )
2059
- if isinstance (is_hex , str ):
2060
- is_hex = True if is_hex .lower () == "true" else False
2061
- safe_mode : bool = request .get ("safe_mode" , True )
2062
- if isinstance (safe_mode , str ):
2063
- safe_mode = True if safe_mode .lower () == "true" else False
2064
2061
mode : SigningMode = SigningMode .CHIP_0002
2065
- if is_hex and safe_mode :
2062
+ if request . is_hex and request . safe_mode :
2066
2063
mode = SigningMode .CHIP_0002_HEX_INPUT
2067
- elif not is_hex and not safe_mode :
2064
+ elif not request . is_hex and not request . safe_mode :
2068
2065
mode = SigningMode .BLS_MESSAGE_AUGMENTATION_UTF8_INPUT
2069
- elif is_hex and not safe_mode :
2066
+ elif request . is_hex and not request . safe_mode :
2070
2067
mode = SigningMode .BLS_MESSAGE_AUGMENTATION_HEX_INPUT
2071
- if is_valid_address (request [ "id" ] , {AddressType .DID }, self .service .config ):
2068
+ if is_valid_address (request . id , {AddressType .DID }, self .service .config ):
2072
2069
for wallet in self .service .wallet_state_manager .wallets .values ():
2073
2070
if wallet .type () == WalletType .DECENTRALIZED_ID .value :
2074
2071
assert isinstance (wallet , DIDWallet )
@@ -2077,11 +2074,11 @@ async def sign_message_by_id(self, request: dict[str, Any]) -> EndpointResult:
2077
2074
selected_wallet = wallet
2078
2075
break
2079
2076
if selected_wallet is None :
2080
- return { "success" : False , "error" : f"DID for { entity_id .hex ()} doesn't exist." }
2077
+ raise ValueError ( f"DID for { entity_id .hex ()} doesn't exist." )
2081
2078
assert isinstance (selected_wallet , DIDWallet )
2082
- pubkey , signature = await selected_wallet .sign_message (request [ " message" ] , mode )
2079
+ pubkey , signature = await selected_wallet .sign_message (request . message , mode )
2083
2080
latest_coin_id = (await selected_wallet .get_coin ()).name ()
2084
- elif is_valid_address (request [ "id" ] , {AddressType .NFT }, self .service .config ):
2081
+ elif is_valid_address (request . id , {AddressType .NFT }, self .service .config ):
2085
2082
target_nft : Optional [NFTCoinInfo ] = None
2086
2083
for wallet in self .service .wallet_state_manager .wallets .values ():
2087
2084
if wallet .type () == WalletType .NFT .value :
@@ -2092,21 +2089,20 @@ async def sign_message_by_id(self, request: dict[str, Any]) -> EndpointResult:
2092
2089
target_nft = nft
2093
2090
break
2094
2091
if selected_wallet is None or target_nft is None :
2095
- return { "success" : False , "error" : f"NFT for { entity_id .hex ()} doesn't exist." }
2092
+ raise ValueError ( f"NFT for { entity_id .hex ()} doesn't exist." )
2096
2093
2097
2094
assert isinstance (selected_wallet , NFTWallet )
2098
- pubkey , signature = await selected_wallet .sign_message (request [ " message" ] , target_nft , mode )
2095
+ pubkey , signature = await selected_wallet .sign_message (request . message , target_nft , mode )
2099
2096
latest_coin_id = target_nft .coin .name ()
2100
2097
else :
2101
- return { "success" : False , "error" : f"Unknown ID type, { request [ 'id' ] } " }
2098
+ raise ValueError ( f"Unknown ID type, { request . id } " )
2102
2099
2103
- return {
2104
- "success" : True ,
2105
- "pubkey" : str (pubkey ),
2106
- "signature" : str (signature ),
2107
- "latest_coin_id" : latest_coin_id .hex () if latest_coin_id is not None else None ,
2108
- "signing_mode" : mode .value ,
2109
- }
2100
+ return SignMessageByIDResponse (
2101
+ pubkey = pubkey ,
2102
+ signature = signature ,
2103
+ latest_coin_id = latest_coin_id ,
2104
+ signing_mode = mode .value ,
2105
+ )
2110
2106
2111
2107
##########################################################################################
2112
2108
# CATs and Trading
0 commit comments