def __init__(self, **kwargs): '''Get app settings from options''' self.make_commit = kwargs.pop('make_commit') self.password = kwargs.pop('password') self.signing_key = kwargs.pop('signing_key') self.testnet = kwargs.pop('testnet') self.wallet_path = kwargs.pop('wallet') script_config = read_config() if script_config: self.make_commit = (self.make_commit or script_config.get('make_commit', False)) self.signing_key = (self.signing_key or script_config.get('signing_key', False)) self.testnet = (self.testnet or script_config.get('testnet', False)) self.wallet_path = (self.wallet_path or script_config.get('wallet', False)) if self.wallet_path: self.wallet_path = os.path.expanduser(self.wallet_path) self.config_options = {'cwd': os.getcwd()} self.config_options['password'] = self.password self.config_options['testnet'] = self.testnet self.config_options['wallet_path'] = self.wallet_path self.config = SimpleConfig(self.config_options) if self.config.get('testnet'): constants.set_testnet() self.storage = WalletStorage(self.config.get_wallet_path())
def get_peers(): config = SimpleConfig() peers = {} # 1. get connected interfaces server = config.get('server') interfaces = get_interfaces([server]) if not interfaces: print("No connection to", server) return [] # 2. get list of peers interface = interfaces[server] interface.queue_request('server.peers.subscribe', [], 0) responses = wait_on_interfaces(interfaces).get(server) if responses: response = responses[0][1] # One response, (req, response) tuple peers = parse_servers(response.get('result')) return peers
def send_request(peers, request): print "Contacting %d servers" % len(peers) # start interfaces q2 = Queue.Queue() config = SimpleConfig() interfaces = map(lambda server: Interface(server, q2, config), peers) reached_servers = [] for i in interfaces: i.start() t0 = time.time() while peers: try: i, r = q2.get(timeout=1) except: if time.time() - t0 > 10: print "timeout" break else: continue if i.server in peers: peers.remove(i.server) if i.is_connected(): reached_servers.append(i) else: print "Connection failed:", i.server print "%d servers could be reached" % len(reached_servers) results_queue = Queue.Queue() for i in reached_servers: i.send_request(request, results_queue) results = {} t0 = time.time() while reached_servers: try: i, r = results_queue.get(timeout=1) except: if time.time() - t0 > 10: break else: continue results[i.server] = r.get('result') reached_servers.remove(i) i.stop() for i in reached_servers: print i.server, "did not answer" print "%d answers" % len(results) return results
def get_interfaces(servers, timeout=10): '''Returns a map of servers to connected interfaces. If any connections fail or timeout, they will be missing from the map. ''' socket_queue = Queue.Queue() config = SimpleConfig() connecting = {} for server in servers: if server not in connecting: connecting[server] = Connection(server, socket_queue, config.path) interfaces = {} timeout = time.time() + timeout count = 0 while time.time() < timeout and count < len(servers): try: server, socket = socket_queue.get(True, 0.3) except Queue.Empty: continue if socket: interfaces[server] = Interface(server, socket) count += 1 return interfaces
class SignApp(object): def __init__(self, **kwargs): '''Get app settings from options''' self.make_commit = kwargs.pop('make_commit') self.password = kwargs.pop('password') self.signing_key = kwargs.pop('signing_key') self.testnet = kwargs.pop('testnet') self.wallet_path = kwargs.pop('wallet') script_config = read_config() if script_config: self.make_commit = (self.make_commit or script_config.get('make_commit', False)) self.signing_key = (self.signing_key or script_config.get('signing_key', False)) self.testnet = (self.testnet or script_config.get('testnet', False)) self.wallet_path = (self.wallet_path or script_config.get('wallet', False)) if self.wallet_path: self.wallet_path = os.path.expanduser(self.wallet_path) self.config_options = {'cwd': os.getcwd()} self.config_options['password'] = self.password self.config_options['testnet'] = self.testnet self.config_options['wallet_path'] = self.wallet_path self.config = SimpleConfig(self.config_options) if self.config.get('testnet'): constants.set_testnet() self.storage = WalletStorage(self.config.get_wallet_path()) def load_wallet(self, storage): print('Lodaing wallet: %s' % self.config.get_wallet_path()) password = None if storage.is_encrypted(): if storage.is_encrypted_with_hw_device(): plugins = Plugins(self.config, 'cmdline') password = get_passwd_for_hw_device_encrypted_storage(plugins) storage.decrypt(password) else: password = get_password(storage.decrypt) db = WalletDB(self.storage.read(), manual_upgrades=True) if db.requires_upgrade(): print('Error: Wallet db need to be upgraded.' ' Do it with the wallet app') sys.exit(1) self.wallet = Wallet(db, self.storage, config=self.config) if self.wallet.has_password() and not password: password = get_password(self.wallet.check_password) self.config_options['password'] = password def commit_latest_version(self): commit_msg = COMMIT_MSG_TEMPLATE.format(fname=LATEST_VER_FNAME, version=ELECTRUM_VERSION) print('commiting: %s' % commit_msg) os.system('git add ./%s' % LATEST_VER_FNAME) os.system('git commit -m "%s"' % commit_msg) def run(self): self.load_wallet(self.storage) if self.signing_key: address = self.signing_key else: address = SIGNING_KEYS[0] message = ELECTRUM_VERSION password = self.config_options.get('password') print('Signing version: %s' % message) print('with address: %s' % address) sig = self.wallet.sign_message(address, message, password) sig = base64.b64encode(sig).decode('ascii') content = { 'version': ELECTRUM_VERSION, 'signatures': { address: sig } } content_json = json.dumps(content, indent=4) print(content_json) with open('./%s' % LATEST_VER_FNAME, 'w') as fd: fd.write('%s\n' % content_json) if self.make_commit: self.commit_latest_version()