Skip to content

Commit

Permalink
Remember which public keys belong to the wallet, and mark TxOuts as O…
Browse files Browse the repository at this point in the history
…wn: True/False
  • Loading branch information
gavinandresen committed Apr 19, 2011
1 parent e99d2a6 commit f45c890
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
4 changes: 0 additions & 4 deletions block.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ def search_odd_scripts(datadir, cursor, block_data):
block_string = _dump_block(datadir, block_data['nFile'], block_data['nBlockPos'],
block_data['hash256'], block_data['hashNext'], False)

import pdb
if block_data['nHeight'] == 71036:
pdb.set_trace()

found_nonstandard = False
for m in re.finditer(r'TxIn:(.*?)$', block_string, re.MULTILINE):
s = m.group(1)
Expand Down
20 changes: 12 additions & 8 deletions deserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def parse_TxIn(vds):
d['scriptSig'] = vds.read_bytes(vds.read_compact_size())
d['sequence'] = vds.read_uint32()
return d
def deserialize_TxIn(d, transaction_index=None):
def deserialize_TxIn(d, transaction_index=None, owner_keys=None):
if d['prevout_hash'] == "\x00"*32:
result = "TxIn: COIN GENERATED"
result += " coinbase:"+d['scriptSig'].encode('hex_codec')
Expand All @@ -62,11 +62,15 @@ def parse_TxOut(vds):
d['value'] = vds.read_int64()
d['scriptPubKey'] = vds.read_bytes(vds.read_compact_size())
return d
def deserialize_TxOut(d):

def deserialize_TxOut(d, owner_keys=None):
result = "TxOut: value: %f"%(d['value']/1.0e8,)
pk = extract_public_key(d['scriptPubKey'])
result += " pubkey: "+pk
result += " Script: "+decode_script(d['scriptPubKey'])
if owner_keys is not None:
if pk in owner_keys: result += " Own: True"
else: result += " Own: False"
return result

def parse_Transaction(vds):
Expand All @@ -82,12 +86,12 @@ def parse_Transaction(vds):
d['txOut'].append(parse_TxOut(vds))
d['lockTime'] = vds.read_uint32()
return d
def deserialize_Transaction(d, transaction_index=None):
def deserialize_Transaction(d, transaction_index=None, owner_keys=None):
result = "%d tx in, %d out\n"%(len(d['txIn']), len(d['txOut']))
for txIn in d['txIn']:
result += deserialize_TxIn(txIn, transaction_index) + "\n"
for txOut in d['txOut']:
result += deserialize_TxOut(txOut) + "\n"
result += deserialize_TxOut(txOut, owner_keys) + "\n"
return result

def parse_MerkleTx(vds):
Expand All @@ -98,8 +102,8 @@ def parse_MerkleTx(vds):
d['nIndex'] = vds.read_int32()
return d

def deserialize_MerkleTx(d, transaction_index=None):
result = deserialize_Transaction(d, transaction_index)
def deserialize_MerkleTx(d, transaction_index=None, owner_keys=None):
result = deserialize_Transaction(d, transaction_index, owner_keys)
result = "Merkle hashBlock: "+short_hex(d['hashBlock'][::-1])+"\n" + result
return result

Expand Down Expand Up @@ -129,8 +133,8 @@ def parse_WalletTx(vds):

return d

def deserialize_WalletTx(d, transaction_index=None):
result = deserialize_MerkleTx(d, transaction_index)
def deserialize_WalletTx(d, transaction_index=None, owner_keys=None):
result = deserialize_MerkleTx(d, transaction_index, owner_keys)

