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_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_get_gcm_decoded_private_key(self): private_key = "75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf" salt = base64.b64decode( "pwLIUKAf2bAbTseH/WYrfQ==".encode('ascii')).decode('latin-1') account = Account(private_key, SignatureScheme.SHA256withECDSA) enc_private_key = account.export_gcm_encrypted_private_key( "1", salt, 16384) self.assertEqual( enc_private_key, 'Yl1e9ugbVADd8a2SbAQ56UfUvr3e9hD2eNXAM9xNjhnefB+YuNXDFvUrIRaYth+L')
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_get_gcm_decoded_private_key(self): encrypted_key_str = 'hhWuyE1jjKjBOT7T5Rlrea3ewJIR8i6UjTv67bnkHz5YsqgeCfXjrHJTBGQtE0bG' b58_address = 'AMeJEzSMSNMZThqGoxBVVFwKsGXpAdVriS' salt = base64.b64decode('GXIs0bRy50tEfFuCF/h/yA==') n = 4096 private_key = Account.get_gcm_decoded_private_key(encrypted_key_str, password, b58_address, salt, n, SignatureScheme.SHA256withECDSA) acct = Account(private_key) export_key = acct.export_gcm_encrypted_private_key(password, salt, n) self.assertEqual(encrypted_key_str, export_key)
def test_export_and_get_gcm_decoded_private_key(self): hex_private_key = '75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf' salt = base64.b64decode('pwLIUKAf2bAbTseH/WYrfQ=='.encode('ascii')).decode('latin-1') account = Account(hex_private_key, SignatureScheme.SHA256withECDSA) b58_address = account.get_address_base58() n = 16384 enc_private_key = account.export_gcm_encrypted_private_key(password, salt, n) decoded_private_key = Account.get_gcm_decoded_private_key(enc_private_key, password, b58_address, salt, n, SignatureScheme.SHA256withECDSA) self.assertEqual(hex_private_key, decoded_private_key)
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 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, salt: bytes, priv_key: bytes, account_flag: bool): account = Account(priv_key, self.scheme) # initialization if self.scheme == SignatureScheme.SHA256withECDSA: acct = AccountData() else: raise ValueError("scheme type is error") # set key if pwd != None: acct.key = account.export_gcm_encrypted_private_key( pwd, salt, Scrypt().get_n()) pwd = None else: acct.key = account.serialize_private_key().hex() acct.address = account.get_address_base58() # set label if label == None or label == "": label = str(uuid.uuid4())[0:8] if account_flag: for index in range(len(self.wallet_in_mem.accounts)): if acct.address == self.wallet_in_mem.accounts[index].address: raise ValueError("wallet account exists") if len(self.wallet_in_mem.accounts) == 0: acct.isDefault = True self.wallet_in_mem.defaultAccountAddress = acct.address acct.label = label acct.salt = base64.b64encode(salt).decode() acct.publicKey = account.serialize_public_key().hex() self.wallet_in_mem.accounts.append(acct) else: for index in range(len(self.wallet_in_mem.identities)): if self.wallet_in_mem.identities[ index].ontid == did_ont + acct.address: raise ValueError("wallet identity exists") idt = Identity() idt.ontid = did_ont + acct.address idt.label = label if len(self.wallet_in_mem.identities) == 0: idt.isDefault = True self.wallet_in_mem.defaultOntid = idt.ontid ctl = Control(id="keys-1", key=acct.key, salt=base64.b64encode(salt).decode(), address=acct.address, public_key=account.serialize_public_key().hex()) idt.controls.append(ctl) self.wallet_in_mem.identities.append(idt) return account
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 __create_account(self, label: str, pwd: str, salt: str, private_key: str, account_flag: bool) -> Account: account = Account(private_key, self.scheme) if self.scheme == SignatureScheme.SHA256withECDSA: acct_data = AccountData() else: raise SDKException(ErrorCode.other_error('Scheme type is error.')) if pwd is not None: acct_data.key = account.export_gcm_encrypted_private_key(pwd, salt) else: acct_data.key = account.get_private_key_hex() acct_data.b58_address = account.get_address_base58() if len(label) == 0 or label is None: label = uuid.uuid4().hex[0:8] if account_flag: for memory_acct in self.wallet_in_mem.accounts: if memory_acct.b58_address == account.get_address_base58(): raise SDKException( ErrorCode.other_error('Wallet account exists.')) if len(self.wallet_in_mem.accounts) == 0: acct_data.is_default = True self.wallet_in_mem.default_account_address = acct_data.b58_address acct_data.label = label acct_data.salt = base64.b64encode( salt.encode('latin-1')).decode('ascii') acct_data.public_key = account.get_public_key_hex() self.wallet_in_mem.accounts.append(acct_data) else: for identity in self.wallet_in_mem.identities: if identity.ont_id == DID_ONT + acct_data.b58_address: raise SDKException( ErrorCode.other_error('Wallet identity exists.')) idt = Identity() idt.ont_id = DID_ONT + acct_data.b58_address idt.label = label if len(self.wallet_in_mem.identities) == 0: idt.is_default = True self.wallet_in_mem.default_ont_id = idt.ont_id ctl = Control(kid='keys-1', key=acct_data.key, salt=base64.b64encode(salt.encode()).decode('ascii'), address=acct_data.b58_address, public_key=account.get_public_key_hex()) idt.controls.append(ctl) self.wallet_in_mem.identities.append(idt) return account
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) 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.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 __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