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_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): WalletManager.__check_ont_id(ont_id) private_key = get_random_hex_str(64) salt = get_random_hex_str(16) b64_salt = base64.b64encode(salt.encode('utf-8')).decode('ascii') 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() 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)
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) acct = self.__create_account(label, pwd, salt, pri_key, True) return self.wallet_in_mem.get_account_by_b58_address(acct.get_address_base58())
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(): password = request.json.get('password') label = request.json.get('label') hex_private_key = utils.get_random_hex_str(64) app.config['WALLET_MANAGER'].create_account_from_private_key(password, hex_private_key, label) app.config['WALLET_MANAGER'].save() return json.jsonify({'hex_private_key': hex_private_key})
def test_query_balance(self): sdk.rpc.connect_to_test_net() asset = sdk.native_vm.asset() private_key = utils.get_random_hex_str(64) acct = Account(private_key, SignatureScheme.SHA256withECDSA) b58_address = acct.get_address_base58() try: balance = asset.query_balance('ont', b58_address) self.assertTrue(isinstance(balance, int)) self.assertGreaterEqual(balance, 0) except SDKException as e: self.assertIn('ConnectTimeout', e.args[1]) try: balance = asset.query_balance('ong', b58_address) self.assertTrue(isinstance(balance, int)) self.assertGreaterEqual(balance, 0) except SDKException as e: self.assertIn('ConnectTimeout', e.args[1]) b58_address = acct2.get_address_base58() try: balance = asset.query_balance('ong', b58_address) self.assertTrue(isinstance(balance, int)) self.assertGreaterEqual(balance, 1) except SDKException as e: self.assertIn('ConnectTimeout', e.args[1])
def test_set_default_identity_by_ont_id(self): wm = WalletManager() self.assertRaises(SDKException, wm.open_wallet) wm.create_wallet_file(path) try: wm.open_wallet(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_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_import_identity(self): wm = WalletManager() wm.create_wallet_file(path) wm.open_wallet(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_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 create_identity(): label = request.json.get('label') password = request.json.get('password') hex_private_key = utils.get_random_hex_str(64) try: new_identity = app.config['WALLET_MANAGER'].create_identity_from_private_key(label, password, hex_private_key) except SDKException as e: return json.jsonify({'result': e}), 500 app.config['WALLET_MANAGER'].save() return json.jsonify({'hex_private_key': hex_private_key, 'ont_id': new_identity.ont_id}), 200
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 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 __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_send_raw_transaction_pre_exec(self): sdk = OntologySdk() sdk.rpc.connect_to_test_net() random_pk = get_random_hex_str(64) random_acct = Account(random_pk) b58_address_1 = acct2.get_address_base58() random_b58_address = random_acct.get_address_base58() tx = sdk.native_vm.asset().new_transfer_transaction('ong', b58_address_1, random_b58_address, 2, b58_address_1, 20000, 500) tx.sign_transaction(acct2) result = sdk.rpc.send_raw_transaction_pre_exec(tx) self.assertEqual(result['Result'], '01') self.assertEqual(result['Gas'], 20000) self.assertEqual(result['State'], 1)
def test_send_raw_transaction_pre_exec(self): random_pk = get_random_hex_str(64) random_acct = Account(random_pk) b58_address_1 = acct2.get_address_base58() random_b58_address = random_acct.get_address_base58() tx = sdk.native_vm.asset().new_transfer_transaction('ong', b58_address_1, random_b58_address, 2, b58_address_1, 20000, 500) tx.sign_transaction(acct2) try: result = sdk.rpc.send_raw_transaction_pre_exec(tx) self.assertEqual(result['Result'], '01') self.assertEqual(result['Gas'], 20000) self.assertEqual(result['State'], 1) except SDKException as e: self.assertTrue('ConnectTimeout' in e.args[1])
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 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 add_control_by_hex_private_key(self, ont_id: str, password: str, hex_private_key: str) -> Account: WalletManager.__check_ont_id(ont_id) if not isinstance(password, str): raise SDKException(ErrorCode.require_str_params) if not isinstance(hex_private_key, str): raise SDKException(ErrorCode.require_str_params) salt = get_random_hex_str(16) b64_salt = base64.b64encode(salt.encode('utf-8')).decode('ascii') account = Account(hex_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() 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_create_write(self): wm = WalletManager() wm.create_wallet_file(path) try: wm.open_wallet(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 test_get_random_hex_str(self): try: length = -1 utils.get_random_hex_str(length) except ValueError: raised = True self.assertTrue(raised, 'Exception raised') length = 0 self.assertEqual(len(utils.get_random_hex_str(length)), length) length = 1 self.assertEqual(len(utils.get_random_hex_str(length)), length) length = 64 self.assertEqual(len(utils.get_random_hex_str(length)), length) length = 256 self.assertEqual(len(utils.get_random_hex_str(length)), length) length = 1024 self.assertEqual(len(utils.get_random_hex_str(length)), length) length = 2048 self.assertEqual(len(utils.get_random_hex_str(length)), length)
def test_set_default_identity_by_index(self): wm = WalletManager() wm.create_wallet_file(path) try: wm.open_wallet(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_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)