result += "mapValue:"+str(d['mapValue'])
if len(d['orderForm']) > 0:
Expand Down
2 changes: 0 additions & 2 deletions fixwallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ def pre_put_callback(type, data):
txid = options.tweakspent.decode('hex_codec')[::-1]
def tweak_spent_callback(type, data):
if txid in data['__key__']:
import pdb
pdb.set_trace()
data['__value__'] = data['__value__'][:-1]+'\0'
return True
rewrite_wallet(db_env, options.outfile, tweak_spent_callback)
Expand Down
60 changes: 33 additions & 27 deletions wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from bsddb.db import *
import logging
import pdb
import re
import sys
import time
Expand Down Expand Up @@ -167,43 +166,50 @@ def dump_wallet(db_env, print_wallet, print_wallet_transactions, transaction_fil

wallet_transactions = []
transaction_index = { }
owner_keys = { }

def item_callback(type, d):
if type == "tx":
wallet_transactions.append( d )
transaction_index[d['tx_id']] = d
elif print_wallet:
if type == "name":
print("ADDRESS "+d['hash']+" : "+d['name'])
elif type == "version":
print("Version: %d"%(d['version'],))
elif type == "setting":
print(d['setting']+": "+str(d['value']))
elif type == "key":
print("PubKey "+ short_hex(d['public_key']) + " " + public_key_to_bc_address(d['public_key']) +
": PriKey "+ short_hex(d['private_key']))
elif type == "wkey":
print("WPubKey 0x"+ short_hex(d['public_key']) + " " + public_key_to_bc_address(d['public_key']) +
": WPriKey 0x"+ short_hex(d['private_key']))
print(" Created: "+time.ctime(d['created'])+" Expires: "+time.ctime(d['expires'])+" Comment: "+d['comment'])
elif type == "defaultkey":
print("Default Key: 0x"+ short_hex(d['key']) + " " + public_key_to_bc_address(d['key']))
elif type == "pool":
print("Change Pool key %d: %s (Time: %s)"% (d['n'], public_key_to_bc_address(d['public_key']), time.ctime(d['nTime'])))
elif type == "acc":
print("Account %s (current key: %s)"%(d['account'], public_key_to_bc_address(d['public_key'])))
elif type == "acentry":
print("Move '%s' %d (other: '%s', time: %s, entry %d) %s"%
(d['account'], d['nCreditDebit'], d['otherAccount'], time.ctime(d['nTime']), d['n'], d['comment']))
else:
print "Unknown key type: "+type
elif type == "key":
owner_keys[public_key_to_bc_address(d['public_key'])] = d['private_key']

if not print_wallet:
return
if type == "tx":
return
elif type == "name":
print("ADDRESS "+d['hash']+" : "+d['name'])
elif type == "version":
print("Version: %d"%(d['version'],))
elif type == "setting":
print(d['setting']+": "+str(d['value']))
elif type == "key":
print("PubKey "+ short_hex(d['public_key']) + " " + public_key_to_bc_address(d['public_key']) +
": PriKey "+ short_hex(d['private_key']))
elif type == "wkey":
print("WPubKey 0x"+ short_hex(d['public_key']) + " " + public_key_to_bc_address(d['public_key']) +
": WPriKey 0x"+ short_hex(d['private_key']))
print(" Created: "+time.ctime(d['created'])+" Expires: "+time.ctime(d['expires'])+" Comment: "+d['comment'])
elif type == "defaultkey":
print("Default Key: 0x"+ short_hex(d['key']) + " " + public_key_to_bc_address(d['key']))
elif type == "pool":
print("Change Pool key %d: %s (Time: %s)"% (d['n'], public_key_to_bc_address(d['public_key']), time.ctime(d['nTime'])))
elif type == "acc":
print("Account %s (current key: %s)"%(d['account'], public_key_to_bc_address(d['public_key'])))
elif type == "acentry":
print("Move '%s' %d (other: '%s', time: %s, entry %d) %s"%
(d['account'], d['nCreditDebit'], d['otherAccount'], time.ctime(d['nTime']), d['n'], d['comment']))
else:
print "Unknown key type: "+type

parse_wallet(db, item_callback)

if print_wallet_transactions:
keyfunc = lambda i: i['timeReceived']
for d in sorted(wallet_transactions, key=keyfunc):
tx_value = deserialize_WalletTx(d, transaction_index)
tx_value = deserialize_WalletTx(d, transaction_index, owner_keys)
if len(transaction_filter) > 0 and re.search(transaction_filter, tx_value) is None: continue

print("==WalletTransaction== "+long_hex(d['tx_id'][::-1]))
Expand Down

0 comments on commit f45c890

Please sign in to comment.