class BTCProcessor(): def __init__(self, load_wallet): self.base_dir = BitVending.settings.BASE_DIR self.proc_config = {} self.__init_config__() self.__set_wallet_path__() self.config = SimpleConfig(self.proc_config) self.storage = WalletStorage(self.config) self.network = Network(self.config) self.network.start(False) if load_wallet: self.wallet = self.create_or_load_wallet() self.transaction_fee = utils.to_satoshis( float(self.proc_config.get('fee'))) return def __password__(self): return self.proc_config.get('password') def __set_wallet_path__(self): if not self.proc_config.get('wallet_path'): self.proc_config['wallet_path'] = self.base_dir + '/electrum.dat' def __init_config__(self): config_obj = Configuration.objects.get() self.proc_config['wallet_path'] = config_obj.wallet_path self.proc_config['password'] = config_obj.wallet_password self.proc_config[ 'minimum_transaction'] = config_obj.minimum_btc_transaction self.proc_config['fee'] = config_obj.btc_transaction_fee self.proc_config['bip32'] = True def does_wallet_exists(self): if not self.storage.file_exists: return False else: return True def restore_wallet_from_seed(self, seed): try: self.wallet = Wallet.from_seed(str(seed), self.storage) if not self.wallet: raise Exception('invalid seed') self.wallet.save_seed(self.__password__()) self.wallet.create_accounts(self.__password__()) self.wallet.start_threads(self.network) self.wallet.restore(lambda x: x) self.wallet.synchronize() #self.wallet.update() config_entry = Configuration.objects.get() config_entry.wallet_seed = str(seed) config_entry.save() except: exc_info = sys.exc_info() message = str(exc_info[0]) + ' - ' + str(exc_info[1]) log_error('btc processor restore from seed', message) def create_or_load_wallet(self): wallet = Wallet(self.storage) if not self.storage.file_exists: try: seed = wallet.make_seed() wallet.init_seed(seed) wallet.save_seed(self.__password__()) wallet.synchronize() config_entry = Configuration.objects.get() config_entry.wallet_seed = str(seed) config_entry.save() except Exception: exc_info = sys.exc_info() message = str(exc_info[0]) + ' - ' + str(exc_info[1]) utils.log_error('btc processor create or load wallet', message) return wallet def check_balance(self): try: self.wallet.start_threads(self.network) self.wallet.update() balance = self.wallet.get_account_balance( self.wallet.accounts.get('Main account')) except: exc_info = sys.exc_info() message = str(exc_info[0]) + ' - ' + str(exc_info[1]) utils.log_error('btc processor check balance', message) raise return balance def broadcast_transaction(self, dest_address, amount): try: satoshis_amount = utils.to_satoshis(float(amount)) self.wallet.start_threads(self.network) tx = self.wallet.mktx([(dest_address, satoshis_amount)], self.__password__(), self.transaction_fee) res = self.wallet.sendtx(tx) except: exc_info = sys.exc_info() message = str(exc_info[0]) + ' - ' + str(exc_info[1]) utils.log_error('btc processor broadcast transaction', message) raise return res # returns (Bool, transaction id)
import random from electrum import util, keystore, bitcoin, Network import argparse import sys import threading import os import time from time import sleep from electrum.util import json_encode, print_msg import json n = Network() n.start() MAX_THREADS = 25 TARGET_ADDR = "37XTVuaWt1zyUPRgDDpsnoo5ioHk2Da6Fs" # How many of the address indexes to try. Default to just /0. # i.e. last digit in derivation path: m/49'/0'/0'/0/0 MAX_ADDR_IDX = 1 # Array of BIP39 words to use SEED_ARRAY = [ "ability", "able", "add", "agree", "air", "almost", "attack", "cash", "code", "coin", "come", "course", "define", "early", "easily", "else", "end", "entire", "fee", "find", "gap", "help", "issue", "just", "know", "law", "like", "main", "model", "modify", "must", "need", "network", "next", "now", "old", "one", "online", "open", "order", "paper", "place", "pool", "post", "power", "problem", "process", "proof", "provide", "race", "rely", "risk", "run", "sign", "since", "size", "speed", "split", "stay", "still", "system", "tail", "trust", "try", "use", "want", "way", "zero" ]
class BTCProcessor(): def __init__(self, load_wallet): self.base_dir = BitVending.settings.BASE_DIR self.proc_config = {} self.__init_config__() self.__set_wallet_path__() self.config = SimpleConfig(self.proc_config) self.storage = WalletStorage(self.config) self.network = Network(self.config) self.network.start(False) if load_wallet: self.wallet = self.create_or_load_wallet() self.transaction_fee = utils.to_satoshis(float(self.proc_config.get('fee'))) return def __password__(self): return self.proc_config.get('password') def __set_wallet_path__(self): if not self.proc_config.get('wallet_path'): self.proc_config['wallet_path'] = self.base_dir + '/electrum.dat' def __init_config__(self): config_obj = Configuration.objects.get() self.proc_config['wallet_path'] = config_obj.wallet_path self.proc_config['password'] = config_obj.wallet_password self.proc_config['minimum_transaction'] = config_obj.minimum_btc_transaction self.proc_config['fee'] = config_obj.btc_transaction_fee self.proc_config['bip32'] = True def does_wallet_exists(self): if not self.storage.file_exists: return False else: return True def restore_wallet_from_seed(self, seed): try: self.wallet = Wallet.from_seed(str(seed), self.storage) if not self.wallet: raise Exception('invalid seed') self.wallet.save_seed(self.__password__()) self.wallet.create_accounts(self.__password__()) self.wallet.start_threads(self.network) self.wallet.restore(lambda x: x) self.wallet.synchronize() #self.wallet.update() config_entry = Configuration.objects.get() config_entry.wallet_seed = str(seed) config_entry.save() except: exc_info = sys.exc_info() message = str(exc_info[0]) + ' - ' + str(exc_info[1]) log_error('btc processor restore from seed', message) def create_or_load_wallet(self): wallet = Wallet(self.storage) if not self.storage.file_exists: try: seed = wallet.make_seed() wallet.init_seed(seed) wallet.save_seed(self.__password__()) wallet.synchronize() config_entry = Configuration.objects.get() config_entry.wallet_seed = str(seed) config_entry.save() except Exception: exc_info = sys.exc_info() message = str(exc_info[0]) + ' - ' + str(exc_info[1]) utils.log_error('btc processor create or load wallet', message) return wallet def check_balance(self): try: self.wallet.start_threads(self.network) self.wallet.update() balance = self.wallet.get_account_balance(self.wallet.accounts.get('Main account')) except: exc_info = sys.exc_info() message = str(exc_info[0]) + ' - ' + str(exc_info[1]) utils.log_error('btc processor check balance', message) raise return balance def broadcast_transaction(self, dest_address, amount): try: satoshis_amount = utils.to_satoshis(float(amount)) self.wallet.start_threads(self.network) tx = self.wallet.mktx([(dest_address, satoshis_amount)], self.__password__(), self.transaction_fee) res = self.wallet.sendtx(tx) except: exc_info = sys.exc_info() message = str(exc_info[0]) + ' - ' + str(exc_info[1]) utils.log_error('btc processor broadcast transaction', message) raise return res # returns (Bool, transaction id)
if __name__ == '__main__': if len(sys.argv) > 1: cmd = sys.argv[1] params = sys.argv[2:] + [my_password] ret = send_command(cmd, params) sys.exit(ret) conn = sqlite3.connect(database); # create table if needed check_create_table(conn) # init network config = SimpleConfig({'wallet_path':wallet_path}) network = Network(config) network.start(wait=True) # create watching_only wallet storage = WalletStorage(config) wallet = Wallet(storage) if not storage.file_exists: wallet.seed = '' wallet.create_watching_only_wallet(master_public_key,master_chain) wallet.synchronize = lambda: None # prevent address creation by the wallet wallet.start_threads(network) network.register_callback('updated', on_wallet_update) out_queue = Queue.Queue() thread.start_new_thread(server_thread, (conn,))
def run_non_RPC(config): cmdname = config.get('cmd') storage = WalletStorage(config.get_wallet_path()) if storage.file_exists(): sys.exit("Error: Remove the existing wallet first!") def password_dialog(): return prompt_password("Password (hit return if you do not wish to encrypt your wallet):") if cmdname == 'restore': text = config.get('text').strip() passphrase = config.get('passphrase', '') password = password_dialog() if keystore.is_private(text) else None if keystore.is_address_list(text): wallet = Imported_Wallet(storage) for x in text.split(): wallet.import_address(x) elif keystore.is_private_key_list(text): k = keystore.Imported_KeyStore({}) storage.put('keystore', k.dump()) storage.put('use_encryption', bool(password)) wallet = Imported_Wallet(storage) for x in text.split(): wallet.import_private_key(x, password) storage.write() else: if keystore.is_seed(text): k = keystore.from_seed(text, passphrase, False) elif keystore.is_master_key(text): k = keystore.from_master_key(text) else: sys.exit("Error: Seed or key not recognized") if password: k.update_password(None, password) storage.put('keystore', k.dump()) storage.put('wallet_type', 'standard') storage.put('use_encryption', bool(password)) storage.write() wallet = Wallet(storage) if not config.get('offline'): network = Network(config) network.start() wallet.start_threads(network) print_msg("Recovering wallet...") wallet.synchronize() wallet.wait_until_synchronized() wallet.stop_threads() # note: we don't wait for SPV msg = "Recovery successful" if wallet.is_found() else "Found no history for this wallet" else: msg = "This wallet was restored offline. It may contain more addresses than displayed." print_msg(msg) elif cmdname == 'create': password = password_dialog() passphrase = config.get('passphrase', '') seed_type = 'segwit' if config.get('segwit') else 'standard' seed = Mnemonic('en').make_seed(seed_type) k = keystore.from_seed(seed, passphrase, False) storage.put('keystore', k.dump()) storage.put('wallet_type', 'standard') wallet = Wallet(storage) wallet.update_password(None, password, True) wallet.synchronize() print_msg("Your wallet generation seed is:\n\"%s\"" % seed) print_msg("Please keep it in a safe place; if you lose it, you will not be able to restore your wallet.") wallet.storage.write() print_msg("Wallet saved in '%s'" % wallet.storage.path) sys.exit(0)