@contentauth/c2pa-node 0.1.3 → 0.2.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.
@@ -1,42 +1,8 @@
1
- "use strict";
2
1
  // Copyright 2024 Adobe. All rights reserved.
3
2
  // This file is licensed to you under the Apache License,
4
3
  // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
5
4
  // or the MIT license (http://opensource.org/licenses/MIT),
6
5
  // at your option.
7
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
8
- if (k2 === undefined) k2 = k;
9
- var desc = Object.getOwnPropertyDescriptor(m, k);
10
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
11
- desc = { enumerable: true, get: function() { return m[k]; } };
12
- }
13
- Object.defineProperty(o, k2, desc);
14
- }) : (function(o, m, k, k2) {
15
- if (k2 === undefined) k2 = k;
16
- o[k2] = m[k];
17
- }));
18
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
19
- Object.defineProperty(o, "default", { enumerable: true, value: v });
20
- }) : function(o, v) {
21
- o["default"] = v;
22
- });
23
- var __importStar = (this && this.__importStar) || (function () {
24
- var ownKeys = function(o) {
25
- ownKeys = Object.getOwnPropertyNames || function (o) {
26
- var ar = [];
27
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
28
- return ar;
29
- };
30
- return ownKeys(o);
31
- };
32
- return function (mod) {
33
- if (mod && mod.__esModule) return mod;
34
- var result = {};
35
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
36
- __setModuleDefault(result, mod);
37
- return result;
38
- };
39
- })();
40
6
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
41
7
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
42
8
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -46,17 +12,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
46
12
  step((generator = generator.apply(thisArg, _arguments || [])).next());
47
13
  });
48
14
  };
