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 __add_control(self, ont_id: str, password: str, private_key: str = '', salt: str = '') -> Account: if len(private_key) == 0: private_key = get_random_hex_str(64) if len(salt) == 0: salt = get_random_hex_str(16) account = Account(private_key, self.scheme) key = account.export_gcm_encrypted_private_key(password, salt) b58_address = account.get_address_base58() public_key = account.get_public_key_hex() b64_salt = base64.b64encode(salt.encode('utf-8')).decode('ascii') ctrl = Control(kid='', key=key, salt=b64_salt, address=b58_address, public_key=public_key) identity = self.get_identity_by_ont_id(ont_id) identity.add_control(ctrl) return account
def test_import_identity(self): wm = WalletManager() wm.create_wallet_file(self.path) wm.open_wallet(self.path) try: private_key = utils.get_random_hex_str(64) acct = Account(private_key) salt = utils.get_random_hex_str(16) scrypt_n = 16384 encrypted_private_key = acct.export_gcm_encrypted_private_key( password, salt, scrypt_n) label = 'label' b58_address = acct.get_address_base58() wm.import_identity(label, encrypted_private_key, password, salt, b58_address) identity = wm.get_default_identity() self.assertEqual(label, identity.label) finally: wm.del_wallet_file()
def __create_account(self, label: str, pwd: str, salt: str, private_key: str, account_flag: bool) -> Account: account = Account(private_key, self.scheme) if self.scheme == SignatureScheme.SHA256withECDSA: acct_data = AccountData() else: raise SDKException(ErrorCode.other_error('Scheme type is error.')) if pwd is not None: acct_data.key = account.export_gcm_encrypted_private_key(pwd, salt) else: acct_data.key = account.get_private_key_hex() acct_data.b58_address = account.get_address_base58() if len(label) == 0 or label is None: label = uuid.uuid4().hex[0:8] if account_flag: for memory_acct in self.wallet_in_mem.accounts: if memory_acct.b58_address == account.get_address_base58(): raise SDKException(ErrorCode.other_error('Wallet account exists.')) if len(self.wallet_in_mem.accounts) == 0: acct_data.is_default = True self.wallet_in_mem.default_account_address = acct_data.b58_address acct_data.label = label acct_data.salt = base64.b64encode(salt.encode('latin-1')).decode('ascii') acct_data.public_key = account.get_public_key_hex() self.wallet_in_mem.accounts.append(acct_data) else: for identity in self.wallet_in_mem.identities: if identity.ont_id == DID_ONT + acct_data.b58_address: raise SDKException(ErrorCode.other_error('Wallet identity exists.')) idt = Identity() idt.ont_id = DID_ONT + acct_data.b58_address idt.label = label if len(self.wallet_in_mem.identities) == 0: idt.is_default = True self.wallet_in_mem.default_ont_id = idt.ont_id ctl = Control(kid='keys-1', key=acct_data.key, salt=base64.b64encode(salt.encode()).decode('ascii'), address=acct_data.b58_address, public_key=account.get_public_key_hex()) idt.controls.append(ctl) self.wallet_in_mem.identities.append(idt) return account