def dump_keys(db_env, addressStart, outputFileName):
	db = DB(db_env)
	try:
		r = db.open("wallet.dat", "main", DB_BTREE, DB_THREAD|DB_RDONLY)
	except DBError:
		logging.error("Couldn't open addr.dat/main. Try quitting Bitcoin and running this again.")
		return

	cString = cStringIO.StringIO()
	kds = BCDataStream()
	vds = BCDataStream()

	for (key, value) in db.items():
		kds.clear(); kds.write(key)
		vds.clear(); vds.write(value)
		type = kds.read_string()
		if type == "key":
			publicKey = kds.read_bytes(kds.read_compact_size())
			privateKey = vds.read_bytes(vds.read_compact_size())
			address = public_key_to_bc_address(publicKey)
			if address.startswith(addressStart):
				privateKey58 = b58encode(privateKey)
				cString.write('%s\n' % privateKey58)
				print("\nPubKey hex: "+ long_hex(publicKey) + "\nPubKey base58: "+ b58encode(publicKey) + "\nAddress: " +
					address + "\nPriKey hex: "+ long_hex(privateKey) + "\nPriKey base58: "+ privateKey58 + "\n")

	outputText = cString.getvalue()
	if outputText != '':
		writeFileText(outputFileName, outputText)

	db.close()
예제 #2
0
def _read_tx(db, txhash):
  kds = BCDataStream()
  vds = BCDataStream()

  key_prefix = "t"+txhash
  cursor = db.iterator(prefix=key_prefix)
  (key, value) = cursor.next()
  kds.clear(); kds.write(key)
  vds.clear(); vds.write(value)

  kds.read_bytes(1)
  hash256 = (kds.read_bytes(32))
  tx_pos = _read_CDiskTxPos(vds)
  return _dump_tx(datadir, hash256, tx_pos)
예제 #3
0
def dump_block(datadir, db_env, block_hash):
    """ Dump a block, given hexadecimal hash-- either the full hash
      OR a short_hex version of the it.
  """
    db = _open_blkindex(db_env)

    kds = BCDataStream()
    vds = BCDataStream()

    n_blockindex = 0

    key_prefix = "\x0ablockindex"
    cursor = db.cursor()
    (key, value) = cursor.set_range(key_prefix)

    while key.startswith(key_prefix):
        kds.clear()
        kds.write(key)
        vds.clear()
        vds.write(value)

        type = kds.read_string()
        hash256 = kds.read_bytes(32)
        hash_hex = long_hex(hash256[::-1])
        block_data = _parse_block_index(vds)

        if (hash_hex.startswith(block_hash)
                or short_hex(hash256[::-1]).startswith(block_hash)):
            print "Block height: " + str(block_data['nHeight'])
            _dump_block(datadir, block_data['nFile'], block_data['nBlockPos'],
                        hash256, block_data['hashNext'])

        (key, value) = cursor.next()

    db.close()
예제 #4
0
def scan_blocks(datadir, db_env, callback_fn):
    """ Scan through blocks, from last through genesis block,
      calling callback_fn(block_data) for each.
      callback_fn should return False if scanning should
      stop, True if it should continue.
      Returns last block_data scanned.
  """
    db = _open_blkindex(db_env)

    kds = BCDataStream()
    vds = BCDataStream()

    # Read the hashBestChain record:
    cursor = db.cursor()
    (key, value) = cursor.set_range("\x0dhashBestChain")
    vds.write(value)
    hashBestChain = vds.read_bytes(32)

    block_data = read_block(cursor, hashBestChain)

    while callback_fn(block_data):
        if block_data['nHeight'] == 0:
            break
        block_data = read_block(cursor, block_data['hashPrev'])
    return block_data
