def remove_contact(index): contacts = db_get('contacts') temp_contact = copy.deepcopy(contacts[index]) del contacts[index] file_name = db_get('default_wallet') key = db_get('default_wallet_key') AESCipher.decrypt_file(key, file_name, file_name + '.dec') with open(file_name + '.dec') as data_file: my_read = data_file.read() data_file.close() os.remove(file_name + '.dec') try: data = json.loads(my_read) except: return 'Could not add contact\n' if 'seed' in data.keys(): data['contacts'] = contacts else: return 'Could not add contact\n' data_file = open(db_get('default_wallet'), 'w') data_file.write(json.dumps(data)) data_file.close() AESCipher.encrypt_into(key, file_name) db_put('contacts', contacts) return 'Removed contact \nName: ' + temp_contact[0] + '\nAddress: ' + temp_contact[1]
def add_contact(contact): if not valid_address(contact[1]): return 'Address is not valid\n' contacts = db_get('contacts') contact_json = {'name': contact[0], 'address': contact[1]} contacts.append(contact_json) file_name = db_get('default_wallet') key = db_get('default_wallet_key') AESCipher.decrypt_file(key, file_name, file_name + '.dec') with open(file_name + '.dec') as data_file: my_read = data_file.read() data_file.close() os.remove(file_name + '.dec') try: data = json.loads(my_read) except: return 'Could not add contact\n' if 'seed' in data.keys(): data['contacts'] = contacts else: return 'Could not add contact\n' data_file = open(db_get('default_wallet'), 'w') data_file.write(json.dumps(data)) data_file.close() AESCipher.encrypt_into(key, file_name) db_put('contacts', contacts) return 'Added contact \nName: ' + contact[0] + '\nAddress: ' + contact[1]
def read_wallet(file_name, key): wallet = {} wallet['valid'] = False file_name = os.path.join(custom.wallets_dir, str(file_name)) try: open(file_name) except: wallet['message'] = 'Wallet file could not be read' return wallet if not key: wallet['message'] = 'You have to enter a password to open the wallet' return wallet AESCipher.decrypt_file(key, file_name, file_name + '.dec') with open(file_name + '.dec') as data_file: my_read = data_file.read() data_file.close() os.remove(file_name + '.dec') try: data = json.loads(my_read) except: wallet['message'] = 'There is a problem with encryption or wallet file is corrupted!' return wallet if 'seed' not in data: wallet['message'] = 'Wallet file has missing arguments(seed)!' else: wallet['valid'] = True wallet['seed'] = data['seed'] if 'contacts' not in data.keys(): wallet['contacts'] = [] else: wallet['contacts'] = data['contacts'] return wallet