def create_identity(self, label: str, pwd: 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) return self.__create_identity(label, pwd, salt, pri_key)
def create_account(self, label: str, pwd: str) -> AccountData: """ 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) account = self.__create_account(label, pwd, salt, pri_key, True) return self.wallet_in_mem.get_account_by_address(account.get_address_base58())
def test_export_gcm_encrypted_private_key(self): private_key = util.get_random_bytes(32).hex() account = Account(private_key, SignatureScheme.SHA256withECDSA) b58_address = account.get_address_base58() salt = util.get_random_hex_str(16) password = '******' 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
def test_send_raw_transaction_pre_exec(self): pri_key_1 = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' acct = Account(pri_key_1) pri_key2 = get_random_hex_str(64) acct2 = Account(pri_key2) b58_address_1 = acct.get_address_base58() b58_address_2 = acct2.get_address_base58() tx = Asset.new_transfer_transaction('ont', b58_address_1, b58_address_2, 2, b58_address_1, 20000, 500) tx = sdk.sign_transaction(tx, acct) result = sdk.rpc.send_raw_transaction_pre_exec(tx) self.assertEqual(result, '01')
def test_query_balance(self): rpc_address = 'http://polaris3.ont.io:20336' sdk = OntologySdk() sdk.rpc.set_address(rpc_address) asset = sdk.native_vm().asset() private_key = util.get_random_hex_str(64) acct = Account(private_key, SignatureScheme.SHA256withECDSA) b58_address = acct.get_address_base58() balance = asset.query_balance('ont', b58_address) self.assertTrue(isinstance(balance, int)) self.assertGreaterEqual(balance, 0) balance = asset.query_balance('ong', b58_address) self.assertTrue(isinstance(balance, int)) self.assertGreaterEqual(balance, 0)
def create_account_from_private_key(self, label: str, password: str, private_key: str) -> AccountData or None: """ 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) info = self.create_account_info(label, password, salt, private_key) for index in range(len(self.wallet_in_mem.accounts)): if info.address_base58 == self.wallet_in_mem.accounts[index].address: return self.wallet_in_mem.accounts[index] return None
def test_get_random_hex_str(self): try: length = -1 util.get_random_hex_str(length) except ValueError: raised = True self.assertTrue(raised, 'Exception raised') length = 0 self.assertEqual(len(util.get_random_hex_str(length)), length) length = 1 self.assertEqual(len(util.get_random_hex_str(length)), length) length = 64 self.assertEqual(len(util.get_random_hex_str(length)), length) length = 256 self.assertEqual(len(util.get_random_hex_str(length)), length) length = 1024 self.assertEqual(len(util.get_random_hex_str(length)), length) length = 2048 self.assertEqual(len(util.get_random_hex_str(length)), length)