예제 #1
0
 def test_initial_key(self):
     RECEIVING_ADDRESSES = [
         "1LDkC1H438qSnJLHCYkQ3WTZQkSEwoYGHc",
         "12mENAcc8ZhZbR6hv7LGm3jV7PwbYeF8Xk",
         "1A3NpABFd6YHvwr1ti1r8brU3BzQuV2Nr4",
         "1Gn6nWAoZrpmtV9zuNbyivWvRBpcygWaQX",
         "1M5i5P3DhtDbnvSTfmnUbcrTVgF8GDWQW9"
     ]
     CHANGE_ADDRESSES = [
         "1iiAbyBTh1J69UzD1JcrfW8JSVJ9ve9gT",
         "146wnqmsQNYCZ6AXRCqLkzZyGM1ZU6nr3F",
         "1Mwexajvia3s8AcaGUkyEg9ZZJPJeTbKTZ"
     ]
     wallet = ElectrumWallet(generator=secp256k1_generator,
                             initial_key="00000000000000000000000000000001")
     for idx, address in enumerate(RECEIVING_ADDRESSES):
         subkey = wallet.subkey("%s/0" % idx)
         calculated_address = subkey.address()
         self.assertEqual(address, calculated_address)
         wif = subkey.wif()
         key = key_from_text(wif)
         self.assertEqual(key.address(use_uncompressed=True), address)
     for idx, address in enumerate(CHANGE_ADDRESSES):
         subkey = wallet.subkey("%s/1" % idx)
         calculated_address = subkey.address()
         self.assertEqual(address, calculated_address)
         wif = subkey.wif()
         key = key_from_text(wif)
         self.assertEqual(key.address(use_uncompressed=True), address)
예제 #2
0
 def test_initial_key(self):
     RECEIVING_ADDRESSES = [
         "1LDkC1H438qSnJLHCYkQ3WTZQkSEwoYGHc",
         "12mENAcc8ZhZbR6hv7LGm3jV7PwbYeF8Xk",
         "1A3NpABFd6YHvwr1ti1r8brU3BzQuV2Nr4",
         "1Gn6nWAoZrpmtV9zuNbyivWvRBpcygWaQX",
         "1M5i5P3DhtDbnvSTfmnUbcrTVgF8GDWQW9",
     ]
     CHANGE_ADDRESSES = [
         "1iiAbyBTh1J69UzD1JcrfW8JSVJ9ve9gT",
         "146wnqmsQNYCZ6AXRCqLkzZyGM1ZU6nr3F",
         "1Mwexajvia3s8AcaGUkyEg9ZZJPJeTbKTZ",
     ]
     wallet = ElectrumWallet(initial_key="00000000000000000000000000000001")
     for idx, address in enumerate(RECEIVING_ADDRESSES):
         subkey = wallet.subkey("%s/0" % idx)
         calculated_address = subkey.address()
         self.assertEqual(address, calculated_address)
         wif = subkey.wif()
         key = Key.from_text(wif)
         self.assertEqual(key.address(use_uncompressed=True), address)
     for idx, address in enumerate(CHANGE_ADDRESSES):
         subkey = wallet.subkey("%s/1" % idx)
         calculated_address = subkey.address()
         self.assertEqual(address, calculated_address)
         wif = subkey.wif()
         key = Key.from_text(wif)
         self.assertEqual(key.address(use_uncompressed=True), address)
예제 #3
0
 def test_1(self):
     wallet = ElectrumWallet(initial_key="00000000000000000000000000000001")
     for idx, address in enumerate(RECEIVING_ADDRESSES):
         subkey = wallet.subkey("%s/0" % idx)
         calculated_address = subkey.address()
         self.assertEqual(address, calculated_address)
         wif = subkey.wif()
         key = Key.from_text(wif)
         self.assertEqual(key.address(use_uncompressed=True), address)
     for idx, address in enumerate(CHANGE_ADDRESSES):
         subkey = wallet.subkey("%s/1" % idx)
         calculated_address = subkey.address()
         self.assertEqual(address, calculated_address)
         wif = subkey.wif()
         key = Key.from_text(wif)
         self.assertEqual(key.address(use_uncompressed=True), address)
