Skip to content

Instantly share code, notes, and snippets.

@nlitsme
Last active February 22, 2022 13:28
Show Gist options
  • Save nlitsme/b3102191c2dc14c587e7 to your computer and use it in GitHub Desktop.
Save nlitsme/b3102191c2dc14c587e7 to your computer and use it in GitHub Desktop.
create readable hex dump of a bitcoin, litecoin, etc wallet.dat
from bsddb.db import *
import sys
import struct
# by Willem Hengeveld <[email protected]>
# usage: python dumpwallet path_to_your/wallet.dat
# note: you need to have the bsddb package installed
def getcompact(k, i):
b= ord(k[i]) ; i+=1
if b<0xfd:
pass
elif b==0xfd:
b= struct.unpack("<h", k[i:i+2])[0] ; i+=2
elif b==0xfe:
b= struct.unpack("<l", k[i:i+4])[0] ; i+=4
elif b==0xff:
b= struct.unpack("<q", k[i:i+8])[0] ; i+=8
return (b, i)
def decodekey(k):
i=0
key=[]
(l,i)=getcompact(k,i)
key.append(k[i:i+l]) ; i += l
if key[0] in ['tx', 'pool', 'version', 'minversion']:
key.append(k[i:])
elif i<len(k):
(l,i)=getcompact(k,i)
key.append(k[i:i+l]) ; i += l
return key
def decodevalue(k):
i=0
key=[]
while i<len(k):
(l,i)=getcompact(k,i)
key.append(k[i:i+l]) ; i += l
return key
# bestblock -> int32, byte, blockhash[len]
# key -> asn1:[01, privkey, =0:[01, [secp256k1, p], [a,b], g, n, 01], =1:pubkey]
def decodekv(k,v):
key=decodekey(k)
if key[0] in ['name']:
return "'%s':%s -> %s" % (key[0], key[1], ",".join(map(lambda x:"'%s'"%x, decodevalue(v))))
elif key[0] in ['key', 'ckey', 'defaultkey']:
return "'%s':%s -> %s" % (key[0], ",".join(map(lambda x:x.encode("hex"), key[1:])), ",".join(map(lambda x:x.encode("hex"), decodevalue(v))))
elif key[0] in ['setting']:
return "'%s':%s -> %s" % (key[0], key[1], ",".join(map(lambda x:x.encode("hex"), decodevalue(v))))
else:
return "'%s':%s -> %s" % (key[0], ",".join(map(lambda x:x.encode("hex"), key[1:])), v.encode("hex"))
db = DB()
r= db.open(sys.argv[1], "main", DB_BTREE, DB_RDONLY)
for (k,v) in db.items():
print decodekv(k,v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment