@contentauth/c2pa-node 0.10.1 → 0.12.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.
@@ -122,7 +122,7 @@ describe("Builder", () => {
122
122
  assertions: [],
123
123
  resources: { resources: {} },
124
124
  };
125
- const builder = Builder.withJson(test_definition);
125
+ const builder = await Builder.withJsonAsync(test_definition);
126
126
  await builder.addIngredient(parent_json, source);
127
127
  builder.addAssertion("org.test.assertion", "assertion");
128
128
  await builder.addResource(thumbnail_ref.identifier, {
@@ -142,8 +142,8 @@ describe("Builder", () => {
142
142
  });
143
143
  describe("Sign and Archive", () => {
144
144
  let builder;
145
- beforeEach(() => {
146
- builder = Builder.withJson(manifestDefinition);
145
+ beforeEach(async () => {
146
+ builder = await Builder.withJsonAsync(manifestDefinition);
147
147
  builder.updateManifestProperty("claim_version", 2);
148
148
  builder.setIntent("edit");
149
149
  });
@@ -197,7 +197,8 @@ describe("Builder", () => {
197
197
  const reader = await Reader.fromAsset(dest);
198
198
  expect(reader).not.toBeNull();
199
199
  const activeManifest = reader.getActive();
200
- const cborAssertion = activeManifest?.assertions?.find((a) => a.label === "c2pa.actions.v2");
200
+ const cborAssertion = activeManifest?.assertions?.find((assertion) => isActionsAssertion(assertion) &&
201
+ assertion.data.actions.some((action) => action.action === "c2pa.edited"));
201
202
  expect(cborAssertion).toBeDefined();
202
203
  if (isActionsAssertion(cborAssertion)) {
203
204
  const actions = cborAssertion.data.actions.map((a) => a.action);
@@ -246,12 +247,12 @@ describe("Builder", () => {
246
247
  expect(stats.size).toBeGreaterThan(0);
247
248
  });
248
249
  it("should construct reader directly from builder archive buffer", async () => {
249
- const settings = JSON.stringify({
250
+ const context = new Context({
250
251
  builder: {
251
- generate_c2pa_archive: true,
252
+ generateC2paArchive: true,
252
253
  },
253
254
  verify: {
254
- verify_after_reading: false,
255
+ verifyAfterReading: false,
255
256
  },
256
257
  });
257
258
  // Create a builder
@@ -267,7 +268,7 @@ describe("Builder", () => {
267
268
  assertions: [],
268
269
  resources: { resources: {} },
269
270
  };
270
- const testBuilder = Builder.withJson(simpleManifestDefinition, settings);
271
+ const testBuilder = await Builder.withJsonAsync(simpleManifestDefinition, context);
271
272
  // Add an ingredient
272
273
  await testBuilder.addIngredient(parent_json, source);
273
274
  // Create an archive from the builder written to a buffer
@@ -282,7 +283,7 @@ describe("Builder", () => {
282
283
  const reader = await Reader.fromAsset({
283
284
  buffer: archive.buffer,
284
285
  mimeType: "application/c2pa",
285
- }, settings);
286
+ }, context);
286
287
  expect(reader).not.toBeNull();
287
288
  });
288
289
  it("should sign data with callback to file", async () => {
@@ -476,7 +477,7 @@ describe("Builder", () => {
476
477
  ],
477
478
  resources: { resources: {} },
478
479
  };
479
- const builder = Builder.withJson(manifestDefinition);
480
+ const builder = await Builder.withJsonAsync(manifestDefinition);
480
481
  const ingredientJson = '{"title": "Test Ingredient"}';
481
482
  const testThumbnail = await fs.readFile("./tests/fixtures/thumbnail.jpg");
482
483
  await builder.addIngredient(ingredientJson, {
@@ -506,7 +507,7 @@ describe("Builder", () => {
506
507
  expect(JSON.stringify(manifestStore)).toContain("thumbnail.ingredient");
507
508
  });
508
509
  it("should add ingredient with custom metadata", async () => {
509
- const builder = Builder.new();
510
+ const builder = await Builder.newAsync();
510
511
  const ingredient = {
511
512
  title: "Test Ingredient",
512
513
  format: "image/jpeg",
@@ -539,7 +540,7 @@ describe("Builder", () => {
539
540
  const testSigner = new TestSigner(privateKey);
540
541
  const signer = CallbackSigner.newSigner(signerConfig, testSigner.sign);
541
542
  // Sign source asset with a thumbnail
542
- const step1Builder = Builder.withJson({
543
+ const step1Builder = await Builder.withJsonAsync({
543
544
  claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
544
545
  title: "Asset With Thumbnail",
545
546
  format: "image/jpeg",
@@ -569,7 +570,7 @@ describe("Builder", () => {
569
570
  expect(originalManifest?.thumbnail).not.toBeNull();
570
571
  // Redact the thumbnail
571
572
  const thumbnailUri = `self#jumbf=/c2pa/${parentLabel}/c2pa.assertions/c2pa.thumbnail.claim`;
572
- const redactionBuilder = Builder.withJson({
573
+ const redactionBuilder = await Builder.withJsonAsync({
573
574
  claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
574
575
  title: "Redacted Thumbnail Manifest",
575
576
  format: "image/jpeg",
@@ -602,7 +603,7 @@ describe("Builder", () => {
602
603
  // Sign source asset with multiple distinct assertions
603
604
  const piiLabel = "stds.schema-org.CreativeWork";
604
605
  const retainedLabel = "org.contentauth.retained";
605
- const step1Builder = Builder.withJson({
606
+ const step1Builder = await Builder.withJsonAsync({
606
607
  claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
607
608
  title: "Asset With Multiple Assertions",
608
609
  format: "image/jpeg",
@@ -642,7 +643,7 @@ describe("Builder", () => {
642
643
  expect(originalLabels).toContain(retainedLabel);
643
644
  // Redact only the PII assertion
644
645
  const redactionUri = `self#jumbf=/c2pa/${parentLabel}/c2pa.assertions/${piiLabel}`;
645
- const redactionBuilder = Builder.withJson({
646
+ const redactionBuilder = await Builder.withJsonAsync({
646
647
  claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
647
648
  title: "Redacted Assertion Manifest",
648
649
  format: "image/jpeg",
@@ -665,18 +666,18 @@ describe("Builder", () => {
665
666
  expect(assertionLabels).not.toContain(piiLabel);
666
667
  expect(assertionLabels).toContain(retainedLabel);
667
668
  });
668
- it("should add redactions via addRedaction method", () => {
669
+ it("should add redactions via addRedaction method", async () => {
669
670
  const uri1 = "self#jumbf=/c2pa/test-label/c2pa.assertions/cawg.identity";
670
671
  const uri2 = "self#jumbf=/c2pa/test-label/c2pa.assertions/stds.schema-org.CreativeWork";
671
- const builder = Builder.new();
672
+ const builder = await Builder.newAsync();
672
673
  builder.addRedaction(uri1, "c2pa.PII.present");
673
674
  builder.addRedaction(uri2, "c2pa.PII.present");
674
675
  const definition = builder.getManifestDefinition();
675
676
  expect(definition.redactions).toEqual([uri1, uri2]);
676
677
  });
677
- it("should set a label", () => {
678
+ it("should set a label", async () => {
678
679
  const label = "urn:c2pa:00000000-0000-4000-8000-000000000000";
679
- const builder = Builder.new();
680
+ const builder = await Builder.newAsync();
680
681
  builder.updateManifestProperty("label", label);
681
682
  expect(builder.getManifestDefinition().label).toBe(label);
682
683
  });
@@ -705,7 +706,7 @@ describe("Builder", () => {
705
706
  },
706
707
  ],
707
708
  };
708
- const builder = Builder.withJson(simpleManifestDefinition);
709
+ const builder = await Builder.withJsonAsync(simpleManifestDefinition);
709
710
  // Set remote URL and no embed flag like the Rust test
710
711
  builder.setRemoteUrl("http://my_remote_url");
711
712
  builder.setNoEmbed(true);
@@ -760,7 +761,7 @@ describe("Builder", () => {
760
761
  assertions: [],
761
762
  resources: { resources: {} },
762
763
  };
763
- const builder = Builder.withJson(simpleManifestDefinition);
764
+ const builder = await Builder.withJsonAsync(simpleManifestDefinition);
764
765
  builder.setIntent({ create: "http://c2pa.org/digitalsourcetype/empty" });
765
766
  // Add an action using addAction method
766
767
  // The action needs to be a structured object matching the c2pa Action type
@@ -776,7 +777,8 @@ describe("Builder", () => {
776
777
  const reader = await Reader.fromAsset(dest);
777
778
  expect(reader).not.toBeNull();
778
779
  const activeManifest = reader.getActive();
779
- const actionsAssertion = activeManifest?.assertions?.find((a) => a.label === "c2pa.actions.v2");
780
+ const actionsAssertion = activeManifest?.assertions?.find((assertion) => isActionsAssertion(assertion) &&
781
+ assertion.data.actions.some((action) => action.action === "c2pa.edited"));
780
782
  expect(actionsAssertion).toBeDefined();
781
783
  if (isActionsAssertion(actionsAssertion)) {
782
784
  const actions = actionsAssertion.data.actions;
@@ -814,7 +816,7 @@ describe("Builder", () => {
814
816
  ],
815
817
  resources: { resources: {} },
816
818
  };
817
- const builder = Builder.withJson(simpleManifestDefinition);
819
+ const builder = await Builder.withJsonAsync(simpleManifestDefinition);
818
820
  // Add an ingredient using addIngredient method
819
821
  const ingredientJson = JSON.stringify({
820
822
  title: "Test Ingredient via addIngredient",
@@ -839,7 +841,7 @@ describe("Builder", () => {
839
841
  expect(addedIngredient?.relationship).toBe("componentOf");
840
842
  });
841
843
  it("should add ingredient from reader", async () => {
842
- const builder1 = Builder.new();
844
+ const builder1 = await Builder.newAsync();
843
845
  builder1.setIntent("edit");
844
846
  const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
845
847
  const dest1 = {
@@ -853,7 +855,7 @@ describe("Builder", () => {
853
855
  });
854
856
  expect(reader).not.toBeNull();
855
857
  // Create a new builder and add ingredient from the reader
856
- const builder2 = Builder.new();
858
+ const builder2 = await Builder.newAsync();
857
859
  builder2.setIntent("edit");
858
860
  const ingredient = builder2.addIngredientFromReader(reader);
859
861
  expect(ingredient).toBeDefined();
@@ -878,10 +880,11 @@ describe("Builder", () => {
878
880
  }, 30000);
879
881
  });
880
882
  describe("Context", () => {
881
- it("newAsync() with no context behaves like new()", async () => {
883
+ it("newAsync() with no context uses an empty Context", async () => {
882
884
  const builder = await Builder.newAsync();
883
885
  const definition = builder.getManifestDefinition();
884
- expect(definition).toEqual(Builder.new().getManifestDefinition());
886
+ const configured = await Builder.newAsync(new Context());
887
+ expect(definition).toEqual(configured.getManifestDefinition());
885
888
  });
886
889
  it("newAsync(context) applies the Context's settings", async () => {
887
890
  const dest = { path: path.join(tempDir, "context_new_signed.jpg") };
@@ -965,8 +968,8 @@ describe("Builder", () => {
965
968
  const manifestStore = reader.json();
966
969
  expect(manifestStore.validation_status ?? []).not.toContainEqual(expect.objectContaining({ code: "signingCredential.untrusted" }));
967
970
  });
968
- it("fromArchive(asset, context) accepts a Context in place of the deprecated raw settings", async () => {
969
- const builder = Builder.withJson(manifestDefinition);
971
+ it("fromArchive(asset, context) restores a Builder with a Context", async () => {
972
+ const builder = await Builder.withJsonAsync(manifestDefinition);
970
973
  await builder.addIngredient(parent_json, source);
971
974
  await builder.addResource("thumbnail.jpg", {
972
975
  mimeType: "jpeg",
@@ -1007,8 +1010,8 @@ describe("Builder", () => {
1007
1010
  ],
1008
1011
  resources: { resources: {} },
1009
1012
  });
1010
- it("filterActions keeps the inception action and drops excluded actions", () => {
1011
- const builder = Builder.withJson(actionsManifest());
1013
+ it("filterActions keeps the inception action and drops excluded actions", async () => {
1014
+ const builder = await Builder.withJsonAsync(actionsManifest());
1012
1015
  // The predicate only keeps c2pa.edited; c2pa.created must survive regardless
1013
1016
  // because it is the inception action.
1014
1017
  builder.filterActions((action) => action.action === "c2pa.edited");
@@ -1020,8 +1023,8 @@ describe("Builder", () => {
1020
1023
  expect(names).toContain("c2pa.edited");
1021
1024
  expect(names).not.toContain("c2pa.color_adjustments");
1022
1025
  });
1023
- it("filterActions surfaces an error thrown by the predicate", () => {
1024
- const builder = Builder.withJson(actionsManifest());
1026
+ it("filterActions surfaces an error thrown by the predicate", async () => {
1027
+ const builder = await Builder.withJsonAsync(actionsManifest());
1025
1028
  expect(() => builder.filterActions((action) => {
1026
1029
  if (action.action === "c2pa.color_adjustments") {
1027
1030
  throw new Error("boom from predicate");
@@ -1029,14 +1032,14 @@ describe("Builder", () => {
1029
1032
  return true;
1030
1033
  })).toThrow("boom from predicate");
1031
1034
  });
1032
- it("filterActions surfaces a non-boolean predicate return", () => {
1033
- const builder = Builder.withJson(actionsManifest());
1035
+ it("filterActions surfaces a non-boolean predicate return", async () => {
1036
+ const builder = await Builder.withJsonAsync(actionsManifest());
1034
1037
  expect(() =>
1035
1038
  // Returning a non-boolean is a programming error and must not be coerced.
1036
1039
  builder.filterActions(() => "yes")).toThrow(/must return a boolean/);
1037
1040
  });
1038
- it("filterIngredients prunes orphans and passes provenance to the predicate", () => {
1039
- const builder = Builder.withJson({
1041
+ it("filterIngredients prunes orphans and passes provenance to the predicate", async () => {
1042
+ const builder = await Builder.withJsonAsync({
1040
1043
  claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
1041
1044
  title: "Test_FilterIngredients",
1042
1045
  format: "image/jpeg",
@@ -1074,8 +1077,8 @@ describe("Builder", () => {
1074
1077
  const definition = builder.getManifestDefinition();
1075
1078
  expect(definition.ingredients ?? []).toHaveLength(0);
1076
1079
  });
1077
- it("filterIngredients surfaces an error thrown by the predicate", () => {
1078
- const builder = Builder.withJson({
1080
+ it("filterIngredients surfaces an error thrown by the predicate", async () => {
1081
+ const builder = await Builder.withJsonAsync({
1079
1082
  claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
1080
1083
  title: "Test_FilterIngredientsThrow",
1081
1084
  format: "image/jpeg",
@@ -1092,8 +1095,8 @@ describe("Builder", () => {
1092
1095
  throw new Error("ingredient boom");
1093
1096
  })).toThrow("ingredient boom");
1094
1097
  });
1095
- it("filterActionsAndIngredients rescues an edited action for a kept ingredient", () => {
1096
- const builder = Builder.withJson({
1098
+ it("filterActionsAndIngredients rescues an edited action for a kept ingredient", async () => {
1099
+ const builder = await Builder.withJsonAsync({
1097
1100
  claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
1098
1101
  title: "Test_FilterActionsAndIngredients",
1099
1102
  format: "image/jpeg",
@@ -1133,8 +1136,8 @@ describe("Builder", () => {
1133
1136
  expect(names).toContain("c2pa.edited");
1134
1137
  expect(definition.ingredients ?? []).toHaveLength(1);
1135
1138
  });
1136
- it("filterActionsAndIngredients drops unreferenced and unrescued", () => {
1137
- const builder = Builder.withJson(actionsManifest());
1139
+ it("filterActionsAndIngredients drops unreferenced and unrescued", async () => {
1140
+ const builder = await Builder.withJsonAsync(actionsManifest());
1138
1141
  builder.addIngredient(JSON.stringify({
1139
1142
  title: "orphan",
1140
1143
  format: "image/jpeg",
@@ -1150,14 +1153,14 @@ describe("Builder", () => {
1150
1153
  expect(names).not.toContain("c2pa.color_adjustments");
1151
1154
  expect(definition.ingredients ?? []).toHaveLength(0);
1152
1155
  });
1153
- it("filterActionsAndIngredients surfaces an error thrown by either predicate", () => {
1154
- const builder = Builder.withJson(actionsManifest());
1156
+ it("filterActionsAndIngredients surfaces an error thrown by either predicate", async () => {
1157
+ const builder = await Builder.withJsonAsync(actionsManifest());
1155
1158
  expect(() => builder.filterActionsAndIngredients(() => {
1156
1159
  throw new Error("action predicate boom");
1157
1160
  }, () => false)).toThrow("action predicate boom");
1158
1161
  });
1159
- it("updateAssertion updates exact label matches in order and preserves metadata", () => {
1160
- const builder = Builder.withJson({
1162
+ it("updateAssertion updates exact label matches in order and preserves metadata", async () => {
1163
+ const builder = await Builder.withJsonAsync({
1161
1164
  ...actionsManifest(),
1162
1165
  assertions: [
1163
1166
  { label: "org.test.note", data: { value: 1 }, kind: "Cbor" },
@@ -1179,7 +1182,7 @@ describe("Builder", () => {
1179
1182
  ]);
1180
1183
  });
1181
1184
  it("updateAssertion preserves JSON and CBOR kinds when signing", async () => {
1182
- const builder = Builder.withJson({
1185
+ const builder = await Builder.withJsonAsync({
1183
1186
  ...actionsManifest(),
1184
1187
  assertions: [
1185
1188
  ...actionsManifest().assertions,
@@ -1210,8 +1213,8 @@ describe("Builder", () => {
1210
1213
  }),
1211
1214
  ]));
1212
1215
  });
1213
- it("updateAssertion skips null and undefined results without dropping other updates", () => {
1214
- const builder = Builder.withJson({
1216
+ it("updateAssertion skips null and undefined results without dropping other updates", async () => {
1217
+ const builder = await Builder.withJsonAsync({
1215
1218
  ...actionsManifest(),
1216
1219
  assertions: [
1217
1220
  { label: "org.test.note", data: { value: 1 }, kind: "Json" },
@@ -1225,8 +1228,8 @@ describe("Builder", () => {
1225
1228
  });
1226
1229
  expect(builder.getManifestDefinition().assertions.map((a) => a.data)).toEqual([{ value: 1 }, { value: 2 }, { value: 33 }]);
1227
1230
  });
1228
- it("updateAssertion is atomic on callback failure and ignores missing labels", () => {
1229
- const builder = Builder.withJson({
1231
+ it("updateAssertion is atomic on callback failure and ignores missing labels", async () => {
1232
+ const builder = await Builder.withJsonAsync({
1230
1233
  ...actionsManifest(),
1231
1234
  assertions: [
1232
1235
  { label: "org.test.note", data: { value: 1 } },
@@ -1252,8 +1255,8 @@ describe("Builder", () => {
1252
1255
  });
1253
1256
  expect(calls).toBe(0);
1254
1257
  });
1255
- it("updateActions patches a parameter on an existing action", () => {
1256
- const builder = Builder.withJson(actionsManifest());
1258
+ it("updateActions patches a parameter on an existing action", async () => {
1259
+ const builder = await Builder.withJsonAsync(actionsManifest());
1257
1260
  builder.updateActions((actions) => actions.map((action) => action.action === "c2pa.created"
1258
1261
  ? {
1259
1262
  ...action,
@@ -1273,12 +1276,12 @@ describe("Builder", () => {
1273
1276
  ]);
1274
1277
  expect(patched[0].parameters.patchMe).toBe("patch-test");
1275
1278
  });
1276
- it("updateActions adds a new parameter alongside the existing ones", () => {
1279
+ it("updateActions adds a new parameter alongside the existing ones", async () => {
1277
1280
  const manifest = actionsManifest();
1278
1281
  manifest.assertions[0].data.actions[1].parameters = {
1279
1282
  existingValue: "kept",
1280
1283
  };
1281
- const builder = Builder.withJson(manifest);
1284
+ const builder = await Builder.withJsonAsync(manifest);
1282
1285
  builder.updateActions((actions) => actions.map((action) => action.action === "c2pa.edited"
1283
1286
  ? {
1284
1287
  ...action,
@@ -1303,8 +1306,8 @@ describe("Builder", () => {
1303
1306
  expect(updated[0].parameters?.addedValue).toBeUndefined();
1304
1307
  expect(updated[2].parameters?.addedValue).toBeUndefined();
1305
1308
  });
1306
- it("updateActions is no-op when there is no actions assertion", () => {
1307
- const builder = Builder.withJson({
1309
+ it("updateActions is no-op when there is no actions assertion", async () => {
1310
+ const builder = await Builder.withJsonAsync({
1308
1311
  claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
1309
1312
  title: "Test_NoActions",
1310
1313
  format: "image/jpeg",
@@ -1321,10 +1324,10 @@ describe("Builder", () => {
1321
1324
  const definition = builder.getManifestDefinition();
1322
1325
  expect((definition.assertions ?? []).filter((a) => a.label.startsWith("c2pa.actions"))).toHaveLength(0);
1323
1326
  });
1324
- it("updateActions drops an assertion whose transform returns an empty list", () => {
1327
+ it("updateActions drops an assertion whose transform returns an empty list", async () => {
1325
1328
  // (this means there could be no actions, so in non-test code, something after such a call
1326
1329
  // should make sure the manifest is still valid and inception actions are here as needed!)
1327
- const builder = Builder.withJson({
1330
+ const builder = await Builder.withJsonAsync({
1328
1331
  claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
1329
1332
  title: "Test_TwoAssertions",
1330
1333
  format: "image/jpeg",
@@ -1356,8 +1359,8 @@ describe("Builder", () => {
1356
1359
  expect(actionsAssertions[0].label).toBe("c2pa.actions");
1357
1360
  expect(actionsAssertions[0].data.actions.map((a) => a.action)).toEqual(["c2pa.created"]);
1358
1361
  });
1359
- it("updateActions rewrites every actions assertion under its own label", () => {
1360
- const builder = Builder.withJson({
1362
+ it("updateActions rewrites every actions assertion under its own label", async () => {
1363
+ const builder = await Builder.withJsonAsync({
1361
1364
  claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
1362
1365
  title: "Test_MultiAssertion",
1363
1366
  format: "image/jpeg",
@@ -1403,8 +1406,8 @@ describe("Builder", () => {
1403
1406
  expect(actions[0].parameters.addedValue).toBe("added-value");
1404
1407
  }
1405
1408
  });
1406
- it("updateActions preserves the created flag", () => {
1407
- const builder = Builder.withJson({
1409
+ it("updateActions preserves the created flag", async () => {
1410
+ const builder = await Builder.withJsonAsync({
1408
1411
  claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
1409
1412
  title: "Test_CreatedFlag",
1410
1413
  format: "image/jpeg",
@@ -1440,7 +1443,7 @@ describe("Builder", () => {
1440
1443
  expect(actionsAssertions[0].data.actions.map((a) => a.action)).toEqual(["c2pa.created"]);
1441
1444
  });
1442
1445
  it("updateActions produces a signable manifest", async () => {
1443
- const builder = Builder.withJson({
1446
+ const builder = await Builder.withJsonAsync({
1444
1447
  claim_generator_info: [{ name: "c2pa_test", version: "1.0.0" }],
1445
1448
  title: "Test_UpdateActionsSign",
1446
1449
  format: "image/jpeg",
@@ -1478,8 +1481,8 @@ describe("Builder", () => {
1478
1481
  expect(codes).not.toContain("assertion.action.malformed");
1479
1482
  expect(codes).not.toContain("assertion.action.ingredientMismatch");
1480
1483
  });
1481
- it("updateActions preserves action order when transform patches in place", () => {
1482
- const builder = Builder.withJson(actionsManifest());
1484
+ it("updateActions preserves action order when transform patches in place", async () => {
1485
+ const builder = await Builder.withJsonAsync(actionsManifest());
1483
1486
  // Patch the middle entry; the transform never reorders.
1484
1487
  builder.updateActions((actions) => actions.map((action) => action.action === "c2pa.edited"
1485
1488
  ? { ...action, parameters: { note: "patched" } }
@@ -1493,13 +1496,13 @@ describe("Builder", () => {
1493
1496
  "c2pa.color_adjustments",
1494
1497
  ]);
1495
1498
  });
1496
- it("updateActions surfaces an error thrown by transform", () => {
1497
- const builder = Builder.withJson(actionsManifest());
1499
+ it("updateActions surfaces an error thrown by transform", async () => {
1500
+ const builder = await Builder.withJsonAsync(actionsManifest());
1498
1501
  expect(() => builder.updateActions(() => {
1499
1502
  throw new Error("transform boom");
1500
1503
  })).toThrow("transform boom");
1501
1504
  });
1502
- it("updateActions preserves allActionsIncluded/softwareAgents/templates/metadata not seen by transform", () => {
1505
+ it("updateActions preserves allActionsIncluded/softwareAgents/templates/metadata not seen by transform", async () => {
1503
1506
  const manifest = actionsManifest();
1504
1507
  manifest.assertions[0].data.allActionsIncluded = true;
1505
1508
  manifest.assertions[0].data.softwareAgents = [
@@ -1511,7 +1514,7 @@ describe("Builder", () => {
1511
1514
  manifest.assertions[0].data.metadata = {
1512
1515
  dataSource: { type: "humanEdits" },
1513
1516
  };
1514
- const builder = Builder.withJson(manifest);
1517
+ const builder = await Builder.withJsonAsync(manifest);
1515
1518
  // transform only ever sees/returns the flattened actions list, never the sibling fields.
1516
1519
  builder.updateActions((actions) => actions);
1517
1520
  const definition = builder.getManifestDefinition();
@@ -6,6 +6,9 @@
6
6
  import fs from "fs-extra";
7
7
  import * as crypto from "crypto";
8
8
  import { spawnSync } from "child_process";
9
+ import { once } from "node:events";
10
+ import { createServer } from "node:http";
11
+ import { Context } from "@contentauth/c2pa-utilities";
9
12
  import { encodeSignerPayload } from "./index.js";
10
13
  class TestSigner {
11
14
  privateKey;
@@ -102,7 +105,7 @@ describe("IdentityAssertionBuilder", () => {
102
105
  tsaUrl: undefined,
103
106
  tsaHeaders: undefined,
104
107
  tsaBody: undefined,
105
- directCoseHandling: true,
108
+ directCoseHandling: false,
106
109
  };
107
110
  // Create signers
108
111
  const c2paTestSigner = new TestSigner(c2paPrivateKey);
@@ -117,7 +120,7 @@ describe("IdentityAssertionBuilder", () => {
117
120
  buffer: null,
118
121
  };
119
122
  // Create the manifest builder
120
- const builder = Builder.withJson(manifestDefinition);
123
+ const builder = await Builder.withJsonAsync(manifestDefinition);
121
124
  // Add the required resources
122
125
  await builder.addResource("thumbnail.jpg", {
123
126
  mimeType: "image/jpeg",
@@ -140,6 +143,81 @@ describe("IdentityAssertionBuilder", () => {
140
143
  mimeType: "image/jpeg",
141
144
  });
142
145
  });
146
+ it("forwards directCoseHandling through the identity signer", async () => {
147
+ const { CallbackSigner } = await import("./Signer.js");
148
+ const { Builder } = await import("./Builder.js");
149
+ const { IdentityAssertionSigner } = await import("./IdentityAssertion.js");
150
+ let calls = 0;
151
+ const signer = CallbackSigner.newSigner({
152
+ alg: "es256",
153
+ certs: [],
154
+ reserveSize: 1000,
155
+ directCoseHandling: true,
156
+ }, async () => {
157
+ calls += 1;
158
+ return Buffer.alloc(100);
159
+ });
160
+ const builder = await Builder.withJsonAsync({
161
+ claim_generator_info: [{ name: "c2pa_test", version: "2.0.0" }],
162
+ format: "image/jpeg",
163
+ assertions: manifestDefinition.assertions,
164
+ }, new Context({ verify: { verifyAfterSign: false } }));
165
+ await expect(builder.signAsync(IdentityAssertionSigner.new(signer.getHandle()), {
166
+ buffer: await fs.readFile("./tests/fixtures/CA.jpg"),
167
+ mimeType: "image/jpeg",
168
+ }, { buffer: null })).resolves.toBeDefined();
169
+ expect(calls).toBe(1);
170
+ });
171
+ it("forwards timestamp requests and failures through the identity signer", async () => {
172
+ const { CallbackSigner } = await import("./Signer.js");
173
+ const { Builder } = await import("./Builder.js");
174
+ const { IdentityAssertionSigner } = await import("./IdentityAssertion.js");
175
+ const requests = [];
176
+ const server = createServer((request, response) => {
177
+ requests.push({
178
+ method: request.method,
179
+ marker: request.headers["x-c2pa-preflight"],
180
+ });
181
+ request.resume();
182
+ response.writeHead(503).end("Timestamp service unavailable");
183
+ });
184
+ server.listen(0, "127.0.0.1");
185
+ await once(server, "listening");
186
+ try {
187
+ const address = server.address();
188
+ if (!address || typeof address === "string") {
189
+ throw new Error("Expected a TCP timestamp test server");
190
+ }
191
+ const testSigner = new TestSigner(await fs.readFile("./tests/fixtures/certs/es256.pem"));
192
+ const signer = CallbackSigner.newSigner({
193
+ alg: "es256",
194
+ certs: [await fs.readFile("./tests/fixtures/certs/es256.pub")],
195
+ reserveSize: 10000,
196
+ tsaUrl: `http://127.0.0.1:${address.port}`,
197
+ tsaHeaders: [["x-c2pa-preflight", "identity-signer"]],
198
+ directCoseHandling: false,
199
+ }, testSigner.sign);
200
+ const builder = await Builder.withJsonAsync({
201
+ claim_generator_info: [{ name: "c2pa_test", version: "2.0.0" }],
202
+ format: "image/jpeg",
203
+ assertions: manifestDefinition.assertions,
204
+ });
205
+ await expect(builder.signAsync(IdentityAssertionSigner.new(signer.getHandle()), {
206
+ buffer: await fs.readFile("./tests/fixtures/CA.jpg"),
207
+ mimeType: "image/jpeg",
208
+ }, { buffer: null })).rejects.toThrow(/timestamp|503/i);
209
+ expect(requests).toContainEqual({
210
+ method: "POST",
211
+ marker: "identity-signer",
212
+ });
213
+ }
214
+ finally {
215
+ server.closeAllConnections();
216
+ await new Promise((resolve, reject) => {
217
+ server.close((error) => (error ? reject(error) : resolve()));
218
+ });
219
+ }
220
+ });
143
221
  });
144
222
  describe("CallbackCredentialHolder accessors", () => {
145
223
  it("returns reserveSize and sigType set on the constructor", async () => {
@@ -220,11 +298,11 @@ describe("handles reserveSize", () => {
220
298
  tsaUrl: undefined,
221
299
  tsaHeaders: undefined,
222
300
  tsaBody: undefined,
223
- directCoseHandling: true,
301
+ directCoseHandling: false,
224
302
  }, c2paTestSigner.sign);
225
303
  const cawgTestSigner = new TestCawgSigner(c2paTestSigner);
226
304
  const cawgSigner = CallbackCredentialHolder.newCallbackCredentialHolder(reserveSize, "cawg.x509.cose", cawgTestSigner.sign);
227
- const builder = Builder.withJson({
305
+ const builder = await Builder.withJsonAsync({
228
306
  claim_generator_info: [{ name: "c2pa_test", version: "2.0.0" }],
229
307
  title: "Test_Manifest",
230
308
  format: "image/jpeg",
package/dist/Reader.js CHANGED
@@ -3,9 +3,9 @@
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
+ import { Context } from "@contentauth/c2pa-utilities";
6
7
  import { getNeonBinary } from "./binary.js";
7
8
  import { validateSourceAssetSize } from "./assetSize.js";
8
- import { resolveSdkConfig, resolveSettingsForNeon } from "./Settings.js";
9
9
  export class Reader {
10
10
  reader;
11
11
  constructor(reader) {
@@ -24,24 +24,20 @@ export class Reader {
24
24
  return getNeonBinary().readerResourceToAsset.call(this.reader, uri, asset);
25
25
  }
26
26
  /**
27
- * @param settingsOrContext A `Context`, or (@deprecated) a raw `C2paSettings` string/object. Passing a
28
- * raw `C2paSettings` value is deprecated and will be removed in a future version. `null` is treated
29
- * the same as `undefined`.
27
+ * @param context A `Context` configuring this Reader. Defaults to an empty `Context`.
30
28
  */
31
- static async fromAsset(asset, settingsOrContext) {
32
- await validateSourceAssetSize(asset, resolveSdkConfig(settingsOrContext));
33
- const settingsStr = await resolveSettingsForNeon(settingsOrContext);
29
+ static async fromAsset(asset, context = new Context()) {
30
+ await validateSourceAssetSize(asset, context.sdkConfig);
31
+ const settingsStr = await context.toJson();
34
32
  const reader = await getNeonBinary().readerFromAsset(asset, settingsStr);
35
33
  return reader ? new Reader(reader) : null;
36
34
  }
37
35
  /**
38
- * @param settingsOrContext A `Context`, or (@deprecated) a raw `C2paSettings` string/object. Passing a
39
- * raw `C2paSettings` value is deprecated and will be removed in a future version. `null` is treated
40
- * the same as `undefined`.
36
+ * @param context A `Context` configuring this Reader. Defaults to an empty `Context`.
41
37
  */
42
- static async fromManifestDataAndAsset(manifestData, asset, settingsOrContext) {
43
- await validateSourceAssetSize(asset, resolveSdkConfig(settingsOrContext));
44
- const settingsStr = await resolveSettingsForNeon(settingsOrContext);
38
+ static async fromManifestDataAndAsset(manifestData, asset, context = new Context()) {
39
+ await validateSourceAssetSize(asset, context.sdkConfig);
40
+ const settingsStr = await context.toJson();
45
41
  const reader = await getNeonBinary().readerFromManifestDataAndAsset(manifestData, asset, settingsStr);
46
42
  return new Reader(reader);
47
43
  }