// Copyright 2024 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.
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());
    });
};
import * as fs from "fs-extra";
import * as crypto from "crypto";
import { encode } from "cbor2";
class TestSigner {
    constructor(privateKey) {
        this.sign = (bytes) => __awaiter(this, void 0, void 0, function* () {
            const sign = crypto.createSign("SHA256");
            sign.update(bytes);
            sign.end();
            return sign.sign(this.privateKey);
        });
        this.privateKey = crypto.createPrivateKey({
            key: privateKey,
            format: "pem",
        });
    }
}
class TestCawgSigner {
    constructor(manifestSigner) {
        this.manifestSigner = manifestSigner;
        this.sign = (payload) => __awaiter(this, void 0, void 0, function* () {
            const cborBytes = Buffer.from(encode(payload));
            return this.manifestSigner.sign(cborBytes);
        });
    }
}
describe("IdentityAssertionBuilder", () => {
    const manifestDefinition = {
        vendor: "test",
        claim_generator_info: [
            {
                name: "c2pa_test",
                version: "2.0.0",
            },
        ],
        claim_generator: "c2pa_test",
        title: "Test_Manifest",
        format: "image/jpeg",
        instance_id: "1234",
        thumbnail: { format: "image/jpeg", identifier: "thumbnail.jpg" },
        resources: { resources: {} },
        ingredients: [
            {
                title: "Test",
                format: "image/jpeg",
                instance_id: "12345",
                relationship: "componentOf",
                thumbnail: { format: "image/jpeg", identifier: "ingredient-thumb.jpg" },
                resources: { resources: {} },
            },
        ],
        assertions: [
            {
                label: "c2pa.actions.v2",
                data: {
                    actions: [
                        {
                            action: "c2pa.created",
                            parameters: {
                                digitalSourceType: "http://cv.iptc.org/newscodes/digitalsourcetype/digitalCapture",
                            },
                        },
                    ],
                },
                kind: "Json",
            },
            {
                label: "cawg.training-mining",
                data: {
                    metadata: {
                        "cawg.ai_inference": {
                            use: "notAllowed",
                        },
                        "cawg.ai_generative_training": {
                            use: "notAllowed",
                        },
                    },
                },
            },
        ],
    };
    it("should build an Identity Assertion Signer", () => __awaiter(void 0, void 0, void 0, function* () {
        const { CallbackSigner } = yield import("./Signer");
        const { Reader } = yield import("./Reader");
        const { Builder } = yield import("./Builder");
        const { IdentityAssertionBuilder, IdentityAssertionSigner, CallbackCredentialHolder, } = yield import("./IdentityAssertion");
        // Read certificate files once
        const c2paPrivateKey = yield fs.readFile("./tests/fixtures/certs/es256.pem");
        const c2paPublicKey = yield fs.readFile("./tests/fixtures/certs/es256.pub");
        // Use the same signer for both C2PA manifest and COSE signing
        // Create signer configurations
        const c2paConfig = {
            alg: "es256",
            certs: [c2paPublicKey],
            reserveSize: 10000,
            tsaUrl: undefined,
            tsaHeaders: undefined,
            tsaBody: undefined,
            directCoseHandling: true,
        };
        // Create signers
        const c2paTestSigner = new TestSigner(c2paPrivateKey);
        const c2paSigner = CallbackSigner.newSigner(c2paConfig, c2paTestSigner.sign);
        const cawgTestSigner = new TestCawgSigner(c2paTestSigner);
        const cawgSigner = CallbackCredentialHolder.newCallbackCredentialHolder(10000, "cawg.x509.cose", cawgTestSigner.sign);
        const source = {
            buffer: yield fs.readFile("./tests/fixtures/CA.jpg"),
            mimeType: "image/jpeg",
        };
        const dest = {
            buffer: null,
        };
        // Create the manifest builder
        const builder = Builder.withJson(manifestDefinition);
        // Add the required resources
        yield builder.addResource("thumbnail.jpg", {
            mimeType: "image/jpeg",
            buffer: yield fs.readFile("./tests/fixtures/thumbnail.jpg"),
        });
        yield builder.addResource("ingredient-thumb.jpg", {
            mimeType: "image/jpeg",
            buffer: yield fs.readFile("./tests/fixtures/thumbnail.jpg"),
        });
        // Create and configure the identity assertion
        const iaSigner = IdentityAssertionSigner.new(c2paSigner.getHandle());
        const iab = yield IdentityAssertionBuilder.identityBuilderForCredentialHolder(cawgSigner);
        iab.addReferencedAssertions(["cawg.training-mining"]);
        iaSigner.addIdentityAssertion(iab);
        // Sign the manifest (standard async flow)
        yield builder.signAsync(iaSigner, source, dest);
        // Verify the manifest
        yield Reader.fromAsset({
            buffer: dest.buffer,
            mimeType: "image/jpeg",
        });
    }));
});
