def test_make_parser_and_packer(self): streamer = standard_streamer(standard_parsing_functions(network.block, network.tx)) btc_parser, btc_packer = make_parser_and_packer( streamer, standard_messages(), standard_message_post_unpacks(streamer)) parser, packer = make_parser_and_packer(streamer, standard_messages(), standard_message_post_unpacks(streamer)) cases = [("verack", {}), ("ping", dict(nonce=1929)), ("pong", dict(nonce=18373))] for msg_name, kwargs in cases: data = packer(msg_name, **kwargs) v = parser(msg_name, data) assert v == kwargs
def test_make_parser_and_packer(self): streamer = standard_streamer(standard_parsing_functions(BitcoinMainnet.block, BitcoinMainnet.tx)) btc_parser, btc_packer = make_parser_and_packer( streamer, standard_messages(), standard_message_post_unpacks(streamer)) parser, packer = make_parser_and_packer(streamer, standard_messages(), standard_message_post_unpacks(streamer)) cases = [("verack", {}), ("ping", dict(nonce=1929)), ("pong", dict(nonce=18373))] for msg_name, kwargs in cases: data = packer(msg_name, **kwargs) v = parser(msg_name, data) assert v == kwargs
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, scriptTools 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 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]) network.script_tools = kwargs.get("scriptTools", BitcoinScriptTools) network.script_info = ScriptInfo(network.script_tools) UI_KEYS = ("bip32_prv_prefix bip32_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} network.ui = UI(network.script_info, generator, **ui_kwargs) network.extras = Extras(network.script_tools, network.ui) NETWORK_KEYS = "network_name subnet_name dns_bootstrap default_port magic_header".split( ) network_kwargs = {k: kwargs.get(k) for k in NETWORK_KEYS if k in kwargs} 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( network.tx) streamer = standard_streamer( standard_parsing_functions(network.block, network.tx)) network.parse_message, network.pack_message = make_parser_and_packer( streamer, standard_messages(), standard_message_post_unpacks(streamer)) network.output_for_hwif = make_output_for_hwif(network) network.output_for_secret_exponent = make_output_for_secret_exponent( network.extras.Key) network.output_for_public_pair = make_output_for_public_pair( network.extras.Key, network) network.output_for_h160 = make_output_for_h160(network) network.BIP32Node = network.extras.BIP32Node network.Key = network.extras.Key network.ElectrumKey = network.extras.ElectrumKey network.Keychain = Keychain return network
def create_bitcoinish_network(**kwargs): # potential kwargs: # netcode, network_name, subnet_name, 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, scriptTools kwargs.setdefault("sec_prefix", "%sSEC" % kwargs["netcode"].upper()) KEYS_TO_H2B = ( "bip32_prv_prefix bip32_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]) scriptTools = kwargs.get("scriptTools", BitcoinScriptTools) _script_info = ScriptInfo(scriptTools) UI_KEYS = ("bip32_prv_prefix bip32_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} ui = UI(_script_info, kwargs.get("generator", secp256k1_generator), **ui_kwargs) extras = Extras(scriptTools, ui) kwargs["ui"] = ui kwargs["extras"] = extras kwargs.setdefault("tx", Tx) kwargs.setdefault("block", Block.make_subclass(kwargs["tx"])) NETWORK_KEYS = ("network_name subnet_name tx block ui extras " "dns_bootstrap default_port magic_header").split() network_kwargs = {k: kwargs.get(k) for k in NETWORK_KEYS if k in kwargs} network_kwargs["code"] = kwargs["netcode"] # BRAIN DAMAGE network = Network(**network_kwargs) streamer = standard_streamer( standard_parsing_functions(network.block, network.tx)) network.parse_message, network.pack_message = make_parser_and_packer( streamer, standard_messages(), standard_message_post_unpacks(streamer)) return network
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