예제 #5
0
def check_block_chain(db_env):
  """ Make sure hashPrev/hashNext pointers are consistent through block chain """
  db = _open_blkindex(db_env)

  kds = BCDataStream()
  vds = BCDataStream()
  
  # Read the hashBestChain record:
  cursor = db.cursor()
  (key, value) = cursor.set_range("\x0dhashBestChain")
  vds.write(value)
  hashBestChain = vds.read_bytes(32)

  back_blocks = []

  block_data = read_block(cursor, hashBestChain)

  while block_data['nHeight'] > 0:
    back_blocks.append( (block_data['nHeight'], block_data['hashMerkle'], block_data['hashPrev'], block_data['hashNext']) )
    block_data = read_block(cursor, block_data['hashPrev'])

  back_blocks.append( (block_data['nHeight'], block_data['hashMerkle'], block_data['hashPrev'], block_data['hashNext']) )
  genesis_block = block_data
  
  print("check block chain: genesis block merkle hash is: %s"%(block_data['hashMerkle'][::-1].encode('hex_codec')))

  while block_data['hashNext'] != ('\0'*32):
    forward = (block_data['nHeight'], block_data['hashMerkle'], block_data['hashPrev'], block_data['hashNext'])
    back = back_blocks.pop()
    if forward != back:
      print("Forward/back block mismatch at height %d!"%(block_data['nHeight'],))
      print(" Forward: "+str(forward))
      print(" Back: "+str(back))
    block_data = read_block(cursor, block_data['hashNext'])
예제 #6
0
def scan_blocks(datadir, db_env, callback_fn):
  """ Scan through blocks, from last through genesis block,
      calling callback_fn(block_data) for each.
      callback_fn should return False if scanning should
      stop, True if it should continue.
      Returns last block_data scanned.
  """
  db = _open_blkindex(db_env)

  kds = BCDataStream()
  vds = BCDataStream()
  
  # Read the hashBestChain record:
  cursor = db.cursor()
  (key, value) = cursor.set_range("\x0dhashBestChain")
  vds.write(value)
  hashBestChain = vds.read_bytes(32)

  block_data = read_block(cursor, hashBestChain)

  while callback_fn(block_data):
    if block_data['nHeight'] == 0:
      break;
    block_data = read_block(cursor, block_data['hashPrev'])
  return block_data
예제 #7
0
def search_blocks(datadir, db_env, pattern):
  """ Dump a block given block number (== height, genesis block is 0)
  """
  db = _open_blkindex(db_env)
  kds = BCDataStream()
  vds = BCDataStream()
  
  # Read the hashBestChain record:
  cursor = db.cursor()
  (key, value) = cursor.set_range("\x0dhashBestChain")
  vds.write(value)
  hashBestChain = vds.read_bytes(32)
  block_data = read_block(cursor, hashBestChain)

  if pattern == "NONSTANDARD_CSCRIPTS": # Hack to look for non-standard transactions
    search_odd_scripts(datadir, cursor, block_data)
    return

  while True:
    block_string = _dump_block(datadir, block_data['nFile'], block_data['nBlockPos'],
                               block_data['hash256'], block_data['hashNext'], False)
    
    if re.search(pattern, block_string) is not None:
      print "MATCH: Block height: "+str(block_data['nHeight'])
      print block_string

    if block_data['nHeight'] == 0:
      break
    block_data = read_block(cursor, block_data['hashPrev'])
예제 #8
0
def dump_block(datadir, db_env, block_hash):
    """ Dump a block, given hexadecimal hash-- either the full hash
      OR a short_hex version of the it.
  """
    db = _open_blkindex(db_env)

    kds = BCDataStream()
    vds = BCDataStream()

    n_blockindex = 0

    key_prefix = "\x0ablockindex"
    cursor = db.cursor()
    (key, value) = cursor.set_range(key_prefix)

    while key.startswith(key_prefix):
        kds.clear()
        kds.write(key)
        vds.clear()
        vds.write(value)

        type = kds.read_string()
        hash256 = kds.read_bytes(32)
        hash_hex = long_hex(hash256[::-1])
        block_data = _parse_block_index(vds)

        if hash_hex.startswith(block_hash) or short_hex(hash256[::-1]).startswith(block_hash):
            print "Block height: " + str(block_data["nHeight"])
            _dump_block(datadir, block_data["nFile"], block_data["nBlockPos"], hash256, block_data["hashNext"])

        (key, value) = cursor.next()

    db.close()
