def shutdown(self): """Shutdown the whole DBManager. *Must* be called before application exit! This instance isn't usable anymore after shutdonw has been called on it! """ self._a() try: # close all cursors on databases for db in self._dbs.values(): db.close_all_cursors() # abort all open transactions for txn in self._txns.values(): txn.abort() # close all databases for db in self._dbs.values(): try: db.close() except NotOpenError: pass self._env.close() self._env = None finally: self._r()
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 finalize(): global db if db: db.close() db = None
sys.exit(2) wallet_filename = os.path.abspath(sys.argv[1]) with open(wallet_filename, "rb") as wallet_file: wallet_file.seek(12) if wallet_file.read(8) != b"\x62\x31\x05\x00\x09\x00\x00\x00": # BDB magic, Btree v9 print(prog+": error: file is not a Bitcoin Core wallet", file=sys.stderr) sys.exit(1) db_env = bsddb.db.DBEnv() db_env.open(os.path.dirname(wallet_filename), bsddb.db.DB_CREATE | bsddb.db.DB_INIT_MPOOL) db = bsddb.db.DB(db_env) db.open(wallet_filename, b"main", bsddb.db.DB_BTREE, bsddb.db.DB_RDONLY) mkey = db.get(b"\x04mkey\x01\x00\x00\x00") db.close() db_env.close() if not mkey: raise ValueError("Encrypted master key #1 not found in the Bitcoin Core wallet file.\n"+ "(is this wallet encrypted? is this a standard Bitcoin Core wallet?)") # This is a little fragile because it assumes the encrypted key and salt sizes are # 48 and 8 bytes long respectively, which although currently true may not always be: # (it will loudly fail if this isn't the case; if smarter it could gracefully succeed): encrypted_master_key, salt, method, iter_count = struct.unpack_from("< 49p 9p I I", mkey) if method != 0: print(prog+": warning: unexpected Bitcoin Core key derivation method", str(method), file=sys.stderr) print("Partial Bitcoin Core encrypted master key, salt, iter_count, and crc in base64:", file=sys.stderr)
with open(wallet_filename, "rb") as wallet_file: wallet_file.seek(12) if wallet_file.read( 8) != b"\x62\x31\x05\x00\x09\x00\x00\x00": # BDB magic, Btree v9 print(prog + ": error: file is not a Bitcoin Core wallet", file=sys.stderr) sys.exit(1) db_env = bsddb.db.DBEnv() db_env.open(os.path.dirname(wallet_filename), bsddb.db.DB_CREATE | bsddb.db.DB_INIT_MPOOL) db = bsddb.db.DB(db_env) db.open(wallet_filename, b"main", bsddb.db.DB_BTREE, bsddb.db.DB_RDONLY) mkey = db.get(b"\x04mkey\x01\x00\x00\x00") db.close() db_env.close() if not mkey: raise ValueError( "Encrypted master key #1 not found in the Bitcoin Core wallet file.\n" + "(is this wallet encrypted? is this a standard Bitcoin Core wallet?)") # This is a little fragile because it assumes the encrypted key and salt sizes are # 48 and 8 bytes long respectively, which although currently true may not always be: # (it will loudly fail if this isn't the case; if smarter it could gracefully succeed): encrypted_master_key, salt, method, iter_count = struct.unpack_from( "< 49p 9p I I", mkey) if method != 0: print(prog + ": warning: unexpected Bitcoin Core key derivation method",