Add verify signature API (#13732)

* Add verify signature API

* Refine response

* Resolve comment

* fix pre-commit
This commit is contained in:
Kronus91 2022-11-08 08:23:06 -08:00 committed by GitHub
parent 50d148eadd
commit 35d5858a06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,7 +5,7 @@ import logging
from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Tuple, Union
from blspy import G1Element, G2Element, PrivateKey
from blspy import G1Element, G2Element, PrivateKey, AugSchemeMPL
from chia.consensus.block_rewards import calculate_base_farmer_reward
from chia.data_layer.data_layer_wallet import DataLayerWallet
@ -50,6 +50,7 @@ from chia.wallet.nft_wallet.uncurry_nft import UncurriedNFT
from chia.wallet.notification_store import Notification
from chia.wallet.outer_puzzles import AssetType
from chia.wallet.puzzle_drivers import PuzzleInfo, Solver
from chia.wallet.puzzles.p2_delegated_puzzle_or_hidden_puzzle import puzzle_hash_for_synthetic_public_key
from chia.wallet.trade_record import TradeRecord
from chia.wallet.trading.offer import Offer
from chia.wallet.transaction_record import TransactionRecord
@ -122,6 +123,7 @@ class WalletRpcApi:
"/send_notification": self.send_notification,
"/sign_message_by_address": self.sign_message_by_address,
"/sign_message_by_id": self.sign_message_by_id,
"/verify_signature": self.verify_signature,
# CATs and trading
"/cat_set_name": self.cat_set_name,
"/cat_asset_id_to_name": self.cat_asset_id_to_name,
@ -1170,6 +1172,28 @@ class WalletRpcApi:
await self.service.wallet_state_manager.add_pending_transaction(tx)
return {"tx": tx.to_json_dict_convenience(self.service.config)}
async def verify_signature(self, request) -> EndpointResult:
"""
Given a public key, message and signature, verify if it is valid.
:param request:
:return:
"""
is_valid = AugSchemeMPL.verify(
G1Element.from_bytes(bytes.fromhex(request["pubkey"])),
bytes.fromhex(request["message"]),
G2Element.from_bytes(bytes.fromhex(request["signature"])),
)
if "address" in request:
puzzle_hash: bytes32 = decode_puzzle_hash(request["address"])
if puzzle_hash != puzzle_hash_for_synthetic_public_key(
G1Element.from_bytes(bytes.fromhex(request["pubkey"]))
):
return {"isValid": False, "error": "Public key doesn't match the address"}
if is_valid:
return {"isValid": is_valid}
else:
return {"isValid": False, "error": "Signature is invalid."}
async def sign_message_by_address(self, request) -> EndpointResult:
"""
Given a derived P2 address, sign the message by its private key.