def msg_sign(args, parser): network = network_for_netcode(args.network) message_signer = MessageSigner(network) message_hash = get_message_hash(args, message_signer) network, key = parse_key(args.WIF, [network]) is_compressed = not key._use_uncompressed() sig = message_signer.signature_for_message_hash( key.secret_exponent(), msg_hash=message_hash, is_compressed=is_compressed) print(sig)
def msg_verify(args, parser): network = network_for_netcode(args.network) message_signer = MessageSigner(network) message_hash = get_message_hash(args, message_signer) try: pair, is_compressed = message_signer.pair_for_message_hash( args.signature, msg_hash=message_hash) except EncodingError: pass ta = network.ui.address_for_p2pkh( public_pair_to_hash160_sec(pair, compressed=is_compressed)) if args.address: if ta == args.address: print("signature ok") return 0 else: print("bad signature, matches %s" % ta) return 1 else: print(ta)
import textwrap import unittest from pycoin.contrib.msg_signing import MessageSigner from pycoin.symbols.btc import network as BitcoinMainnet from pycoin.symbols.xtn import network as BitcoinTestnet # BRAIN DAMAGE Key = BitcoinMainnet.extras.Key message_signer = MessageSigner(BitcoinMainnet) XTN_message_signer = MessageSigner(BitcoinTestnet) def test_against_myself(): """ Test code that verifies against ourselves only. Useful but not so great. """ for wif, right_addr in [ ('L4gXBvYrXHo59HLeyem94D9yLpRkURCHmCwQtPuWW9m6o1X8p8sp', '1LsPb3D1o1Z7CzEt1kv5QVxErfqzXxaZXv'), ('5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss', '1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN'), ]: k = BitcoinMainnet.ui.parse(wif) assert k.address() == right_addr vk = Key(public_pair=k.public_pair(), is_compressed=not k._use_uncompressed()) assert vk.address() == right_addr
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
import textwrap import unittest from pycoin.contrib.msg_signing import MessageSigner from pycoin.ecdsa.secp256k1 import secp256k1_generator # BRAIN DAMAGE from pycoin.coins.bitcoin.networks import BitcoinMainnet, BitcoinTestnet Key = BitcoinMainnet.extras.Key message_signer = MessageSigner(BitcoinMainnet.network_name, BitcoinMainnet.ui, secp256k1_generator) XTN_message_signer = MessageSigner(BitcoinTestnet.network_name, BitcoinTestnet.ui, secp256k1_generator) def test_against_myself(): """ Test code that verifies against ourselves only. Useful but not so great. """ for wif, right_addr in [ ('L4gXBvYrXHo59HLeyem94D9yLpRkURCHmCwQtPuWW9m6o1X8p8sp', '1LsPb3D1o1Z7CzEt1kv5QVxErfqzXxaZXv'), ('5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss', '1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN'), ]: k = BitcoinMainnet.ui.parse(wif) assert k.address() == right_addr vk = Key(public_pair=k.public_pair(),