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 __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