コード例 #1
0
ファイル: hd_wallet.py プロジェクト: codyli2002hk/pmes
def create_private_key(network, xprv, uid, path=0):
    assert xprv is not None
    assert uid is not None

    res = {
        "type": network,
        "uid": uid,
    }

    if network == 'ethereum' or network.upper() == 'ETH':
        acct_prv_key = HDKey.from_b58check(xprv)
        keys = HDPrivateKey.from_path(
            acct_prv_key, '{change}/{index}'.format(change=path, index=uid))

        res['private_key'] = keys[-1]._key.to_hex()

        return res

    # else ...

    if network == 'PUT':
        wallet_obj = Wallet.deserialize(xprv, network='QTUM')
    elif network == 'PUTTEST':
        wallet_obj = Wallet.deserialize(xprv, network='QTUMTEST')
    else:
        wallet_obj = Wallet.deserialize(xprv, network=network.upper())

    child_wallet = wallet_obj.get_child(uid, is_prime=False)

    net = get_network(network)
    res['private_key'] = child_wallet.export_to_wif()

    return res
コード例 #2
0
ファイル: hd_wallet.py プロジェクト: codyli2002hk/pmes
def create_address(network, xpub, uid, path=0):
    assert xpub is not None
    assert uid is not None

    res = {
        "coinid": network,
        "uid": uid,
    }

    if network == 'ethereum' or network.upper() in [
            'ETH', 'ETHRINKEBY', 'ETHROPSTEN'
    ]:
        acct_pub_key = HDKey.from_b58check(xpub)
        keys = HDKey.from_path(
            acct_pub_key, '{change}/{index}'.format(change=path, index=uid))

        res['address'] = keys[-1].address()

        return res

    # else ...
    if network == 'PUT':
        wallet_obj = Wallet.deserialize(xpub, network='QTUM')
    elif network == 'PUTTEST':
        wallet_obj = Wallet.deserialize(xpub, network='QTUMTEST')
    else:
        wallet_obj = Wallet.deserialize(xpub, network=network.upper())
    child_wallet = wallet_obj.get_child(uid, is_prime=False)

    net = get_network(network)
    res['address'] = child_wallet.to_address()

    return res
コード例 #3
0
def create_address(network='btctest', xpub=None, child=None, path=0):
    """

    :param network:
    :param xpub:
    :param child:
    :param path:
    :return:
    """
    assert xpub is not None

    if child is None:
        child = generate_child_id()

    if network == 'ethereum' or network.upper() == 'ETH':
        acct_pub_key = HDKey.from_b58check(xpub)

        keys = HDKey.from_path(
            acct_pub_key, '{change}/{index}'.format(change=path, index=child))

        res = {
            "path":
            "m/" + str(acct_pub_key.index) + "/" + str(keys[-1].index),
            "bip32_path":
            "m/44'/60'/0'/" + str(acct_pub_key.index) + "/" +
            str(keys[-1].index),
            "address":
            keys[-1].address(),
            "private_key":
            keys[-1]._key.to_hex()
        }

        if inspect.stack()[1][3] == "create_wallet":
            res["xpublic_key"] = keys[-1].to_b58check()

        return res

    # else ...
    wallet_obj = Wallet.deserialize(xpub, network=network.upper())
    child_wallet = wallet_obj.get_child(child, is_prime=False)

    net = get_network(network)

    return {
        "path":
        "m/" + str(wallet_obj.child_number) + "/" +
        str(child_wallet.child_number),
        "bip32_path":
        net.BIP32_PATH + str(wallet_obj.child_number) + "/" +
        str(child_wallet.child_number),
        "address":
        child_wallet.to_address(),
        "private_key":
        child_wallet.get_private_key_hex(),
        # "xpublic_key": child_wallet.serialize_b58(private=False),
        # "wif": child_wallet.export_to_wif() # needs private key
    }
コード例 #4
0
# In your VM, run `pip3 install pywallet` beforehand.
from pprint import pprint
import account_test
import requests
from pywallet.utils import Wallet

# Set up initial config. The account xpub must be a testnet xpub.

# It is at the level of a bip44 account, so starting at for example m/44'/0'/0'.

# `xpub/0/<index>` and `xpub/1/<index>` will enumerate the receive and change addresses.

xpub_str = "tpubDDjcecUiJegnYexfPHbxVsmsrqoGUeQr6DdU7Apz8Qg56GcThfgcZBdyTR7Ax7xRPLbJX7qS98zkgmvrMfNPhxLfax3KkGtAeV98EJPsbF9"
gap_limit = 20

xpub = Wallet.deserialize(xpub_str, "bitcoin_testnet")
# An account consists of two address chains, the receive and the change addresses.
# account xpub (e.g. m/44'/0'/0'), with addresses living under xpub/<change>/<address>
receive_xpub = xpub.get_child(0)
change_xpub = xpub.get_child(1)


def get_address(xpub):
    """
    Determines the account type. By default p2pkh addresses are generated.
    """
    return xpub.to_address()  # p2pkh


# Example:
print(get_address(receive_xpub.get_child(0)))
コード例 #5
0
 def get_coin(self, private_key):
     wif = private_key
     key = PrivateKey.from_wif(wif=private_key,
                               network=Wallet.get_network('OMNI'))
     address = key.get_public_key().to_address()
     return CryptoCoin(address, wif, '')
