def encrypt(self, password:str='', current_password:str=None): """Encrypt - or re-encrypt """ if len(self._addresses) <= 0: raise RuntimeWarning("Can't encrypt empty wallet.") if self._locked: raise RuntimeWarning("Can't encrypt locked down wallet.") if self._infos['encrypted']: # TODO raise RuntimeWarning("TODO: decrypt and re-encrypt - WIP") encrypted_addresses = [] try: for address in self._addresses: content = json.dumps(address) encrypted_addresses.append(b64encode(encrypt(password, content, level=1)).decode('utf-8')) encrypted = b64encode(encrypt(password, json.dumps(self._data['spend']), level=2)).decode('utf-8') self._data['addresses'] = encrypted_addresses self._data['encrypted'] = True self._data['spend'] = encrypted self.save() self._master_password = password self._infos['encrypted'] = True self._locked = False except: raise
def set_spend(self, spend_type:str, spend_value: str, password: str=''): """Saves the spend protection if the pass is ok""" if not self.password_ok(password): raise RuntimeWarning("Password does not seem to match") if spend_type == 'None': spend_type = None spend = {'type': spend_type, 'value': spend_value} if self._infos['encrypted']: content = json.dumps(spend) encrypted = b64encode(encrypt(self._master_password, content, level=2)).decode('utf-8') self._data['spend'] = encrypted else: self._data['spend'] = spend self._infos['spend'] = spend self.save()
def set_label(self, address:str ='', label: str=''): """ Set a label for given address """ if self._infos['encrypted'] and self._locked: raise RuntimeError("Wallet must be unlocked") #print(self._data['addresses']) for i, single_address in enumerate(self._addresses): if single_address['address'] == address: # self._addresses[i]['label'] = label if self._infos['encrypted'] and self._master_password: content = json.dumps(self._addresses[i]) encrypted = b64encode(encrypt(self._master_password, content, level=1)).decode('utf-8') self._data['addresses'][i] = encrypted else: self._data['addresses'][i]['label'] = label self.save()
def import_der(self, wallet_der: str='wallet.der', label: str='', source_password: str=''): """Import an existing wallet.der like file into the wallet""" if self._infos['encrypted'] and self._locked: # TODO: check could be done via a decorator raise RuntimeError("Wallet must be unlocked") key = self.get_der_key(wallet_der, password=source_password) if not key: raise RuntimeWarning("Error importing the der file") key['label'] = label if self.is_address_in_wallet(key['address']): raise RuntimeError("Duplicate address") self._addresses.append(key) if self._infos['encrypted']: content = json.dumps(key) encrypted = b64encode(encrypt(self._master_password, content, level=1)).decode('utf-8') self._data['addresses'].append(encrypted) else: print('1') self._data['addresses'].append(key) self.save()
def new_address(self, label: str='', password: str='', salt: str='', type="RSA"): """ Add a new address to the wallet (and save it) """ if type != "RSA": raise RuntimeError("Only RSA is available for now") if self._infos['encrypted'] and self._locked: raise RuntimeError("Wallet must be unlocked") keys = bismuthcrypto.keys_gen(password=password, salt=salt) keys['label'] = label keys['timestamp'] = int(time()) self._addresses.append(keys) if self._infos['encrypted']: content = json.dumps(keys) encrypted = b64encode(encrypt(self._master_password, content, level=1)).decode('utf-8') self._data['addresses'].append(encrypted) else: print('1') self._data['addresses'].append(keys) self.save()