def createmultisig(self, threshold, pubkeys): """Create multisig address""" assert isinstance(pubkeys, list), (type(threshold), type(pubkeys)) public_keys = [PublicKey.from_hex(pubkey) for pubkey in sorted(pubkeys)] output = P2MultiSig_Output(public_keys, threshold) redeem_script = output.to_script_bytes() address = P2SH_Address(hash160(redeem_script)).to_string() return {'address':address, 'redeemScript':redeem_script.hex()}
def _parse_script_sig(script, kwargs): try: decoded = list(_script_GetOp(script)) except Exception: # coinbase transactions raise an exception logger.exception("cannot find address in input script %s", bh2u(script)) return # P2PK match = [Ops.OP_PUSHDATA4] if _match_decoded(decoded, match): item = decoded[0][1] kwargs['signatures'] = [item] kwargs['threshold'] = 1 return # P2PKH inputs push a signature (around seventy bytes) and then their public key # (65 bytes) onto the stack match = [Ops.OP_PUSHDATA4, Ops.OP_PUSHDATA4] if _match_decoded(decoded, match): sig = decoded[0][1] x_pubkey = XPublicKey(decoded[1][1]) kwargs['signatures'] = [sig] kwargs['threshold'] = 1 kwargs['x_pubkeys'] = [x_pubkey] kwargs['address'] = x_pubkey.to_address() return # p2sh transaction, m of n match = [Ops.OP_0] + [Ops.OP_PUSHDATA4] * (len(decoded) - 1) if not _match_decoded(decoded, match): logger.error("cannot find address in input script %s", bh2u(script)) return nested_script = decoded[-1][1] dec2 = [x for x in _script_GetOp(nested_script)] x_pubkeys = [XPublicKey(x[1]) for x in dec2[1:-2]] m = dec2[0][0] - Ops.OP_1 + 1 n = dec2[-2][0] - Ops.OP_1 + 1 op_m = Ops.OP_1 + m - 1 op_n = Ops.OP_1 + n - 1 match_multisig = [op_m] + [Ops.OP_PUSHDATA4] * n + [ op_n, Ops.OP_CHECKMULTISIG ] if not _match_decoded(dec2, match_multisig): logger.error("cannot find address in input script %s", bh2u(script)) return kwargs['x_pubkeys'] = x_pubkeys kwargs['threshold'] = m kwargs['address'] = P2SH_Address(hash160(multisig_script(x_pubkeys, m)), Net.COIN) kwargs['signatures'] = [x[1] for x in decoded[1:-1]] return
def parse_script_sig(script: bytes, kwargs: Dict[str, Any]) -> None: try: decoded = list(_script_GetOp(script)) except Exception: # coinbase transactions raise an exception logger.exception("cannot find address in input script %s", script.hex()) return # P2PK match = [Ops.OP_PUSHDATA4] if _match_decoded(decoded, match): item = decoded[0][1] kwargs['signatures'] = [item] kwargs['threshold'] = 1 kwargs['script_type'] = ScriptType.P2PK return # P2PKH inputs push a signature (around seventy bytes) and then their public key # (65 bytes) onto the stack match = [Ops.OP_PUSHDATA4, Ops.OP_PUSHDATA4] if _match_decoded(decoded, match): sig = decoded[0][1] x_pubkey = XPublicKey.from_bytes(decoded[1][1]) kwargs['signatures'] = [sig] kwargs['threshold'] = 1 kwargs['x_pubkeys'] = [x_pubkey] kwargs['script_type'] = ScriptType.P2PKH kwargs['address'] = x_pubkey.to_address() return # p2sh transaction, m of n match = [Ops.OP_0] + [Ops.OP_PUSHDATA4] * (len(decoded) - 1) if not _match_decoded(decoded, match): logger.error("cannot find address in input script %s", script.hex()) return nested_script = decoded[-1][1] dec2 = [x for x in _script_GetOp(nested_script)] x_pubkeys = [XPublicKey.from_bytes(x[1]) for x in dec2[1:-2]] m, n, match_multisig = _extract_multisig_pattern(dec2) if not _match_decoded(dec2, match_multisig): logger.error("cannot find address in input script %s", script.hex()) return kwargs['script_type'] = ScriptType.MULTISIG_P2SH kwargs['x_pubkeys'] = x_pubkeys kwargs['threshold'] = m kwargs['address'] = P2SH_Address(hash160(multisig_script(x_pubkeys, m)), Net.COIN) kwargs['signatures'] = [x[1] for x in decoded[1:-1]] return
def get_fingerprint(self) -> bytes: return hash160(bytes.fromhex(self.mpk))[:4]
def test_hash160(self): output = P2MultiSig_Output(MS_PUBKEYS, 1) assert output.hash160() == hash160(output.to_script_bytes())
def test_hash160(self): pubkey_hex = '0363f75554e05e05a04551e59d78d78965ec6789f42199f7cbaa9fa4bd2df0a4b4' pubkey = PublicKey.from_hex(pubkey_hex) output = P2PK_Output(pubkey, Bitcoin) assert output.hash160() == hash160(bytes.fromhex(pubkey_hex))
k = h_secret secret_wrong = b'This is the wrong secret message!' h_secret_wrong = sha256(secret_wrong) k_wrong = h_secret G = PublicKey.from_hex( '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') Q = G.multiply(k) r, _ = Q.to_point() r = int_to_be_bytes(r) if r[0] & 0x80: r0 = pack_byte(0) + r else: r0 = r r_hash = hash160(r0) # Ephermal key to generate the r signature key_priv_R = PrivateKey.from_arbitrary_bytes(b'123test') key_pub_R = key_priv_R.public_key contract = './test/res/rpuzzle.scrypt' compiler_result = scryptlib.utils.compile_contract(contract) desc = compiler_result.to_desc() RPuzzle = scryptlib.contract.build_contract_class(desc) r_puzzle = RPuzzle(Ripemd160(r_hash)) utxo_satoshis = 100004 context = scryptlib.utils.create_dummy_input_context()
import scryptlib.utils import scryptlib.contract from scryptlib.types import Int, PubKey, Sig, SigHashPreimage, Ripemd160, Bytes import bitcoinx from bitcoinx import PrivateKey, TxOutput, SigHash, pack_byte, Script, hash160, P2PKH_Address, Bitcoin key_priv_A = PrivateKey.from_arbitrary_bytes(b'test123') key_pub_A = key_priv_A.public_key pkh_A = key_pub_A.hash160() key_priv_B = PrivateKey.from_arbitrary_bytes(b'test123') key_pub_B = key_priv_B.public_key pkh_B = key_pub_B.hash160() player_A_data = hash160(b'\x01' + key_pub_A.to_bytes()) sighash_flag = SigHash(SigHash.ANYONE_CAN_PAY | SigHash.ALL | SigHash.FORKID) pub_key_hashlen = 20 action_INIT = 0 action_ROCK = 1 action_PAPER = 2 action_SCISSORS = 3 contract = './test/res/rps.scrypt' compiler_result = scryptlib.utils.compile_contract(contract) desc = compiler_result.to_desc()