async def wallet_representative_set(wallet_id: str, rep: str, update_existing: bool = False): # Retrieve wallet # Retrieve wallet crypt = None password=None if not Validators.is_valid_address(rep): print("Invalid representative") exit(1) try: wallet = await Wallet.get_wallet(wallet_id) except WalletNotFound: print(f"No wallet found with ID: {wallet_id}") exit(1) except WalletLocked as wl: wallet = wl.wallet if update_existing: while True: try: npass = getpass.getpass(prompt='Enter current password to decrypt wallet:') crypt = AESCrypt(npass) try: decrypted = crypt.decrypt(wl.wallet.seed) wallet = wl.wallet wallet.seed = decrypted password=npass except DecryptionError: print("**Invalid password**") except KeyboardInterrupt: break exit(0) wallet.representative = rep await wallet.save(update_fields=['representative']) await wallet.bulk_representative_update(rep) print(f"Representative changed")
async def unlock_wallet(self, password: str): """Unlock wallet with given password, raise DecryptionError if invalid password""" crypt = AESCrypt(password) decrypted = crypt.decrypt(self.seed) # Store decrypted wallet in memory SeedStorage.instance().set_decrypted_seed(self.id, decrypted) # Decrypt any ad-hoc accounts for a in await self.adhoc_accounts.all(): SeedStorage.instance().set_decrypted_seed( f"{self.id}:{a.address}", crypt.decrypt(a.private_key))
class TestAESCrypt(unittest.TestCase): def setUp(self): self.crypt1 = AESCrypt('mypassword') self.crypt2 = AESCrypt('someotherpassword') def test_encrypt_decrypt(self): """Test encryption and decryption""" some_string = "Some Random String to Encrypt" encrypted = self.crypt1.encrypt(some_string) self.assertNotEqual(encrypted, some_string) self.assertEqual(self.crypt1.decrypt(encrypted), some_string) with self.assertRaises(DecryptionError) as exc: self.crypt2.decrypt(encrypted)
async def wallet_change_seed(wallet_id: str, seed: str, password: str) -> str: encrypt = False old_password = None if len(password) > 0: encrypt = True # Retrieve wallet try: wallet = await Wallet.get_wallet(wallet_id) except WalletNotFound: print(f"No wallet found with ID: {wallet_id}") exit(1) except WalletLocked as wl: wallet = wl.wallet while True: try: npass = getpass.getpass(prompt='Enter current password:'******'seed', 'encrypted']) for a in await wallet.adhoc_accounts.all(): a.private_key = crypt.decrypt(a.private_key) await a.save(using_db=conn, update_fields=['private_key']) old_password = npass break except DecryptionError: print("**Invalid password**") except KeyboardInterrupt: break exit(0) # Change key await wallet.change_seed(seed) # Encrypt if necessary if encrypt: await wallet.encrypt_wallet(password) # Get newest account newest = await wallet.get_newest_account() print(f"Seed changed for wallet {wallet.id}\nFirst account: {newest.address}")
async def account_create(wallet_id: str, key: str, count: int = 1) -> str: # Retrieve wallet crypt = None password=None if count is None: count = 1 try: wallet = await Wallet.get_wallet(wallet_id) except WalletNotFound: print(f"No wallet found with ID: {wallet_id}") exit(1) except WalletLocked as wl: wallet = wl.wallet if key is not None: while True: try: npass = getpass.getpass(prompt='Enter current password to encrypt ad-hoc key:') crypt = AESCrypt(npass) try: decrypted = crypt.decrypt(wl.wallet.seed) wallet = wl.wallet wallet.seed = decrypted password=npass except DecryptionError: print("**Invalid password**") except KeyboardInterrupt: break exit(0) if key is None: if count == 1: a = await wallet.account_create() print(f"account: {a}") else: async with in_transaction() as conn: ass = await wallet.accounts_create(count=count) for a in ass: print(f"account: {a}") else: a = await wallet.adhoc_account_create(key, password=password) print(f"account: {a}")
async def encrypt_wallet(self, password: str): """Encrypt wallet seed with password""" async with in_transaction() as conn: # If password is empty string then decrypted wallet if len(password.strip()) == 0: self.encrypted = False for a in await self.adhoc_accounts.all(): decrypted = SeedStorage.instnace().get_decrypted_seed( f"{self.id}:{a.address}") if decrypted is not None: a.private_key = decrypted await a.save(using_db=conn, update_fields=['private_key']) else: crypt = AESCrypt(password) encrypted = crypt.encrypt(self.seed) self.seed = encrypted self.encrypted = True for a in await self.adhoc_accounts.all(): a.private_key = crypt.encrypt(a.private_key_get()) await a.save(using_db=conn, update_fields=['private_key']) await self.save(using_db=conn, update_fields=['seed', 'encrypted'])
async def adhoc_account_create(self, key: str, password: str = None) -> str: """Add an adhoc private key to the wallet, raise AccountAlreadyExists if it already exists""" pubkey = nanopy.ed25519_blake2b.publickey(bytes.fromhex(key)).hex() address = nanopy.account_get(pubkey) # See if address already exists a = await self.accounts.filter(address=address).first() if a is None: a = await self.adhoc_accounts.filter(address=address).first() if a is not None: raise AccountAlreadyExists(a) # Create it crypt = None if password is not None: crypt = AESCrypt(password) a = adhoc_acct.AdHocAccount( wallet=self, private_key=crypt.encrypt(key) if crypt is not None else key, address=address) await a.save() return address
async def wallet_view_seed(wallet_id: str, password: str, all_keys: bool) -> str: # Retrieve wallet crypt = None try: wallet = await Wallet.get_wallet(wallet_id) except WalletNotFound: print(f"No wallet found with ID: {wallet_id}") exit(1) except WalletLocked as wl: wallet = None if password is not None: crypt = AESCrypt(password) try: decrypted = crypt.decrypt(wl.wallet.seed) wallet = wl.wallet wallet.seed = decrypted except DecryptionError: pass if wallet is None: while True: try: npass = getpass.getpass(prompt='Enter current password:') crypt = AESCrypt(npass) try: decrypted = crypt.decrypt(wl.wallet.seed) wallet = wl.wallet wallet.seed = decrypted except DecryptionError: print("**Invalid password**") except KeyboardInterrupt: break exit(0) print(f"Seed: {wallet.seed}") if all_keys: for a in await wallet.accounts.all(): print(f"Addr: {a.address} PrivKey: {nanopy.deterministic_key(wallet.seed, index=a.account_index)[0].upper()}") else: print(f"AdHoc accounts:") for a in await wallet.adhoc_accounts.all(): if not wallet.encrypted: print(f"Addr: {a.address} PrivKey: {a.private_key.upper()}") else: print(f"Addr: {a.address} PrivKey: {crypt.decrypt(a.private_key)}")
def setUp(self): self.crypt1 = AESCrypt('mypassword') self.crypt2 = AESCrypt('someotherpassword')