Esempio n. 1
0
def keychain(args, parser):
    network = network_for_netcode(args.netcode)

    parse = network.ui.parse

    keychain = network.keychain(sqlite3.connect(args.keychain))

    keys = []
    for _ in args.key:
        key = parse(_, types=["electrum", "bip32"])
        if not key:
            raise ValueError("can't parse %s" % _)
        keys.append(key)

    subkey_paths = args.subkey_paths

    m = args.multisig
    if m and m > len(keys):
        raise ValueError("not enough keys for %d signatures" % m)

    total_paths = 0

    for path in subpaths_for_path_range(subkey_paths):
        if m:
            secs = sorted([_.subkey_for_path(path).sec() for _ in keys])
            script = network.contract.for_multisig(m, secs)
            keychain.add_p2s_script(script)
            print(network.ui.address_for_p2s(script))
        total_paths += keychain.add_keys_path(keys, path)
    keychain.commit()
    print("%d total paths" % total_paths, file=sys.stderr)
Esempio n. 2
0
def parse_private_key_file(args, keychain):
    wif_re = re.compile(r"[1-9a-km-zA-LMNP-Z]{51,111}")
    # address_re = re.compile(r"[1-9a-kmnp-zA-KMNP-Z]{27-31}")
    for f in args.private_key_file:
        if f.name.endswith(".gpg"):
            f = replace_with_gpg_pipe(args, f)
        for line in f.readlines():
            # decode
            if isinstance(line, bytes):
                line = line.decode("utf8")
            # look for WIFs
            possible_keys = wif_re.findall(line)

            def make_key(x):
                try:
                    return key_from_text(x)
                except Exception:
                    return None

            keys = [make_key(x) for x in possible_keys]
            for key in keys:
                if key:
                    keychain.add_secrets([key])
                    keychain.add_key_paths(
                        key, subpaths_for_path_range(args.key_paths))
Esempio n. 3
0
def keychain(args, parser):
    network = network_for_netcode(args.netcode)

    parse = network.ui.parse

    keychain = Keychain(sqlite3.connect(args.keychain))

    keys = []
    for _ in args.key:
        key = parse(_, types=["electrum", "bip32"])
        if not key:
            raise ValueError("can't parse %s" % _)
        keys.append(key)

    subkey_paths = args.subkey_paths

    m = args.multisig
    if m and m > len(keys):
        raise ValueError("not enough keys for %d signatures" % m)

    total_paths = 0

    script_for_multisig = network.ui._script_info.script_for_multisig
    for path in subpaths_for_path_range(subkey_paths):
        if m:
            secs = sorted([_.subkey_for_path(path).sec() for _ in keys])
            script = script_for_multisig(m, secs)
            keychain.add_p2s_script(script)
            print(network.ui.address_for_p2s(script))
        total_paths += keychain.add_keys_path(keys, path)
    keychain.commit()
    print("%d total paths" % total_paths, file=sys.stderr)
Esempio n. 4
0
def key_found(arg, keychain, key_paths, network):
    try:
        secret = network.parse.secret(arg)
        if secret:
            # TODO: check network
            keychain.add_secrets([secret])
            keychain.add_key_paths(secret, subpaths_for_path_range(key_paths))
            return True
    except Exception:
        pass

    return False
Esempio n. 5
0
def key_found(arg, keychain, key_paths, network):
    try:
        secret = network.parse.secret(arg)
        if secret:
            # TODO: check network
            keychain.add_secrets([secret])
            keychain.add_key_paths(secret, subpaths_for_path_range(key_paths))
            return True
    except Exception:
        pass

    return False
Esempio n. 6
0
 def test_keychain(self):
     keychain = Keychain()
     bip32_list = [BIP32.from_master_secret(_) for _ in [b"foo", b"bar"]]
     for bip32 in bip32_list:
         keychain.add_key_paths(bip32.public_copy(), subpaths_for_path_range("0-1/0-10"))
     keychain.add_secrets(bip32_list)
     for bip32 in bip32_list:
         for path in ["0/5", "1/2", "0/9"]:
             subkey = bip32.subkey_for_path("0/5")
             v = keychain.get(subkey.hash160())
             self.assertEqual(v[0], subkey.secret_exponent())
     v = keychain.get(b'0' * 32)
     self.assertEqual(v, None)
Esempio n. 7
0
 def test_keychain(self):
     keychain = network.keychain()
     bip32_list = [network.keys.bip32_seed(_) for _ in [b"foo", b"bar"]]
     for bip32 in bip32_list:
         keychain.add_key_paths(bip32.public_copy(),
                                subpaths_for_path_range("0-1/0-10"))
     keychain.add_secrets(bip32_list)
     for bip32 in bip32_list:
         for path in ["0/5", "1/2", "0/9"]:
             subkey = bip32.subkey_for_path("0/5")
             v = keychain.get(subkey.hash160())
             self.assertEqual(v[0], subkey.secret_exponent())
     v = keychain.get(b'0' * 32)
     self.assertEqual(v, None)
Esempio n. 8
0
def key_found(arg, payables, keychain, key_paths, network):
    try:
        key = network.ui.parse(arg)
        # TODO: check network
        if not hasattr(key, "secret_exponent") or key.secret_exponent() is None:
            payables.append((network.ui.script_for_address(key.address()), 0))
            return True
        keychain.add_secrets([key])
        keychain.add_key_paths(key, subpaths_for_path_range(key_paths))
        return True
    except Exception:
        pass

    return False
Esempio n. 9
0
def key_found(arg, payables, keychain, key_paths, network):
    try:
        key = network.ui.parse(arg)
        # TODO: check network
        if not hasattr(key,
                       "secret_exponent") or key.secret_exponent() is None:
            payables.append((network.ui.script_for_address(key.address()), 0))
            return True
        keychain.add_secrets([key])
        keychain.add_key_paths(key, subpaths_for_path_range(key_paths))
        return True
    except Exception:
        pass

    return False
Esempio n. 10
0
 def test_keychain(self):
     netcode = "BTC"
     network = network_for_netcode(netcode)
     BIP32 = network.ui._bip32node_class
     keychain = Keychain()
     bip32_list = [BIP32.from_master_secret(_) for _ in [b"foo", b"bar"]]
     for bip32 in bip32_list:
         keychain.add_key_paths(bip32.public_copy(), subpaths_for_path_range("0-1/0-10"))
     keychain.add_secrets(bip32_list)
     for bip32 in bip32_list:
         for path in ["0/5", "1/2", "0/9"]:
             subkey = bip32.subkey_for_path("0/5")
             v = keychain.get(subkey.hash160())
             self.assertEqual(v[0], subkey.secret_exponent())
     v = keychain.get(b'0' * 32)
     self.assertEqual(v, None)
Esempio n. 11
0
def parse_private_key_file(args, keychain, network):
    wif_re = re.compile(r"[1-9a-km-zA-LMNP-Z]{51,111}")
    # address_re = re.compile(r"[1-9a-kmnp-zA-KMNP-Z]{27-31}")
    for f in args.private_key_file:
        if f.name.endswith(".gpg"):
            f = replace_with_gpg_pipe(args, f)
        for line in f.readlines():
            # decode
            if isinstance(line, bytes):
                line = line.decode("utf8")
            # look for WIFs
            possible_keys = wif_re.findall(line)

            def make_key(x):
                try:
                    return network.parse.secret(x)
                except Exception:
                    return None

            keys = [make_key(x) for x in possible_keys]
            for key in keys:
                if key:
                    keychain.add_secrets([key])
                    keychain.add_key_paths(key, subpaths_for_path_range(args.key_paths))