@contentauth/c2pa-node 0.4.2 → 0.5.1

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 CHANGED
@@ -11,7 +11,7 @@ crate-type = ["cdylib"]
11
11
  [dependencies]
12
12
  async-trait = "0.1.77"
13
13
  ciborium = "0.2.2"
14
- c2pa = { version = "0.72.0", default-features = false, features = ["file_io", "pdf", "fetch_remote_manifests", "add_thumbnails", "rust_native_crypto", "default_http"] }
14
+ c2pa = { version = "0.75.6", default-features = false, features = ["file_io", "pdf", "fetch_remote_manifests", "add_thumbnails", "rust_native_crypto", "default_http"] }
15
15
  futures = "0.3"
16
16
  image = "0.25.6"
17
17
  neon = { version = "1.0.0", default-features = false, features = [
package/README.md CHANGED
@@ -49,7 +49,7 @@ This command will download precompiled binaries for the following systems:
49
49
 
50
50
  ### Reader
51
51
 
52
- The `Reader` class is used to read and validate C2PA manifests from media files. It can parse embedded manifests or fetch remote manifests.
52
+ The `Reader` class is used to read and validate C2PA manifests from media files. It can parse embedded manifests or fetch remote manifests. Refer to the [Rust SDK](https://github.com/contentauth/c2pa-rs) for the list of settings and their effects.
53
53
 
54
54
  ```javascript
55
55
  import { Reader } from '@contentauth/c2pa-node';
@@ -57,6 +57,15 @@ import { Reader } from '@contentauth/c2pa-node';
57
57
  // Read from an asset file
58
58
  const reader = await Reader.fromAsset(inputAsset);
59
59
 
60
+ // Read with custom settings
61
+ const settings = {
62
+ verify: {
63
+ verify_after_reading: false,
64
+ verify_trust: true
65
+ }
66
+ };
67
+ const reader = await Reader.fromAsset(inputAsset, settings);
68
+
60
69
  // Read from manifest data and asset
61
70
  const reader = await Reader.fromManifestDataAndAsset(manifestData, asset);
62
71
 
@@ -75,7 +84,7 @@ const remoteUrl = reader.remoteUrl();
75
84
 
76
85
  ### Builder
77
86
 
78
- The `Builder` class is the main component for creating and signing C2PA manifests. It provides methods to add assertions, resources, and ingredients to manifests, and handles the signing process. Use the `Signer` class to sign the manifests.
87
+ The `Builder` class is the main component for creating and signing C2PA manifests. It provides methods to add assertions, resources, and ingredients to manifests, and handles the signing process. Use the `Signer` class to sign the manifests. Refer to the [Rust SDK](https://github.com/contentauth/c2pa-rs) for the list of settings and their effects.
79
88
 
80
89
  ```javascript
81
90
  import { Builder } from '@contentauth/c2pa-node';
@@ -83,9 +92,20 @@ import { Builder } from '@contentauth/c2pa-node';
83
92
  // Create a new builder
84
93
  const builder = Builder.new();
85
94
 
95
+ // Create with custom settings
96
+ const settings = {
97
+ builder: {
98
+ generate_c2pa_archive: true
99
+ }
100
+ };
101
+ const builder = Builder.new(settings);
102
+
86
103
  // Or create from an existing manifest definition
87
104
  const builder = Builder.withJson(manifestDefinition);
88
105
 
106
+ // Or create with both manifest and settings
107
+ const builder = Builder.withJson(manifestDefinition, settings);
108
+
89
109
  // Add assertions to the manifest
90
110
  builder.addAssertion('c2pa.actions', actionsAssertion);
91
111
 
@@ -214,40 +234,116 @@ const decodedData = await trustmark.decode(imageBuffer);
214
234
 
215
235
  ### Settings and Configuration
216
236
 
217
- The library provides comprehensive settings management for trust configuration, verification settings, and global C2PA settings:
237
+ The library provides comprehensive settings management that can be configured per Reader/Builder instance or using helper functions. Refer to the [Rust SDK](https://github.com/contentauth/c2pa-rs) for the list of settings and their effects.
238
+
239
+ #### Per-Instance Settings
240
+
241
+ Settings can be passed directly to `Reader` and `Builder` constructors:
242
+
243
+ ```javascript
244
+ import { Reader, Builder } from '@contentauth/c2pa-node';
245
+
246
+ // Create settings object
247
+ const settings = {
248
+ verify: {
249
+ verify_after_reading: false,
250
+ verify_after_sign: false,
251
+ verify_trust: true,
252
+ ocsp_fetch: true
253
+ },
254
+ trust: {
255
+ verify_trust_list: true,
256
+ trust_anchors: "path/to/anchors.pem"
257
+ }
258
+ };
259
+
260
+ // Pass settings to Reader
261
+ const reader = await Reader.fromAsset(inputAsset, settings);
262
+
263
+ // Pass settings to Builder
264
+ const builder = Builder.new(settings);
265
+
266
+ // Settings can also be JSON strings
267
+ const settingsJson = JSON.stringify(settings);
268
+ const builder2 = Builder.new(settingsJson);
269
+ ```
270
+
271
+ #### Settings Helper Functions
272
+
273
+ The library provides helper functions to create and manage settings objects:
218
274
 
219
275
  ```javascript
220
276
  import {
221
- loadC2paSettings,
222
- loadTrustConfig,
223
- loadVerifyConfig,
277
+ createTrustSettings,
278
+ createCawgTrustSettings,
279
+ createVerifySettings,
280
+ mergeSettings,
281
+ settingsToJson,
224
282
  loadSettingsFromFile,
225
283
  loadSettingsFromUrl
226
284
  } from '@contentauth/c2pa-node';
227
285
 
228
- // Load settings from JSON string
229
- loadC2paSettings('{"trust": {"verify_trust_list": true}}');
230
-
231
- // Load settings from file
232
- await loadSettingsFromFile('./c2pa-settings.json');
233
-
234
- // Load settings from URL
235
- await loadSettingsFromUrl('https://example.com/c2pa-settings.json');
286
+ // Create trust settings
287
+ const trustSettings = createTrustSettings({
288
+ verifyTrustList: true,
289
+ userAnchors: "path/to/user-anchors.pem",
290
+ trustAnchors: "path/to/trust-anchors.pem",
291
+ allowedList: "path/to/allowed-list.pem"
292
+ });
236
293
 
237
- // Configure trust settings
238
- loadTrustConfig({
294
+ // Create CAWG trust settings
295
+ const cawgTrustSettings = createCawgTrustSettings({
239
296
  verifyTrustList: true,
240
- userAnchors: ['anchor1', 'anchor2'],
241
- trustAnchors: ['trust-anchor1'],
242
- allowedList: ['allowed-cert1']
297
+ trustAnchors: "path/to/cawg-anchors.pem"
243
298
  });
244
299
 
245
- // Configure verification settings
246
- loadVerifyConfig({
247
- verifyAfterReading: true,
248
- verifyAfterSign: true,
300
+ // Create verify settings
301
+ const verifySettings = createVerifySettings({
302
+ verifyAfterReading: false,
303
+ verifyAfterSign: false,
249
304
  verifyTrust: true,
305
+ verifyTimestampTrust: true,
250
306
  ocspFetch: true,
251
- remoteManifestFetch: true
307
+ remoteManifestFetch: true,
308
+ skipIngredientConflictResolution: false,
309
+ strictV1Validation: false
252
310
  });
311
+
312
+ // Merge multiple settings
313
+ const combinedSettings = mergeSettings(trustSettings, verifySettings);
314
+
315
+ // Convert settings to JSON string
316
+ const jsonString = settingsToJson(combinedSettings);
317
+
318
+ // Load settings from file (JSON or TOML)
319
+ const fileSettings = await loadSettingsFromFile('./c2pa-settings.toml');
320
+ const reader = await Reader.fromAsset(inputAsset, fileSettings);
321
+
322
+ // Load settings from URL
323
+ const urlSettings = await loadSettingsFromUrl('https://example.com/c2pa-settings.json');
324
+ const builder = Builder.new(urlSettings);
253
325
  ```
326
+
327
+ #### Available Settings
328
+
329
+ **Trust Settings:**
330
+ - `verifyTrustList` - Whether to verify against the trust list
331
+ - `userAnchors` - User-provided trust anchors (PEM format or path)
332
+ - `trustAnchors` - Trust anchors for validation (PEM format or path)
333
+ - `trustConfig` - Path to trust configuration file
334
+ - `allowedList` - Allowed list of certificates (PEM format or path)
335
+
336
+ **Verify Settings:**
337
+ - `verifyAfterReading` - Whether to verify after reading a manifest
338
+ - `verifyAfterSign` - Whether to verify after signing a manifest
339
+ - `verifyTrust` - Whether to verify trust during validation
340
+ - `verifyTimestampTrust` - Whether to verify timestamp trust
341
+ - `ocspFetch` - Whether to fetch OCSP responses
342
+ - `remoteManifestFetch` - Whether to fetch remote manifests
343
+ - `skipIngredientConflictResolution` - Whether to skip ingredient conflict resolution
344
+ - `strictV1Validation` - Whether to use strict v1 validation
345
+
346
+ **Builder Settings:**
347
+ - `generate_c2pa_archive` - Whether to generate C2PA archive format
348
+
349
+ **Note:** Settings are passed as per-instance configuration. There are no global settings that affect all Readers and Builders.
package/dist/Builder.js CHANGED
@@ -18,11 +18,16 @@ export class Builder {
18
18
  constructor(builder) {
19
19
  this.builder = builder;
20
20
  }
21
- static new() {
22
- const builder = getNeonBinary().builderNew();
21
+ static new(settings) {
22
+ const settingsStr = settings
23
+ ? typeof settings === "string"
24
+ ? settings
25
+ : JSON.stringify(settings)
26
+ : undefined;
27
+ const builder = getNeonBinary().builderNew(settingsStr);
23
28
  return new Builder(builder);
24
29
  }
25
- static withJson(json) {
30
+ static withJson(json, settings) {
26
31
  let jsonString;
27
32
  try {
28
33
  jsonString = JSON.stringify(json);
@@ -34,7 +39,12 @@ export class Builder {
34
39
  }
35
40
  throw new Error("Failed to stringify JSON Manifest Definition: Unknown error");
36
41
  }
37
- const builder = getNeonBinary().builderWithJson(jsonString);
42
+ const settingsStr = settings
43
+ ? typeof settings === "string"
44
+ ? settings
45
+ : JSON.stringify(settings)
46
+ : undefined;
47
+ const builder = getNeonBinary().builderWithJson(jsonString, settingsStr);
38
48
  return new Builder(builder);
39
49
  }
40
50
  setIntent(intent) {
@@ -78,9 +88,14 @@ export class Builder {
78
88
  return getNeonBinary().builderToArchive.call(this.builder, asset);
79
89
  });
80
90
  }
81
- static fromArchive(asset) {
91
+ static fromArchive(asset, settings) {
82
92
  return __awaiter(this, void 0, void 0, function* () {
83
- return new Builder(yield getNeonBinary().builderFromArchive(asset));
93
+ const settingsStr = settings
94
+ ? typeof settings === "string"
95
+ ? settings
96
+ : JSON.stringify(settings)
97
+ : undefined;
98
+ return new Builder(yield getNeonBinary().builderFromArchive(asset, settingsStr));
84
99
  });
85
100
  }
86
101
  sign(signer, input, output) {
@@ -19,7 +19,6 @@ import { isActionsAssertion } from "./assertions.js";
19
19
  import { CallbackSigner, LocalSigner } from "./Signer.js";
20
20
  import { Reader } from "./Reader.js";
21
21
  import { Builder } from "./Builder.js";
22
- import { loadC2paSettings, resetSettings } from "./Settings.js";
23
22
  const tempDir = path.join(__dirname, "tmp");
24
23
  class TestSigner {
25
24
  constructor(privateKey) {
@@ -253,51 +252,44 @@ describe("Builder", () => {
253
252
  expect(stats.size).toBeGreaterThan(0);
254
253
  }));
255
254
  it("should construct reader directly from builder archive buffer", () => __awaiter(void 0, void 0, void 0, function* () {
256
- // Enable c2pa archive format
257
- loadC2paSettings(JSON.stringify({
255
+ const settings = JSON.stringify({
258
256
  builder: {
259
257
  generate_c2pa_archive: true,
260
258
  },
261
259
  verify: {
262
260
  verify_after_reading: false,
263
261
  },
264
- }));
265
- try {
266
- // Create a builder
267
- const simpleManifestDefinition = {
268
- claim_generator_info: [
269
- {
270
- name: "c2pa_test",
271
- version: "1.0.0",
272
- },
273
- ],
274
- title: "Test_Manifest",
275
- format: "image/jpeg",
276
- assertions: [],
277
- resources: { resources: {} },
278
- };
279
- const testBuilder = Builder.withJson(simpleManifestDefinition);
280
- // Add an ingredient
281
- yield testBuilder.addIngredient(parent_json, source);
282
- // Create an archive from the builder written to a buffer
283
- const archive = {
284
- buffer: null,
285
- };
286
- yield testBuilder.toArchive(archive);
287
- // Verify buffer was populated
288
- expect(archive.buffer).not.toBeNull();
289
- expect(archive.buffer.length).toBeGreaterThan(0);
290
- // Construct a reader from the builder archive with mime-type "application/c2pa"
291
- const reader = yield Reader.fromAsset({
292
- buffer: archive.buffer,
293
- mimeType: "application/c2pa",
294
- });
295
- expect(reader).not.toBeNull();
296
- }
297
- finally {
298
- // Reset settings to defaults
299
- resetSettings();
300
- }
262
+ });
263
+ // Create a builder
264
+ const simpleManifestDefinition = {
265
+ claim_generator_info: [
266
+ {
267
+ name: "c2pa_test",
268
+ version: "1.0.0",
269
+ },
270
+ ],
271
+ title: "Test_Manifest",
272
+ format: "image/jpeg",
273
+ assertions: [],
274
+ resources: { resources: {} },
275
+ };
276
+ const testBuilder = Builder.withJson(simpleManifestDefinition, settings);
277
+ // Add an ingredient
278
+ yield testBuilder.addIngredient(parent_json, source);
279
+ // Create an archive from the builder written to a buffer
280
+ const archive = {
281
+ buffer: null,
282
+ };
283
+ yield testBuilder.toArchive(archive);
284
+ // Verify buffer was populated
285
+ expect(archive.buffer).not.toBeNull();
286
+ expect(archive.buffer.length).toBeGreaterThan(0);
287
+ // Construct a reader from the builder archive with mime-type "application/c2pa"
288
+ const reader = yield Reader.fromAsset({
289
+ buffer: archive.buffer,
290
+ mimeType: "application/c2pa",
291
+ }, settings);
292
+ expect(reader).not.toBeNull();
301
293
  }));
302
294
  it("should sign data with callback to file", () => __awaiter(void 0, void 0, void 0, function* () {
303
295
  const dest = {
package/dist/Reader.js CHANGED
@@ -31,15 +31,17 @@ export class Reader {
31
31
  return getNeonBinary().readerResourceToAsset.call(this.reader, uri, asset);
32
32
  });
33
33
  }
34
- static fromAsset(asset) {
34
+ static fromAsset(asset, settings) {
35
35
  return __awaiter(this, void 0, void 0, function* () {
36
- const reader = yield getNeonBinary().readerFromAsset(asset);
36
+ const settingsStr = settings ? (typeof settings === 'string' ? settings : JSON.stringify(settings)) : undefined;
37
+ const reader = yield getNeonBinary().readerFromAsset(asset, settingsStr);
37
38
  return reader ? new Reader(reader) : null;
38
39
  });
39
40
  }
40
- static fromManifestDataAndAsset(manifestData, asset) {
41
+ static fromManifestDataAndAsset(manifestData, asset, settings) {
41
42
  return __awaiter(this, void 0, void 0, function* () {
42
- const reader = yield getNeonBinary().readerFromManifestDataAndAsset(manifestData, asset);
43
+ const settingsStr = settings ? (typeof settings === 'string' ? settings : JSON.stringify(settings)) : undefined;
44
+ const reader = yield getNeonBinary().readerFromManifestDataAndAsset(manifestData, asset, settingsStr);
43
45
  return new Reader(reader);
44
46
  });
45
47
  }
@@ -22,6 +22,7 @@ describe("Reader", () => {
22
22
  "manifests": {
23
23
  "contentauth:urn:uuid:c2677d4b-0a93-4444-876f-ed2f2d40b8cf": {
24
24
  "claim_generator": "make_test_images/0.33.1 c2pa-rs/0.33.1",
25
+ "claim_version": 1,
25
26
  "claim_generator_info": [
26
27
  {
27
28
  "name": "make_test_images",
@@ -152,6 +153,28 @@ describe("Reader", () => {
152
153
  });
153
154
  const json = reader.json();
154
155
  expect(json.manifests).toEqual(manifestStore.manifests);
156
+ expect(json.validation_status[0].code).toEqual("signingCredential.untrusted");
157
+ }));
158
+ it("should read from manifest data and file with settings context", () => __awaiter(void 0, void 0, void 0, function* () {
159
+ const manifestData = yield fs.readFile("./tests/fixtures/CA/manifest_data.c2pa");
160
+ const buffer = yield fs.readFile("./tests/fixtures/CA.jpg");
161
+ const settings = {
162
+ verify: {
163
+ verify_after_reading: false,
164
+ verify_trust: false,
165
+ },
166
+ };
167
+ const reader = yield Reader.fromManifestDataAndAsset(manifestData, {
168
+ buffer,
169
+ mimeType: "jpeg",
170
+ }, settings);
171
+ expect(reader).not.toBeNull();
172
+ const json = reader.json();
173
+ // Settings should affect validation behavior
174
+ // With verify_after_reading: false, validation_status should be undefined
175
+ expect(json.validation_status).toBeUndefined();
176
+ expect(json.manifests).toBeDefined();
177
+ expect(json.active_manifest).toEqual(manifestStore.active_manifest);
155
178
  }));
156
179
  it("should write to a file", () => __awaiter(void 0, void 0, void 0, function* () {
157
180
  var _a;