예제 #9
0
def search_blocks(datadir, db_env, pattern):
    """ Dump a block given block number (== height, genesis block is 0)
  """
    db = _open_blkindex(db_env)
    kds = BCDataStream()
    vds = BCDataStream()

    # Read the hashBestChain record:
    cursor = db.cursor()
    (key, value) = cursor.set_range("\x0dhashBestChain")
    vds.write(value)
    hashBestChain = vds.read_bytes(32)
    block_data = read_block(cursor, hashBestChain)

    if pattern == "NONSTANDARD_CSCRIPTS":  # Hack to look for non-standard transactions
        search_odd_scripts(datadir, cursor, block_data)
        return

    while True:
        block_string = _dump_block(datadir, block_data['nFile'],
                                   block_data['nBlockPos'],
                                   block_data['hash256'],
                                   block_data['hashNext'], False)

        if re.search(pattern, block_string) is not None:
            print "MATCH: Block height: " + str(block_data['nHeight'])
            print block_string

        if block_data['nHeight'] == 0:
            break
        block_data = read_block(cursor, block_data['hashPrev'])
예제 #10
0
def dump_block(datadir, db_env, block_hash, print_raw_tx=False, print_json=False):
  """ Dump a block, given hexadecimal hash-- either the full hash
      OR a short_hex version of the it.
  """
  db = _open_blkindex(db_env)

  kds = BCDataStream()
  vds = BCDataStream()

  n_blockindex = 0

  key_prefix = "\x0ablockindex"
  cursor = db.cursor()
  (key, value) = cursor.set_range(key_prefix)

  while key.startswith(key_prefix):
    kds.clear(); kds.write(key)
    vds.clear(); vds.write(value)

    type = kds.read_string()
    hash256 = kds.read_bytes(32)
    hash_hex = long_hex(hash256[::-1])
    block_data = _parse_block_index(vds)

    if (hash_hex.startswith(block_hash) or short_hex(hash256[::-1]).startswith(block_hash)):
      if print_json == False:
          print "Block height: "+str(block_data['nHeight'])
          _dump_block(datadir, block_data['nFile'], block_data['nBlockPos'], hash256, block_data['hashNext'], print_raw_tx=print_raw_tx)
      else:
          _dump_block(datadir, block_data['nFile'], block_data['nBlockPos'], hash256, block_data['hashNext'], print_json=print_json, do_print=False)

    (key, value) = cursor.next()

  db.close()
예제 #11
0
def _read_tx(db, txhash):
    kds = BCDataStream()
    vds = BCDataStream()

    key_prefix = "t" + txhash
    cursor = db.iterator(prefix=key_prefix)
    (key, value) = cursor.next()
    kds.clear()
    kds.write(key)
    vds.clear()
    vds.write(value)

    kds.read_bytes(1)
    hash256 = (kds.read_bytes(32))
    tx_pos = _read_CDiskTxPos(vds)
    return _dump_tx(datadir, hash256, tx_pos)
