def create_identity(self, pwd: str, label: str = '') -> Identity: """ :param label: a label for identity. :param pwd: a password which will be used to encrypt and decrypt the private key. :return: if succeed, an Identity object will be returned. """ pri_key = get_random_hex_str(64) salt = get_random_hex_str(16) if len(label) == 0 or label is None: label = uuid.uuid4().hex[0:8] return self.__create_identity(label, pwd, salt, pri_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 create_account(self, pwd: str, label: str = '') -> Account: """ This interface is used to create account based on given password and label. :param label: a label for account. :param pwd: a password which will be used to encrypt and decrypt the private key :return: if succeed, return an data structure which contain the information of a wallet account. """ pri_key = get_random_hex_str(64) salt = get_random_hex_str(16) if len(label) == 0 or label is None: label = uuid.uuid4().hex[0:8] acct = self.__create_account(label, pwd, salt, pri_key, True) return self.get_account_by_b58_address(acct.get_address_base58(), pwd)
def create_account_from_wif(self, wif: str, password: str, label: str = '') -> Account: private_key = Account.get_private_key_from_wif(wif).hex() salt = get_random_hex_str(16) if len(label) == 0 or label is None: label = uuid.uuid4().hex[0:8] info = self.create_account_info(label, password, salt, private_key) return self.get_account_by_b58_address(info.address_base58, password)
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 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 create_identity_from_private_key(self, label: str, pwd: str, private_key: str) -> Identity: """ This interface is used to create identity based on given label, password and private key. :param label: a label for identity. :param pwd: a password which will be used to encrypt and decrypt the private key. :param private_key: a private key in the form of string. :return: if succeed, an Identity object will be returned. """ salt = get_random_hex_str(16) identity = self.__create_identity(label, pwd, salt, private_key) return identity
async def test_send_raw_transaction_pre_exec(self): random_pk = get_random_hex_str(64) random_acct = Account(random_pk) b58_from_address = acct2.get_address_base58() rand_to_address = random_acct.get_address_base58() tx = sdk.native_vm.ong().new_transfer_tx(b58_from_address, rand_to_address, 2, b58_from_address, 500, 20000) tx.sign_transaction(acct2) result = await sdk.aio_rpc.send_raw_transaction_pre_exec(tx) self.assertEqual(result['Result'], '01') self.assertEqual(result['Gas'], 20000) self.assertEqual(result['State'], 1)
def test_create_write(self): wm = WalletManager() wm.create_wallet_file(self.path) try: wm.open_wallet(self.path) random_password = utils.get_random_hex_str(10) label = 'label' wm.create_account(random_password, label) default_account = wm.get_default_account_data() self.assertEqual(label, default_account.label) wm.create_identity(random_password, label) default_identity = wm.get_default_identity() self.assertEqual(default_identity.label, label) wm.write_wallet() finally: wm.del_wallet_file()
def create_account_from_private_key(self, password: str, private_key: str, label: str = '') -> AccountData: """ This interface is used to create account by providing an encrypted private key and it's decrypt password. :param label: a label for account. :param password: a password which is used to decrypt the encrypted private key. :param private_key: a private key in the form of string. :return: if succeed, return an AccountData object. if failed, return a None object. """ salt = get_random_hex_str(16) if len(label) == 0 or label is None: label = uuid.uuid4().hex[0:8] info = self.create_account_info(label, password, salt, private_key) for acct in self.wallet_in_mem.accounts: if info.address_base58 == acct.b58_address: return acct raise SDKException(ErrorCode.other_error(f'Create account from key {private_key} failed.'))
def test_set_default_identity_by_index(self): wm = WalletManager() wm.create_wallet_file(self.path) try: wm.open_wallet(self.path) size = 3 for i in range(size): private_key = utils.get_random_hex_str(64) wm.create_identity_from_private_key("ide", str(i), private_key) identities = wm.get_wallet().get_identities() self.assertEqual(len(identities), size) self.assertRaises(SDKException, wm.get_wallet().set_default_identity_by_index, size) for index in range(size): wm.get_wallet().set_default_identity_by_index(index) default_identity = wm.get_default_identity() self.assertEqual(identities[index], default_identity) finally: wm.del_wallet_file()
def test_set_default_identity_by_ont_id(self): wm = WalletManager() wm.create_wallet_file(self.path) try: wm.open_wallet(self.path) size = 3 for i in range(size): private_key = utils.get_random_hex_str(64) wm.create_identity_from_private_key("ide", str(i), private_key) identities = wm.get_wallet().get_identities() self.assertEqual(len(identities), size) self.assertRaises(SDKException, wm.get_wallet().set_default_identity_by_ont_id, '') ont_id_list = list() for identity in wm.get_wallet().identities: ont_id_list.append(identity.ont_id) for _ in range(size * 5): rand_ont_id = choice(ont_id_list) wm.get_wallet().set_default_identity_by_ont_id(rand_ont_id) default_identity = wm.get_default_identity() self.assertEqual(rand_ont_id, default_identity.ont_id) finally: wm.del_wallet_file()
def test_get_random_hex_str(self): self.assertRaises(ValueError, utils.get_random_hex_str, -1) len_list = [0, 1, 64, 256, 1024, 2048] for length in len_list: self.assertEqual(len(utils.get_random_hex_str(length)), length)