def get_private_key_from_qr_code(qr_code: str, password: str): data = json.loads(qr_code) key = data['key'] address = data['address'] salt = data['salt'] n = json.loads(data['scrypt'])['n'] acct = Account.get_gcm_decoded_private_key( key, password, address, base64.b64decode(salt), int(n), SignatureScheme.SHA256withECDSA) return acct
def get_account_by_b58_address(self, b58_address: str, password: str) -> Account: """ :param b58_address: a base58 encode address. :param password: a password which is used to decrypt the encrypted private key. :return: """ acct = self.get_account_data_by_b58_address(b58_address) n = self.wallet_in_mem.scrypt.n salt = base64.b64decode(acct.salt) private_key = Account.get_gcm_decoded_private_key(acct.key, password, b58_address, salt, n, self.scheme) return Account(private_key, self.scheme)
def test_get_gcm_decoded_private_key(self): encrypted_key_str = 'hhWuyE1jjKjBOT7T5Rlrea3ewJIR8i6UjTv67bnkHz5YsqgeCfXjrHJTBGQtE0bG' b58_address = 'AMeJEzSMSNMZThqGoxBVVFwKsGXpAdVriS' salt = base64.b64decode('GXIs0bRy50tEfFuCF/h/yA==') n = 4096 private_key = Account.get_gcm_decoded_private_key( encrypted_key_str, password, b58_address, salt, n, SignatureScheme.SHA256withECDSA) acct = Account(private_key) export_key = acct.export_gcm_encrypted_private_key(password, salt, n) self.assertEqual(encrypted_key_str, export_key)
def test_export_gcm_encrypted_private_key(self): private_key = utils.get_random_bytes(32).hex() account = Account(private_key, SignatureScheme.SHA256withECDSA) b58_address = account.get_address_base58() salt = utils.get_random_hex_str(16) enc_private_key = account.export_gcm_encrypted_private_key( password, salt, 16384) decoded_private_key = account.get_gcm_decoded_private_key( enc_private_key, password, b58_address, salt, 16384, SignatureScheme.SHA256withECDSA) self.assertEqual(private_key, decoded_private_key)
def test_export_and_get_gcm_decoded_private_key(self): hex_private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' salt = base64.b64decode( 'pwLIUKAf2bAbTseH/WYrfQ=='.encode('ascii')).decode('latin-1') account = Account(hex_private_key, SignatureScheme.SHA256withECDSA) b58_address = account.get_address_base58() n = 16384 enc_private_key = account.export_gcm_encrypted_private_key( password, salt, n) decoded_private_key = Account.get_gcm_decoded_private_key( enc_private_key, password, b58_address, salt, n, SignatureScheme.SHA256withECDSA) self.assertEqual(hex_private_key, decoded_private_key)
def get_account_by_ont_id(self, ont_id: str, password: str) -> Account: """ :param ont_id: OntId. :param password: a password which is used to decrypt the encrypted private key. :return: """ WalletManager.__check_ont_id(ont_id) for identity in self.wallet_in_mem.identities: if identity.ont_id == ont_id: addr = identity.ont_id.replace(DID_ONT, "") key = identity.controls[0].key salt = base64.b64decode(identity.controls[0].salt) n = self.wallet_in_mem.scrypt.n private_key = Account.get_gcm_decoded_private_key(key, password, addr, salt, n, self.scheme) return Account(private_key, self.scheme) raise SDKException(ErrorCode.other_error(f'Get account {ont_id} failed.'))
def import_identity(self, label: str, encrypted_pri_key: str, pwd: str, salt: str, b58_address: str) -> Identity: """ This interface is used to import identity by providing encrypted private key, password, salt and base58 encode address which should be correspond to the encrypted private key provided. :param label: a label for identity. :param encrypted_pri_key: an encrypted private key in base64 encoding from. :param pwd: a password which is used to encrypt and decrypt the private key. :param salt: a salt value which will be used in the process of encrypt private key. :param b58_address: a base58 encode address which correspond with the encrypted private key provided. :return: if succeed, an Identity object will be returned. """ scrypt_n = Scrypt().n pri_key = Account.get_gcm_decoded_private_key(encrypted_pri_key, pwd, b58_address, salt, scrypt_n, self.scheme) info = self.__create_identity(label, pwd, salt, pri_key) for identity in self.wallet_in_mem.identities: if identity.ont_id == info.ont_id: return identity raise SDKException(ErrorCode.other_error('Import identity failed.'))
def import_account(self, label: str, encrypted_pri_key: str, pwd: str, b58_address: str, b64_salt: str, n: int = 16384) -> AccountData: """ This interface is used to import account by providing account data. :param label: str, wallet label :param encrypted_pri_key: str, an encrypted private key in base64 encoding from :param pwd: str, a password which is used to encrypt and decrypt the private key :param b58_address: str, a base58 encode wallet address value :param b64_salt: str, a base64 encode salt value which is used in the encryption of private key :param n: int, CPU/Memory cost parameter. It must be a power of 2 and less than :math:`2^{32}` :return: if succeed, return an data structure which contain the information of a wallet account. if failed, return a None object. """ salt = base64.b64decode(b64_salt.encode('ascii')).decode('latin-1') private_key = Account.get_gcm_decoded_private_key(encrypted_pri_key, pwd, b58_address, salt, n, self.scheme) acct_info = self.create_account_info(label, pwd, salt, private_key) for acct in self.wallet_in_mem.accounts: if not isinstance(acct, AccountData): raise SDKException(ErrorCode.other_error('Invalid account data in memory.')) if acct_info.address_base58 == acct.b58_address: return acct raise SDKException(ErrorCode.other_error('Import account failed.'))
def get_default_account(self, password: str) -> Account: acct = self.get_default_account_data() salt = base64.b64decode(acct.salt) n = self.wallet_in_mem.scrypt.n private_key = Account.get_gcm_decoded_private_key(acct.key, password, acct.b58_address, salt, n, self.scheme) return Account(private_key, self.scheme)
def __get_control_account(self, ctrl_info: Control, password: str) -> Account: salt = base64.b64decode(ctrl_info.salt) n = self.wallet_in_mem.scrypt.n private_key = Account.get_gcm_decoded_private_key(ctrl_info.key, password, ctrl_info.b58_address, salt, n, self.scheme) return Account(private_key, self.scheme)