"use strict";
/*
 *
 * Copyright 2018-present NEM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrivateKeyProfile = void 0;
const clime_1 = require("clime");
const symbol_sdk_1 = require("symbol-sdk");
const signing_service_1 = require("../services/signing.service");
const networkCurrency_model_1 = require("./networkCurrency.model");
const profile_model_1 = require("./profile.model");
class PrivateKeyProfile extends profile_model_1.Profile {
    /**
     * Creates a new Private Key Profile from a DTO
     * @static
     * @param {ProfileDTOBase} DTO
     * @returns {PrivateKeyProfile}
     */
    static createFromDTO(DTO) {
        return new PrivateKeyProfile(symbol_sdk_1.SimpleWallet.createFromDTO(DTO.simpleWallet), DTO.url, DTO.networkGenerationHash, DTO.epochAdjustment || profile_model_1.epochAdjustment, networkCurrency_model_1.NetworkCurrency.createFromDTO(DTO.networkCurrency), DTO.version, DTO.type, DTO.default);
    }
    /**
     * Creates a new Private Key Profile
     * @static
     * @param {PrivateKeyProfileCreation} args
     * @returns {PrivateKeyProfile}
     */
    static create(args) {
        const simpleWallet = symbol_sdk_1.SimpleWallet.createFromPrivateKey(args.name, args.password, args.privateKey, args.networkType);
        return new PrivateKeyProfile(simpleWallet, args.url, args.generationHash, args.epochAdjustment, args.networkCurrency, profile_model_1.CURRENT_PROFILE_VERSION, 'PrivateKey', args.isDefault ? '1' : '0');
    }
    /**
     * Creates a DTO
     * @returns {ProfileDTOBase}
     */
    toDTO() {
        return {
            simpleWallet: this.simpleWallet.toDTO(),
            url: this.url,
            epochAdjustment: this.epochAdjustment,
            networkGenerationHash: this.networkGenerationHash,
            networkCurrency: this.networkCurrency.toDTO(),
            version: this.version,
            default: this.isDefault,
            type: this.type,
        };
    }
    /**
     * Returns true if the password is valid.
     * @param {Password} password.
     * @returns {boolean}
     */
    isPasswordValid(password) {
        try {
            this.simpleWallet.open(password);
            return true;
        }
        catch (error) {
            return false;
        }
    }
    /**
     * Opens a wallet.
     * @param {Password} options - The  attribute "password" should contain the profile's password.
     * @throws {ExpectedError}
     * @returns {Account}
     */
    decrypt(password) {
        if (!this.isPasswordValid(password)) {
            throw new clime_1.ExpectedError('The password provided does not match your account password');
        }
        return this.simpleWallet.open(password);
    }
    getAccount(password) {
        if (!password) {
            throw new Error('Password must be provided');
        }
        return this.decrypt(password);
    }
    getTable(password) {
        const table = this.getBaseTable();
        if (password) {
            table.push(['Password', password.value]);
            table.push(['Private key', this.decrypt(password).privateKey]);
        }
        return table;
    }
    async getSigningAccount(passwordResolver) {
        return new signing_service_1.PrivateKeyAccount(this.decrypt(await passwordResolver()));
    }
}
exports.PrivateKeyProfile = PrivateKeyProfile;