コード例 #6
0
def create_wallet(network='btctest', seed=None, children=1):
    """

    :param network:
    :param seed:
    :param children:
    :return:
    """
    if seed is None:
        seed = generate_mnemonic()

    net = get_network(network)
    wallet = {
        "coin": net.COIN,
        "seed": seed,
        "private_key": "",
        "public_key": "",
        "xprivate_key": "",
        "xpublic_key": "",
        "address": "",
        "wif": "",
        "children": []
    }

    if network == 'ethereum' or network.upper() == 'ETH':
        wallet["coin"] = "ETH"

        master_key = HDPrivateKey.master_key_from_mnemonic(seed)
        root_keys = HDKey.from_path(master_key, "m/44'/60'/0'")

        acct_priv_key = root_keys[-1]
        acct_pub_key = acct_priv_key.public_key

        wallet["private_key"] = acct_priv_key.to_hex()
        wallet["public_key"] = acct_pub_key.to_hex()
        wallet["xprivate_key"] = acct_priv_key.to_b58check()
        wallet["xpublic_key"] = acct_pub_key.to_b58check()

        child_wallet = create_address(network=network.upper(),
                                      xpub=wallet["xpublic_key"],
                                      child=0,
                                      path=0)
        wallet["address"] = child_wallet["address"]
        wallet["xpublic_key_prime"] = child_wallet["xpublic_key"]

        # get public info from first prime child
        for child in range(children):
            child_wallet = create_address(network=network.upper(),
                                          xpub=wallet["xpublic_key"],
                                          child=child,
                                          path=0)
            wallet["children"].append({
                "address":
                child_wallet["address"],
                "private_key":
                child_wallet["private_key"],
                "xpublic_key":
                child_wallet["xpublic_key"],
                "path":
                "m/" + str(child),
                "bip32_path":
                "m/44'/60'/0'/" + str(child),
            })
    elif network == 'ripple' or network.upper() == 'XRP':
        wallet["coin"] = "XRP"
        (seed, master) = mnemonic_to_master(seed, '')
        (address, private_key, path) = compute_address('xrp', master, 0)

        wallet["private_key"] = private_key
        wallet["public_key"] = address
        wallet["address"] = address

        # get public info from first prime child
        for child in range(children):
            (child_address, child_private_key,
             child_path) = compute_address('xrp', master, child)
            wallet["children"].append({
                "address": child_address,
                "private_key": child_private_key,
                # "xpublic_key": child_wallet["xpublic_key"],
                "path": "m/" + str(child),
                "bip32_path": child_path,
            })
    elif network == 'zcash' or network.upper() == 'ZCASH':
        wallet["coin"] = "ZCASH"
        (seed, master) = mnemonic_to_master(seed, '')
        (address, private_key, path) = compute_address('zcash', master, 0)

        wallet["private_key"] = private_key
        wallet["public_key"] = address
        wallet["address"] = address

        # get public info from first prime child
        for child in range(children):
            (child_address, child_private_key,
             child_path) = compute_address('zcash', master, child)
            wallet["children"].append({
                "address": child_address,
                "private_key": child_private_key,
                # "xpublic_key": child_wallet["xpublic_key"],
                "path": "m/" + str(child),
                "bip32_path": child_path,
            })
    elif network == 'stellar_testnet':
        (binary_seed,
         seed_phrase_type) = to_binary_seed(seed, test_stellar_passphrase,
                                            my_language)
        keypair = account_keypair(binary_seed, 0)
        wallet["private_key"] = keypair.seed().decode("utf-8")
        wallet["public_key"] = keypair.address().decode("utf-8")
        wallet["address"] = keypair.address().decode("utf-8")
        # get public info from first prime child
        for child in range(children):
            keypair = account_keypair(binary_seed, child)
            wallet["children"].append({
                "address":
                keypair.address().decode("utf-8"),
                "private_key":
                keypair.seed().decode("utf-8"),
                "path":
                "m/" + str(child),
                "bip39_path":
                STELLAR_ACCOUNT_PATH_FORMAT % child,
            })
    elif network == 'stellar' or network.upper() == 'XLM':
        (binary_seed,
         seed_phrase_type) = to_binary_seed(seed, public_stellar_passphrase,
                                            my_language)
        keypair = account_keypair(binary_seed, 0)
        wallet["private_key"] = keypair.seed().decode("utf-8")
        wallet["public_key"] = keypair.address().decode("utf-8")
        wallet["address"] = keypair.address().decode("utf-8")
        # get public info from first prime child
        for child in range(children):
            keypair = account_keypair(binary_seed, child)
            wallet["children"].append({
                "address":
                keypair.address().decode("utf-8"),
                "private_key":
                keypair.seed().decode("utf-8"),
                "path":
                "m/" + str(child),
                "bip39_path":
                STELLAR_ACCOUNT_PATH_FORMAT % child,
            })
    else:
        my_wallet = Wallet.from_master_secret(network=network.upper(),
                                              seed=seed)

        # account level
        wallet["private_key"] = my_wallet.private_key.get_key().decode()
        wallet["public_key"] = my_wallet.public_key.get_key().decode()
        wallet["xprivate_key"] = my_wallet.serialize_b58(private=True)
        wallet["xpublic_key"] = my_wallet.serialize_b58(private=False)
        wallet["address"] = my_wallet.to_address()
        wallet["wif"] = my_wallet.export_to_wif()

        prime_child_wallet = my_wallet.get_child(0, is_prime=True)
        wallet["xpublic_key_prime"] = prime_child_wallet.serialize_b58(
            private=False)

        # prime children
        for child in range(children):
            child_wallet = my_wallet.get_child(child,
                                               is_prime=False,
                                               as_private=True)
            wallet["children"].append({
                "xpublic_key":
                child_wallet.serialize_b58(private=False),
                "xprivate_key":
                child_wallet.serialize_b58(private=True),
                "address":
                child_wallet.to_address(),
                "private_key":
                child_wallet.get_private_key_hex(),
                "path":
                "m/" + str(child),
                "bip32_path":
                net.BIP32_PATH + str(child_wallet.child_number),
            })

    return wallet