def test_public_key_recovery(): s = "3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \ "d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb" p = "eb696a065ef48a2192da5b28b694f87544b30fae8327c4510137a922f32c6dcf" msg = "64f3b0f4dd2bb3aa1ce8566d220cc74dda9df97d8490cc81d89d735c92e59fb6" assert public_key_recovery(s, msg, 0) == private_to_public_key(p, hex=True) assert public_key_recovery(s, msg, 1) != private_to_public_key(p, hex=True) assert public_key_recovery(s, msg, 2) != private_to_public_key(p, hex=True) assert public_key_recovery(s, msg, 3) is None s = "1044022047ac8e878252d3ebbde1c94ce3a10d057c24175747116f8288e5d794" \ "d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb" with pytest.raises(RuntimeError): assert public_key_recovery(s, msg, 3) is None
def __get_multisig_script_sig__(self, script_sig, keys, signatures, script_code, redeem_script, n, amount=None): sig_map = {keys[i]:signatures[i] for i in range(len(keys))} pub_keys = get_multisig_public_keys(redeem_script) p2wsh = True if isinstance(script_sig, list) else False if not p2wsh: s = get_stream(script_sig) o, d = read_opcode(s) while o: o, d = read_opcode(s) if d and is_valid_signature_encoding(d): for i in range(4): sighash = self.sig_hash(n, script_pub_key=script_code, sighash_type=d[-1]) sighash = s2rh(sighash) if isinstance(sighash, str) else sighash pk = public_key_recovery(d[:-1], sighash, i, hex=0) if pk in pub_keys: sig_map[pk] = d break # recreate script sig r = [OP_0] for k in pub_keys: try: r.append(op_push_data(sig_map[k])) except: pass r += [op_push_data(redeem_script)] else: for w in script_sig: if isinstance(w, str): w = bytes.fromhex(w) if w and is_valid_signature_encoding(w): d = w[:-1] for i in range(4): sighash = self.sig_hash_segwit(n, amount, script_pub_key=script_code, sighash_type=w[-1]) pk = public_key_recovery(d, sighash, i, hex=0) if pk in pub_keys: sig_map[pk] = w break r = [b""] for k in pub_keys: try: r.append(sig_map[k]) except: pass r += [redeem_script] return r
def public_key_recovery(signature, messsage, rec_id, compressed=True, hex=True): return __parent__.public_key_recovery(signature, messsage, rec_id, compressed=compressed, hex=hex)
def __get_bare_multisig_script_sig__(self, script_sig, script_pub_key, keys, signatures, n): sig_map = {keys[i]:signatures[i] for i in range(len(keys))} pub_keys = get_multisig_public_keys(script_pub_key) s = get_stream(script_sig) o, d = read_opcode(s) while o: o, d = read_opcode(s) if d and is_valid_signature_encoding(d): for i in range(4): sighash = self.sig_hash(n, script_pub_key=script_pub_key, sighash_type=d[-1]) sighash = s2rh(sighash) if isinstance(sighash, str) else sighash pk = public_key_recovery(d[:-1], sighash, i, hex=0) if pk in pub_keys: sig_map[pk] = d break # recreate script sig r = [OP_0] for k in pub_keys: try: r.append(op_push_data(sig_map[k])) except: pass return r