bip32 path + prime child xpub on creation
This commit is contained in:
parent
cfb1247d04
commit
e637d7f214
@ -5,6 +5,7 @@ from datetime import datetime
|
|||||||
from .utils import (
|
from .utils import (
|
||||||
Wallet, HDPrivateKey, HDKey
|
Wallet, HDPrivateKey, HDKey
|
||||||
)
|
)
|
||||||
|
from .network import *
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
|
|
||||||
@ -34,61 +35,66 @@ def create_address(network='btctest', xpub=None, child=None, path=0):
|
|||||||
|
|
||||||
res = {
|
res = {
|
||||||
"path": "m/" + str(acct_pub_key.index) + "/" + str(keys[-1].index),
|
"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()
|
"address": keys[-1].address()
|
||||||
}
|
}
|
||||||
|
|
||||||
if inspect.stack()[1][3] == "create_wallet":
|
if inspect.stack()[1][3] == "create_wallet":
|
||||||
res["xpublic_key"] = keys[-1].to_b58check()
|
res["xpublic_key"] = keys[-1].to_b58check()
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
# else ...
|
# else ...
|
||||||
wallet_obj = Wallet.deserialize(xpub, network=network.upper())
|
wallet_obj = Wallet.deserialize(xpub, network=network.upper())
|
||||||
child_wallet = wallet_obj.get_child(child, is_prime=False)
|
child_wallet = wallet_obj.get_child(child, is_prime=False)
|
||||||
|
|
||||||
|
net = get_network(network)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"path": "m/" + str(wallet_obj.child_number) + "/" +str(child_wallet.child_number),
|
"path": "m/" + str(wallet_obj.child_number) + "/" +str(child_wallet.child_number),
|
||||||
|
"bip32_path": "m/" + net.BIP32_PATH + str(wallet_obj.child_number) + "/" +str(child_wallet.child_number),
|
||||||
"address": child_wallet.to_address(),
|
"address": child_wallet.to_address(),
|
||||||
# "xpublic_key": child_wallet.serialize_b58(private=False),
|
# "xpublic_key": child_wallet.serialize_b58(private=False),
|
||||||
# "wif": child_wallet.export_to_wif() # needs private key
|
# "wif": child_wallet.export_to_wif() # needs private key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_network(network='btctest'):
|
||||||
def coin_name_from_network(network):
|
|
||||||
network = network.lower()
|
network = network.lower()
|
||||||
|
|
||||||
if network == "btctest" or network == "btctest":
|
if network == "bitcoin_testnet" or network == "btctest":
|
||||||
return "BTCTEST"
|
return BitcoinTestNet
|
||||||
elif network == "bitcoin" or network == "btc":
|
elif network == "bitcoin" or network == "btc":
|
||||||
return "BTC"
|
return BitcoinMainNet
|
||||||
elif network == "dogecoin" or network == "doge":
|
elif network == "dogecoin" or network == "doge":
|
||||||
return "DOGE"
|
return DogecoinMainNet
|
||||||
elif network == "dogecoin_testnet" or network == "dogetest":
|
elif network == "dogecoin_testnet" or network == "dogetest":
|
||||||
return "DOGETEST"
|
return DogecoinTestNet
|
||||||
elif network == "litecoin" or network == "ltc":
|
elif network == "litecoin" or network == "ltc":
|
||||||
return "LTC"
|
return LitecoinMainNet
|
||||||
elif network == "litecoin_testnet" or network == "ltctest":
|
elif network == "litecoin_testnet" or network == "ltctest":
|
||||||
return "LTCTEST"
|
return LitecoinTestNet
|
||||||
elif network == "bitcoin_cash" or network == "bch":
|
elif network == "bitcoin_cash" or network == "bch":
|
||||||
return "BCH"
|
return BitcoinCashMainNet
|
||||||
elif network == "bitcoin_gold" or network == "btg":
|
elif network == "bitcoin_gold" or network == "btg":
|
||||||
return "BTG"
|
return BitcoinGoldMainNet
|
||||||
elif network == "dash" or network == "dash":
|
elif network == "dash" or network == "dash":
|
||||||
return "DASH"
|
return DashMainNet
|
||||||
elif network == "etheretum" or network == "eth":
|
|
||||||
return "ETH"
|
|
||||||
|
|
||||||
return ""
|
return BitcoinTestNet
|
||||||
|
|
||||||
|
|
||||||
def create_wallet(network='btctest', seed=None, children=1):
|
def create_wallet(network='btctest', seed=None, children=1):
|
||||||
if seed is None:
|
if seed is None:
|
||||||
seed = generate_mnemonic()
|
seed = generate_mnemonic()
|
||||||
|
|
||||||
|
|
||||||
|
net = get_network(network)
|
||||||
wallet = {
|
wallet = {
|
||||||
"coin": coin_name_from_network(network),
|
"coin": net.COIN,
|
||||||
"seed": seed,
|
"seed": seed,
|
||||||
# "private_key": "",
|
"private_key": "",
|
||||||
# "public_key": "",
|
"public_key": "",
|
||||||
"xprivate_key": "",
|
"xprivate_key": "",
|
||||||
"xpublic_key": "",
|
"xpublic_key": "",
|
||||||
"address": "",
|
"address": "",
|
||||||
@ -97,14 +103,16 @@ def create_wallet(network='btctest', seed=None, children=1):
|
|||||||
}
|
}
|
||||||
|
|
||||||
if network == 'ethereum' or network.upper() == 'ETH':
|
if network == 'ethereum' or network.upper() == 'ETH':
|
||||||
|
wallet["coin"] = "ETH"
|
||||||
|
|
||||||
master_key = HDPrivateKey.master_key_from_mnemonic(seed)
|
master_key = HDPrivateKey.master_key_from_mnemonic(seed)
|
||||||
root_keys = HDKey.from_path(master_key, "m/44'/60'/0'")
|
root_keys = HDKey.from_path(master_key, "m/44'/60'/0'")
|
||||||
|
|
||||||
acct_priv_key = root_keys[-1]
|
acct_priv_key = root_keys[-1]
|
||||||
acct_pub_key = acct_priv_key.public_key
|
acct_pub_key = acct_priv_key.public_key
|
||||||
|
|
||||||
# wallet["private_key"] = acct_priv_key.to_hex()
|
wallet["private_key"] = acct_priv_key.to_hex()
|
||||||
# wallet["public_key"] = acct_pub_key.to_hex()
|
wallet["public_key"] = acct_pub_key.to_hex()
|
||||||
wallet["xprivate_key"] = acct_priv_key.to_b58check()
|
wallet["xprivate_key"] = acct_priv_key.to_b58check()
|
||||||
wallet["xpublic_key"] = acct_pub_key.to_b58check()
|
wallet["xpublic_key"] = acct_pub_key.to_b58check()
|
||||||
|
|
||||||
@ -112,7 +120,7 @@ def create_wallet(network='btctest', seed=None, children=1):
|
|||||||
network=network.upper(), xpub=wallet["xpublic_key"],
|
network=network.upper(), xpub=wallet["xpublic_key"],
|
||||||
child=0, path=0)
|
child=0, path=0)
|
||||||
wallet["address"] = child_wallet["address"]
|
wallet["address"] = child_wallet["address"]
|
||||||
wallet["xpublic_key"] = child_wallet["xpublic_key"]
|
wallet["xpublic_key_prime"] = child_wallet["xpublic_key"]
|
||||||
|
|
||||||
# get public info from first prime child
|
# get public info from first prime child
|
||||||
for child in range(children):
|
for child in range(children):
|
||||||
@ -124,7 +132,7 @@ def create_wallet(network='btctest', seed=None, children=1):
|
|||||||
"address": child_wallet["address"],
|
"address": child_wallet["address"],
|
||||||
"xpublic_key": child_wallet["xpublic_key"],
|
"xpublic_key": child_wallet["xpublic_key"],
|
||||||
"path": "m/" + str(child),
|
"path": "m/" + str(child),
|
||||||
"wif": ""
|
"bip32_path": "m/44'/60'/0'/" + str(child),
|
||||||
})
|
})
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@ -132,22 +140,24 @@ def create_wallet(network='btctest', seed=None, children=1):
|
|||||||
network=network.upper(), seed=seed)
|
network=network.upper(), seed=seed)
|
||||||
|
|
||||||
# account level
|
# account level
|
||||||
# wallet["private_key"] = my_wallet.private_key.get_key().decode()
|
wallet["private_key"] = my_wallet.private_key.get_key().decode()
|
||||||
# wallet["public_key"] = my_wallet.public_key.get_key().decode()
|
wallet["public_key"] = my_wallet.public_key.get_key().decode()
|
||||||
wallet["xprivate_key"] = my_wallet.serialize_b58(
|
wallet["xprivate_key"] = my_wallet.serialize_b58(private=True)
|
||||||
private=True) # most important!
|
|
||||||
wallet["xpublic_key"] = my_wallet.serialize_b58(private=False)
|
wallet["xpublic_key"] = my_wallet.serialize_b58(private=False)
|
||||||
wallet["address"] = my_wallet.to_address()
|
wallet["address"] = my_wallet.to_address()
|
||||||
wallet["wif"] = my_wallet.export_to_wif()
|
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
|
# prime children
|
||||||
for child in range(children):
|
for child in range(children):
|
||||||
child_wallet = my_wallet.get_child(child, is_prime=True)
|
child_wallet = my_wallet.get_child(child, is_prime=False, as_private=False)
|
||||||
wallet["children"].append({
|
wallet["children"].append({
|
||||||
"xpublic_key": child_wallet.serialize_b58(private=False),
|
"xpublic_key": child_wallet.serialize_b58(private=False),
|
||||||
"address": child_wallet.to_address(),
|
"address": child_wallet.to_address(),
|
||||||
"path": "m/" + str(child),
|
"path": "m/" + str(child),
|
||||||
"wif": child_wallet.export_to_wif()
|
"bip32_path": net.BIP32_PATH + str(child_wallet.child_number),
|
||||||
})
|
})
|
||||||
|
|
||||||
return wallet
|
return wallet
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user