예제 #4
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
예제 #5
0
 def test_1(self):
     wallet = ElectrumWallet(initial_key="00000000000000000000000000000001")
     for idx, address in enumerate(RECEIVING_ADDRESSES):
         subkey = wallet.subkey("%s/0" % idx)
         calculated_address = subkey.address()
         self.assertEqual(address, calculated_address)
         wif = subkey.wif()
         key = Key.from_text(wif)
         self.assertEqual(key.address(use_uncompressed=True), address)
     for idx, address in enumerate(CHANGE_ADDRESSES):
         subkey = wallet.subkey("%s/1" % idx)
         calculated_address = subkey.address()
         self.assertEqual(address, calculated_address)
         wif = subkey.wif()
         key = Key.from_text(wif)
         self.assertEqual(key.address(use_uncompressed=True), address)
예제 #6
0
    def test_master_public_and_private(self):
        # these addresses were generated by hand using electrum with a master public key
        # corresponding to secret exponent 1
        RECEIVING_ADDRESSES = [
            "1AYPdHLna6bKFUbeXoAEVbaXUxifUwCMay",
            "13UeuWJba5epizAKyfCfiFKY5Kbxfdxe7B",
            "19f6KJUTL5AGBRvLBGiL6Zpcx53QA7zaKT",
            "1Cm33VuSkoUETwx5nsF1wgmGqYwJZxpZdY",
            "14Z6ErkETixQMUeivsYbrdoUFns2J1iSct",
        ]
        CHANGE_ADDRESSES = [
            "1JVYsmjrqSy1BKvo1gYpNjX7AYea74nQYe",
            "1Cc7itfQaDqZK3vHYphFsySujQjBNba8mw",
            "15wrXvrAnyv3usGeQRohnnZ8tz9XAekbag",
            "1MnWCEjE5YiZpZrkP8HcXEeDqwg43RxLwu",
            "1Fgyp3PUx9AAg8yJe1zGXHP5dVC6i1tXbs",
            "12XTLd4u9jeqw4egLAUhoKLxHARCdKWkty"
        ]
        k = Key(secret_exponent=1, generator=secp256k1_generator)
        master_public_key = k.sec(use_uncompressed=True)[1:]

        wallet = ElectrumWallet(generator=secp256k1_generator,
                                master_public_key=master_public_key)
        for idx, address in enumerate(RECEIVING_ADDRESSES):
            subkey = wallet.subkey("%s/0" % idx)
            calculated_address = subkey.address()
            self.assertEqual(address, calculated_address)
        for idx, address in enumerate(CHANGE_ADDRESSES):
            subkey = wallet.subkey("%s/1" % idx)
            calculated_address = subkey.address()
            self.assertEqual(address, calculated_address)

        wallet = ElectrumWallet(generator=secp256k1_generator,
                                master_private_key=1)
        for idx, address in enumerate(RECEIVING_ADDRESSES):
            subkey = wallet.subkey("%s/0" % idx)
            calculated_address = subkey.address()
            self.assertEqual(address, calculated_address)
            wif = subkey.wif()
            key = key_from_text(wif)
            self.assertEqual(key.address(use_uncompressed=True), address)
        for idx, address in enumerate(CHANGE_ADDRESSES):
            subkey = wallet.subkey("%s/1" % idx)
            calculated_address = subkey.address()
            self.assertEqual(address, calculated_address)
            wif = subkey.wif()
            key = key_from_text(wif)
            self.assertEqual(key.address(use_uncompressed=True), address)
예제 #7
0
파일: uiclass.py 프로젝트: alanxu89/pycoin
 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
