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_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 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 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_get_account(self): wm = WalletManager() password = '******' acct = wm.create_account('', password) self.assertTrue(isinstance(acct, AccountData)) b58_address = wm.wallet_in_mem.default_account_address print(b58_address) acct = wm.get_account(b58_address, password) self.assertEqual(acct.get_address_base58(), b58_address)
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_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_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_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_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_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_export_identity_qrcode(self): wm = WalletManager() pwd = "1" identity = wm.create_identity_from_prikey("sss", pwd, util.hex_to_bytes("75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf")) # account = wm.create_random_account("sss2", pwd) wqr = WalletQR() d = wqr.export_identity_qrcode(wm.wallet_file, identity) dstr = json.dumps(d, default=lambda obj: obj.__dict__, sort_keys=True, indent=4) privatekey = wqr.get_prikey_from_qrcode(dstr, pwd) print(privatekey.hex())
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_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_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) wm.write_wallet() os.remove(path)
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.__service = Service(self) self.__wallet_manager = WalletManager() self.__default_signature_scheme = default_signature_scheme
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_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_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 read_wallet(wallet_dir_path: str, wallet_file_name: str = '') -> WalletManager: if not os.path.isdir(wallet_dir_path): raise PunicaException(PunicaError.directory_error) wallet_manager = WalletManager() if wallet_file_name == '': dir_list = os.listdir(wallet_dir_path) if len(dir_list) == 1: wallet_file_name = dir_list[0] else: raise PunicaException(PunicaError.wallet_file_unspecified) wallet_path = os.path.join(wallet_dir_path, wallet_file_name) if not os.path.isfile(wallet_path): raise PunicaException(PunicaError.wallet_file_not_found) try: wallet_manager.open_wallet(wallet_path) except SDKException: raise PunicaException(PunicaError.wallet_file_error) return wallet_manager
def test_import_account(self): private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' account = Account(private_key, SignatureScheme.SHA256withECDSA) salt = "pwLIUKAf2bAbTseH/WYrfQ==" pwd = "1" enc_private_key = account.export_gcm_encrypted_private_key( pwd, salt, 16384) print('enc_private_key is ', enc_private_key) wm = WalletManager() path = os.path.join(os.getcwd(), 'test.json') wm.open_wallet(path) wm.open_wallet("./test.json") encrypted_pri_key = "Yl1e9ugbVADd8a2SbAQ56UfUvr3e9hD2eNXAM9xNjhnefB+YuNXDFvUrIRaYth+L" #import_account(label: str, encrypted_pri_key: str, pwd: str, base58_addr: str, base64_salt: str) addr = "AazEvfQPcQ2GEFFPLF1ZLwQ7K5jDn81hve" wm.import_account("label2", encrypted_pri_key, pwd, addr, salt) wm.save() 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()) wm.write_wallet() os.remove(path)
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 unlock_account(b58_address: str, wallet_manager: WalletManager): print('\tUnlock account: {}'.format(b58_address)) while True: try: acct_password = getpass.getpass('\tPlease input account password: '******'\tUnlock successful...') break except AssertionError: print('\tPassword uncorrected...') return acct
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 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_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_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 generate_signer_acct_list(b58_signers: list, wallet_manager: WalletManager): print('Unlock signer accounts...') signer_acct_list = list() for b58_signer in b58_signers: while True: try: print('\tUnlock signer account {}...'.format(b58_signer)) signer_password = getpass.getpass('\tPlease input account password: '******'\tUnlock successful...') break except AssertionError: print('\tPassword uncorrected...') return signer_acct_list
def read_wallet(project_path: str, wallet_file_name: str = '') -> WalletManager: if not os.path.isdir(project_path): raise IPAlbumException(IPAlbumError.directory_error) wallet_manager = WalletManager() if 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]) else: raise IPAlbumException(IPAlbumError.wallet_file_unspecified) else: wallet_path = os.path.join(project_path, wallet_file_name) if not os.path.exists(wallet_path): wallet_path = os.path.join(project_path, 'wallet', wallet_file_name) if not os.path.exists(wallet_path): raise IPAlbumError.other_error('wallet_path is error') try: wallet_manager.open_wallet(wallet_path) except SDKException: raise IPAlbumException(IPAlbumError.wallet_file_error) return wallet_manager