forked from JoinMarket-Org/joinmarket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet-tool.py
271 lines (250 loc) · 11.3 KB
/
wallet-tool.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from __future__ import absolute_import, print_function
import datetime
import getpass
import json
import os
import sys
from optparse import OptionParser
from joinmarket import load_program_config, get_network, Wallet, encryptData, \
get_p2pk_vbyte, jm_single, mn_decode, mn_encode
import bitcoin as btc
# data_dir = os.path.dirname(os.path.realpath(__file__))
# sys.path.insert(0, os.path.join(data_dir, 'joinmarket'))
# structure for cj market wallet
# m/0/ root key
# m/0/n/ nth mixing depth, where n=0 is unmixed, n=1 is coinjoined once, etc
# pay in coins to mix at n=0 addresses
# coins move up a level when they are cj'd and stay at same level if they're
# the change from a coinjoin
# using coins from different levels as inputs to the same tx is probably
# detrimental to privacy
# m/0/n/0/k kth external address, for mixing depth n
# m/0/n/1/k kth internal address, for mixing depth n
description = (
'Does useful little tasks involving your bip32 wallet. The '
'method is one of the following: (display) Shows addresses and '
'balances. (displayall) Shows ALL addresses and balances. '
'(summary) Shows a summary of mixing depth balances. (generate) '
'Generates a new wallet. (recover) Recovers a wallet from the 12 '
'word recovery seed. (showseed) Shows the wallet recovery seed '
'and hex seed. (importprivkey) Adds privkeys to this wallet, '
'privkeys are spaces or commas separated. (listwallets) Lists '
'all wallets with creator and timestamp.')
parser = OptionParser(usage='usage: %prog [options] [wallet file] [method]',
description=description)
parser.add_option('-p',
'--privkey',
action='store_true',
dest='showprivkey',
help='print private key along with address, default false')
parser.add_option('-m',
'--maxmixdepth',
action='store',
type='int',
dest='maxmixdepth',
help='maximum mixing depth to look for, default=5')
parser.add_option('-g',
'--gap-limit',
type="int",
action='store',
dest='gaplimit',
help='gap limit for wallet, default=6',
default=6)
parser.add_option('-M',
'--mix-depth',
type="int",
action='store',
dest='mixdepth',
help='mixing depth to import private key into',
default=0)
(options, args) = parser.parse_args()
# if the index_cache stored in wallet.json is longer than the default
# then set maxmixdepth to the length of index_cache
maxmixdepth_configured = True
if not options.maxmixdepth:
maxmixdepth_configured = False
options.maxmixdepth = 5
noseed_methods = ['generate', 'recover', 'listwallets']
methods = ['display', 'displayall', 'summary', 'showseed', 'importprivkey']
methods.extend(noseed_methods)
noscan_methods = ['showseed', 'importprivkey']
if len(args) < 1:
parser.error('Needs a wallet file or method')
sys.exit(0)
load_program_config()
if args[0] in noseed_methods:
method = args[0]
else:
seed = args[0]
method = ('display' if len(args) == 1 else args[1].lower())
wallet = Wallet(seed,
options.maxmixdepth,
options.gaplimit,
extend_mixdepth=not maxmixdepth_configured,
storepassword=(method == 'importprivkey'))
if method not in noscan_methods:
# if nothing was configured, we override bitcoind's options so that
# unconfirmed balance is included in the wallet display by default
if 'listunspent_args' not in jm_single().config.options('POLICY'):
jm_single().config.set('POLICY','listunspent_args', '[0]')
jm_single().bc_interface.sync_wallet(wallet)
if method == 'display' or method == 'displayall' or method == 'summary':
def cus_print(s):
if method != 'summary':
print(s)
total_balance = 0
for m in range(wallet.max_mix_depth):
cus_print('mixing depth %d m/0/%d/' % (m, m))
balance_depth = 0
for forchange in [0, 1]:
cus_print(' ' + ('external' if forchange == 0 else 'internal') +
' addresses m/0/%d/%d/' % (m, forchange))
for k in range(wallet.index[m][forchange] + options.gaplimit):
addr = wallet.get_addr(m, forchange, k)
balance = 0.0
for addrvalue in wallet.unspent.values():
if addr == addrvalue['address']:
balance += addrvalue['value']
balance_depth += balance
used = ('used' if k < wallet.index[m][forchange] else ' new')
privkey = btc.encode_privkey(
wallet.get_key(m, forchange, k), 'wif_compressed',
get_p2pk_vbyte()) if options.showprivkey else ''
if (method == 'displayall' or balance > 0 or
(used == ' new' and forchange == 0)):
cus_print(' m/0/%d/%d/%03d %-35s%s %.8f btc %s' %
(m, forchange, k, addr, used, balance / 1e8,
privkey))
if m in wallet.imported_privkeys:
cus_print(' import addresses')
for privkey in wallet.imported_privkeys[m]:
addr = btc.privtoaddr(privkey, get_p2pk_vbyte())
balance = 0.0
for addrvalue in wallet.unspent.values():
if addr == addrvalue['address']:
balance += addrvalue['value']
used = (' used' if balance > 0.0 else 'empty')
balance_depth += balance
wip_privkey = btc.encode_privkey(
privkey, 'wif_compressed',
get_p2pk_vbyte()) if options.showprivkey else ''
cus_print(' ' * 13 + '%-35s%s %.8f btc %s' % (
addr, used, balance / 1e8, wip_privkey))
total_balance += balance_depth
print('for mixdepth=%d balance=%.8fbtc' % (m, balance_depth / 1e8))
print('total balance = %.8fbtc' % (total_balance / 1e8))
elif method == 'generate' or method == 'recover':
if method == 'generate':
seed = btc.sha256(os.urandom(64))[:32]
words = mn_encode(seed)
print('Write down this wallet recovery seed\n\n' + ' '.join(words) +
'\n')
elif method == 'recover':
words = raw_input('Input 12 word recovery seed: ')
words = words.split() # default for split is 1 or more whitespace chars
if len(words) != 12:
print('ERROR: Recovery seed phrase must be exactly 12 words.')
sys.exit(0)
seed = mn_decode(words)
print(seed)
password = getpass.getpass('Enter wallet encryption passphrase: ')
password2 = getpass.getpass('Reenter wallet encryption passphrase: ')
if password != password2:
print('ERROR. Passwords did not match')
sys.exit(0)
password_key = btc.bin_dbl_sha256(password)
encrypted_seed = encryptData(password_key, seed.decode('hex'))
timestamp = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
walletfile = json.dumps({'creator': 'joinmarket project',
'creation_time': timestamp,
'encrypted_seed': encrypted_seed.encode('hex'),
'network': get_network()})
walletname = raw_input('Input wallet file name (default: wallet.json): ')
if len(walletname) == 0:
walletname = 'wallet.json'
walletpath = os.path.join('wallets', walletname)
# Does a wallet with the same name exist?
if os.path.isfile(walletpath):
print('ERROR: ' + walletpath + ' already exists. Aborting.')
sys.exit(0)
else:
fd = open(walletpath, 'w')
fd.write(walletfile)
fd.close()
print('saved to ' + walletname)
elif method == 'showseed':
hexseed = wallet.seed
print('hexseed = ' + hexseed)
words = mn_encode(hexseed)
print('Wallet recovery seed\n\n' + ' '.join(words) + '\n')
elif method == 'importprivkey':
print('WARNING: This imported key will not be recoverable with your 12 ' +
'word mnemonic seed. Make sure you have backups.')
print('WARNING: Handling of raw ECDSA bitcoin private keys can lead to '
'non-intuitive behaviour and loss of funds.\n Recommended instead '
'is to use the \'sweep\' feature of sendpayment.py ')
privkeys = raw_input('Enter private key(s) to import: ')
privkeys = privkeys.split(',') if ',' in privkeys else privkeys.split()
# TODO read also one key for each line
for privkey in privkeys:
# TODO is there any point in only accepting wif format? check what
# other wallets do
privkey_format = btc.get_privkey_format(privkey)
if privkey_format not in ['wif', 'wif_compressed']:
print('ERROR: privkey not in wallet import format')
print(privkey, 'skipped')
continue
if privkey_format == 'wif':
# TODO if they actually use an unc privkey, make sure the unc
# address is used
# r = raw_input('WARNING: Using uncompressed private key, the vast ' +
# 'majority of JoinMarket transactions use compressed keys\n' +
# 'being so unusual is bad for privacy. Continue? (y/n):')
# if r != 'y':
# sys.exit(0)
print('Uncompressed privkeys not supported (yet)')
print(privkey, 'skipped')
continue
privkey_bin = btc.encode_privkey(privkey, 'hex').decode('hex')
encrypted_privkey = encryptData(wallet.password_key, privkey_bin)
if 'imported_keys' not in wallet.walletdata:
wallet.walletdata['imported_keys'] = []
wallet.walletdata['imported_keys'].append(
{'encrypted_privkey': encrypted_privkey.encode('hex'),
'mixdepth': options.mixdepth})
if wallet.walletdata['imported_keys']:
fd = open(wallet.path, 'w')
fd.write(json.dumps(wallet.walletdata))
fd.close()
print('Private key(s) successfully imported')
elif method == 'listwallets':
# Fetch list of wallets
possible_wallets = []
for (dirpath, dirnames, filenames) in os.walk('wallets'):
possible_wallets.extend(filenames)
# Breaking as we only want the top dir, not subdirs
break
# For each possible wallet file, read json to list
walletjsons = []
for possible_wallet in possible_wallets:
fd = open(os.path.join('wallets', possible_wallet), 'r')
try:
walletfile = fd.read()
walletjson = json.loads(walletfile)
# Add filename to json format
walletjson['filename'] = possible_wallet
walletjsons.append(walletjson)
except ValueError:
pass
# Sort wallets by date
walletjsons.sort(key=lambda r: r['creation_time'])
i = 1
print(' ')
for walletjson in walletjsons:
print('Wallet #' + str(i) + ' (' + walletjson['filename'] + '):')
print('Creation time:\t' + walletjson['creation_time'])
print('Creator:\t' + walletjson['creator'])
print('Network:\t' + walletjson['network'])
print(' ')
i += 1
print(str(i - 1) + ' Wallets have been found.')