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_str(64) salt = get_random_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_str(64) salt = get_random_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 create_account(self, label: str, pwd: str) -> AccountData: """ This interface is used to create account by seeting password and label. :param label: str :param pwd: str :return: AccountData """ pri_key = get_random_str(64) salt = get_random_str(16) account = self.__create_account(label, pwd, salt, pri_key, True) return self.wallet_file.get_account_by_address( account.get_address_base58())
def create_account_from_prikey(self, label: str, pwd: str, private_key: str): salt = get_random_str(16) info = self.create_account_info(label, pwd, 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_import_identity(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) private_key = util.get_random_str(64) acct = Account(private_key) password = util.get_random_str(10) salt = util.get_random_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) wm.write_wallet() os.remove(path)
def test_export_gcm_encrypted_private_key(self): private_key = "75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf" account = Account(private_key, SignatureScheme.SHA256withECDSA) salt = util.get_random_str(16) enc_private_key = account.export_gcm_encrypted_private_key( "1", salt, 16384) import_private_key = account.get_gcm_decoded_private_key( enc_private_key, "1", account.get_address_base58(), salt, 16384, SignatureScheme.SHA256withECDSA) self.assertEqual(import_private_key, private_key)
def test_query_balance(self): asset = sdk.native_vm().asset() private_key = util.get_random_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 test_import_identity(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) salt = util.get_random_str(16) private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' acct = Account(private_key) enpri = acct.export_gcm_encrypted_private_key("1", salt, 16384) wm.import_identity("label2", enpri, "1", salt, acct.get_address_base58()) os.remove(path)
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_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_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_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_send_raw_transaction(self): pri_key_1 = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' acct = Account(pri_key_1) length = 64 pri_key_2 = get_random_str(length) acct2 = Account(pri_key_2) 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) tx_hash = sdk.rpc.send_raw_transaction(tx) self.assertEqual(tx_hash, tx.hash256_explorer())
def test_create_write(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) password = util.get_random_str(10) label = 'label' wm.create_account(label, password) default_account = wm.get_default_account() self.assertEqual(label, default_account.label) wm.create_identity(label, password) default_identity = wm.get_default_identity() self.assertEqual(label, default_identity.label) wm.write_wallet() os.remove(path)
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_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_set_default_identity_by_index(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) size = 3 for i in range(size): private_key = util.get_random_str(64) wm.create_identity_from_pri_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) os.remove(path)
def test_get_random_str(self): try: length = -1 util.get_random_str(length) except ValueError: raised = True self.assertTrue(raised, 'Exception raised') length = 0 self.assertEqual(len(util.get_random_str(length)), length) length = 1 self.assertEqual(len(util.get_random_str(length)), length) length = 64 self.assertEqual(len(util.get_random_str(length)), length) length = 256 self.assertEqual(len(util.get_random_str(length)), length) length = 1024 self.assertEqual(len(util.get_random_str(length)), length) length = 2048 self.assertEqual(len(util.get_random_str(length)), length)
def create_account_from_private_key( self, label: str, pwd: 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: str :param pwd: str :param private_key: str :return: AccountData or None """ salt = get_random_str(16) info = self.create_account_info(label, pwd, 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_set_default_identity_by_ont_id(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) password = "******" size = 3 for i in range(size): private_key = util.get_random_str(64) wm.create_identity_from_pri_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 index in range(size * 5): rand_ont_id = random.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) os.remove(path)
def create_identity(self, label: str, pwd: str): priv_key = get_random_str(64) salt = get_random_str(16) return self.__create_identity(label, pwd, salt, priv_key)
def create_account(self, label: str, pwd: str) -> AccountData: pri_key = get_random_str(64) salt = get_random_str(16) account = self.__create_account(label, pwd, salt, pri_key, True) return self.wallet_file.get_account_by_address(account.get_address_base58())
def create_identity_from_pri_key(self, label: str, pwd: str, private_key: str): salt = get_random_str(16) identity = self.__create_identity(label, pwd, salt, private_key) return identity