예제 #8
0
    def test_master_public_and_private(self):
        # these addresses were generated by hand using electrum with a master public key
        # corresponding to secret exponent 1
        RECEIVING_ADDRESSES = [
            "1AYPdHLna6bKFUbeXoAEVbaXUxifUwCMay",
            "13UeuWJba5epizAKyfCfiFKY5Kbxfdxe7B",
            "19f6KJUTL5AGBRvLBGiL6Zpcx53QA7zaKT",
            "1Cm33VuSkoUETwx5nsF1wgmGqYwJZxpZdY",
            "14Z6ErkETixQMUeivsYbrdoUFns2J1iSct",
        ]
        CHANGE_ADDRESSES = [
            "1JVYsmjrqSy1BKvo1gYpNjX7AYea74nQYe",
            "1Cc7itfQaDqZK3vHYphFsySujQjBNba8mw",
            "15wrXvrAnyv3usGeQRohnnZ8tz9XAekbag",
            "1MnWCEjE5YiZpZrkP8HcXEeDqwg43RxLwu",
            "1Fgyp3PUx9AAg8yJe1zGXHP5dVC6i1tXbs",
            "12XTLd4u9jeqw4egLAUhoKLxHARCdKWkty",
        ]
        k = Key(secret_exponent=1)
        master_public_key = k.sec(use_uncompressed=True)[1:]

        wallet = ElectrumWallet(master_public_key=master_public_key)
        for idx, address in enumerate(RECEIVING_ADDRESSES):
            subkey = wallet.subkey("%s/0" % idx)
            calculated_address = subkey.address()
            self.assertEqual(address, calculated_address)
        for idx, address in enumerate(CHANGE_ADDRESSES):
            subkey = wallet.subkey("%s/1" % idx)
            calculated_address = subkey.address()
            self.assertEqual(address, calculated_address)

        wallet = ElectrumWallet(master_private_key=1)
        for idx, address in enumerate(RECEIVING_ADDRESSES):
            subkey = wallet.subkey("%s/0" % idx)
            calculated_address = subkey.address()
            self.assertEqual(address, calculated_address)
            wif = subkey.wif()
            key = Key.from_text(wif)
            self.assertEqual(key.address(use_uncompressed=True), address)
        for idx, address in enumerate(CHANGE_ADDRESSES):
            subkey = wallet.subkey("%s/1" % idx)
            calculated_address = subkey.address()
            self.assertEqual(address, calculated_address)
            wif = subkey.wif()
            key = Key.from_text(wif)
            self.assertEqual(key.address(use_uncompressed=True), address)
예제 #9
0
import unittest

from pycoin.coins.bitcoin.networks import BitcoinMainnet
from pycoin.ecdsa.secp256k1 import secp256k1_generator
from pycoin.key import Key
from pycoin.key.electrum import ElectrumWallet
from pycoin.ui.key_from_text import key_from_text

# BRAIN DAMAGE
ElectrumWallet = ElectrumWallet.make_subclass(
    default_ui_context=BitcoinMainnet.ui)


class ElectrumTest(unittest.TestCase):
    def test_initial_key(self):
        RECEIVING_ADDRESSES = [
            "1LDkC1H438qSnJLHCYkQ3WTZQkSEwoYGHc",
            "12mENAcc8ZhZbR6hv7LGm3jV7PwbYeF8Xk",
            "1A3NpABFd6YHvwr1ti1r8brU3BzQuV2Nr4",
            "1Gn6nWAoZrpmtV9zuNbyivWvRBpcygWaQX",
            "1M5i5P3DhtDbnvSTfmnUbcrTVgF8GDWQW9"
        ]
        CHANGE_ADDRESSES = [
            "1iiAbyBTh1J69UzD1JcrfW8JSVJ9ve9gT",
            "146wnqmsQNYCZ6AXRCqLkzZyGM1ZU6nr3F",
            "1Mwexajvia3s8AcaGUkyEg9ZZJPJeTbKTZ"
        ]
        wallet = ElectrumWallet(generator=secp256k1_generator,
                                initial_key="00000000000000000000000000000001")
        for idx, address in enumerate(RECEIVING_ADDRESSES):
            subkey = wallet.subkey("%s/0" % idx)
예제 #10
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