@contentauth/c2pa-node 0.5.4 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +2 -1
- package/README.md +145 -7
- package/dist/Builder.js +63 -80
- package/dist/Builder.spec.js +251 -202
- package/dist/IdentityAssertion.js +8 -18
- package/dist/IdentityAssertion.spec.js +28 -35
- package/dist/Reader.js +13 -28
- package/dist/Reader.spec.js +53 -64
- package/dist/Settings.js +16 -29
- package/dist/Settings.spec.js +47 -62
- package/dist/Signer.js +4 -13
- package/dist/Signer.spec.js +35 -43
- package/dist/Trustmark.js +8 -22
- package/dist/Trustmark.spec.js +50 -65
- package/dist/binary.js +2 -1
- package/dist/index.node +0 -0
- package/dist/types/Builder.d.ts +2 -1
- package/dist/types/types.d.ts +11 -1
- package/package.json +54 -51
- package/scripts/postinstall.cjs +18 -1
- package/src/lib.rs +4 -0
- package/src/neon_builder.rs +30 -6
- package/src/neon_reader.rs +4 -2
- package/src/neon_trustmark.rs +74 -30
- package/src/utils.rs +8 -6
- package/Cargo.lock +0 -5050
- package/LICENSE +0 -21
package/dist/Builder.spec.js
CHANGED
|
@@ -3,15 +3,6 @@
|
|
|
3
3
|
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|
4
4
|
// or the MIT license (http://opensource.org/licenses/MIT),
|
|
5
5
|
// at your option.
|
|
6
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
7
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
-
});
|
|
14
|
-
};
|
|
15
6
|
import * as fs from "fs-extra";
|
|
16
7
|
import path from "path";
|
|
17
8
|
import * as crypto from "crypto";
|
|
@@ -21,27 +12,28 @@ import { Reader } from "./Reader.js";
|
|
|
21
12
|
import { Builder } from "./Builder.js";
|
|
22
13
|
const tempDir = path.join(__dirname, "tmp");
|
|
23
14
|
class TestSigner {
|
|
15
|
+
privateKey;
|
|
24
16
|
constructor(privateKey) {
|
|
25
|
-
this.sign = (bytes) => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
const sign = crypto.createSign("SHA256");
|
|
27
|
-
sign.update(bytes);
|
|
28
|
-
sign.end();
|
|
29
|
-
const signature = sign.sign(this.privateKey);
|
|
30
|
-
return signature;
|
|
31
|
-
});
|
|
32
17
|
this.privateKey = crypto.createPrivateKey({
|
|
33
18
|
key: privateKey,
|
|
34
19
|
format: "pem",
|
|
35
20
|
});
|
|
36
21
|
}
|
|
22
|
+
sign = async (bytes) => {
|
|
23
|
+
const sign = crypto.createSign("SHA256");
|
|
24
|
+
sign.update(bytes);
|
|
25
|
+
sign.end();
|
|
26
|
+
const signature = sign.sign(this.privateKey);
|
|
27
|
+
return signature;
|
|
28
|
+
};
|
|
37
29
|
}
|
|
38
30
|
describe("TestSigner", () => {
|
|
39
|
-
it("should sign data", () =>
|
|
40
|
-
const signer = new TestSigner(
|
|
31
|
+
it("should sign data", async () => {
|
|
32
|
+
const signer = new TestSigner(await fs.readFile("./tests/fixtures/certs/es256.pem"));
|
|
41
33
|
const bytes = Buffer.from("Hello, World!");
|
|
42
|
-
const signature =
|
|
34
|
+
const signature = await signer.sign(bytes);
|
|
43
35
|
expect(signature.length).toBeGreaterThan(0);
|
|
44
|
-
})
|
|
36
|
+
});
|
|
45
37
|
});
|
|
46
38
|
describe("Builder", () => {
|
|
47
39
|
const parent_json = `{
|
|
@@ -93,22 +85,22 @@ describe("Builder", () => {
|
|
|
93
85
|
let source;
|
|
94
86
|
let publicKey;
|
|
95
87
|
let privateKey;
|
|
96
|
-
beforeAll(() =>
|
|
97
|
-
|
|
88
|
+
beforeAll(async () => {
|
|
89
|
+
await fs.ensureDir(tempDir);
|
|
98
90
|
// Read all test files once
|
|
99
91
|
source = {
|
|
100
|
-
buffer:
|
|
92
|
+
buffer: await fs.readFile("./tests/fixtures/CA.jpg"),
|
|
101
93
|
mimeType: "image/jpeg",
|
|
102
94
|
};
|
|
103
|
-
testThumbnail =
|
|
104
|
-
publicKey =
|
|
105
|
-
privateKey =
|
|
95
|
+
testThumbnail = await fs.readFile("./tests/fixtures/thumbnail.jpg");
|
|
96
|
+
publicKey = await fs.readFile("./tests/fixtures/certs/es256.pub");
|
|
97
|
+
privateKey = await fs.readFile("./tests/fixtures/certs/es256.pem");
|
|
106
98
|
testAssertion = JSON.stringify({ answer: 42 });
|
|
107
|
-
})
|
|
108
|
-
afterAll(() =>
|
|
109
|
-
|
|
110
|
-
})
|
|
111
|
-
it("should build a manifest store", () =>
|
|
99
|
+
});
|
|
100
|
+
afterAll(async () => {
|
|
101
|
+
await fs.remove(tempDir);
|
|
102
|
+
});
|
|
103
|
+
it("should build a manifest store", async () => {
|
|
112
104
|
const test_definition = {
|
|
113
105
|
claim_generator: "test-generator",
|
|
114
106
|
claim_generator_info: [
|
|
@@ -128,14 +120,14 @@ describe("Builder", () => {
|
|
|
128
120
|
resources: { resources: {} },
|
|
129
121
|
};
|
|
130
122
|
const builder = Builder.withJson(test_definition);
|
|
131
|
-
|
|
123
|
+
await builder.addIngredient(parent_json, source);
|
|
132
124
|
builder.addAssertion("org.test.assertion", "assertion");
|
|
133
|
-
|
|
125
|
+
await builder.addResource(thumbnail_ref.identifier, {
|
|
134
126
|
buffer: Buffer.from("12345"),
|
|
135
127
|
mimeType: "jpeg",
|
|
136
128
|
});
|
|
137
129
|
// Wait for any pending async operations
|
|
138
|
-
|
|
130
|
+
await Promise.resolve();
|
|
139
131
|
const definition = builder.getManifestDefinition();
|
|
140
132
|
expect(definition.vendor).toBe("test");
|
|
141
133
|
expect(definition.title).toBe("builder-test-manifest");
|
|
@@ -144,48 +136,47 @@ describe("Builder", () => {
|
|
|
144
136
|
expect(definition.ingredients[0].title).toStrictEqual("c2pa-bindings Test");
|
|
145
137
|
expect(definition.assertions[0].label).toBe("org.test.assertion");
|
|
146
138
|
expect(definition.label).toBe("ABCDE");
|
|
147
|
-
})
|
|
139
|
+
});
|
|
148
140
|
describe("Sign and Archive", () => {
|
|
149
141
|
let builder;
|
|
150
142
|
beforeEach(() => {
|
|
151
143
|
builder = Builder.withJson(manifestDefinition);
|
|
152
144
|
builder.updateManifestProperty("claim_version", 2);
|
|
153
145
|
});
|
|
154
|
-
beforeEach(() =>
|
|
146
|
+
beforeEach(async () => {
|
|
155
147
|
// Add ingredients and resources after builder is initialized
|
|
156
|
-
|
|
157
|
-
|
|
148
|
+
await builder.addIngredient(parent_json, source);
|
|
149
|
+
await builder.addResource("thumbnail.jpg", {
|
|
158
150
|
mimeType: "jpeg",
|
|
159
151
|
buffer: testThumbnail,
|
|
160
152
|
});
|
|
161
|
-
|
|
153
|
+
await builder.addResource("ingredient-thumb.jpg", {
|
|
162
154
|
mimeType: "jpeg",
|
|
163
155
|
buffer: testThumbnail,
|
|
164
156
|
});
|
|
165
|
-
|
|
157
|
+
await builder.addResource("prompt.txt", {
|
|
166
158
|
buffer: Buffer.from("a random prompt"),
|
|
167
159
|
mimeType: "text/plain",
|
|
168
160
|
});
|
|
169
161
|
builder.addAssertion("org.life.meaning", testAssertion);
|
|
170
162
|
builder.addAssertion("org.life.meaning.json", testAssertion, "Json");
|
|
171
163
|
// Wait for any pending operations
|
|
172
|
-
|
|
173
|
-
})
|
|
174
|
-
it("should sign data", () =>
|
|
164
|
+
await Promise.resolve();
|
|
165
|
+
});
|
|
166
|
+
it("should sign data", async () => {
|
|
175
167
|
const dest = { path: path.join(tempDir, "signed.jpg") };
|
|
176
168
|
const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
|
|
177
169
|
const bytes = builder.sign(signer, source, dest);
|
|
178
170
|
expect(bytes.length).toBeGreaterThan(0);
|
|
179
|
-
const reader =
|
|
171
|
+
const reader = await Reader.fromAsset(dest);
|
|
180
172
|
expect(reader).not.toBeNull();
|
|
181
173
|
const manifestStore = reader.json();
|
|
182
174
|
const activeManifest = reader.getActive();
|
|
183
175
|
expect(manifestStore.validation_status[0].code).toBe("signingCredential.untrusted");
|
|
184
176
|
expect(manifestStore.active_manifest).not.toBeUndefined();
|
|
185
|
-
expect(activeManifest
|
|
186
|
-
})
|
|
187
|
-
it("should add a CBOR assertion, sign, and verify it in the signed manifest", () =>
|
|
188
|
-
var _a;
|
|
177
|
+
expect(activeManifest?.title).toBe("Test_Manifest");
|
|
178
|
+
});
|
|
179
|
+
it("should add a CBOR assertion, sign, and verify it in the signed manifest", async () => {
|
|
189
180
|
// Add the c2pa.watermarked action as a CBOR assertion
|
|
190
181
|
const actionsAssertion = {
|
|
191
182
|
actions: [
|
|
@@ -200,10 +191,10 @@ describe("Builder", () => {
|
|
|
200
191
|
const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
|
|
201
192
|
builder.sign(signer, source, dest);
|
|
202
193
|
// Read and verify the assertion in the signed manifest
|
|
203
|
-
const reader =
|
|
194
|
+
const reader = await Reader.fromAsset(dest);
|
|
204
195
|
expect(reader).not.toBeNull();
|
|
205
196
|
const activeManifest = reader.getActive();
|
|
206
|
-
const cborAssertion =
|
|
197
|
+
const cborAssertion = activeManifest?.assertions?.find((a) => a.label === "c2pa.actions.v2");
|
|
207
198
|
expect(cborAssertion).toBeDefined();
|
|
208
199
|
if (isActionsAssertion(cborAssertion)) {
|
|
209
200
|
const actions = cborAssertion.data.actions.map((a) => a.action);
|
|
@@ -212,18 +203,18 @@ describe("Builder", () => {
|
|
|
212
203
|
else {
|
|
213
204
|
throw new Error("CBOR assertion does not have the expected structure");
|
|
214
205
|
}
|
|
215
|
-
})
|
|
216
|
-
it("should archive and construct from archive", () =>
|
|
206
|
+
});
|
|
207
|
+
it("should archive and construct from archive", async () => {
|
|
217
208
|
const dest = {
|
|
218
209
|
buffer: null,
|
|
219
210
|
};
|
|
220
211
|
const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
|
|
221
212
|
const outputPath = path.join(tempDir, "archive.zip");
|
|
222
213
|
const archive = { path: outputPath };
|
|
223
|
-
|
|
224
|
-
const builder2 =
|
|
214
|
+
await builder.toArchive(archive);
|
|
215
|
+
const builder2 = await Builder.fromArchive(archive);
|
|
225
216
|
builder2.sign(signer, source, dest);
|
|
226
|
-
const reader =
|
|
217
|
+
const reader = await Reader.fromAsset({
|
|
227
218
|
buffer: dest.buffer,
|
|
228
219
|
mimeType: "jpeg",
|
|
229
220
|
});
|
|
@@ -232,26 +223,26 @@ describe("Builder", () => {
|
|
|
232
223
|
const activeManifest = reader.getActive();
|
|
233
224
|
expect(manifestStore.validation_status[0].code).toBe("signingCredential.untrusted");
|
|
234
225
|
expect(manifestStore.active_manifest).not.toBeUndefined();
|
|
235
|
-
expect(activeManifest
|
|
236
|
-
})
|
|
237
|
-
it("should populate buffer when archiving to buffer", () =>
|
|
226
|
+
expect(activeManifest?.title).toBe("Test_Manifest");
|
|
227
|
+
});
|
|
228
|
+
it("should populate buffer when archiving to buffer", async () => {
|
|
238
229
|
const archive = {
|
|
239
230
|
buffer: null,
|
|
240
231
|
};
|
|
241
|
-
|
|
232
|
+
await builder.toArchive(archive);
|
|
242
233
|
expect(archive.buffer).not.toBeNull();
|
|
243
234
|
expect(archive.buffer.length).toBeGreaterThan(0);
|
|
244
|
-
})
|
|
245
|
-
it("should write archive to file", () =>
|
|
235
|
+
});
|
|
236
|
+
it("should write archive to file", async () => {
|
|
246
237
|
const archivePath = path.join(tempDir, "archive_file_test.zip");
|
|
247
238
|
const archive = { path: archivePath };
|
|
248
|
-
|
|
239
|
+
await builder.toArchive(archive);
|
|
249
240
|
// Verify file was created and has content
|
|
250
|
-
expect(
|
|
251
|
-
const stats =
|
|
241
|
+
expect(await fs.pathExists(archivePath)).toBe(true);
|
|
242
|
+
const stats = await fs.stat(archivePath);
|
|
252
243
|
expect(stats.size).toBeGreaterThan(0);
|
|
253
|
-
})
|
|
254
|
-
it("should construct reader directly from builder archive buffer", () =>
|
|
244
|
+
});
|
|
245
|
+
it("should construct reader directly from builder archive buffer", async () => {
|
|
255
246
|
const settings = JSON.stringify({
|
|
256
247
|
builder: {
|
|
257
248
|
generate_c2pa_archive: true,
|
|
@@ -275,23 +266,23 @@ describe("Builder", () => {
|
|
|
275
266
|
};
|
|
276
267
|
const testBuilder = Builder.withJson(simpleManifestDefinition, settings);
|
|
277
268
|
// Add an ingredient
|
|
278
|
-
|
|
269
|
+
await testBuilder.addIngredient(parent_json, source);
|
|
279
270
|
// Create an archive from the builder written to a buffer
|
|
280
271
|
const archive = {
|
|
281
272
|
buffer: null,
|
|
282
273
|
};
|
|
283
|
-
|
|
274
|
+
await testBuilder.toArchive(archive);
|
|
284
275
|
// Verify buffer was populated
|
|
285
276
|
expect(archive.buffer).not.toBeNull();
|
|
286
277
|
expect(archive.buffer.length).toBeGreaterThan(0);
|
|
287
278
|
// Construct a reader from the builder archive with mime-type "application/c2pa"
|
|
288
|
-
const reader =
|
|
279
|
+
const reader = await Reader.fromAsset({
|
|
289
280
|
buffer: archive.buffer,
|
|
290
281
|
mimeType: "application/c2pa",
|
|
291
282
|
}, settings);
|
|
292
283
|
expect(reader).not.toBeNull();
|
|
293
|
-
})
|
|
294
|
-
it("should sign data with callback to file", () =>
|
|
284
|
+
});
|
|
285
|
+
it("should sign data with callback to file", async () => {
|
|
295
286
|
const dest = {
|
|
296
287
|
path: path.join(tempDir, "callback_signed.jpg"),
|
|
297
288
|
};
|
|
@@ -303,17 +294,17 @@ describe("Builder", () => {
|
|
|
303
294
|
directCoseHandling: false,
|
|
304
295
|
};
|
|
305
296
|
const signer = new TestSigner(privateKey);
|
|
306
|
-
const bytes =
|
|
297
|
+
const bytes = await builder.signConfigAsync(signer.sign, signerConfig, source, dest);
|
|
307
298
|
expect(bytes.length).toBeGreaterThan(0);
|
|
308
|
-
const reader =
|
|
299
|
+
const reader = await Reader.fromAsset(dest);
|
|
309
300
|
expect(reader).not.toBeNull();
|
|
310
301
|
const manifestStore = reader.json();
|
|
311
302
|
const activeManifest = reader.getActive();
|
|
312
303
|
expect(manifestStore.validation_status[0].code).toBe("signingCredential.untrusted");
|
|
313
304
|
expect(manifestStore.active_manifest).not.toBeUndefined();
|
|
314
|
-
expect(activeManifest
|
|
315
|
-
})
|
|
316
|
-
it("should sign data with callback to buffer", () =>
|
|
305
|
+
expect(activeManifest?.title).toBe("Test_Manifest");
|
|
306
|
+
});
|
|
307
|
+
it("should sign data with callback to buffer", async () => {
|
|
317
308
|
const dest = {
|
|
318
309
|
buffer: null,
|
|
319
310
|
};
|
|
@@ -325,9 +316,9 @@ describe("Builder", () => {
|
|
|
325
316
|
directCoseHandling: false,
|
|
326
317
|
};
|
|
327
318
|
const signer = new TestSigner(privateKey);
|
|
328
|
-
const bytes =
|
|
319
|
+
const bytes = await builder.signConfigAsync(signer.sign, signerConfig, source, dest);
|
|
329
320
|
expect(bytes.length).toBeGreaterThan(0);
|
|
330
|
-
const reader =
|
|
321
|
+
const reader = await Reader.fromAsset({
|
|
331
322
|
buffer: dest.buffer,
|
|
332
323
|
mimeType: "jpeg",
|
|
333
324
|
});
|
|
@@ -336,9 +327,9 @@ describe("Builder", () => {
|
|
|
336
327
|
const activeManifest = reader.getActive();
|
|
337
328
|
expect(manifestStore.validation_status[0].code).toBe("signingCredential.untrusted");
|
|
338
329
|
expect(manifestStore.active_manifest).not.toBeUndefined();
|
|
339
|
-
expect(activeManifest
|
|
340
|
-
})
|
|
341
|
-
it("should sign data with callback signer to buffer", () =>
|
|
330
|
+
expect(activeManifest?.title).toBe("Test_Manifest");
|
|
331
|
+
});
|
|
332
|
+
it("should sign data with callback signer to buffer", async () => {
|
|
342
333
|
const dest = {
|
|
343
334
|
buffer: null,
|
|
344
335
|
};
|
|
@@ -351,9 +342,9 @@ describe("Builder", () => {
|
|
|
351
342
|
};
|
|
352
343
|
const testSigner = new TestSigner(privateKey);
|
|
353
344
|
const signer = CallbackSigner.newSigner(signerConfig, testSigner.sign);
|
|
354
|
-
const bytes =
|
|
345
|
+
const bytes = await builder.signAsync(signer, source, dest);
|
|
355
346
|
expect(bytes.length).toBeGreaterThan(0);
|
|
356
|
-
const reader =
|
|
347
|
+
const reader = await Reader.fromAsset({
|
|
357
348
|
buffer: dest.buffer,
|
|
358
349
|
mimeType: "jpeg",
|
|
359
350
|
});
|
|
@@ -362,10 +353,9 @@ describe("Builder", () => {
|
|
|
362
353
|
const activeManifest = reader.getActive();
|
|
363
354
|
expect(manifestStore.validation_status[0].code).toBe("signingCredential.untrusted");
|
|
364
355
|
expect(manifestStore.active_manifest).not.toBeUndefined();
|
|
365
|
-
expect(activeManifest
|
|
366
|
-
})
|
|
367
|
-
it("should preserve JSON assertion characters without escaping", () =>
|
|
368
|
-
var _a;
|
|
356
|
+
expect(activeManifest?.title).toBe("Test_Manifest");
|
|
357
|
+
});
|
|
358
|
+
it("should preserve JSON assertion characters without escaping", async () => {
|
|
369
359
|
const fingerprintAssertion = JSON.stringify({
|
|
370
360
|
alg: "sha256",
|
|
371
361
|
blocks: [
|
|
@@ -379,14 +369,14 @@ describe("Builder", () => {
|
|
|
379
369
|
const dest = { path: path.join(tempDir, "json_test.jpg") };
|
|
380
370
|
const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
|
|
381
371
|
builder.sign(signer, source, dest);
|
|
382
|
-
const reader =
|
|
372
|
+
const reader = await Reader.fromAsset(dest);
|
|
383
373
|
expect(reader).not.toBeNull();
|
|
384
374
|
const manifest = reader.json();
|
|
385
375
|
// Check that our specific JSON assertion doesn't have escaped characters
|
|
386
376
|
const activeManifest = manifest.manifests[manifest.active_manifest];
|
|
387
|
-
const fingerprintAssertionData =
|
|
377
|
+
const fingerprintAssertionData = activeManifest?.assertions?.find((a) => a.label === "org.contentauth.fingerprint");
|
|
388
378
|
expect(fingerprintAssertionData).toBeDefined();
|
|
389
|
-
expect(fingerprintAssertionData
|
|
379
|
+
expect(fingerprintAssertionData?.data).toEqual({
|
|
390
380
|
alg: "sha256",
|
|
391
381
|
blocks: [
|
|
392
382
|
{
|
|
@@ -396,10 +386,10 @@ describe("Builder", () => {
|
|
|
396
386
|
],
|
|
397
387
|
});
|
|
398
388
|
// The assertion data should not be a string with escaped quotes
|
|
399
|
-
expect(typeof
|
|
400
|
-
expect(JSON.stringify(fingerprintAssertionData
|
|
401
|
-
})
|
|
402
|
-
it("should archive and restore builder with ingredient thumbnail", () =>
|
|
389
|
+
expect(typeof fingerprintAssertionData?.data).toBe("object");
|
|
390
|
+
expect(JSON.stringify(fingerprintAssertionData?.data)).not.toContain("\\");
|
|
391
|
+
});
|
|
392
|
+
it("should archive and restore builder with ingredient thumbnail", async () => {
|
|
403
393
|
const manifestDefinition = {
|
|
404
394
|
claim_generator_info: [
|
|
405
395
|
{
|
|
@@ -427,25 +417,25 @@ describe("Builder", () => {
|
|
|
427
417
|
};
|
|
428
418
|
const builder = Builder.withJson(manifestDefinition);
|
|
429
419
|
const ingredientJson = '{"title": "Test Ingredient"}';
|
|
430
|
-
const testThumbnail =
|
|
431
|
-
|
|
420
|
+
const testThumbnail = await fs.readFile("./tests/fixtures/thumbnail.jpg");
|
|
421
|
+
await builder.addIngredient(ingredientJson, {
|
|
432
422
|
buffer: testThumbnail,
|
|
433
423
|
mimeType: "jpeg",
|
|
434
424
|
});
|
|
435
425
|
// Archive the builder
|
|
436
426
|
const archivePath = path.join(tempDir, `ingredient_thumb_archive_${Date.now()}.zip`);
|
|
437
|
-
|
|
427
|
+
await builder.toArchive({ path: archivePath });
|
|
438
428
|
// Ensure the file is written to disk before proceeding
|
|
439
|
-
|
|
429
|
+
await fs.access(archivePath);
|
|
440
430
|
// Restore from archive
|
|
441
|
-
const builder2 =
|
|
431
|
+
const builder2 = await Builder.fromArchive({ path: archivePath });
|
|
442
432
|
// Sign with restored builder
|
|
443
433
|
const dest = { buffer: null };
|
|
444
|
-
const publicKey =
|
|
445
|
-
const privateKey =
|
|
434
|
+
const publicKey = await fs.readFile("./tests/fixtures/certs/es256.pub");
|
|
435
|
+
const privateKey = await fs.readFile("./tests/fixtures/certs/es256.pem");
|
|
446
436
|
const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
|
|
447
437
|
builder2.sign(signer, source, dest);
|
|
448
|
-
const reader =
|
|
438
|
+
const reader = await Reader.fromAsset({
|
|
449
439
|
buffer: dest.buffer,
|
|
450
440
|
mimeType: "jpeg",
|
|
451
441
|
});
|
|
@@ -453,8 +443,8 @@ describe("Builder", () => {
|
|
|
453
443
|
const manifestStore = reader.json();
|
|
454
444
|
expect(JSON.stringify(manifestStore)).toContain("Test Ingredient");
|
|
455
445
|
expect(JSON.stringify(manifestStore)).toContain("thumbnail.ingredient");
|
|
456
|
-
})
|
|
457
|
-
it("should add ingredient with custom metadata", () =>
|
|
446
|
+
});
|
|
447
|
+
it("should add ingredient with custom metadata", async () => {
|
|
458
448
|
const builder = Builder.new();
|
|
459
449
|
const ingredient = {
|
|
460
450
|
title: "Test Ingredient",
|
|
@@ -467,60 +457,75 @@ describe("Builder", () => {
|
|
|
467
457
|
customBool: true,
|
|
468
458
|
customObject: {
|
|
469
459
|
nested: "value",
|
|
470
|
-
count: 123
|
|
460
|
+
count: 123,
|
|
471
461
|
},
|
|
472
|
-
customArray: ["item1", "item2", "item3"]
|
|
473
|
-
}
|
|
462
|
+
customArray: ["item1", "item2", "item3"],
|
|
463
|
+
},
|
|
474
464
|
};
|
|
475
|
-
|
|
465
|
+
await builder.addIngredient(JSON.stringify(ingredient));
|
|
476
466
|
const definition = builder.getManifestDefinition();
|
|
477
467
|
expect(definition.ingredients).toHaveLength(1);
|
|
478
468
|
expect(definition.ingredients[0]).toMatchObject(ingredient);
|
|
479
|
-
})
|
|
480
|
-
it("should
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
// Create a redacted URI for the assertion we are going to redact
|
|
488
|
-
// Using a common assertion label that might exist
|
|
489
|
-
const assertionLabel = "stds.schema-org.CreativeWork";
|
|
490
|
-
const redactedUri = `contentauth:urn:uuid:${parentManifestLabel}/c2pa.assertions/${assertionLabel}`;
|
|
491
|
-
// Create a builder with edit intent and redactions
|
|
492
|
-
const redactionManifestDefinition = {
|
|
493
|
-
claim_generator: "test-generator",
|
|
494
|
-
claim_generator_info: [
|
|
495
|
-
{
|
|
496
|
-
name: "c2pa_test",
|
|
497
|
-
version: "1.0.0",
|
|
498
|
-
},
|
|
499
|
-
],
|
|
500
|
-
title: "Test_Redaction_Manifest",
|
|
501
|
-
format: "image/jpeg",
|
|
502
|
-
instance_id: "1234",
|
|
503
|
-
intent: "edit",
|
|
504
|
-
redactions: [redactedUri],
|
|
505
|
-
assertions: [
|
|
506
|
-
{
|
|
507
|
-
label: "org.test.assertion",
|
|
508
|
-
data: {},
|
|
509
|
-
},
|
|
510
|
-
],
|
|
511
|
-
resources: { resources: {} },
|
|
469
|
+
});
|
|
470
|
+
it("should redact a thumbnail from an ingredient manifest", async () => {
|
|
471
|
+
const signerConfig = {
|
|
472
|
+
alg: "es256",
|
|
473
|
+
certs: [publicKey],
|
|
474
|
+
reserveSize: 10000,
|
|
475
|
+
tsaUrl: undefined,
|
|
476
|
+
directCoseHandling: false,
|
|
512
477
|
};
|
|
513
|
-
const
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
478
|
+
const testSigner = new TestSigner(privateKey);
|
|
479
|
+
const signer = CallbackSigner.newSigner(signerConfig, testSigner.sign);
|
|
480
|
+
// Sign source asset with a thumbnail
|
|
481
|
+
const step1Builder = Builder.withJson({
|
|
482
|
+
claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
|
|
483
|
+
title: "Asset With Thumbnail",
|
|
484
|
+
format: "image/jpeg",
|
|
485
|
+
instance_id: "thumb-step1-1234",
|
|
486
|
+
thumbnail: { format: "image/jpeg", identifier: "thumbnail.jpg" },
|
|
487
|
+
});
|
|
488
|
+
await step1Builder.addResource("thumbnail.jpg", {
|
|
489
|
+
mimeType: "image/jpeg",
|
|
490
|
+
buffer: testThumbnail,
|
|
491
|
+
});
|
|
492
|
+
const step1Dest = { buffer: null };
|
|
493
|
+
await step1Builder.signAsync(signer, source, step1Dest);
|
|
494
|
+
const step1Asset = {
|
|
495
|
+
buffer: step1Dest.buffer,
|
|
496
|
+
mimeType: "image/jpeg",
|
|
521
497
|
};
|
|
522
|
-
|
|
523
|
-
|
|
498
|
+
// Verify thumbnail exists in original manifest
|
|
499
|
+
const originalReader = await Reader.fromAsset(step1Asset);
|
|
500
|
+
expect(originalReader).not.toBeNull();
|
|
501
|
+
const parentLabel = originalReader.activeLabel();
|
|
502
|
+
expect(parentLabel).toBeDefined();
|
|
503
|
+
const originalManifest = originalReader.getActive();
|
|
504
|
+
expect(originalManifest?.thumbnail).toBeDefined();
|
|
505
|
+
expect(originalManifest?.thumbnail).not.toBeNull();
|
|
506
|
+
// Redact the thumbnail
|
|
507
|
+
const thumbnailUri = `self#jumbf=/c2pa/${parentLabel}/c2pa.assertions/c2pa.thumbnail.claim`;
|
|
508
|
+
const redactionBuilder = Builder.withJson({
|
|
509
|
+
claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
|
|
510
|
+
title: "Redacted Thumbnail Manifest",
|
|
511
|
+
format: "image/jpeg",
|
|
512
|
+
instance_id: "thumb-step2-1234",
|
|
513
|
+
});
|
|
514
|
+
redactionBuilder.setIntent("update");
|
|
515
|
+
redactionBuilder.addRedaction(thumbnailUri, "c2pa.PII.present");
|
|
516
|
+
const step2Dest = { buffer: null };
|
|
517
|
+
await redactionBuilder.signAsync(signer, step1Asset, step2Dest);
|
|
518
|
+
const finalReader = await Reader.fromAsset({
|
|
519
|
+
buffer: step2Dest.buffer,
|
|
520
|
+
mimeType: "image/jpeg",
|
|
521
|
+
});
|
|
522
|
+
expect(finalReader).not.toBeNull();
|
|
523
|
+
const store = finalReader.json();
|
|
524
|
+
const parentManifest = store.manifests?.[parentLabel];
|
|
525
|
+
expect(parentManifest).toBeDefined();
|
|
526
|
+
expect(parentManifest?.thumbnail).toBeUndefined();
|
|
527
|
+
});
|
|
528
|
+
it("should redact an assertion from an ingredient manifest", async () => {
|
|
524
529
|
const signerConfig = {
|
|
525
530
|
alg: "es256",
|
|
526
531
|
certs: [publicKey],
|
|
@@ -530,32 +535,79 @@ describe("Builder", () => {
|
|
|
530
535
|
};
|
|
531
536
|
const testSigner = new TestSigner(privateKey);
|
|
532
537
|
const signer = CallbackSigner.newSigner(signerConfig, testSigner.sign);
|
|
533
|
-
// Sign
|
|
534
|
-
const
|
|
535
|
-
const
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
538
|
+
// Sign source asset with multiple distinct assertions
|
|
539
|
+
const piiLabel = "stds.schema-org.CreativeWork";
|
|
540
|
+
const retainedLabel = "org.contentauth.retained";
|
|
541
|
+
const step1Builder = Builder.withJson({
|
|
542
|
+
claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
|
|
543
|
+
title: "Asset With Multiple Assertions",
|
|
544
|
+
format: "image/jpeg",
|
|
545
|
+
instance_id: "assert-step1-1234",
|
|
546
|
+
assertions: [
|
|
547
|
+
{
|
|
548
|
+
label: piiLabel,
|
|
549
|
+
data: {
|
|
550
|
+
"@context": "http://schema.org/",
|
|
551
|
+
"@type": "CreativeWork",
|
|
552
|
+
author: [{ "@type": "Person", name: "John Doe" }],
|
|
553
|
+
},
|
|
554
|
+
},
|
|
555
|
+
{
|
|
556
|
+
label: retainedLabel,
|
|
557
|
+
data: { keep: true },
|
|
558
|
+
},
|
|
559
|
+
],
|
|
560
|
+
});
|
|
561
|
+
const step1Dest = { buffer: null };
|
|
562
|
+
await step1Builder.signAsync(signer, source, step1Dest);
|
|
563
|
+
const step1Asset = {
|
|
564
|
+
buffer: step1Dest.buffer,
|
|
540
565
|
mimeType: "image/jpeg",
|
|
566
|
+
};
|
|
567
|
+
// Verify both assertions exist in original manifest
|
|
568
|
+
const originalReader = await Reader.fromAsset(step1Asset);
|
|
569
|
+
expect(originalReader).not.toBeNull();
|
|
570
|
+
const parentLabel = originalReader.activeLabel();
|
|
571
|
+
expect(parentLabel).toBeDefined();
|
|
572
|
+
const originalStore = originalReader.json();
|
|
573
|
+
const originalLabels = originalStore.manifests[parentLabel].assertions.map((a) => a.label);
|
|
574
|
+
expect(originalLabels).toContain(piiLabel);
|
|
575
|
+
expect(originalLabels).toContain(retainedLabel);
|
|
576
|
+
// Redact only the PII assertion
|
|
577
|
+
const redactionUri = `self#jumbf=/c2pa/${parentLabel}/c2pa.assertions/${piiLabel}`;
|
|
578
|
+
const redactionBuilder = Builder.withJson({
|
|
579
|
+
claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
|
|
580
|
+
title: "Redacted Assertion Manifest",
|
|
581
|
+
format: "image/jpeg",
|
|
582
|
+
instance_id: "assert-step2-1234",
|
|
541
583
|
});
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
expect(
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
584
|
+
redactionBuilder.setIntent("update");
|
|
585
|
+
redactionBuilder.addRedaction(redactionUri, "c2pa.PII.present");
|
|
586
|
+
const step2Dest = { buffer: null };
|
|
587
|
+
await redactionBuilder.signAsync(signer, step1Asset, step2Dest);
|
|
588
|
+
const finalReader = await Reader.fromAsset({
|
|
589
|
+
buffer: step2Dest.buffer,
|
|
590
|
+
mimeType: "image/jpeg",
|
|
591
|
+
});
|
|
592
|
+
expect(finalReader).not.toBeNull();
|
|
593
|
+
const store = finalReader.json();
|
|
594
|
+
const parentManifest = store.manifests?.[parentLabel];
|
|
595
|
+
expect(parentManifest).toBeDefined();
|
|
596
|
+
const assertionLabels = parentManifest?.assertions?.map((a) => a.label);
|
|
597
|
+
// PII assertion removed, retained assertion still present
|
|
598
|
+
expect(assertionLabels).not.toContain(piiLabel);
|
|
599
|
+
expect(assertionLabels).toContain(retainedLabel);
|
|
600
|
+
});
|
|
601
|
+
it("should add redactions via addRedaction method", () => {
|
|
602
|
+
const uri1 = "self#jumbf=/c2pa/test-label/c2pa.assertions/cawg.identity";
|
|
603
|
+
const uri2 = "self#jumbf=/c2pa/test-label/c2pa.assertions/stds.schema-org.CreativeWork";
|
|
604
|
+
const builder = Builder.new();
|
|
605
|
+
builder.addRedaction(uri1, "c2pa.PII.present");
|
|
606
|
+
builder.addRedaction(uri2, "c2pa.PII.present");
|
|
607
|
+
const definition = builder.getManifestDefinition();
|
|
608
|
+
expect(definition.redactions).toEqual([uri1, uri2]);
|
|
609
|
+
});
|
|
610
|
+
it("should test builder remote url", async () => {
|
|
559
611
|
// This test mirrors the Rust test_builder_remote_url test
|
|
560
612
|
// Create a simple manifest definition similar to simple_manifest_json()
|
|
561
613
|
const simpleManifestDefinition = {
|
|
@@ -596,12 +648,12 @@ describe("Builder", () => {
|
|
|
596
648
|
const signer = CallbackSigner.newSigner(signerConfig, testSigner.sign);
|
|
597
649
|
// Sign the Builder and write it to the output stream
|
|
598
650
|
const dest = { buffer: null };
|
|
599
|
-
const manifestData =
|
|
651
|
+
const manifestData = await builder.signAsync(signer, source, dest);
|
|
600
652
|
// Check to make sure we have a remote url and no manifest data embedded
|
|
601
653
|
// Reading the image directly should fail since no_embed = true
|
|
602
654
|
// Note: This might fail due to remote URL fetch, which is expected behavior
|
|
603
655
|
try {
|
|
604
|
-
|
|
656
|
+
await Reader.fromAsset({
|
|
605
657
|
buffer: dest.buffer,
|
|
606
658
|
mimeType: "image/jpeg",
|
|
607
659
|
});
|
|
@@ -613,17 +665,16 @@ describe("Builder", () => {
|
|
|
613
665
|
expect(error).toBeDefined();
|
|
614
666
|
}
|
|
615
667
|
// Now validate the manifest against the written asset using the separate manifest data
|
|
616
|
-
const reader =
|
|
668
|
+
const reader = await Reader.fromManifestDataAndAsset(manifestData, {
|
|
617
669
|
buffer: dest.buffer,
|
|
618
670
|
mimeType: "image/jpeg",
|
|
619
671
|
});
|
|
620
672
|
// Check if the manifest has the expected structure
|
|
621
673
|
const activeManifest = reader.getActive();
|
|
622
674
|
expect(activeManifest).toBeDefined();
|
|
623
|
-
expect(activeManifest
|
|
624
|
-
})
|
|
625
|
-
it("should add action using addAction method", () =>
|
|
626
|
-
var _a;
|
|
675
|
+
expect(activeManifest?.title).toBe("Test_Manifest");
|
|
676
|
+
});
|
|
677
|
+
it("should add action using addAction method", async () => {
|
|
627
678
|
const simpleManifestDefinition = {
|
|
628
679
|
claim_generator_info: [
|
|
629
680
|
{
|
|
@@ -648,23 +699,22 @@ describe("Builder", () => {
|
|
|
648
699
|
const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
|
|
649
700
|
builder.sign(signer, source, dest);
|
|
650
701
|
// Verify the action was added
|
|
651
|
-
const reader =
|
|
702
|
+
const reader = await Reader.fromAsset(dest);
|
|
652
703
|
expect(reader).not.toBeNull();
|
|
653
704
|
const activeManifest = reader.getActive();
|
|
654
|
-
const actionsAssertion =
|
|
705
|
+
const actionsAssertion = activeManifest?.assertions?.find((a) => a.label === "c2pa.actions.v2");
|
|
655
706
|
expect(actionsAssertion).toBeDefined();
|
|
656
707
|
if (isActionsAssertion(actionsAssertion)) {
|
|
657
708
|
const actions = actionsAssertion.data.actions;
|
|
658
709
|
const editedAction = actions.find((a) => a.action === "c2pa.edited");
|
|
659
710
|
expect(editedAction).toBeDefined();
|
|
660
|
-
expect(editedAction
|
|
711
|
+
expect(editedAction?.action).toBe("c2pa.edited");
|
|
661
712
|
}
|
|
662
713
|
else {
|
|
663
714
|
throw new Error("Actions assertion does not have the expected structure");
|
|
664
715
|
}
|
|
665
|
-
})
|
|
666
|
-
it("should add ingredient using addIngredient method", () =>
|
|
667
|
-
var _a, _b;
|
|
716
|
+
});
|
|
717
|
+
it("should add ingredient using addIngredient method", async () => {
|
|
668
718
|
const simpleManifestDefinition = {
|
|
669
719
|
claim_generator_info: [
|
|
670
720
|
{
|
|
@@ -704,18 +754,17 @@ describe("Builder", () => {
|
|
|
704
754
|
const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
|
|
705
755
|
builder.sign(signer, source, dest);
|
|
706
756
|
// Verify the ingredient was added
|
|
707
|
-
const reader =
|
|
757
|
+
const reader = await Reader.fromAsset(dest);
|
|
708
758
|
expect(reader).not.toBeNull();
|
|
709
759
|
const activeManifest = reader.getActive();
|
|
710
|
-
expect(activeManifest
|
|
711
|
-
expect(
|
|
712
|
-
const addedIngredient =
|
|
760
|
+
expect(activeManifest?.ingredients).toBeDefined();
|
|
761
|
+
expect(activeManifest?.ingredients?.length).toBeGreaterThan(0);
|
|
762
|
+
const addedIngredient = activeManifest?.ingredients?.find((ing) => ing.title === "Test Ingredient via addIngredient");
|
|
713
763
|
expect(addedIngredient).toBeDefined();
|
|
714
|
-
expect(addedIngredient
|
|
715
|
-
expect(addedIngredient
|
|
716
|
-
})
|
|
717
|
-
it("should add ingredient from reader", () =>
|
|
718
|
-
var _a;
|
|
764
|
+
expect(addedIngredient?.instance_id).toBe("ingredient-12345");
|
|
765
|
+
expect(addedIngredient?.relationship).toBe("componentOf");
|
|
766
|
+
});
|
|
767
|
+
it("should add ingredient from reader", async () => {
|
|
719
768
|
const builder1 = Builder.new();
|
|
720
769
|
builder1.setIntent("edit");
|
|
721
770
|
const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
|
|
@@ -724,7 +773,7 @@ describe("Builder", () => {
|
|
|
724
773
|
};
|
|
725
774
|
builder1.sign(signer, source, dest1);
|
|
726
775
|
// Read the signed file back with Reader
|
|
727
|
-
const reader =
|
|
776
|
+
const reader = await Reader.fromAsset({
|
|
728
777
|
buffer: dest1.buffer,
|
|
729
778
|
mimeType: "image/jpeg",
|
|
730
779
|
});
|
|
@@ -743,14 +792,14 @@ describe("Builder", () => {
|
|
|
743
792
|
};
|
|
744
793
|
builder2.sign(signer, source, dest2);
|
|
745
794
|
// Verify the ingredient is in the signed manifest
|
|
746
|
-
const reader2 =
|
|
795
|
+
const reader2 = await Reader.fromAsset({
|
|
747
796
|
buffer: dest2.buffer,
|
|
748
797
|
mimeType: "image/jpeg",
|
|
749
798
|
});
|
|
750
799
|
expect(reader2).not.toBeNull();
|
|
751
800
|
const activeManifest = reader2.getActive();
|
|
752
|
-
expect(activeManifest
|
|
753
|
-
expect(
|
|
754
|
-
})
|
|
801
|
+
expect(activeManifest?.ingredients).toBeDefined();
|
|
802
|
+
expect(activeManifest?.ingredients?.length).toBe(1);
|
|
803
|
+
});
|
|
755
804
|
});
|
|
756
805
|
});
|