Esempio n. 1
0
    def from_key(name, wallet_id, session, key='', account_id=0, network='bitcoin', change=0, purpose=44, parent_id=0,
                 path='m'):
        k = HDKey(import_key=key, network=network)
        keyexists = session.query(DbKey).filter(DbKey.key_wif == k.extended_wif()).all()
        if keyexists:
            raise WalletError("Key %s already exists" % k.extended_wif())

        if k.depth() != len(path.split('/'))-1:
            if path == 'm' and k.depth() == 3:
                # Create path when importing new account-key
                networkcode = networks.NETWORKS[network]['bip44_cointype']
                path = "m/%d'/%s'/%d'" % (purpose, networkcode, account_id)
            else:
                raise WalletError("Key depth of %d does not match path lenght of %d" %
                                  (k.depth(), len(path.split('/')) - 1))

        wk = session.query(DbKey).filter(or_(DbKey.key == str(k.private()),
                                             DbKey.key_wif == k.extended_wif(),
                                             DbKey.address == k.public().address())).first()
        if wk:
            return HDWalletKey(wk.id, session)

        nk = DbKey(name=name, wallet_id=wallet_id, key=str(k.private()), purpose=purpose,
                   account_id=account_id, depth=k.depth(), change=change, address_index=k.child_index(),
                   key_wif=k.extended_wif(), address=k.public().address(), parent_id=parent_id,
                   is_private=True, path=path, key_type=k.key_type)
        session.add(nk)
        session.commit()
        return HDWalletKey(nk.id, session)
Esempio n. 2
0
#
# Multisig wallet using single keys for cosigner wallet instead of BIP32 type key structures
#

NETWORK = 'bitcoinlib_test'
pk1 = HDKey(
    'YXscyqNJ5YK411nwB33KeVkhSVjwwUkSG9xG3hkaoQFEbTwNJSrNTfni3aSSYiKtPeUPrFLwDsqHwZjNXhYm2DLEkQoaoikHoK2emrHv'
    'mqSEZrKP',
    network=NETWORK)
pk2 = HDKey(
    'YXscyqNJ5YK411nwB3kXiApMaJySYss8sMM9FYgXMtmQKmDTF9yiu7yBNKnVjE8WdVVvuhxLqS6kHvW2MPHKmYzbzEHQsDXXAZuu1rCs'
    'Hcp7rrJx',
    network=NETWORK,
    key_type='single')
wl1 = HDWallet.create('multisig_single_keys1', [pk1, pk2.public()],
                      sigs_required=2,
                      network=NETWORK,
                      db_uri=test_database)
wl2 = HDWallet.create('multisig_single_keys2',
                      [pk1.public_master_multisig(), pk2],
                      sigs_required=2,
                      network=NETWORK,
                      db_uri=test_database)

# Create multisig keys and update UTXO's
wl1.new_key(cosigner_id=0)
wl2.new_key(cosigner_id=0)
wl1.utxos_update()
wl2.utxos_update()
w1 = wallet_create_or_open('segwit_testnet_p2sh_p2wpkh',
                           key=wif,
                           witness_type='p2sh-segwit',
                           network='testnet')
w1_key = w1.get_key()

# Segwit Native P2WPKH Wallet
w2 = wallet_create_or_open('segwit_testnet_p2wpkh',
                           key=wif,
                           witness_type='segwit',
                           network='testnet')
w2_key = w2.get_key()

# Segwit Native P2WSH Wallet
w3 = wallet_create_or_open_multisig('segwit_testnet_p2wsh',
                                    key_list=[wif, cowif2.public()],
                                    witness_type='segwit',
                                    network='testnet')
w3_key = w3.get_key()

# Segwit P2SH-P2WSH Wallet
w4 = wallet_create_or_open_multisig('segwit_testnet_p2sh_p2wsh',
                                    key_list=[wif, cowif2.public()],
                                    witness_type='p2sh-segwit',
                                    network='testnet')
w4_key = w4.get_key()

#
# SEND TRANSACTIONS
#
            raise ValueError(
                "Please create private keys with mnemonic_key_create.py and add to COSIGNERS definitions"
            )
        if len(cosigner['key'].split(" ")) > 1:
            hdkey = HDKey.from_passphrase(cosigner['key'],
                                          key_type=cosigner['key_type'],
                                          witness_type=WITNESS_TYPE,
                                          network=NETWORK)
        else:
            hdkey = HDKey(cosigner['key'],
                          key_type=cosigner['key_type'],
                          witness_type=WITNESS_TYPE,
                          network=NETWORK)
        if cosigner['name'] != COSIGNER_NAME_THIS_WALLET:
            if hdkey.key_type == 'single':
                hdkey = hdkey.public()
            else:
                hdkey = hdkey.public_master_multisig()
        cosigner['hdkey'] = hdkey
        key_list.append(hdkey)

    if len(key_list) != SIGS_N:
        raise ValueError(
            "Number of cosigners (%d) is different then expected. SIG_N=%d" %
            (len(key_list), SIGS_N))
    wallet3o5 = HDWallet.create(WALLET_NAME,
                                key_list,
                                sigs_required=SIGS_REQUIRED,
                                witness_type=WITNESS_TYPE,
                                network=NETWORK)
    wallet3o5.new_key()