예제 #12
0
def dump_blkindex_summary(db_env):
    db = DB(db_env)
    try:
        r = db.open("blkindex.dat", "main", DB_BTREE, DB_THREAD | DB_RDONLY)
    except DBError:
        r = True

    if r is not None:
        logging.error(
            "Couldn't open blkindex.dat/main.  Try quitting any running Bitcoin apps."
        )
        sys.exit(1)

    kds = BCDataStream()
    vds = BCDataStream()

    n_tx = 0
    n_blockindex = 0

    print("blkindex file summary:")
    for (key, value) in db.items():
        kds.clear()
        kds.write(key)
        vds.clear()
        vds.write(value)

        type = kds.read_string()

        if type == "tx":
            n_tx += 1
        elif type == "blockindex":
            n_blockindex += 1
        elif type == "version":
            version = vds.read_int32()
            print(" Version: %d" % (version, ))
        elif type == "hashBestChain":
            hash = vds.read_bytes(32)
            print(" HashBestChain: %s" % (hash.encode('hex_codec'), ))
        else:
            logging.warn("blkindex: unknown type '%s'" % (type, ))
            continue

    print(" %d transactions, %d blocks." % (n_tx, n_blockindex))
    db.close()
예제 #13
0
def dump_transaction(datadir, db_env, tx_id):
    """ Dump a transaction, given hexadecimal tx_id-- either the full ID
      OR a short_hex version of the id.
  """
    db = DB(db_env)
    try:
        r = db.open("blkindex.dat", "main", DB_BTREE, DB_THREAD | DB_RDONLY)
    except DBError:
        r = True

    if r is not None:
        logging.error(
            "Couldn't open blkindex.dat/main.  Try quitting any running Bitcoin apps."
        )
        sys.exit(1)

    kds = BCDataStream()
    vds = BCDataStream()

    n_tx = 0
    n_blockindex = 0

    key_prefix = "\x02tx" + (tx_id[-4:].decode('hex_codec')[::-1])
    cursor = db.cursor()
    (key, value) = cursor.set_range(key_prefix)

    while key.startswith(key_prefix):
        kds.clear()
        kds.write(key)
        vds.clear()
        vds.write(value)

        type = kds.read_string()
        hash256 = (kds.read_bytes(32))
        hash_hex = long_hex(hash256[::-1])
        version = vds.read_uint32()
        tx_pos = _read_CDiskTxPos(vds)
        if (hash_hex.startswith(tx_id)
                or short_hex(hash256[::-1]).startswith(tx_id)):
            _dump_tx(datadir, hash256, tx_pos)

        (key, value) = cursor.next()

    db.close()
예제 #14
0
def dump_transaction(datadir, db_env, tx_id):
    """ Dump a transaction, given hexadecimal tx_id-- either the full ID
      OR a short_hex version of the id.
  """
    db = DB(db_env)
    try:
        r = db.open("blkindex.dat", "main", DB_BTREE, DB_THREAD | DB_RDONLY)
    except DBError:
        r = True

    if r is not None:
        logging.error("Couldn't open blkindex.dat/main.  Try quitting any running Bitcoin apps.")
        sys.exit(1)

    kds = BCDataStream()
    vds = BCDataStream()

    n_tx = 0
    n_blockindex = 0

    key_prefix = "\x02tx" + (tx_id[-4:].decode("hex_codec")[::-1])
    cursor = db.cursor()
    (key, value) = cursor.set_range(key_prefix)

    while key.startswith(key_prefix):
        kds.clear()
        kds.write(key)
        vds.clear()
        vds.write(value)

        type = kds.read_string()
        hash256 = kds.read_bytes(32)
        hash_hex = long_hex(hash256[::-1])
        version = vds.read_uint32()
        tx_pos = _read_CDiskTxPos(vds)
        if hash_hex.startswith(tx_id) or short_hex(hash256[::-1]).startswith(tx_id):
            _dump_tx(datadir, hash256, tx_pos)

        (key, value) = cursor.next()

    db.close()
