<reponame>zouantchaw/node-ipfs-app
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("./utils");
const cid_1 = require("multiformats/cid");
function fromSplit(split) {
    const [protectedHeader, payload, signature] = split;
    return {
        payload,
        signatures: [{ protected: protectedHeader, signature }],
        link: cid_1.CID.decode(utils_1.fromBase64url(payload)),
    };
}
function encodeSignature(signature) {
    const encoded = {
        signature: utils_1.fromBase64url(signature.signature),
    };
    if (signature.header)
        encoded.header = signature.header;
    if (signature.protected)
        encoded.protected = utils_1.fromBase64url(signature.protected);
    return encoded;
}
function encode(jws) {
    const payload = utils_1.fromBase64url(jws.payload);
    try {
        cid_1.CID.decode(payload);
    }
    catch (e) {
        throw new Error('Not a valid DagJWS');
    }
    const encodedJws = {
        payload,
        signatures: jws.signatures.map(encodeSignature),
    };
    return encodedJws;
}
function decodeSignature(encoded) {
    const sign = {
        signature: utils_1.toBase64url(encoded.signature),
    };
    if (encoded.header)
        sign.header = encoded.header;
    if (encoded.protected)
        sign.protected = utils_1.toBase64url(encoded.protected);
    return sign;
}
function decode(encoded) {
    const decoded = {
        payload: utils_1.toBase64url(encoded.payload),
        signatures: encoded.signatures.map(decodeSignature),
    };
    decoded.link = cid_1.CID.decode(new Uint8Array(encoded.payload));
    return decoded;
}
exports.default = {
    fromSplit,
    encode,
    decode,
};
//# sourceMappingURL=signing.js.map