"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Jws = void 0;
const isPlainObject_js_1 = __importDefault(require("lodash/isPlainObject.js"));
const encoder_js_1 = require("./encoder.js");
const signers_js_1 = require("../jose/algorithms/signing/signers.js");
/**
 * Utility class for JWS related operations.
 */
class Jws {
    /**
     * Gets the `kid` from a general JWS signature entry.
     */
    static getKid(signatureEntry) {
        const { kid } = encoder_js_1.Encoder.base64UrlToObject(signatureEntry.protected);
        return kid;
    }
    /**
     * Gets the signer DID from a general JWS signature entry.
     */
    static getSignerDid(signatureEntry) {
        const kid = Jws.getKid(signatureEntry);
        const did = Jws.extractDid(kid);
        return did;
    }
    /**
     * Verifies the signature against the given payload.
     * @returns `true` if signature is valid; `false` otherwise
     */
    static verifySignature(base64UrlPayload, signatureEntry, jwkPublic) {
        return __awaiter(this, void 0, void 0, function* () {
            const verifier = signers_js_1.signers[jwkPublic.crv];
            if (!verifier) {
                throw new Error(`unsupported crv. crv must be one of ${Object.keys(signers_js_1.signers)}`);
            }
            const payload = encoder_js_1.Encoder.stringToBytes(`${signatureEntry.protected}.${base64UrlPayload}`);
            const signatureBytes = encoder_js_1.Encoder.base64UrlToBytes(signatureEntry.signature);
            return yield verifier.verify(payload, signatureBytes, jwkPublic);
        });
    }
    /**
     * Decodes the payload of the given JWS object as a plain object.
     */
    static decodePlainObjectPayload(jws) {
        let payloadJson;
        try {
            payloadJson = encoder_js_1.Encoder.base64UrlToObject(jws.payload);
        }
        catch (_a) {
            throw new Error('payload is not a JSON object');
        }
        if (!(0, isPlainObject_js_1.default)(payloadJson)) {
            throw new Error('signed payload must be a plain object');
        }
        return payloadJson;
    }
    /**
     * Extracts the DID from the given `kid` string.
     */
    static extractDid(kid) {
        const [did] = kid.split('#');
        return did;
    }
    /**
     * Creates a SignatureInput[] from the given Personas.
     */
    static createSignatureInputs(keyMaterials) {
        const signatureInputs = keyMaterials.map((keyMaterial) => Jws.createSignatureInput(keyMaterial));
        return signatureInputs;
    }
    /**
     * Creates a SignatureInput from the given Persona.
     */
    static createSignatureInput(keyMaterial) {
        const signatureInput = {
            privateJwk: keyMaterial.keyPair.privateJwk,
            protectedHeader: {
                alg: keyMaterial.keyPair.privateJwk.alg,
                kid: keyMaterial.keyId
            }
        };
        return signatureInput;
    }
}
exports.Jws = Jws;
//# sourceMappingURL=jws.js.map