예제 #15
0
def dump_block_n(datadir, db_env, block_number):
    """ Dump a block given block number (== height, genesis block is 0)
  """
    db = _open_blkindex(db_env)

    kds = BCDataStream()
    vds = BCDataStream()

    # Read the hashBestChain record:
    cursor = db.cursor()
    (key, value) = cursor.set_range("\x0dhashBestChain")
    vds.write(value)
    hashBestChain = vds.read_bytes(32)

    block_data = read_block(cursor, hashBestChain)

    while block_data["nHeight"] > block_number:
        block_data = read_block(cursor, block_data["hashPrev"])

    print "Block height: " + str(block_data["nHeight"])
    _dump_block(datadir, block_data["nFile"], block_data["nBlockPos"], block_data["hash256"], block_data["hashNext"])
예제 #16
0
def dump_blockindex(db_env, owner=None, n_to_dump=1000):
    db = DB(db_env)
    r = db.open("blkindex.dat", "main", DB_BTREE, DB_THREAD | DB_RDONLY)
    if r is not None:
        logging.error("Couldn't open blkindex.dat/main")
        sys.exit(1)

    kds = BCDataStream()
    vds = BCDataStream()

    wallet_transactions = []

    for (i, (key, value)) in enumerate(db.items()):
        if i > n_to_dump:
            break

        kds.clear()
        kds.write(key)
        vds.clear()
        vds.write(value)

        type = kds.read_string()

        if type == "tx":
            hash256 = kds.read_bytes(32)
            version = vds.read_uint32()
            tx_pos = _read_CDiskTxPos(vds)
            print("Tx(%s:%d %d %d)" % ((short_hex(hash256), ) + tx_pos))
            n_tx_out = vds.read_compact_size()
            for i in range(0, n_tx_out):
                tx_out = _read_CDiskTxPos(vds)
                if tx_out[
                        0] != 0xffffffffL:  # UINT_MAX means no TxOuts (unspent)
                    print("  ==> TxOut(%d %d %d)" % tx_out)

        else:
            logging.warn("blkindex: type %s" % (type, ))
            continue

    db.close()
예제 #17
0
def dump_blkindex_summary(db_env):
  db = DB(db_env)
  try:
    r = db.open("blkindex.dat", "main", DB_BTREE, DB_THREAD|DB_RDONLY)
  except DBError:
    r = True

  if r is not None:
    logging.error("Couldn't open blkindex.dat/main.  Try quitting any running Bitcoin apps.")
    sys.exit(1)

  kds = BCDataStream()
  vds = BCDataStream()

  n_tx = 0
  n_blockindex = 0

  print("blkindex file summary:")
  for (key, value) in db.items():
    kds.clear(); kds.write(key)
    vds.clear(); vds.write(value)

    type = kds.read_string()

    if type == "tx":
      n_tx += 1
    elif type == "blockindex":
      n_blockindex += 1
    elif type == "version":
      version = vds.read_int32()
      print(" Version: %d"%(version,))
    elif type == "hashBestChain":
      hash = vds.read_bytes(32)
      print(" HashBestChain: %s"%(hash.encode('hex_codec'),))
    else:
      logging.warn("blkindex: unknown type '%s'"%(type,))
      continue

  print(" %d transactions, %d blocks."%(n_tx, n_blockindex))
  db.close()
예제 #18
0
def check_block_chain(db_env):
    """ Make sure hashPrev/hashNext pointers are consistent through block chain """
    db = _open_blkindex(db_env)

    kds = BCDataStream()
    vds = BCDataStream()

    # Read the hashBestChain record:
    cursor = db.cursor()
    (key, value) = cursor.set_range("\x0dhashBestChain")
    vds.write(value)
    hashBestChain = vds.read_bytes(32)

    back_blocks = []

    block_data = read_block(cursor, hashBestChain)

    while block_data['nHeight'] > 0:
        back_blocks.append((block_data['nHeight'], block_data['hashMerkle'],
                            block_data['hashPrev'], block_data['hashNext']))
        block_data = read_block(cursor, block_data['hashPrev'])

    back_blocks.append((block_data['nHeight'], block_data['hashMerkle'],
                        block_data['hashPrev'], block_data['hashNext']))
    genesis_block = block_data

    print("check block chain: genesis block merkle hash is: %s" %
          (block_data['hashMerkle'][::-1].encode('hex_codec')))

    while block_data['hashNext'] != ('\0' * 32):
        forward = (block_data['nHeight'], block_data['hashMerkle'],
                   block_data['hashPrev'], block_data['hashNext'])
        back = back_blocks.pop()
        if forward != back:
            print("Forward/back block mismatch at height %d!" %
                  (block_data['nHeight'], ))
            print(" Forward: " + str(forward))
            print(" Back: " + str(back))
        block_data = read_block(cursor, block_data['hashNext'])
