-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlibrary.py
More file actions
196 lines (159 loc) · 6.8 KB
/
library.py
File metadata and controls
196 lines (159 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python
"""
library to generate hd account for bitcoin and ethereum
follow https://iancoleman.io/bip39/ patern
"""
import sys
from mnemonic import Mnemonic
from bip32utils import BIP32Key
from bip32utils import BIP32_HARDEN
from ethereum.utils import privtoaddr,checksum_encode # for ethereum derivation
coins = ['bitcoin','bitcoin-testnet','ethereum']
"""
bytes.fromhex("stuff")
"""
def create_words():
m = Mnemonic('english')
words = m.generate(strength=256)
return words
def create_seed(words):
m = Mnemonic('english')
seed = m.to_seed(words)
return seed
def create_mnemonic():
words = create_words()
seed = create_seed(words)
return words,seed
def rootkey_from_seed(seed,network):
if network == 'bitcoin' or network == 'ethereum':
xprv = BIP32Key.fromEntropy(seed).ExtendedKey()
elif network == 'bitcoin-testnet':
xprv = BIP32Key.fromEntropy(seed,testnet=True).ExtendedKey()
else:
print("network should not be" + network)
sys.exit("Wrong Network : should be ethereum or bitcoin or bitcoin-testnet!")
rootkey = BIP32Key.fromExtendedKey(xprv)
return rootkey
def account_from_rootkey(key,account_number,network):
if network == 'bitcoin':
network_number = 0
elif network == 'bitcoin-testnet':
network_number = 1
elif network == 'ethereum':
network_number = 60
else:
print("network should not be" + network)
sys.exit("Wrong Network : should be ethereum or bitcoin or bitcoin-testnet!")
account = key.ChildKey(44 + BIP32_HARDEN) \
.ChildKey(network_number + BIP32_HARDEN) \
.ChildKey(0 + BIP32_HARDEN) \
.ChildKey(0) \
.ChildKey(account_number)
return account
def gen_bitcoin_account_from_seed(seed,account_number):
key = rootkey_from_seed(seed,"bitcoin")
account = account_from_rootkey(key,account_number,"bitcoin")
return account
def gen_bitcoin_testnet_account_from_seed(seed,account_number):
key = rootkey_from_seed(seed,"bitcoin-testnet")
account = account_from_rootkey(key,account_number,"bitcoin-testnet")
return account
def gen_ethereum_account_from_seed(seed,account_number):
key = rootkey_from_seed(seed,"ethereum")
account = account_from_rootkey(key,account_number,"ethereum")
return account
def bitcoin_data(account):
private_key = account.PrivateKey()
wif = account.WalletImportFormat()
public_key = account.PublicKey()
address = account.Address()
return private_key,address,wif,public_key
def ethereum_data(account):
private_key_eth = account.PrivateKey()
address_eth = checksum_encode(privtoaddr(private_key_eth))
return private_key_eth,address_eth
def gen_bitcoin_wallet(seed,account_number):
account = gen_bitcoin_account_from_seed(seed,account_number)
return bitcoin_data(account)
def gen_bitcoin_testnet_wallet(seed,account_number):
account = gen_bitcoin_testnet_account_from_seed(seed,account_number)
return bitcoin_data(account)
def gen_ethereum_wallet(seed,account_number):
account = gen_ethereum_account_from_seed(seed,account_number)
return ethereum_data(account)
if __name__ == '__main__':
m = Mnemonic('english')
words = m.generate(strength=256)
print(words)
seed = m.to_seed(words)
print("************")
print("Seed " + seed.hex())
#key = BIP32Key.fromEntropy(seed)
print("******************Test Bitcoin********************")
globalkey = BIP32Key.fromEntropy(seed)
root = globalkey.ExtendedKey()
print("Root Key is " + root)
key = BIP32Key.fromExtendedKey(root)
bitcoinkey = key.ChildKey(44 + BIP32_HARDEN) \
.ChildKey(0 + BIP32_HARDEN) \
.ChildKey(0 + BIP32_HARDEN)
AccountExtendedPrivateKey = bitcoinkey.ExtendedKey()
print("Account Extended Private Key is " + AccountExtendedPrivateKey)
AccountExtendedPublicKey = bitcoinkey.ExtendedKey(private=False)
print("Account Extended Public Key is " + AccountExtendedPublicKey)
bitcoinaccount = bitcoinkey.ChildKey(0)
BIP32ExtendedPrivateKey = bitcoinaccount.ExtendedKey()
print("BIP32 Extended Private Key is : " + BIP32ExtendedPrivateKey)
BIP32ExtendedPublicKey = bitcoinaccount.ExtendedKey(private=False)
print("BIP32 Extended Public Key is : " + BIP32ExtendedPublicKey)
bitcoin0 = bitcoinaccount.ChildKey(0)
print("first private key")
print(bitcoin0.WalletImportFormat())
print("first address")
print(bitcoin0.Address())
print("******************Test Bitcoin Testnet********************")
globalkey = BIP32Key.fromEntropy(seed,testnet=True)
root = globalkey.ExtendedKey()
print("Root Key is " + root)
key = BIP32Key.fromExtendedKey(root)
bitcoinkey = key.ChildKey(44 + BIP32_HARDEN) \
.ChildKey(1 + BIP32_HARDEN) \
.ChildKey(0 + BIP32_HARDEN)
AccountExtendedPrivateKey = bitcoinkey.ExtendedKey()
print("Account Extended Private Key is " + AccountExtendedPrivateKey)
AccountExtendedPublicKey = bitcoinkey.ExtendedKey(private=False)
print("Account Extended Public Key is " + AccountExtendedPublicKey)
bitcoinaccount = bitcoinkey.ChildKey(0)
BIP32ExtendedPrivateKey = bitcoinaccount.ExtendedKey()
print("BIP32 Extended Private Key is : " + BIP32ExtendedPrivateKey)
BIP32ExtendedPublicKey = bitcoinaccount.ExtendedKey(private=False)
print("BIP32 Extended Public Key is : " + BIP32ExtendedPublicKey)
bitcoin0 = bitcoinaccount.ChildKey(0)
print("first private key")
print(bitcoin0.WalletImportFormat())
print("first address")
print(bitcoin0.Address())
print("******************Test Ethereum********************")
globalkey = BIP32Key.fromEntropy(seed)
root = globalkey.ExtendedKey()
print("Root Key is " + root)
key = BIP32Key.fromExtendedKey(root)
ethereumkey = key.ChildKey(44 + BIP32_HARDEN) \
.ChildKey(60 + BIP32_HARDEN) \
.ChildKey(0 + BIP32_HARDEN)
AccountExtendedPrivateKey = ethereumkey.ExtendedKey()
print("Account Extended Private Key is " + AccountExtendedPrivateKey)
AccountExtendedPublicKey = ethereumkey.ExtendedKey(private=False)
print("Account Extended Public Key is " + AccountExtendedPublicKey)
ethereumaccount = ethereumkey.ChildKey(0)
BIP32ExtendedPrivateKey = ethereumaccount.ExtendedKey()
print("BIP32 Extended Private Key is : " + BIP32ExtendedPrivateKey)
BIP32ExtendedPublicKey = ethereumaccount.ExtendedKey(private=False)
print("BIP32 Extended Public Key is : " + BIP32ExtendedPublicKey)
ethereum0 = ethereumaccount.ChildKey(0)
private_key_eth = ethereum0.PrivateKey()
print("first private key")
print(private_key_eth.hex())
address_eth = checksum_encode(privtoaddr(private_key_eth))
print("first address")
print(address_eth)