Exemplo n.º 1
0
    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
Exemplo n.º 2
0
 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