예제 #19
0
def dump_block_n(datadir, db_env, block_number):
    """ Dump a block given block number (== height, genesis block is 0)
  """
    db = _open_blkindex(db_env)

    kds = BCDataStream()
    vds = BCDataStream()

    # Read the hashBestChain record:
    cursor = db.cursor()
    (key, value) = cursor.set_range("\x0dhashBestChain")
    vds.write(value)
    hashBestChain = vds.read_bytes(32)

    block_data = read_block(cursor, hashBestChain)

    while block_data['nHeight'] > block_number:
        block_data = read_block(cursor, block_data['hashPrev'])

    print "Block height: " + str(block_data['nHeight'])
    _dump_block(datadir, block_data['nFile'], block_data['nBlockPos'],
                block_data['hash256'], block_data['hashNext'])
예제 #20
0
def dump_blockindex(db_env, owner=None, n_to_dump=1000):
  db = DB(db_env)
  r = db.open("blkindex.dat", "main", DB_BTREE, DB_THREAD|DB_RDONLY)
  if r is not None:
    logging.error("Couldn't open blkindex.dat/main")
    sys.exit(1)

  kds = BCDataStream()
  vds = BCDataStream()

  wallet_transactions = []

  for (i, (key, value)) in enumerate(db.items()):
    if i > n_to_dump:
      break

    kds.clear(); kds.write(key)
    vds.clear(); vds.write(value)

    type = kds.read_string()

    if type == "tx":
      hash256 = kds.read_bytes(32)
      version = vds.read_uint32()
      tx_pos = _read_CDiskTxPos(vds)
      print("Tx(%s:%d %d %d)"%((short_hex(hash256),)+tx_pos))
      n_tx_out = vds.read_compact_size()
      for i in range(0,n_tx_out):
        tx_out = _read_CDiskTxPos(vds)
        if tx_out[0] != 0xffffffffL:  # UINT_MAX means no TxOuts (unspent)
          print("  ==> TxOut(%d %d %d)"%tx_out)
      
    else:
      logging.warn("blkindex: type %s"%(type,))
      continue

  db.close()
