def item_callback(type, d): if type == "tx": wallet_transactions.append( 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
def import_key(keyLine, db_dir, input_mode="b58", dryrun=False, verbose=False): if len(keyLine.strip()) == 0: return if input_mode == "b58": priv_bin = privkey_b58_bin(keyLine) elif input_mode == "b64": priv_bin = privkey_b64_bin(keyLine) elif input_mode == "bin": if len(keyLine) not in (32, 279): raise ValueError("Expected a key of 32 or 279 bytes") priv_bin = keyLine if len(priv_bin) == 32: # Get the full DER key priv_bin = priv_to_der(priv_bin) # The public key of a DER-encoded private key is just the last 65 bytes pub_bin = priv_bin[-65:] # Print out the key and address if verbose: print "Private key: %s" % util.long_hex(priv_bin) print "Public key: %s" % util.long_hex(pub_bin) else: print "Private key: %s" % util.short_hex(priv_bin) print "Public key: %s" % util.short_hex(pub_bin) addr = base58.public_key_to_bc_address(pub_bin) if addr == '': # This can happen if pycrypto is not installed, or if the RIPEMD160 # hash is not available (it has been removed in the Debian/Ubuntu # version) print "Warning: Cannot calculate address; check pycrypto library" else: print "Address: %s" % addr # Data for wallet.update_wallet data = { 'private_key': priv_bin, 'public_key': pub_bin, } try: db_env = util.create_env(db_dir) except bsddb.db.DBNoSuchFileError: logging.error("Couldn't open " + db_dir) sys.exit(1) if not dryrun: db = wallet.open_wallet(db_env, writable=True) wallet.update_wallet(db, 'key', data) db.close()
def import_key(keyLine, db_dir, input_mode="b58", dryrun=False,verbose=False): if len(keyLine.strip()) == 0: return if input_mode == "b58": priv_bin = privkey_b58_bin(keyLine) elif input_mode == "b64": priv_bin = privkey_b64_bin(keyLine) elif input_mode == "bin": if len(keyLine) not in (32, 279): raise ValueError("Expected a key of 32 or 279 bytes") priv_bin = keyLine if len(priv_bin) == 32: # Get the full DER key priv_bin = priv_to_der(priv_bin) # The public key of a DER-encoded private key is just the last 65 bytes pub_bin = priv_bin[-65:] # Print out the key and address if verbose: print "Private key: %s" % util.long_hex(priv_bin) print "Public key: %s" % util.long_hex(pub_bin) else: print "Private key: %s" % util.short_hex(priv_bin) print "Public key: %s" % util.short_hex(pub_bin) addr = base58.public_key_to_bc_address(pub_bin) if addr == '': # This can happen if pycrypto is not installed, or if the RIPEMD160 # hash is not available (it has been removed in the Debian/Ubuntu # version) print "Warning: Cannot calculate address; check pycrypto library" else: print "Address: %s" % addr # Data for wallet.update_wallet data = { 'private_key': priv_bin, 'public_key': pub_bin, } try: db_env = util.create_env(db_dir) except bsddb.db.DBNoSuchFileError: logging.error("Couldn't open " + db_dir) sys.exit(1) if not dryrun: db = wallet.open_wallet(db_env, writable=True) wallet.update_wallet(db, 'key', data) db.close()
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()
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()
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()
def item_callback(type, d): if type == "tx": wallet_transactions.append( d ) transaction_index[d['tx_id']] = d elif type == "key": owner_keys[public_key_to_bc_address(d['public_key'])] = d['private_key'] elif type == "ckey": owner_keys[public_key_to_bc_address(d['public_key'])] = d['crypted_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['crypted_key'])) print(" Created: "+time.ctime(d['created'])+" Expires: "+time.ctime(d['expires'])+" Comment: "+d['comment']) elif type == "ckey": print("PubKey "+ short_hex(d['public_key']) + " " + public_key_to_bc_address(d['public_key']) + ": Encrypted PriKey "+ short_hex(d['crypted_key'])) elif type == "mkey": print("Master Key %d"%(d['nID']) + ": 0x"+ short_hex(d['crypted_key']) + ", Salt: 0x"+ short_hex(d['salt']) + ". Passphrase hashed %d times with method %d with other parameters 0x"%(d['nDeriveIterations'], d['nDerivationMethod']) + long_hex(d['vchOtherDerivationParameters'])) 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'])) elif type == "bestblock": print deserialize_BlockLocator(d) elif type == "cscript": print("CScript: %s : %s"%(public_key_to_bc_address(d['scriptHash'], "\x01"), long_hex(d['script']))) else: print "Unknown key type: "+type
def decode_script(bytes): result = '' for (opcode, vch) in script_GetOp(bytes): if len(result) > 0: result += " " if opcode <= opcodes.OP_PUSHDATA4: result += "%d:" % (opcode, ) result += short_hex(vch) else: result += script_GetOpName(opcode) return result
def decode_script(bytes): result = '' for (opcode, vch) in script_GetOp(bytes): if len(result) > 0: result += " " if opcode <= opcodes.OP_PUSHDATA4: result += "%d:"%(opcode,) result += short_hex(vch) else: result += script_GetOpName(opcode) return result
def decode_script(bytes, shortHex=True): result = '' for (opcode, vch) in script_GetOp(bytes): if len(result) > 0: result += " " if opcode <= opcodes.OP_PUSHDATA4: result += "%d:"%(opcode,) if shortHex == True: result += short_hex(vch) else: result += vch.encode('hex') else: result += script_GetOpName(opcode) return result
def decode_script(bytes, shortHex=True): result = '' for (opcode, vch) in script_GetOp(bytes): if len(result) > 0: result += " " if opcode <= opcodes.OP_PUSHDATA4: result += "%d:" % (opcode, ) if shortHex == True: result += short_hex(vch) else: result += vch.encode('hex') else: result += script_GetOpName(opcode) return result
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()
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()
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()
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()
def deserialize_MerkleTx(d): result = deserialize_Transaction(d) result = "Merkle hashBlock: " + short_hex( d['hashBlock'][::-1]) + "\n" + result return result
def deserialize_MerkleTx(d): result = deserialize_Transaction(d) result = "Merkle hashBlock: "+short_hex(d['hashBlock'][::-1])+"\n" + result return result