49
- var __importDefault = (this && this.__importDefault) || function (mod) {
50
- return (mod && mod.__esModule) ? mod : { "default": mod };
51
- };
52
- Object.defineProperty(exports, "__esModule", { value: true });
53
- const Signer_1 = require("./Signer");
54
- const Reader_1 = require("./Reader");
55
- const Builder_1 = require("./Builder");
56
- const fs = __importStar(require("fs-extra"));
57
- const path_1 = __importDefault(require("path"));
58
- const crypto = __importStar(require("crypto"));
59
- const tempDir = path_1.default.join(__dirname, "tmp");
15
+ import { isActionsAssertion } from "./assertions";
16
+ import { CallbackSigner, LocalSigner } from "./Signer";
17
+ import { Reader } from "./Reader";
18
+ import { Builder } from "./Builder";
19
+ import * as fs from "fs-extra";
20
+ import path from "path";
21
+ import * as crypto from "crypto";
22
+ const tempDir = path.join(__dirname, "tmp");
60
23
  class TestSigner {
61
24
  constructor(privateKey) {
62
25
  this.sign = (bytes) => __awaiter(this, void 0, void 0, function* () {
@@ -93,20 +56,15 @@ describe("Builder", () => {
93
56
  };
94
57
  const manifestDefinition = {
95
58
  vendor: "test",
59
+ claim_generator: "test-generator",
96
60
  claim_generator_info: [
97
61
  {
98
62
  name: "c2pa_test",
99
63
  version: "1.0.0",
100
64
  },
101
65
  ],
102
- metadata: [
103
- {
104
- dateTime: "1985-04-12T23:20:50.52Z",
105
- my_custom_metadata: "my custom metatdata value",
106
- },
107
- ],
108
66
  title: "Test_Manifest",
109
- format: "image/tiff",
67
+ format: "image/jpeg",
110
68
  instance_id: "1234",
111
69
  thumbnail: {
112
70
  format: "image/jpeg",
@@ -118,14 +76,17 @@ describe("Builder", () => {
118
76
  format: "image/jpeg",
119
77
  instance_id: "12345",
120
78
  relationship: "componentOf",
79
+ thumbnail: { format: "image/jpeg", identifier: "ingredient-thumb.jpg" },
80
+ resources: { resources: {} },
121
81
  },
122
82
  ],
123
83
  assertions: [
124
84
  {
125
85
  label: "org.test.assertion",
126
- data: "assertion",
86
+ data: {},
127
87
  },
128
88
  ],
89
+ resources: { resources: {} },
129
90
  };
130
91
  let testAssertion;
131
92
  let testThumbnail;
@@ -137,7 +98,7 @@ describe("Builder", () => {
137
98
  // Read all test files once
138
99
  source = {
139
100
  buffer: yield fs.readFile("./tests/fixtures/CA.jpg"),
140
- mimeType: "jpeg",
101
+ mimeType: "image/jpeg",
141
102
  };
142
103
  testThumbnail = yield fs.readFile("./tests/fixtures/thumbnail.jpg");
143
104
  publicKey = yield fs.readFile("./tests/fixtures/certs/es256.pub");
@@ -149,6 +110,7 @@ describe("Builder", () => {
149
110
  }));
150
111
  it("should build a manifest store", () => __awaiter(void 0, void 0, void 0, function* () {
151
112
  const test_definition = {
113
+ claim_generator: "test-generator",
152
114
  claim_generator_info: [
153
115
  {
154
116
  name: "c2pa-js tests",
@@ -161,8 +123,11 @@ describe("Builder", () => {
161
123
  vendor: "test",
162
124
  thumbnail: thumbnail_ref,
163
125
  label: "ABCDE",
126
+ ingredients: [],
127
+ assertions: [],
128
+ resources: { resources: {} },
164
129
  };
165
- const builder = Builder_1.Builder.withJson(test_definition);
130
+ const builder = Builder.withJson(test_definition);
166
131
  yield builder.addIngredient(parent_json, source);
167
132
  builder.addAssertion("org.test.assertion", "assertion");
168
133
  yield builder.addResource(thumbnail_ref.identifier, {
@@ -183,8 +148,8 @@ describe("Builder", () => {
183
148
  describe("Sign and Archive", () => {
184
149
  let builder;
185
150
  beforeEach(() => {
186
- builder = Builder_1.Builder.withJson(manifestDefinition);
187
- builder.updateManifestProperty("claim_version", 1);
151
+ builder = Builder.withJson(manifestDefinition);
152
+ builder.updateManifestProperty("claim_version", 2);
188
153
  });
189
154
  beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
190
155
  // Add ingredients and resources after builder is initialized
@@ -193,6 +158,10 @@ describe("Builder", () => {
193
158
  mimeType: "jpeg",
194
159
  buffer: testThumbnail,
195
160
  });
161
+ yield builder.addResource("ingredient-thumb.jpg", {
162
+ mimeType: "jpeg",
163
+ buffer: testThumbnail,
164
+ });
196
165
  yield builder.addResource("prompt.txt", {
197
166
  buffer: Buffer.from("a random prompt"),
198
167
  mimeType: "text/plain",
@@ -203,28 +172,56 @@ describe("Builder", () => {
203
172
  yield Promise.resolve();
204
173
  }));
205
174
  it("should sign data", () => __awaiter(void 0, void 0, void 0, function* () {
206
- const dest = { path: path_1.default.join(tempDir, "signed.jpg") };
207
- const signer = Signer_1.LocalSigner.newSigner(publicKey, privateKey, "es256");
175
+ const dest = { path: path.join(tempDir, "signed.jpg") };
176
+ const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
208
177
  const bytes = builder.sign(signer, source, dest);
209
178
  expect(bytes.length).toBeGreaterThan(0);
210
- const reader = yield Reader_1.Reader.fromAsset(dest);
179
+ const reader = yield Reader.fromAsset(dest);
211
180
  const manifestStore = reader.json();
212
181
  const activeManifest = reader.getActive();
213
182
  expect(manifestStore.validation_status).toBeUndefined();
214
183
  expect(manifestStore.active_manifest).not.toBeUndefined();
215
184
  expect(activeManifest === null || activeManifest === void 0 ? void 0 : activeManifest.title).toBe("Test_Manifest");
216
185
  }));
186
+ it("should add a CBOR assertion, sign, and verify it in the signed manifest", () => __awaiter(void 0, void 0, void 0, function* () {
187
+ var _a;
188
+ // Add the c2pa.watermarked action as a CBOR assertion
189
+ const actionsAssertion = {
190
+ actions: [
191
+ {
192
+ action: "c2pa.watermarked",
193
+ },
194
+ ],
195
+ };
196
+ builder.addAssertion("c2pa.actions", actionsAssertion, "Cbor");
197
+ // Sign the manifest
198
+ const dest = { path: path.join(tempDir, "cbor_signed.jpg") };
199
+ const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
200
+ builder.sign(signer, source, dest);
201
+ // Read and verify the assertion in the signed manifest
202
+ const reader = yield Reader.fromAsset(dest);
203
+ const activeManifest = reader.getActive();
204
+ const cborAssertion = (_a = activeManifest === null || activeManifest === void 0 ? void 0 : activeManifest.assertions) === null || _a === void 0 ? void 0 : _a.find((a) => a.label === "c2pa.actions.v2");
205
+ expect(cborAssertion).toBeDefined();
206
+ if (isActionsAssertion(cborAssertion)) {
207
+ const actions = cborAssertion.data.actions.map((a) => a.action);
208
+ expect(actions).toContain("c2pa.watermarked");
209
+ }
210
+ else {
211
+ throw new Error("CBOR assertion does not have the expected structure");
212
+ }
213
+ }));
217
214
  it("should archive and construct from archive", () => __awaiter(void 0, void 0, void 0, function* () {
218
215
  const dest = {
219
216
  buffer: null,
220
217
  };
221
- const signer = Signer_1.LocalSigner.newSigner(publicKey, privateKey, "es256");
222
- const outputPath = path_1.default.join(tempDir, "archive.zip");
218
+ const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
219
+ const outputPath = path.join(tempDir, "archive.zip");
223
220
  const archive = { path: outputPath };
224
221
  yield builder.toArchive(archive);
225
- const builder2 = yield Builder_1.Builder.fromArchive(archive);
222
+ const builder2 = yield Builder.fromArchive(archive);
226
223
  builder2.sign(signer, source, dest);
227
- const reader = yield Reader_1.Reader.fromAsset({
224
+ const reader = yield Reader.fromAsset({
228
225
  buffer: dest.buffer,
229
226
  mimeType: "jpeg",
230
227
  });
@@ -235,17 +232,20 @@ describe("Builder", () => {
235
232
  expect(activeManifest === null || activeManifest === void 0 ? void 0 : activeManifest.title).toBe("Test_Manifest");
236
233
  }));
237
234
  it("should sign data with callback to file", () => __awaiter(void 0, void 0, void 0, function* () {
238
- const dest = { path: path_1.default.join(tempDir, "callback_signed.jpg") };
235
+ const dest = {
236
+ path: path.join(tempDir, "callback_signed.jpg"),
237
+ };
239
238
  const signerConfig = {
240
239
  alg: "es256",
241
240
  certs: [publicKey],
242
241
  reserveSize: 10000,
243
242
  tsaUrl: undefined,
243
+ directCoseHandling: false,
244
244
  };
245
245
  const signer = new TestSigner(privateKey);
246
246
  const bytes = yield builder.signConfigAsync(signer.sign, signerConfig, source, dest);
247
247
  expect(bytes.length).toBeGreaterThan(0);
248
- const reader = yield Reader_1.Reader.fromAsset(dest);
248
+ const reader = yield Reader.fromAsset(dest);
249
249
  const manifestStore = reader.json();
250
250
  const activeManifest = reader.getActive();
251
251
  expect(manifestStore.validation_status).toBeUndefined();
@@ -261,11 +261,12 @@ describe("Builder", () => {
261
261
  certs: [publicKey],
262
262
  reserveSize: 10000,
263
263
  tsaUrl: undefined,
264
+ directCoseHandling: false,
264
265
  };
265
266
  const signer = new TestSigner(privateKey);
266
267
  const bytes = yield builder.signConfigAsync(signer.sign, signerConfig, source, dest);
267
268
  expect(bytes.length).toBeGreaterThan(0);
268
- const reader = yield Reader_1.Reader.fromAsset({
269
+ const reader = yield Reader.fromAsset({
269
270
  buffer: dest.buffer,
270
271
  mimeType: "jpeg",
271
272
  });
@@ -284,12 +285,13 @@ describe("Builder", () => {
284
285
  certs: [publicKey],
285
286
  reserveSize: 10000,
286
287
  tsaUrl: undefined,
288
+ directCoseHandling: false,
287
289
  };
288
290
  const testSigner = new TestSigner(privateKey);
289
- const signer = Signer_1.CallbackSigner.newSigner(signerConfig, testSigner.sign);
291
+ const signer = CallbackSigner.newSigner(signerConfig, testSigner.sign);
290
292
  const bytes = yield builder.signAsync(signer, source, dest);
291
293
  expect(bytes.length).toBeGreaterThan(0);
292
- const reader = yield Reader_1.Reader.fromAsset({
294
+ const reader = yield Reader.fromAsset({
293
295
  buffer: dest.buffer,
294
296
  mimeType: "jpeg",
295
297
  });
@@ -299,5 +301,93 @@ describe("Builder", () => {
299
301
  expect(manifestStore.active_manifest).not.toBeUndefined();
300
302
  expect(activeManifest === null || activeManifest === void 0 ? void 0 : activeManifest.title).toBe("Test_Manifest");
301
303
  }));
304
+ it("should preserve JSON assertion characters without escaping", () => __awaiter(void 0, void 0, void 0, function* () {
305
+ var _a;
306
+ const fingerprintAssertion = JSON.stringify({
307
+ alg: "sha256",
308
+ blocks: [
309
+ {
310
+ scope: {},
311
+ value: "test-fingerprint-data",
312
+ },
313
+ ],
314
+ });
315
+ builder.addAssertion("org.contentauth.fingerprint", fingerprintAssertion, "Json");
316
+ const dest = { path: path.join(tempDir, "json_test.jpg") };
317
+ const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
318
+ builder.sign(signer, source, dest);
319
+ const reader = yield Reader.fromAsset(dest);
320
+ const manifest = reader.json();
321
+ // Check that our specific JSON assertion doesn't have escaped characters
322
+ const activeManifest = manifest.manifests[manifest.active_manifest];
323
+ const fingerprintAssertionData = (_a = activeManifest === null || activeManifest === void 0 ? void 0 : activeManifest.assertions) === null || _a === void 0 ? void 0 : _a.find((a) => a.label === "org.contentauth.fingerprint");
324
+ expect(fingerprintAssertionData).toBeDefined();
325
+ expect(fingerprintAssertionData === null || fingerprintAssertionData === void 0 ? void 0 : fingerprintAssertionData.data).toEqual({
326
+ alg: "sha256",
327
+ blocks: [
328
+ {
329
+ scope: {},
330
+ value: "test-fingerprint-data",
331
+ },
332
+ ],
333
+ });
334
+ // The assertion data should not be a string with escaped quotes
335
+ expect(typeof (fingerprintAssertionData === null || fingerprintAssertionData === void 0 ? void 0 : fingerprintAssertionData.data)).toBe("object");
336
+ expect(JSON.stringify(fingerprintAssertionData === null || fingerprintAssertionData === void 0 ? void 0 : fingerprintAssertionData.data)).not.toContain("\\");
337
+ }));
338
+ it("should archive and restore builder with ingredient thumbnail", () => __awaiter(void 0, void 0, void 0, function* () {
339
+ const manifestDefinition = {
340
+ claim_generator_info: [
341
+ {
342
+ name: "c2pa_test",
343
+ version: "1.0.0",
344
+ },
345
+ ],
346
+ title: "Test_Manifest",
347
+ format: "image/jpeg",
348
+ ingredients: [],
349
+ assertions: [
350
+ {
351
+ label: "c2pa.actions",
352
+ data: {
353
+ actions: [
354
+ {
355
+ action: "c2pa.created",
356
+ digitalSourceType: "http://c2pa.org/digitalsourcetype/empty",
357
+ },
358
+ ],
359
+ },
360
+ },
361
+ ],
362
+ resources: { resources: {} },
363
+ };
364
+ const builder = Builder.withJson(manifestDefinition);
365
+ const ingredientJson = '{"title": "Test Ingredient"}';
366
+ const testThumbnail = yield fs.readFile("./tests/fixtures/thumbnail.jpg");
367
+ yield builder.addIngredient(ingredientJson, {
368
+ buffer: testThumbnail,
369
+ mimeType: "jpeg",
370
+ });
371
+ // Archive the builder
372
+ const archivePath = path.join(tempDir, `ingredient_thumb_archive_${Date.now()}.zip`);
373
+ yield builder.toArchive({ path: archivePath });
374
+ // Ensure the file is written to disk before proceeding
375
+ yield fs.access(archivePath);
376
+ // Restore from archive
377
+ const builder2 = yield Builder.fromArchive({ path: archivePath });
378
+ // Sign with restored builder
379
+ const dest = { buffer: null };
380
+ const publicKey = yield fs.readFile("./tests/fixtures/certs/es256.pub");
381
+ const privateKey = yield fs.readFile("./tests/fixtures/certs/es256.pem");
382
+ const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
383
+ builder2.sign(signer, source, dest);
384
+ const reader = yield Reader.fromAsset({
385
+ buffer: dest.buffer,
386
+ mimeType: "jpeg",
387
+ });
388
+ const manifestStore = reader.json();
389
+ expect(JSON.stringify(manifestStore)).toContain("Test Ingredient");
390
+ expect(JSON.stringify(manifestStore)).toContain("thumbnail.ingredient");
391
+ }));
302
392
  });
303
393
  });
@@ -1,42 +1,8 @@
1
- "use strict";
2
1
  // Copyright 2024 Adobe. All rights reserved.
3
2
  // This file is licensed to you under the Apache License,
4
3
  // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
5
4
  // or the MIT license (http://opensource.org/licenses/MIT),
6
5
  // at your option.
7
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
8
- if (k2 === undefined) k2 = k;
9
- var desc = Object.getOwnPropertyDescriptor(m, k);
10
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
11
- desc = { enumerable: true, get: function() { return m[k]; } };
12
- }
13
- Object.defineProperty(o, k2, desc);
14
- }) : (function(o, m, k, k2) {
15
- if (k2 === undefined) k2 = k;
16
- o[k2] = m[k];
17
- }));
18
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
19
- Object.defineProperty(o, "default", { enumerable: true, value: v });
20
- }) : function(o, v) {
21
- o["default"] = v;
22
- });
23
- var __importStar = (this && this.__importStar) || (function () {
24
- var ownKeys = function(o) {
25
- ownKeys = Object.getOwnPropertyNames || function (o) {
26
- var ar = [];
27
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
28
- return ar;
29
- };
30
- return ownKeys(o);
31
- };
32
- return function (mod) {
33
- if (mod && mod.__esModule) return mod;
34
- var result = {};
35
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
36
- __setModuleDefault(result, mod);
37
- return result;
38
- };
39
- })();
40
6
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
41
7
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
42
8
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -46,16 +12,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
46
12
  step((generator = generator.apply(thisArg, _arguments || [])).next());
47
13
  });
48
14
  };
49
- Object.defineProperty(exports, "__esModule", { value: true });
50
- exports.IdentityAssertionSigner = exports.IdentityAssertionBuilder = void 0;
51
15
  // Unless required by applicable law or agreed to in writing,
52
16
  // this software is distributed on an "AS IS" BASIS, WITHOUT
53
17
  // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
54
18
  // implied. See the LICENSE-MIT and LICENSE-APACHE files for the
55
19
  // specific language governing permissions and limitations under
56
20
  // each license.
57
- const neon = __importStar(require("./index.node"));
58
- class IdentityAssertionBuilder {
21
+ const neon = require("./index.node");
22
+ export class IdentityAssertionBuilder {
59
23
  constructor(_builder) {
60
24
  this._builder = _builder;
61
25
  }
@@ -75,13 +39,12 @@ class IdentityAssertionBuilder {
75
39
  return this._builder;
76
40
  }
77
41
  }
78
- exports.IdentityAssertionBuilder = IdentityAssertionBuilder;
79
- class IdentityAssertionSigner {
42
+ export class IdentityAssertionSigner {
80
43
  constructor(_signer) {
81
44
  this._signer = _signer;
82
45
  }
83
46
  static new(signer) {
84
- const identitySigner = neon.identitySignerNew(signer.signer());
47
+ const identitySigner = neon.identitySignerNew(signer);
85
48
  return new IdentityAssertionSigner(identitySigner);
86
49
  }
87
50
  addIdentityAssertion(identityAssertionBuilder) {
@@ -91,4 +54,26 @@ class IdentityAssertionSigner {
91
54
  return this._signer;
92
55
  }
93
56
  }
94
- exports.IdentityAssertionSigner = IdentityAssertionSigner;
57
+ export class CallbackCredentialHolder {
58
+ constructor(callbackCredentialHolder) {
59
+ this.callbackCredentialHolder = callbackCredentialHolder;
60
+ }
61
+ signer() {
62
+ return this.callbackCredentialHolder;
63
+ }
64
+ static newCallbackCredentialHolder(reserveSize, sigType, callback) {
65
+ const credentialHolder = neon.newCallbackCredentialHolder(reserveSize, sigType, callback);
66
+ return new CallbackCredentialHolder(credentialHolder);
67
+ }
68
+ sign(payload) {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ return neon.callbackSignerSignPayload.call(this.callbackCredentialHolder, payload);
71
+ });
72
+ }
73
+ reserveSize() {
74
+ return neon.callbackCredentialHolderReserveSize.call(this.callbackCredentialHolder);
75
+ }
76
+ sigType() {
77
+ return neon.callbackCredentialHolderSigType.call(this.callbackCredentialHolder);
78
+ }
79
+ }