예제 #21
0
def parse_wallet(db, item_callback):
    kds = BCDataStream()
    vds = BCDataStream()

    for (key, value) in db.items():
        d = {}

        kds.clear()
        kds.write(key)
        vds.clear()
        vds.write(value)

        type = kds.read_string()

        d["__key__"] = key
        d["__value__"] = value
        d["__type__"] = type

        try:
            if type == "tx":
                d["tx_id"] = kds.read_bytes(32)
                d.update(parse_WalletTx(vds))
            elif type == "name":
                d['hash'] = kds.read_string()
                d['name'] = vds.read_string()
            elif type == "version":
                d['version'] = vds.read_uint32()
            elif type == "setting":
                d['setting'] = kds.read_string()
                d['value'] = parse_setting(d['setting'], vds)
            elif type == "key":
                d['public_key'] = kds.read_bytes(kds.read_compact_size())
                d['private_key'] = vds.read_bytes(vds.read_compact_size())
            elif type == "wkey":
                d['public_key'] = kds.read_bytes(kds.read_compact_size())
                d['private_key'] = vds.read_bytes(vds.read_compact_size())
                d['created'] = vds.read_int64()
                d['expires'] = vds.read_int64()
                d['comment'] = vds.read_string()
            elif type == "ckey":
                d['public_key'] = kds.read_bytes(kds.read_compact_size())
                d['crypted_key'] = vds.read_bytes(vds.read_compact_size())
            elif type == "mkey":
                d['nID'] = kds.read_int32()
                d['crypted_key'] = vds.read_bytes(vds.read_compact_size())
                d['salt'] = vds.read_bytes(vds.read_compact_size())
                d['nDerivationMethod'] = vds.read_int32()
                d['nDeriveIterations'] = vds.read_int32()
                d['vchOtherDerivationParameters'] = vds.read_bytes(
                    vds.read_compact_size())
            elif type == "defaultkey":
                d['key'] = vds.read_bytes(vds.read_compact_size())
            elif type == "pool":
                d['n'] = kds.read_int64()
                d['nVersion'] = vds.read_int32()
                d['nTime'] = vds.read_int64()
                d['public_key'] = vds.read_bytes(vds.read_compact_size())
            elif type == "acc":
                d['account'] = kds.read_string()
                d['nVersion'] = vds.read_int32()
                d['public_key'] = vds.read_bytes(vds.read_compact_size())
            elif type == "acentry":
                d['account'] = kds.read_string()
                d['n'] = kds.read_uint64()
                d['nVersion'] = vds.read_int32()
                d['nCreditDebit'] = vds.read_int64()
                d['nTime'] = vds.read_int64()
                d['otherAccount'] = vds.read_string()
                d['comment'] = vds.read_string()
            elif type == "bestblock":
                d['nVersion'] = vds.read_int32()
                d.update(parse_BlockLocator(vds))
            else:
                print "Unknown key type: " + type

            item_callback(type, d)

        except Exception, e:
            print("ERROR parsing wallet.dat, type %s" % type)
            print("key data in hex: %s" % key.encode('hex_codec'))
            print("value data in hex: %s" % value.encode('hex_codec'))
예제 #22
0
def parse_wallet(db, item_callback):
  kds = BCDataStream()
  vds = BCDataStream()

  for (key, value) in db.items():
    d = { }

    kds.clear(); kds.write(key)
    vds.clear(); vds.write(value)

    try:
      type = kds.read_string()

      d["__key__"] = key
      d["__value__"] = value
      d["__type__"] = type

    except Exception, e:
      print("ERROR attempting to read data from wallet.dat, type %s"%type)
      continue

    try:
      if type == "tx":
        d["tx_id"] = kds.read_bytes(32)
        d.update(parse_WalletTx(vds))
      elif type == "name":
        d['hash'] = kds.read_string()
        d['name'] = vds.read_string()
      elif type == "version":
        d['version'] = vds.read_uint32()
      elif type == "setting":
        d['setting'] = kds.read_string()
        d['value'] = parse_setting(d['setting'], vds)
      elif type == "key":
        d['public_key'] = kds.read_bytes(kds.read_compact_size())
        d['private_key'] = vds.read_bytes(vds.read_compact_size())
      elif type == "wkey":
        d['public_key'] = kds.read_bytes(kds.read_compact_size())
        d['private_key'] = vds.read_bytes(vds.read_compact_size())
        d['created'] = vds.read_int64()
        d['expires'] = vds.read_int64()
        d['comment'] = vds.read_string()
      elif type == "ckey":
        d['public_key'] = kds.read_bytes(kds.read_compact_size())
        d['crypted_key'] = vds.read_bytes(vds.read_compact_size())
      elif type == "mkey":
        d['nID'] = kds.read_int32()
        d['crypted_key'] = vds.read_bytes(vds.read_compact_size())
        d['salt'] = vds.read_bytes(vds.read_compact_size())
        d['nDerivationMethod'] = vds.read_int32()
        d['nDeriveIterations'] = vds.read_int32()
        d['vchOtherDerivationParameters'] = vds.read_bytes(vds.read_compact_size())
      elif type == "defaultkey":
        d['key'] = vds.read_bytes(vds.read_compact_size())
      elif type == "pool":
        d['n'] = kds.read_int64()
        d['nVersion'] = vds.read_int32()
        d['nTime'] = vds.read_int64()
        d['public_key'] = vds.read_bytes(vds.read_compact_size())
      elif type == "acc":
        d['account'] = kds.read_string()
        d['nVersion'] = vds.read_int32()
        d['public_key'] = vds.read_bytes(vds.read_compact_size())
      elif type == "acentry":
        d['account'] = kds.read_string()
        d['n'] = kds.read_uint64()
        d['nVersion'] = vds.read_int32()
        d['nCreditDebit'] = vds.read_int64()
        d['nTime'] = vds.read_int64()
        d['otherAccount'] = vds.read_string()
        d['comment'] = vds.read_string()
      elif type == "bestblock":
        d['nVersion'] = vds.read_int32()
        d.update(parse_BlockLocator(vds))
      elif type == "cscript":
        d['scriptHash'] = kds.read_bytes(20)
        d['script'] = vds.read_bytes(vds.read_compact_size())
      else:
        print "Skipping item of type "+type
        continue
      
      item_callback(type, d)

    except Exception, e:
      print("ERROR parsing wallet.dat, type %s"%type)
      print("key data in hex: %s"%key.encode('hex_codec'))
      print("value data in hex: %s"%value.encode('hex_codec'))
