3 # Recover from a semi-corrupt wallet
9 from wallet import rewrite_wallet, trim_wallet
10 from util import determine_db_dir, create_env
14 parser = optparse.OptionParser(usage="%prog [options]")
15 parser.add_option("--datadir", dest="datadir", default=None,
16 help="Look for files here (defaults to bitcoin default)")
17 parser.add_option("--out", dest="outfile", default="walletNEW.dat",
18 help="Name of output file (default: walletNEW.dat)")
19 parser.add_option("--clean", action="store_true", dest="clean", default=False,
20 help="Clean out old, spent change addresses and transactions")
21 parser.add_option("--skipkey", dest="skipkey",
22 help="Skip entries with keys that contain given string")
23 parser.add_option("--tweakspent", dest="tweakspent",
24 help="Tweak transaction to mark unspent")
25 (options, args) = parser.parse_args()
27 if options.datadir is None:
28 db_dir = determine_db_dir()
30 db_dir = options.datadir
33 db_env = create_env(db_dir)
34 except DBNoSuchFileError:
35 logging.error("Couldn't open " + db_dir)
39 trim_wallet(db_env, options.outfile)
42 def pre_put_callback(type, data):
43 if options.skipkey in data['__key__']:
46 rewrite_wallet(db_env, options.outfile, pre_put_callback)
47 elif options.tweakspent:
48 txid = options.tweakspent.decode('hex_codec')[::-1]
49 def tweak_spent_callback(type, data):
50 if txid in data['__key__']:
51 data['__value__'] = data['__value__'][:-1]+'\0'
53 rewrite_wallet(db_env, options.outfile, tweak_spent_callback)
56 rewrite_wallet(db_env, options.outfile)
60 if __name__ == '__main__':