<commit_before>from ..script import opcodes, tools
from ..script.check_signature import parse_signature_blob
from ..script.der import UnexpectedDER

from ... import ecdsa
from ... import encoding

from ...serialize import b2h

from ..exceptions import SolvingError

from .ScriptType import ScriptType, DEFAULT_PLACEHOLDER_SIGNATURE


# see BIP11 https://github.com/bitcoin/bips/blob/master/bip-0011.mediawiki

class ScriptMultisig(ScriptType):
    def __init__(self, m, sec_keys):
        self.m = m
        self.sec_keys = sec_keys
        self._script = None

    @classmethod
    def from_script(cls, script):
        pc = 0
        if len(script) == 0:
            raise ValueError("blank script")
        opcode, data, pc = tools.get_opcode(script, pc)

        if not opcodes.OP_1 <= opcode < opcodes.OP_16:
            raise ValueError("m value invalid")
        m = opcode + (1 - opcodes.OP_1)
        sec_keys = []
        while 1:
            if pc >= len(script):
                raise ValueError("unexpected end of script")
            opcode, data, pc = tools.get_opcode(script, pc)
            l = len(data) if data else 0
            if l < 33 or l > 120:
                break
            sec_keys.append(data)
        n = opcode + (1 - opcodes.OP_1)
        if m > n or len(sec_keys) != n:
            raise ValueError("n value wrong")

        opcode, data, pc = tools.get_opcode(script, pc)
        if opcode != opcodes.OP_CHECKMULTISIG:
            raise ValueError("no OP_CHECKMULTISIG")
        if pc != len(script):
            raise ValueError("extra stuff at end")

        return cls(sec_keys=sec_keys, m=m)

    def script(self):
        if self._script is None:
            # create the script
            # TEMPLATE = m {pubkey}...{pubkey} n OP_CHECKMULTISIG
            public_keys = [b2h(sk) for sk in self.sec_keys]
            script_source = "%d %s %d OP_CHECKMULTISIG" % (self.m, " ".join(public_keys), len(public_keys))
            self._script = tools.compile(script_source)
        return self._script<commit_msg>throw exception on N of M multisig<commit_after>from ..script import opcodes, tools
from ..script.check_signature import parse_signature_blob
from ..script.der import UnexpectedDER

from ... import ecdsa
from ... import encoding

from ...serialize import b2h

from ..exceptions import SolvingError

from .ScriptType import ScriptType, DEFAULT_PLACEHOLDER_SIGNATURE


# see BIP11 https://github.com/bitcoin/bips/blob/master/bip-0011.mediawiki

class ScriptMultisig(ScriptType):
    def __init__(self, m, sec_keys):
        self.m = m
        self.sec_keys = sec_keys
        self._script = None

    @classmethod
    def from_script(cls, script):
        pc = 0
        if len(script) == 0:
            raise ValueError("blank script")
        opcode, data, pc = tools.get_opcode(script, pc)

        if not opcodes.OP_1 <= opcode < opcodes.OP_16:
            raise ValueError("m value invalid")
        m = opcode + (1 - opcodes.OP_1)
        sec_keys = []
        while 1:
            if pc >= len(script):
                raise ValueError("unexpected end of script")
            opcode, data, pc = tools.get_opcode(script, pc)
            l = len(data) if data else 0
            if l < 33 or l > 120:
                break
            sec_keys.append(data)
        n = opcode + (1 - opcodes.OP_1)
        if m > n or len(sec_keys) != n:
            raise ValueError("n value wrong")

        opcode, data, pc = tools.get_opcode(script, pc)
        if opcode != opcodes.OP_CHECKMULTISIG:
            raise ValueError("no OP_CHECKMULTISIG")
        if pc != len(script):
            raise ValueError("extra stuff at end")

        return cls(sec_keys=sec_keys, m=m)

    def script(self):
        if self._script is None:
            # create the script
            # TEMPLATE = m {pubkey}...{pubkey} n OP_CHECKMULTISIG
            if len(self.sec_keys) < self.m:
                raise ValueError("m value invalid: Swap M and N (M of N) to match convention")

            public_keys = [b2h(sk) for sk in self.sec_keys]
            script_source = "%d %s %d OP_CHECKMULTISIG" % (self.m, " ".join(public_keys), len(public_keys))
            self._script = tools.compile(script_source)
        return self._script
{'fraction_non_alphanumeric': 0.10110250996950504, 'fraction_numerical': 0.011259676284306826, 'mean_word_length': 2.1491875923190547, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4021626', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1304, 'n_words': 372}