def verify_sig(msg_b, sig_s, raw=True): hash_b = msg_b if raw else Transaction.hash256(msg_b) recid = (sig_s[0] - 27) & 0x3 sig_b = sig_s[1:] empty = secp256k1.PublicKey(raw=True, flags=secp256k1.ALL_FLAGS) sig = empty.ecdsa_recoverable_deserialize(sig_b, recid) pubkey_r = empty.ecdsa_recover(hash_b, sig, raw=True) pubkey = secp256k1.PublicKey(pubkey_r, flags=secp256k1.ALL_FLAGS) new_sig = pubkey.ecdsa_recoverable_convert(sig) return pubkey.ecdsa_verify(hash_b, new_sig, raw=True)
def sign(privkey_b, msg_b, raw=True): hash_b = msg_b if raw else Transaction.hash256(msg_b) private_key = secp256k1.PrivateKey(privkey=privkey_b, raw=True) signature = private_key.ecdsa_sign_recoverable(hash_b, raw=True) sig, recid = private_key.ecdsa_recoverable_serialize(signature) is_compressed = False r = bytes([27 + recid + (4 if is_compressed else 0)]) + sig if not verify_sig(hash_b, r): raise RuntimeError('could not verify signature') return r
def get_hash(self): self.hash = Transaction.hash256(self.serialize(True)) return self.hash