Skip to content

Instantly share code, notes, and snippets.

@gavinandresen
Created November 12, 2011 19:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gavinandresen/1361001 to your computer and use it in GitHub Desktop.
Save gavinandresen/1361001 to your computer and use it in GitHub Desktop.
Wallet encryption bugfix testplan
#!/usr/bin/env python
# Find private keys in a file.
# First argument: file containing one private key per line,
# in hexadecimal format (64 hex characters == 32 bytes of private key)
# Second argument: file to search
import sys
import os
tofind = set()
s = sys.argv[1].strip()
if os.access(s, os.R_OK):
keyfile = open(s, "r")
for key in keyfile.readlines():
hexstring = key.strip()
if len(hexstring) < 2:
continue
tofind.add(hexstring.decode('hex_codec'))
else:
tofind.add(s.decode('hex_codec'))
f = open(sys.argv[2], "rb")
data = f.read()
nfound = 0
for k in tofind:
first = data.find(k)
if first >= 0:
print "Found %s at position %d"%(k.encode('hex_codec'), first)
nfound += 1
else:
print "Did not find %s"%k.encode('hex_codec')
print "Found %d of %d"%(nfound, len(tofind))

TEST: No private keys left in any file on disk after wallet encryption/re-encryption is complete.

Preparation:

Start with an unencrypted wallet.dat; save a copy to wallet.unencrypted.dat

Extract all private keys into a 'privatekeys.txt' file:

Fetch or clone sipa's 'dumpallkeys' branch and build a bitcoind with it
  https://github.com/sipa/bitcoin/tree/dumpallkeys 
Run it:  bitcoind -datadir=... -daemon
Then, after it has started up:
     bitcoind -datadir=... gethexprivkeys > privatekeys.txt

VERIFY: bfind.py privatekeys.txt wallet.dat finds all the keys.

TESTS

1. Encrypt wallet.dat using patched bitcoin/bitcoind
VERIFY:
  bfind.py privatekeys.txt wallet.dat  finds no keys
  no temporary database files left behind.
Generate a couple of new private keys, perform some test sends/receives to/from
those keys
Extract all private keys from encrypted wallet
  (using sipa's gethexprivkeys after unlocking wallet using walletpassphrase)
VERIFY:
   bfind.py privatekeys.txt wallet.dat  finds no keys

2. Encrypt wallet.dat using version 0.4.0 or previous 0.5.0 release candidate
VERIFY:
  bfind.py privatekeys.txt wallet.dat  finds keys
  bfind.py privatekeys.txt database/log.*  finds keys
Run patched bitcoin/bitcoind
VERIFY:
   bfind.py privatekeys.txt wallet.dat  finds no keys
   no temporary database files left behind.


3. Run patched bitcoin/bitcoind against a new, empty -datadir
Encrypt wallet.
Extract all private keys from wallet (unlock/gethexprivkeys)
VERIFY:
  bfind.py newprivatekeys.txt wallet.dat  Finds no keys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment