Beispiel #1
0
 def __init__(self,
              puzzle_scripts,
              generator,
              bip32_prv_prefix=None,
              bip32_pub_prefix=None,
              wif_prefix=None,
              sec_prefix=None,
              address_prefix=None,
              pay_to_script_prefix=None,
              bech32_hrp=None):
     self._script_info = puzzle_scripts
     self._key_class = Key.make_subclass(ui_context=self,
                                         generator=generator)
     self._electrum_class = ElectrumWallet.make_subclass(
         ui_context=self, generator=generator)
     self._bip32node_class = BIP32Node.make_subclass(ui_context=self,
                                                     generator=generator)
     self._parsers = [
         WIFParser(generator, wif_prefix, address_prefix, self._key_class),
         ElectrumParser(generator, self._electrum_class),
         BIP32Parser(generator, bip32_prv_prefix, bip32_pub_prefix,
                     self._bip32node_class),
         Hash160Parser(address_prefix, self._key_class),
         SECParser(generator, sec_prefix, self._key_class),
         AddressParser(puzzle_scripts, address_prefix, pay_to_script_prefix,
                       bech32_hrp)
     ]
     self._bip32_prv_prefix = bip32_prv_prefix
     self._bip32_pub_prefix = bip32_pub_prefix
     self._wif_prefix = wif_prefix
     self._sec_prefix = sec_prefix
     self._address_prefix = address_prefix
     self._pay_to_script_prefix = pay_to_script_prefix
     self._bech32_hrp = bech32_hrp
Beispiel #2
0
 def __init__(self, puzzle_scripts, generator, bip32_prv_prefix=None, bip32_pub_prefix=None,
              wif_prefix=None, sec_prefix=None, address_prefix=None, pay_to_script_prefix=None, bech32_hrp=None):
     self._script_info = puzzle_scripts
     self._key_class = Key.make_subclass(ui_context=self, generator=generator)
     self._electrum_class = ElectrumWallet.make_subclass(ui_context=self, generator=generator)
     self._bip32node_class = BIP32Node.make_subclass(ui_context=self, generator=generator)
     self._parsers = [
         WIFParser(generator, wif_prefix, address_prefix, self._key_class),
         ElectrumParser(generator, self._electrum_class),
         BIP32Parser(generator, bip32_prv_prefix, bip32_pub_prefix, self._bip32node_class),
         Hash160Parser(address_prefix, self._key_class),
         SECParser(generator, sec_prefix, self._key_class),
         AddressParser(puzzle_scripts, address_prefix, pay_to_script_prefix, bech32_hrp)
     ]
     self._bip32_prv_prefix = bip32_prv_prefix
     self._bip32_pub_prefix = bip32_pub_prefix
     self._wif_prefix = wif_prefix
     self._sec_prefix = sec_prefix
     self._address_prefix = address_prefix
     self._pay_to_script_prefix = pay_to_script_prefix
     self._bech32_hrp = bech32_hrp
Beispiel #3
0
def create_bitcoinish_network(symbol, network_name, subnet_name, **kwargs):
    # potential kwargs:
    #   tx, block, magic_header_hex, default_port, dns_bootstrap,
    #   wif_prefix_hex, address_prefix_hex, pay_to_script_prefix_hex
    #   bip32_prv_prefix_hex, bip32_pub_prefix_hex, sec_prefix, script_tools
    #   bip49_prv_prefix, bip49_pub_prefix, bip84_prv_prefix, bip84_pub_prefix

    network = Network(symbol, network_name, subnet_name)

    generator = kwargs.get("generator", secp256k1_generator)
    kwargs.setdefault("sec_prefix", "%sSEC" % symbol.upper())
    KEYS_TO_H2B = (
        "bip32_prv_prefix bip32_pub_prefix bip49_prv_prefix bip49_pub_prefix "
        "bip84_prv_prefix bip84_pub_prefix wif_prefix address_prefix "
        "pay_to_script_prefix sec_prefix magic_header").split()
    for k in KEYS_TO_H2B:
        k_hex = "%s_hex" % k
        if k_hex in kwargs:
            kwargs[k] = h2b(kwargs[k_hex])

    script_tools = kwargs.get("script_tools", BitcoinScriptTools)

    UI_KEYS = (
        "bip32_prv_prefix bip32_pub_prefix bip49_prv_prefix bip49_pub_prefix "
        "bip84_prv_prefix bip84_pub_prefix wif_prefix sec_prefix "
        "address_prefix pay_to_script_prefix bech32_hrp").split()
    ui_kwargs = {k: kwargs[k] for k in UI_KEYS if k in kwargs}

    _bip32_prv_prefix = ui_kwargs.get("bip32_prv_prefix")
    _bip32_pub_prefix = ui_kwargs.get("bip32_pub_prefix")
    _wif_prefix = ui_kwargs.get("wif_prefix")
    _sec_prefix = ui_kwargs.get("sec_prefix")

    def bip32_as_string(blob, as_private):
        prefix = ui_kwargs.get("bip32_%s_prefix" %
                               ("prv" if as_private else "pub"))
        return b2a_hashed_base58(prefix + blob)

    def bip49_as_string(blob, as_private):
        prefix = ui_kwargs.get("bip49_%s_prefix" %
                               ("prv" if as_private else "pub"))
        return b2a_hashed_base58(prefix + blob)

    def bip84_as_string(blob, as_private):
        prefix = ui_kwargs.get("bip84_%s_prefix" %
                               ("prv" if as_private else "pub"))
        return b2a_hashed_base58(prefix + blob)

    def wif_for_blob(blob):
        return b2a_hashed_base58(_wif_prefix + blob)

    def sec_text_for_blob(blob):
        return _sec_prefix + b2h(blob)

    NetworkKey = Key.make_subclass(symbol,
                                   network=network,
                                   generator=generator)
    NetworkElectrumKey = ElectrumWallet.make_subclass(symbol,
                                                      network=network,
                                                      generator=generator)
    NetworkBIP32Node = BIP32Node.make_subclass(symbol,
                                               network=network,
                                               generator=generator)
    NetworkBIP49Node = BIP49Node.make_subclass(symbol,
                                               network=network,
                                               generator=generator)
    NetworkBIP84Node = BIP84Node.make_subclass(symbol,
                                               network=network,
                                               generator=generator)

    NETWORK_KEYS = "network_name subnet_name dns_bootstrap default_port magic_header".split(
    )
    for k in NETWORK_KEYS:
        if k in kwargs:
            setattr(network, k, kwargs[k])

    network.Tx = network.tx = kwargs.get("tx") or Tx
    network.Block = network.block = kwargs.get("block") or Block.make_subclass(
        symbol, network.tx)

    streamer = standard_streamer(
        standard_parsing_functions(network.block, network.tx))

    network.message = API()
    network.message.parse, network.message.pack = make_parser_and_packer(
        streamer, standard_messages(), standard_message_post_unpacks(streamer))

    network.output_for_secret_exponent = make_output_for_secret_exponent(
        NetworkKey)
    network.output_for_public_pair = make_output_for_public_pair(
        NetworkKey, network)

    network.keychain = Keychain

    parse_api_class = kwargs.get("parse_api_class", ParseAPI)
    network.parse = parse_api_class(network, **ui_kwargs)

    network.contract = ContractAPI(network, script_tools)

    network.address = make_address_api(network.contract, **ui_kwargs)

    def keys_private(secret_exponent, is_compressed=True):
        return NetworkKey(secret_exponent=secret_exponent,
                          is_compressed=is_compressed)

    def keys_public(item, is_compressed=None):
        if isinstance(item, tuple):
            if is_compressed is None:
                is_compressed = True
            # it's a public pair
            return NetworkKey(public_pair=item, is_compressed=is_compressed)
        if is_compressed is not None:
            raise ValueError("can't set is_compressed from sec")
        return NetworkKey.from_sec(item)

    network.keys = API()
    network.keys.private = keys_private
    network.keys.public = keys_public

    def electrum_seed(seed):
        return NetworkElectrumKey(initial_key=seed)

    def electrum_private(master_private_key):
        return NetworkElectrumKey(master_private_key=master_private_key)

    def electrum_public(master_public_key):
        return NetworkElectrumKey(master_public_key=master_public_key)

    network.keys.bip32_seed = NetworkBIP32Node.from_master_secret
    network.keys.bip32_deserialize = NetworkBIP32Node.deserialize
    network.keys.bip49_deserialize = NetworkBIP49Node.deserialize
    network.keys.bip84_deserialize = NetworkBIP84Node.deserialize

    network.keys.electrum_seed = electrum_seed
    network.keys.electrum_private = electrum_private
    network.keys.electrum_public = electrum_public
    network.keys.InvalidSecretExponentError = InvalidSecretExponentError
    network.keys.InvalidPublicPairError = InvalidPublicPairError

    network.msg = API()
    message_signer = MessageSigner(network, generator)
    network.msg.sign = message_signer.sign_message
    network.msg.verify = message_signer.verify_message
    network.msg.parse_signed = message_signer.parse_signed_message
    network.msg.hash_for_signing = message_signer.hash_for_signing
    network.msg.signature_for_message_hash = message_signer.signature_for_message_hash
    network.msg.pair_for_message_hash = message_signer.pair_for_message_hash
    network.script = script_tools

    network.bip32_as_string = bip32_as_string
    network.bip49_as_string = bip49_as_string
    network.sec_text_for_blob = sec_text_for_blob
    network.wif_for_blob = wif_for_blob

    def network_build_hash160_lookup(iter):
        return build_hash160_lookup(iter, [generator])

    network.tx.solve = API()
    network.tx.solve.build_hash160_lookup = network_build_hash160_lookup
    network.tx.solve.build_p2sh_lookup = build_p2sh_lookup
    network.tx.solve.build_sec_lookup = build_sec_lookup

    network.validator = API()
    network.validator.ScriptError = ScriptError
    network.validator.ValidationFailureError = ValidationFailureError
    network.validator.errno = errno
    network.validator.flags = flags

    def my_create_tx(*args, **kwargs):
        return create_tx(network, *args, **kwargs)

    def my_sign_tx(*args, **kwargs):
        return sign_tx(network, *args, **kwargs)

    def my_create_signed_tx(*args, **kwargs):
        return create_signed_tx(network, *args, **kwargs)

    def my_split_with_remainder(*args, **kwargs):
        return split_with_remainder(network, *args, **kwargs)

    network.tx_utils = API()
    network.tx_utils.create_tx = my_create_tx
    network.tx_utils.sign_tx = my_sign_tx
    network.tx_utils.create_signed_tx = my_create_signed_tx
    network.tx_utils.split_with_remainder = my_split_with_remainder
    network.tx_utils.distribute_from_split_pool = distribute_from_split_pool

    network.annotate = Annotate(script_tools, network.address)

    network.who_signed = WhoSigned(script_tools, network.address, generator)

    network.str = parseable_str

    network.generator = generator

    return network