def multisig_script(x_pubkeys: List[XPublicKey], threshold: int) -> bytes: '''Returns bytes. x_pubkeys is an array of XPulicKey objects or an array of PublicKey objects. ''' assert 1 <= threshold <= len(x_pubkeys) parts = [push_int(threshold)] parts.extend(push_item(x_pubkey.to_bytes()) for x_pubkey in x_pubkeys) parts.append(push_int(len(x_pubkeys))) parts.append(pack_byte(Ops.OP_CHECKMULTISIG)) return b''.join(parts)
def multisig_script(public_keys, threshold): '''public_keys should be sorted hex strings. P2MultiSig_Ouput is not used as they may be derivation rules and not valid public keys. ''' if sorted(public_keys) != public_keys: logger.warning('public keys are not sorted') assert 1 <= threshold <= len(public_keys) parts = [push_int(threshold)] parts.extend( push_item(bytes.fromhex(public_key)) for public_key in public_keys) parts.append(push_int(len(public_keys))) parts.append(pack_byte(Ops.OP_CHECKMULTISIG)) return b''.join(parts).hex()
def test_to_script_bytes(self, threshold, count): output = P2MultiSig_Output(MS_PUBKEYS[:count], threshold) assert output.public_key_count() == count raw = output.to_script_bytes() assert isinstance(raw, bytes) assert raw == b''.join(( push_int(threshold), b''.join(push_item(public_key.to_bytes()) for public_key in MS_PUBKEYS[:count]), push_int(count), pack_byte(OP_CHECKMULTISIG), )) S = output.to_script() assert isinstance(S, Script) assert S == raw
def to_script_bytes(self): parts = [ pack_byte(Ops.OP_0), pack_byte(Ops.OP_TOALTSTACK), ] for public_key in self.public_keys: parts.extend([ pack_byte(Ops.OP_IF), pack_byte(Ops.OP_DUP), pack_byte(Ops.OP_HASH160), push_item(public_key.hash160()), pack_byte(Ops.OP_EQUALVERIFY), pack_byte(Ops.OP_CHECKSIGVERIFY), pack_byte(Ops.OP_FROMALTSTACK), pack_byte(Ops.OP_1ADD), pack_byte(Ops.OP_TOALTSTACK), pack_byte(Ops.OP_ENDIF), ]) parts.extend([ # Is this the right order? pack_byte(Ops.OP_FROMALTSTACK), push_int(self.threshold), pack_byte(Ops.OP_GREATERTHANOREQUAL), ]) return b''.join(parts)
def hex(self): return bitcoinx.push_int(self.value).hex()
def asm(self): if self.value == -1: return 'OP_1NEGATE' if self.value >= 0 and self.value <= 16: return 'OP_{}'.format(self.value) return bitcoinx.push_int(self.value)[1:].hex()