예제 #23
0
def parse_wallet(db, item_callback):
  kds = BCDataStream()
  vds = BCDataStream()

  for (key, value) in db.items():
    d = { }

    kds.clear(); kds.write(key)
    vds.clear(); vds.write(value)

    type = kds.read_string()

    d["__key__"] = key
    d["__value__"] = value
    d["__type__"] = type

    try:
      if type == "tx":
        d["tx_id"] = kds.read_bytes(32)
        d.update(parse_WalletTx(vds))
      elif type == "name":
        d['hash'] = kds.read_string()
        d['name'] = vds.read_string()
      elif type == "version":
        d['version'] = vds.read_uint32()
      elif type == "setting":
        d['setting'] = kds.read_string()
        d['value'] = parse_setting(d['setting'], vds)
      elif type == "key":
        d['public_key'] = kds.read_bytes(kds.read_compact_size())
        d['private_key'] = vds.read_bytes(vds.read_compact_size())
      elif type == "wkey":
        d['public_key'] = kds.read_bytes(kds.read_compact_size())
        d['private_key'] = vds.read_bytes(vds.read_compact_size())
        d['created'] = vds.read_int64()
        d['expires'] = vds.read_int64()
        d['comment'] = vds.read_string()
      elif type == "defaultkey":
        d['key'] = vds.read_bytes(vds.read_compact_size())
      elif type == "pool":
        d['n'] = kds.read_int64()
        d['nVersion'] = vds.read_int32()
        d['nTime'] = vds.read_int64()
        d['public_key'] = vds.read_bytes(vds.read_compact_size())
      elif type == "acc":
        d['account'] = kds.read_string()
        d['nVersion'] = vds.read_int32()
        d['public_key'] = vds.read_bytes(vds.read_compact_size())
      elif type == "acentry":
        d['account'] = kds.read_string()
        d['n'] = kds.read_uint64()
        d['nVersion'] = vds.read_int32()
        d['nCreditDebit'] = vds.read_int64()
        d['nTime'] = vds.read_int64()
        d['otherAccount'] = vds.read_string()
        d['comment'] = vds.read_string()
      else:
        print "Unknown key type: "+type
      
      item_callback(type, d)

    except Exception, e:
      print("ERROR parsing wallet.dat, type %s"%type)
      print("key data in hex: %s"%key.encode('hex_codec'))
      print("value data in hex: %s"%value.encode('hex_codec'))