def serialize_block(chain, block): ds = BCDataStream.BCDataStream() chain.ds_serialize_block(ds, block) return ds.input
def str_to_ds(s): import BCDataStream ds = BCDataStream.BCDataStream() ds.write(s) return ds
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: 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: 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'))
def update_wallet(db, type, data): """Write a single item to the wallet. db must be open with writable=True. type and data are the type code and data dictionary as parse_wallet would give to item_callback. data's __key__, __value__ and __type__ are ignored; only the primary data fields are used. """ d = data kds = BCDataStream() vds = BCDataStream() # Write the type code to the key kds.write_string(type) vds.write("") # Ensure there is something try: if type == "tx": raise NotImplementedError("Writing items of type 'tx'") kds.write(d['tx_id']) #d.update(parse_WalletTx(vds)) elif type == "name": kds.write(d['hash']) vds.write(d['name']) elif type == "version": vds.write_uint32(d['version']) elif type == "setting": raise NotImplementedError("Writing items of type 'setting'") kds.write_string(d['setting']) #d['value'] = parse_setting(d['setting'], vds) elif type == "key": kds.write_string(d['public_key']) vds.write_string(d['private_key']) elif type == "wkey": kds.write_string(d['public_key']) vds.write_string(d['private_key']) vds.write_int64(d['created']) vds.write_int64(d['expires']) vds.write_string(d['comment']) elif type == "ckey": kds.write_string(d['public_key']) kds.write_string(d['crypted_key']) elif type == "mkey": kds.write_int32(d['nID']) vds.write_string(d['crypted_key']) vds.write_string(d['salt']) vds.write_int32(d['nDeriveIterations']) vds.write_int32(d['nDerivationMethod']) vds.write_string(d['vchOtherDerivationParameters']) elif type == "defaultkey": vds.write_string(d['key']) elif type == "pool": kds.write_int64(d['n']) vds.write_int32(d['nVersion']) vds.write_int64(d['nTime']) vds.write_string(d['public_key']) elif type == "acc": kds.write_string(d['account']) vds.write_int32(d['nVersion']) vds.write_string(d['public_key']) elif type == "acentry": kds.write_string(d['account']) kds.write_uint64(d['n']) vds.write_int32(d['nVersion']) vds.write_int64(d['nCreditDebit']) vds.write_int64(d['nTime']) vds.write_string(d['otherAccount']) vds.write_string(d['comment']) elif type == "bestblock": vds.write_int32(d['nVersion']) vds.write_compact_size(len(d['hashes'])) for h in d['hashes']: vds.write(h) else: print "Unknown key type: "+type # Write the key/value pair to the database db.put(kds.input, vds.input) except Exception, e: print("ERROR writing to wallet.dat, type %s"%type) print("data dictionary: %r"%data)
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'))
def serialize_transaction(chain, tx): ds = BCDataStream.BCDataStream() chain.ds_serialize_transaction(ds, tx) return ds.input