def sign_message(self, path: List[int], message: str) -> bytearray: message = message.encode('utf-8') formatted = wally.format_bitcoin_message(message, wally.BITCOIN_MESSAGE_FLAG_HASH) privkey = self.get_privkey(path) signature = wally.ec_sig_from_bytes(privkey, formatted, wally.EC_FLAG_ECDSA | wally.EC_FLAG_GRIND_R) return wally.ec_sig_to_der(signature)
def sign_message(privkey_wif: str, message: str) -> str: """Sign a message with a private key of a P2PKH address 'privkey_wif' must be in WIF format, message and signature are formatted as 'signmessage' RPC does. """ privkey = PrivateKey.from_wif(privkey_wif) h = wally.format_bitcoin_message(message.encode('ascii'), wally.BITCOIN_MESSAGE_FLAG_HASH) flags = wally.EC_FLAG_ECDSA | wally.EC_FLAG_RECOVERABLE recoverable_signature = wally.ec_sig_from_bytes(privkey.prv, h, flags) return base64.b64encode(recoverable_signature)
def verify_message(address: str, signature: str, message: str) -> str: """Sign a message with a private key of a P2PKH address 'signature' must be a recoverable signature in base64. """ h = wally.format_bitcoin_message(message.encode('ascii'), wally.BITCOIN_MESSAGE_FLAG_HASH) recoverable_sig = base64.b64decode(signature) try: pub = wally.ec_sig_to_public_key(h, recoverable_sig) except ValueError: raise InvalidRecoverableSignature return P2PKH(PublicKey(pub)).address() == address
def sign_message(self, details: Dict) -> Dict: message = details['message'].encode('utf-8') formatted = wally.format_bitcoin_message( message, wally.BITCOIN_MESSAGE_FLAG_HASH) privkey = self.get_privkey(details['path']) result = {} if details['use_ae_protocol']: signer_commitment, signature = self._make_ae_signature( privkey, formatted, details) signer_commitment = signer_commitment.hex() logging.debug('Signer commitment: %s', signer_commitment) result['signer_commitment'] = signer_commitment else: signature = wally.ec_sig_from_bytes( privkey, formatted, wally.EC_FLAG_ECDSA | wally.EC_FLAG_GRIND_R) result['signature'] = wally.ec_sig_to_der(signature).hex() return result