def read_wallet(project_path: str, wallet_file_name: str = '') -> WalletManager: if not os.path.isdir(project_path): raise PunicaException(PunicaError.directory_error) wallet_manager = WalletManager() if wallet_file_name == '' or wallet_file_name == '"': wallet_dir_path = os.path.join(project_path, 'wallet') dir_list = os.listdir(wallet_dir_path) if len(dir_list) == 1: wallet_path = os.path.join(wallet_dir_path, dir_list[0]) elif os.path.exists(os.path.join(wallet_dir_path, 'wallet.json')): print('Use the default wallet file: wallet.json') wallet_path = os.path.join(wallet_dir_path, 'wallet.json') else: raise PunicaException(PunicaError.wallet_file_unspecified) else: wallet_path = os.path.join(project_path, wallet_file_name) if not os.path.exists(wallet_path): if os.path.dirname(wallet_file_name) != '': raise PunicaException(PunicaError.other_error(wallet_file_name + ' not found')) wallet_path = os.path.join(project_path, 'wallet', wallet_file_name) if not os.path.exists(wallet_path): raise PunicaError.other_error(wallet_path, ' is error') try: wallet_manager.open_wallet(wallet_path) except SDKException as e: raise PunicaException(PunicaError.wallet_file_error) return wallet_manager
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_add_control_by_private_key(self): wm = WalletManager() wm.create_wallet_file(path) try: wm.open_wallet(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_open_wallet(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) self.assertEqual(wm.__dict__['scheme'], SignatureScheme.SHA256withECDSA) os.remove(path)
def get_wallet_manager(project_path: str): wallet_path_dir = os.path.join(project_path, 'wallet') wallet_path = os.path.join(wallet_path_dir, 'wallet.json') if not os.path.exists(wallet_path): os.makedirs(wallet_path_dir) wallet_manager = WalletManager() wallet_manager.open_wallet(wallet_path) return wallet_manager
def test_create_identity_from_prikey(self): wm = WalletManager() wm.open_wallet("./test6.json") ide = wm.create_identity_from_prikey( "ide", "1", util.hex_to_bytes( "75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf" )) print(ide)
def test_create_account_from_prikey(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' account = wm.create_account_from_prikey("myaccount", "1", private_key) wm.save() self.assertEqual(account.address, 'AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve') os.remove(path)
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_import_account(self): wm = WalletManager() wm.open_wallet("./test.json") wm.import_account( "label2", "Yl1e9ugbVADd8a2SbAQ56UfUvr3e9hD2eNXAM9xNjhnefB+YuNXDFvUrIRaYth+L", "1", "AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve", base64.b64decode("pwLIUKAf2bAbTseH/WYrfQ==")) wm.save()
def test_import_identity(self): wm = WalletManager() wm.open_wallet("./test.json") salt = get_random_bytes(16) privete_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' acct = Account(a2b_hex(privete_key.encode())) enpri = acct.export_gcm_encrypted_private_key("1", salt, 16384) wm.import_identity("label2", enpri, "1", salt, acct.get_address_base58())
def test_create_write(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) password = '******' label = 'label' wm.create_account(label, password) wm.create_identity(label, password) wm.write_wallet() os.remove(path)
def test_create_account_from_prikey(self): wm = WalletManager() wm.open_wallet("/Users/zhaoxavi/test.txt") account = wm.create_account_from_prikey( "myaccount", "1", util.hex_to_bytes( "75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf" )) wm.save() print(account)
def test_open_wallet(self): wm = WalletManager() self.assertRaises(SDKException, wm.open_wallet) wm.create_wallet_file(path) try: wm.open_wallet(path) self.assertEqual(wm.__dict__['scheme'], SignatureScheme.SHA256withECDSA) finally: wm.del_wallet_file()
def test_create_identity_from_prikey(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' ide = wm.create_identity_from_pri_key("ide", "1", private_key) self.assertEqual(ide.label, 'ide') self.assertEqual(ide.ont_id, 'did:ont:AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve') os.remove(path)
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_get_accounts(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) password = "******" size = 5 for i in range(size): wm.create_account('', password) accounts = wm.get_wallet().get_accounts() self.assertEqual(len(accounts), size) os.remove(path)
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_create_identity_from_pri_key(self): wm = WalletManager() wm.create_wallet_file(path) try: wm.open_wallet(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:ont:AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve') finally: wm.del_wallet_file()
def test_import_account(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) wm.open_wallet("./test.json") wm.import_account( "label2", "Yl1e9ugbVADd8a2SbAQ56UfUvr3e9hD2eNXAM9xNjhnefB+YuNXDFvUrIRaYth+L", "1", "AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve", "pwLIUKAf2bAbTseH/WYrfQ==") wm.save() os.remove(path)
def test_get_accounts(self): wm = WalletManager() wm.create_wallet_file(path) try: wm.open_wallet(path) size = 5 for i 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() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) label = 'label' encrypted_pri_key = 'Yl1e9ugbVADd8a2SbAQ56UfUvr3e9hD2eNXAM9xNjhnefB+YuNXDFvUrIRaYth+L' password = '******' b58_address = 'AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve' b64_salt = 'pwLIUKAf2bAbTseH/WYrfQ==' account = wm.import_account(label, encrypted_pri_key, password, b58_address, b64_salt) self.assertTrue(isinstance(account, AccountData)) wm.save() os.remove(path)
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 test_get_default_account(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) password = "******" size = 3 for i 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.address) default_account = wm.get_default_account() self.assertEqual(default_account.address, acct.address)
def test_create_account_from_private_key(self): wm = WalletManager() wm.create_wallet_file(path) try: wm.open_wallet(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 import_account(project_path, private_key): wallet_path_dir = os.path.join(project_path, 'wallet') # TODO: the file name 'wallet.json' should be custom defined here wallet_path = os.path.join(wallet_path_dir, 'wallet.json') if not os.path.exists(wallet_path): os.makedirs(wallet_path_dir) wallet_manager = WalletManager() wallet_manager.open_wallet(wallet_path) pwd = Account.get_password() try: wallet_manager.create_account_from_private_key('', pwd, private_key) wallet_manager.write_wallet() except ValueError as e: print('import account error:') print(e.args)
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_set_default_account_by_index(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) password = "******" size = 3 for i 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].address, default_address) os.remove(path)
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_default_account(self): wm = WalletManager() wm.create_wallet_file(path) try: wm.open_wallet(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_address(self): wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) password = '******' size = 3 for i 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_address, '1') for acct in accounts: wm.get_wallet().set_default_account_by_address(acct.address) default_address = wm.get_wallet().get_default_account_address() self.assertEqual(default_address, acct.address) wm.write_wallet() os.remove(path)