def test_aes_homomorphic(self): """Make sure AES is homomorphic.""" payload = u'\u66f4\u7a33\u5b9a\u7684\u4ea4\u6613\u5e73\u53f0' password = u'secret' enc = crypto.pw_encode(payload, password) dec = crypto.pw_decode(enc, password) self.assertEqual(dec, payload)
def create_new_wallet(parent: QWidget, initial_dirpath: str) -> Optional[str]: create_filepath, __ = QFileDialog.getSaveFileName(parent, _("Enter a new wallet file name"), initial_dirpath) assert not os.path.exists(create_filepath) dirpath, filename = os.path.split(create_filepath) if not dirpath or not os.path.isdir(dirpath) or not os.access(dirpath, os.R_OK | os.W_OK): MessageBox.show_error(_("The selected directory is not accessible.")) return None name_edit = QLabel(filename) fields = [ (QLabel(_("Wallet") +":"), name_edit), ] from .password_dialog import ChangePasswordDialog, PasswordAction from .wallet_wizard import PASSWORD_NEW_TEXT d = ChangePasswordDialog(parent, PASSWORD_NEW_TEXT, _("Create New Wallet"), fields, kind=PasswordAction.NEW) success, _old_password, new_password = d.run() if not success or not new_password.strip(): return None from electrumsv.storage import WalletStorage storage = WalletStorage(create_filepath) storage.put("password-token", pw_encode(os.urandom(32).hex(), new_password)) storage.close() return create_filepath
def test_import_privkey(self, WIF, pk_string): enc_prvkey_text = pw_encode(WIF, "password") public_key = PrivateKey.from_text(WIF).public_key d = Imported_KeyStore({}) d.import_private_key(1, public_key, enc_prvkey_text) assert d.get_public_key_for_id(1) == public_key assert WIF == d.export_private_key(public_key, "password")
def test_aes_decode_with_invalid_password(self): """pw_decode raises an Exception when supplied an invalid password.""" payload = u"blah" password = u"uber secret" wrong_password = u"not the password" enc = crypto.pw_encode(payload, password) with self.assertRaises(InvalidPassword): crypto.pw_decode(enc, wrong_password)
def test_get_private_key(self): xprv = ('xprv9s21ZrQH143K4XLpSd2berkCzJTXDv68rusDQFiQGSqa1ZmVXnYzYpTQ9' 'qYiSB7mHvg6kEsrd2ZtnHRJ61sZhSN4jZ2T8wxA4T75BE4QQZ1') password = '******' xprv = pw_encode(xprv, password) keystore = BIP32_KeyStore({'xprv': xprv}) privkey = keystore.get_private_key((1, 2, 3), password) assert privkey == (bytes.fromhex('985e4b09a0b05702c073b5086fcbb4b7dde4625bb98' '9ec51ce4c3337a7de2a13'), True)
def test_decrypt_message(self): password = '******' message = b'BitcoinSV' prvkey_text = "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ" enc_prvkey_text = pw_encode(prvkey_text, password) public_key = PrivateKey.from_text(prvkey_text).public_key d = Imported_KeyStore({}) d.import_private_key(1, public_key, enc_prvkey_text) enc_msg = ('QklFMQNkonLnVmRMF3dl+P0rHSbM4lvDPmnE2CFcD+98gGsOe6qtKtmVbCg4' '9bxmT6vfmzl7udrvT81wH1Ri7wZItndtLiNHii6FBNVzoSV/1ZqN3w==') decrypted_message = d.decrypt_message(public_key, enc_msg, password) assert decrypted_message == message
def test_check_password(self, password): xprv = ('xprv9s21ZrQH143K4XLpSd2berkCzJTXDv68rusDQFiQGSqa1ZmVXnYzYpTQ9' 'qYiSB7mHvg6kEsrd2ZtnHRJ61sZhSN4jZ2T8wxA4T75BE4QQZ1') xpub = ('xpub661MyMwAqRbcH1RHYeZc1zgwYLJ1dNozE8npCe81pnNYtN6e5KsF6cmt17Fv8w' 'GvJrRiv6Kewm8ggBG6N3XajhoioH3stUmLRi53tk46CiA') xprv = pw_encode(xprv, password) keystore = BIP32_KeyStore({'xprv': xprv, 'xpub': xpub}) keystore.check_password(password) with pytest.raises(InvalidPassword): keystore.check_password('guess') if password is not None: with pytest.raises(InvalidPassword): keystore.check_password(None)
def validatePage(self) -> bool: # Called when 'Next' or 'Finish' is clicked for last-minute validation. result = self.isComplete() if result: new_password = self._password_layout.new_pw.text().strip() # If we are going to exit then create the empty wallet. wizard: WalletWizard = self.wizard() wallet_filepath = wizard.get_wallet_path() storage = WalletStorage(wallet_filepath) storage.put("password-token", pw_encode(os.urandom(32).hex(), new_password)) storage.close() return result
def test_sign_message(self): password = '******' message = 'BitcoinSV' prvkey_text = "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ" enc_prvkey_text = pw_encode(prvkey_text, password) public_key = PrivateKey.from_text(prvkey_text).public_key d = Imported_KeyStore({}) d.import_private_key(1, public_key, enc_prvkey_text) msg_sig = d.sign_message(public_key, message, password) assert msg_sig.hex() == ( '1c26a18cb236e54bbe7e3db56639ef5cbefcf5a2e28850cdd304970832f84031' 'fc073bed1a151f0510e5558a22d23f16ed8032a1b74ffcac05227c053e1a1d8af5' )
def test_aes_encode_without_password(self): """When not passed a password, pw_encode is noop on the payload.""" payload = u'\u66f4\u7a33\u5b9a\u7684\u4ea4\u6613\u5e73\u53f0' enc = crypto.pw_encode(payload, None) self.assertEqual(payload, enc)