import { utils } from '../../index.js';
/**
 * Serialize the key information to a string format.
 * @param keyInfo The KeyInformation object to be serialized.
 * @returns The serialized string representation of the KeyInformation.
 */
export function serialiseKeyInformation(keyInfo) {
    return JSON.stringify(keyInfo);
}
/**
 * Deserialize a serialized string to obtain the KeyInformation object.
 * @param parsed The serialized string representation of KeyInformation.
 * @returns The deserialized KeyInformation object.
 */
export function deserialiseKeyInformation(parsed) {
    return JSON.parse(parsed);
}
/**
 * A service class for interacting with derivations API
 */
export class APIDerivationService {
    /**
     * Constructor for the APIDerivationService class.
     * @param api The AxiosInstance used for API communication.
     * @param keys The MintBlue key storage configuration.
     */
    constructor(api, keys) {
        this.api = api;
        this.keys = keys;
    }
    /**
     * Get the master key for encryption and decryption operations.
     * @returns The CryptoKey representing the master key.
     */
    getMasterKey() {
        return this.keys.masterKey;
    }
    /**
     * Retrieve the list of keys from the API.
     * @returns A promise that resolves to a map of APIDerivation objects by fingerprint.
     */
    async getKeys() {
        const response = await this.api.get('/derivations');
        const derivations = response.data;
        const map = new Map();
        for (const key in derivations) {
            map.set(key, derivations[key]);
        }
        return map;
    }
    /**
     * Save a key by posting it to the API.
     * @param derivationKey The key or derived key to be saved.
     * @param name The name associated with the key.
     */
    async saveKey(derivationKey, name, keyProtected = false) {
        const keyInfo = await derivationKey.exportKeyInformation();
        await this.api.post('/derivations', {
            name,
            path: await utils.encryptToB64String(Buffer.from(serialiseKeyInformation(keyInfo)), this.keys.masterKey),
            pubkey: keyInfo.publicKey,
            fingerprint: keyInfo.fingerprint,
            keyProtected,
        });
    }
}
//# sourceMappingURL=DerivationService.js.map