def test_create_account_from_wif(self): wm = WalletManager() wif = 'L1eCFtiZH2ZU6KjTR9MR14wfTEHnGGGoxSuRB2TUXRqoGwa7NAjN' acct = wm.create_account_from_wif(wif, password) self.assertTrue(isinstance(acct, Account)) self.assertEqual('AdqppUTygLd2ws9n6u4NJ1pRJtxmBse2pv', acct.get_address_base58())
def test_wallet_data(self): wm = WalletManager() ont_path = os.path.join(os.path.dirname(__file__), 'wallet.json') self.assertRaises(SDKException, wm.create_wallet_file, ont_path) wm.open_wallet(ont_path) self.assertTrue(isinstance(wm, WalletManager)) self.assertEqual(wm.__dict__['scheme'], SignatureScheme.SHA256withECDSA) size = 32 dict_wallet_in_mem = wm.wallet_in_mem.__dict__ self.assertEqual(dict_wallet_in_mem['name'], 'MyWallet') self.assertEqual(dict_wallet_in_mem['version'], '1.1') self.assertEqual(len(dict_wallet_in_mem['accounts']), size) dict_accounts = dict(wm.wallet_in_mem.accounts[0]) self.assertEqual(dict_accounts['address'], 'Ad4pjz2bqep4RhQrUAzMuZJkBC3qJ1tZuT') self.assertEqual(dict_accounts['algorithm'], 'ECDSA') self.assertEqual(dict_accounts['enc-alg'], 'aes-256-gcm') self.assertEqual(dict_accounts['isDefault'], True) self.assertEqual( dict_accounts['key'], 'O6/Ens58XsV4+TqbKIZ5qgM76pTC0KsufNYV3VKDmHtG6VFvDZUblVWSAM6XBwKk') self.assertEqual(dict_accounts['label'], '') self.assertEqual(dict_accounts['lock'], False) self.assertEqual(dict_accounts['parameters']['curve'], 'P-256') self.assertEqual(dict_accounts['salt'], 'OkX96EG0OaCNUFD3hdc50Q==') self.assertEqual(dict_accounts['signatureScheme'], 'SHA256withECDSA') dict_accounts = dict(wm.wallet_in_mem.accounts[15]) self.assertEqual(dict_accounts['address'], 'AZy1ApV47jLM4m4a2MSx92hzwpDcMtn96z') self.assertEqual(dict_accounts['enc-alg'], 'aes-256-gcm') self.assertEqual(dict_accounts['isDefault'], False) self.assertEqual( dict_accounts['key'], 'ATqEeReytF1Ma16KJKWlvnSmHeH7p8l5Es3Ngp/62l/1Pp4K4fhAaXOfahZ6g8Wd') self.assertEqual(dict_accounts['label'], '4c0638c9') self.assertEqual(dict_accounts['lock'], False) self.assertEqual(dict_accounts['parameters']['curve'], 'P-256') self.assertEqual(dict_accounts['salt'], 'tJHCzvar3e5dPkIyXswx5w==') self.assertEqual(dict_accounts['signatureScheme'], 'SHA256withECDSA') dict_accounts = dict(wm.wallet_in_mem.accounts[31]) self.assertEqual(dict_accounts['address'], 'Aa4diLddFtHg5fU7Nf71q3KwBmu21D4ZyM') self.assertEqual(dict_accounts['enc-alg'], 'aes-256-gcm') self.assertEqual(dict_accounts['isDefault'], False) self.assertEqual( dict_accounts['key'], 'epWSyUe9VUI81WIe30ul2B/ahjHhc1sXRg7IJjV2jk39BPzkrMbIa5p9UOOrAZ3e') self.assertEqual(dict_accounts['label'], '') self.assertEqual(dict_accounts['lock'], False) self.assertEqual(dict_accounts['parameters']['curve'], 'P-256') self.assertEqual(dict_accounts['salt'], 'DqS/T2FmSmYabWdW1vQYzQ==') self.assertEqual(dict_accounts['signatureScheme'], 'SHA256withECDSA')
def test_open_cyano_wallet(self): wm = WalletManager() cyano_path = os.path.join(os.path.dirname(__file__), 'cyano_wallet.json') wm.open_wallet(cyano_path, is_create=False) self.assertEqual(wm.__dict__['scheme'], SignatureScheme.SHA256withECDSA) account = wm.get_account_by_b58_address( 'ANH5bHrrt111XwNEnuPZj6u95Dd6u7G4D6', '1234567890') self.assertTrue(isinstance(account, Account))
def test_create_identity_from_pri_key(self): wm = WalletManager() wm.create_wallet_file(self.path) try: wm.open_wallet(self.path) private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' ide = wm.create_identity_from_private_key("ide", "1", private_key) self.assertEqual(ide.label, 'ide') self.assertEqual(ide.ont_id, 'did:dna:AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve') finally: wm.del_wallet_file()
def __init__(self, rpc_address: str = '', restful_address: str = '', ws_address: str = '', default_signature_scheme: SignatureScheme = SignatureScheme.SHA256withECDSA): if not isinstance(default_signature_scheme, SignatureScheme): raise SDKException(ErrorCode.param_err('SignatureScheme object is required.')) self.__rpc = Rpc(rpc_address) self.__aio_rpc = AioRpc(rpc_address) self.__restful = Restful(restful_address) self.__aio_restful = AioRestful(restful_address) self.__websocket = Websocket(ws_address) self.__default_network = self.__rpc self.__default_aio_network = self.__aio_rpc self.__native_vm = NativeVm(self) self.__neo_vm = NeoVm(self) self.__wasm_vm = WasmVm(self) self.__service = Service(self) self.__wallet_manager = WalletManager() self.__default_signature_scheme = default_signature_scheme
def test_open_wallet(self): wm = WalletManager() self.assertRaises(SDKException, wm.open_wallet) wm.create_wallet_file(self.path) try: wm.open_wallet(self.path) self.assertEqual(wm.__dict__['scheme'], SignatureScheme.SHA256withECDSA) finally: wm.del_wallet_file()
def test_del_account_by_b58_address(self): wm = WalletManager() b58_addr_lst = list() count = 0 for _ in range(5): acct = wm.create_account(password) b58_addr_lst.append(acct.get_address_base58()) count += 1 self.assertEqual(count, wm.get_account_count()) for _ in range(5): wm.del_account_by_b58_address(b58_addr_lst.pop(-1)) wm.get_acct_data_list() count -= 1 self.assertEqual(count, wm.get_account_count())
def test_get_account(self): wallet_manager = WalletManager() acct0 = wallet_manager.create_account(password) self.assertTrue(isinstance(acct0, Account)) b58_address = wallet_manager.wallet_in_mem.default_account_address acct0 = wallet_manager.get_account_by_b58_address( b58_address, password) self.assertEqual(acct0.get_address_base58(), b58_address) self.assertRaises(SDKException, wallet_manager.get_account_by_b58_address, b58_address, 'wrong_password') base64_salt = 'S2JpQ1VyNTNDWlVmS0cxTTNHb2pqdz09' b58_address = 'AHX1wzvdw9Yipk7E9MuLY4GGX4Ym9tHeDe' encrypted_private_key = 'nw7qMrOEDsNurW3dKBruv3iNGeoZppSKe06QoqMZ9S8msoCvtn864rCSvAbgk1oS' label = 'label' acct1 = wallet_manager.import_account(label, encrypted_private_key, password, b58_address, base64_salt) self.assertEqual(b58_address, acct1.b58_address) import_acct = wallet_manager.get_account_by_b58_address( b58_address, password) self.assertEqual(b58_address, import_acct.get_address_base58()) self.assertEqual(base64_salt, acct1.salt)
class DNA(AioRunner, metaclass=_Singleton): def __init__(self, rpc_address: str = '', restful_address: str = '', ws_address: str = '', default_signature_scheme: SignatureScheme = SignatureScheme.SHA256withECDSA): if not isinstance(default_signature_scheme, SignatureScheme): raise SDKException(ErrorCode.param_err('SignatureScheme object is required.')) self.__rpc = Rpc(rpc_address) self.__aio_rpc = AioRpc(rpc_address) self.__restful = Restful(restful_address) self.__aio_restful = AioRestful(restful_address) self.__websocket = Websocket(ws_address) self.__default_network = self.__rpc self.__default_aio_network = self.__aio_rpc self.__native_vm = NativeVm(self) self.__neo_vm = NeoVm(self) self.__wasm_vm = WasmVm(self) self.__service = Service(self) self.__wallet_manager = WalletManager() self.__default_signature_scheme = default_signature_scheme @property def default_network(self): return self.__default_network @default_network.setter def default_network(self, network: Union[Rpc, Restful]): self.__default_network = network @property def default_aio_network(self): return self.__default_aio_network @default_aio_network.setter def default_aio_network(self, network: Union[AioRpc, AioRestful, Websocket]): self.__default_aio_network = network @property def wallet_manager(self): if self.__wallet_manager is None: self.__wallet_manager = WalletManager() return self.__wallet_manager @wallet_manager.setter def wallet_manager(self, wallet_manager: WalletManager): if isinstance(self.wallet_manager, WalletManager): self.__wallet_manager = wallet_manager else: raise SDKException(ErrorCode.other_error('Invalid WalletManager instance')) @property def default_signature_scheme(self): if self.__default_signature_scheme is None: self.__default_signature_scheme = SignatureScheme.SHA256withECDSA return self.__default_signature_scheme @default_signature_scheme.setter def default_signature_scheme(self, scheme: SignatureScheme): if isinstance(scheme, SignatureScheme): self.__default_signature_scheme = scheme self.__wallet_manager.set_signature_scheme(scheme) else: raise SDKException(ErrorCode.other_error('Invalid signature scheme')) @property def rpc(self) -> Rpc: return self.__rpc @rpc.setter def rpc(self, rpc_client: Rpc): if isinstance(rpc_client, Rpc): self.__rpc = rpc_client @property def aio_rpc(self) -> AioRpc: return self.__aio_rpc @aio_rpc.setter def aio_rpc(self, aio_rpc: AioRpc): if isinstance(aio_rpc, AioRpc): self.__rpc = aio_rpc @property def restful(self) -> Restful: return self.__restful @restful.setter def restful(self, restful_client: Restful): if isinstance(restful_client, Restful): self.__restful = restful_client @property def aio_restful(self) -> AioRestful: return self.__aio_restful @restful.setter def restful(self, aio_restful: AioRestful): if isinstance(aio_restful, AioRestful): self.__aio_restful = aio_restful @property def websocket(self) -> Websocket: return self.__websocket @websocket.setter def websocket(self, websocket_client: Websocket): if isinstance(websocket_client, Websocket): self.__websocket = websocket_client @property def rpc_address(self): if self.__rpc is None: return '' return self.__rpc.get_address() @rpc_address.setter def rpc_address(self, rpc_address: str): if isinstance(self.__rpc, Rpc): self.__rpc.set_address(rpc_address) else: self.__rpc = Rpc(rpc_address) @property def restful_address(self): if not isinstance(self.__restful, Restful): return '' return self.__restful.get_address() @restful_address.setter def restful_address(self, restful_address: str): if isinstance(self.__restful, Restful): self.__restful.set_address(restful_address) else: self.__restful = Restful(restful_address) @property def websocket_address(self) -> str: if not isinstance(self.__websocket, Websocket): return '' return self.__websocket.get_address() @websocket_address.setter def websocket_address(self, websocket_address: str): if isinstance(self.__websocket, Websocket): self.__websocket.set_address(websocket_address) else: self.__websocket = Websocket(websocket_address) @property def native_vm(self): return self.__native_vm @property def neo_vm(self): return self.__neo_vm @property def wasm_vm(self): return self.__wasm_vm @property def service(self): return self.__service @staticmethod def get_random_test_rpc_address(): return choice(TEST_RPC_ADDRESS) @staticmethod def get_random_main_rpc_address(): return choice(MAIN_RPC_ADDRESS) @staticmethod def get_random_test_restful_address(): choice(TEST_RESTFUL_ADDRESS) @staticmethod def get_random_main_restful_address(): return choice(MAIN_RESTFUL_ADDRESS) @staticmethod def get_test_net_restful_address_list(): return TEST_RESTFUL_ADDRESS @staticmethod def get_main_net_restful_address_list(): return MAIN_RESTFUL_ADDRESS
def wallet_manager(self): if self.__wallet_manager is None: self.__wallet_manager = WalletManager() return self.__wallet_manager
def test_deep_copy(self): wm = WalletManager() wm.create_wallet_file(self.path) try: wm.open_wallet(self.path) for index in range(5): wm.create_account(password, f'label{index}') wm.write_wallet() self.assertEqual(len(wm.wallet_file.identities), len(wm.wallet_in_mem.identities)) self.assertEqual(len(wm.wallet_file.accounts), len(wm.wallet_in_mem.accounts)) self.assertNotEqual(id(wm.wallet_file), id(wm.wallet_in_mem)) self.assertNotEqual(id(wm.wallet_file.identities), id(wm.wallet_in_mem.identities)) self.assertNotEqual(id(wm.wallet_file.accounts), id(wm.wallet_in_mem.accounts)) self.assertNotEqual(id(wm.wallet_file.scrypt), id(wm.wallet_in_mem.scrypt)) finally: wm.del_wallet_file()
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 test_create_account_from_private_key(self): wm = WalletManager() wm.create_wallet_file(self.path) try: wm.open_wallet(self.path) private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' label = 'hello_account' account = wm.create_account_from_private_key( password, private_key, label) b58_address = 'AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve' wm.save() self.assertEqual(b58_address, account.b58_address) finally: wm.del_wallet_file()
def test_add_control_by_private_key(self): wm = WalletManager() wm.create_wallet_file(self.path) try: wm.open_wallet(self.path) private_key = utils.get_random_bytes(32) hex_private_key = private_key.hex() public_key = Signature.ec_get_public_key_by_private_key( private_key, Curve.P256) hex_public_key = public_key.hex() identity = wm.create_identity('label', password) wm.write_wallet() wm.add_control_by_hex_private_key(identity.ont_id, password, hex_private_key) ctrl_acct = wm.get_control_account_by_index( identity.ont_id, 1, password) acct_private_key = ctrl_acct.get_private_key_hex() acct_public_key = ctrl_acct.get_public_key_hex() self.assertEqual(hex_public_key, acct_public_key) self.assertEqual(hex_private_key, acct_private_key) ctrl_len_1 = len(wm.wallet_in_mem.identities[0].controls) ctrl_len_2 = len(wm.wallet_file.identities[0].controls) self.assertEqual(ctrl_len_1, ctrl_len_2 + 1) finally: wm.del_wallet_file()
def test_get_accounts(self): wm = WalletManager() wm.create_wallet_file(self.path) try: wm.open_wallet(self.path) size = 5 for _ in range(size): wm.create_account(password) accounts = wm.get_wallet().get_accounts() self.assertEqual(len(accounts), size) finally: wm.del_wallet_file()
def test_import_account(self): wm = WalletManager(wallet_path=self.path) self.assertEqual(self.path, wm.wallet_path) wm.create_wallet_file() try: wm.open_wallet() label = 'label' b64_salt = 'MGEzY2Y0MWYyODhhOTQ3MA==' encrypted_pri_key = 'E6Yb/UmgAggwqHrj/OVYjVVacVhXiehRctKrxzVE/bi+tZId0AEN2wLoKsahpNq2' b58_address = 'AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve' account = wm.import_account(label, encrypted_pri_key, password, b58_address, b64_salt) acct = wm.get_account_by_b58_address(b58_address, password) self.assertTrue(isinstance(account, AccountData)) self.assertTrue(isinstance(acct, Account)) wm.save() finally: wm.del_wallet_file()
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_get_default_account(self): wm = WalletManager() wm.create_wallet_file(self.path) try: wm.open_wallet(self.path) size = 3 for _ in range(size): wm.create_account(password) accounts = wm.get_wallet().get_accounts() self.assertEqual(len(accounts), size) for acct in accounts: wm.get_wallet().set_default_account_by_address( acct.b58_address) default_account = wm.get_default_account_data() self.assertEqual(default_account.b58_address, acct.b58_address) finally: wm.del_wallet_file()
def test_set_default_account_by_index(self): wm = WalletManager() wm.create_wallet_file(self.path) try: wm.open_wallet(self.path) size = 3 for _ in range(size): wm.create_account(password) accounts = wm.get_wallet().get_accounts() self.assertEqual(len(accounts), size) self.assertRaises(SDKException, wm.get_wallet().set_default_account_by_index, size) for index in range(size): wm.get_wallet().set_default_account_by_index(index) default_address = wm.get_wallet().get_default_account_address() self.assertEqual(accounts[index].b58_address, default_address) 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_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()