@contentauth/c2pa-node 0.1.3 → 0.2.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.
@@ -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,25 +12,16 @@ 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
- // Unless required by applicable law or agreed to in writing,
51
- // this software is distributed on an "AS IS" BASIS, WITHOUT
52
- // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
53
- // implied. See the LICENSE-MIT and LICENSE-APACHE files for the
54
- // specific language governing permissions and limitations under
55
- // each license.
56
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
57
- const Signer_1 = require("./Signer");
58
- const Reader_1 = require("./Reader");
59
- const Builder_1 = require("./Builder");
60
- const IdentityAssertion_1 = require("./IdentityAssertion");
61
- const fs = __importStar(require("fs-extra"));
62
- const crypto = __importStar(require("crypto"));
63
- // TODO: move to a separate test file
15
+ import * as fs from "fs-extra";
16
+ import * as crypto from "crypto";
17
+ import { encode } from "cbor2";
64
18
  class TestSigner {
65
19
  constructor(privateKey) {
66
20
  this.sign = (bytes) => __awaiter(this, void 0, void 0, function* () {
67
- return crypto.sign(null, bytes, this.privateKey);
21
+ const sign = crypto.createSign("SHA256");
22
+ sign.update(bytes);
23
+ sign.end();
24
+ return sign.sign(this.privateKey);
68
25
  });
69
26
  this.privateKey = crypto.createPrivateKey({
70
27
  key: privateKey,
@@ -72,49 +29,59 @@ class TestSigner {
72
29
  });
73
30
  }
74
31
  }
32
+ class TestCawgSigner {
33
+ constructor(manifestSigner) {
34
+ this.manifestSigner = manifestSigner;
35
+ this.sign = (payload) => __awaiter(this, void 0, void 0, function* () {
36
+ const cborBytes = Buffer.from(encode(payload));
37
+ return this.manifestSigner.sign(cborBytes);
38
+ });
39
+ }
40
+ }
75
41
  describe("IdentityAssertionBuilder", () => {
76
42
  const manifestDefinition = {
77
43
  vendor: "test",
78
- claim_version: 2,
79
44
  claim_generator_info: [
80
45
  {
81
46
  name: "c2pa_test",
82
47
  version: "2.0.0",
83
48
  },
84
49
  ],
85
- metadata: [
86
- {
87
- dateTime: "1985-04-12T23:20:50.52Z",
88
- my_custom_metadata: "my custom metatdata value",
89
- },
90
- ],
50
+ claim_generator: "c2pa_test",
91
51
  title: "Test_Manifest",
92
52
  format: "image/jpeg",
93
53
  instance_id: "1234",
54
+ thumbnail: { format: "image/jpeg", identifier: "thumbnail.jpg" },
55
+ resources: { resources: {} },
94
56
  ingredients: [
95
57
  {
96
58
  title: "Test",
97
59
  format: "image/jpeg",
98
60
  instance_id: "12345",
99
61
  relationship: "componentOf",
62
+ thumbnail: { format: "image/jpeg", identifier: "ingredient-thumb.jpg" },
63
+ resources: { resources: {} },
100
64
  },
101
65
  ],
102
66
  assertions: [
103
67
  {
104
- label: "c2pa.actions",
68
+ label: "c2pa.actions.v2",
105
69
  data: {
106
70
  actions: [
107
71
  {
108
72
  action: "c2pa.created",
109
- digitalSourceType: "http://cv.iptc.org/newscodes/digitalsourcetype/digitalCapture",
73
+ parameters: {
74
+ digitalSourceType: "http://cv.iptc.org/newscodes/digitalsourcetype/digitalCapture",
75
+ },
110
76
  },
111
77
  ],
112
78
  },
79
+ kind: "Json",
113
80
  },
114
81
  {
115
82
  label: "cawg.training-mining",
116
83
  data: {
117
- entries: {
84
+ metadata: {
118
85
  "cawg.ai_inference": {
119
86
  use: "notAllowed",
120
87
  },
@@ -127,11 +94,14 @@ describe("IdentityAssertionBuilder", () => {
127
94
  ],
128
95
  };
129
96
  it("should build an Identity Assertion Signer", () => __awaiter(void 0, void 0, void 0, function* () {
97
+ const { CallbackSigner } = yield import("./Signer");
98
+ const { Reader } = yield import("./Reader");
99
+ const { Builder } = yield import("./Builder");
100
+ const { IdentityAssertionBuilder, IdentityAssertionSigner, CallbackCredentialHolder, } = yield import("./IdentityAssertion");
130
101
  // Read certificate files once
131
102
  const c2paPrivateKey = yield fs.readFile("./tests/fixtures/certs/es256.pem");
132
103
  const c2paPublicKey = yield fs.readFile("./tests/fixtures/certs/es256.pub");
133
- const cawgPrivateKey = yield fs.readFile("./tests/fixtures/certs/ed25519.pem");
134
- const cawgPublicKey = yield fs.readFile("./tests/fixtures/certs/ed25519.pub");
104
+ // Use the same signer for both C2PA manifest and COSE signing
135
105
  // Create signer configurations
136
106
  const c2paConfig = {
137
107
  alg: "es256",
@@ -140,40 +110,42 @@ describe("IdentityAssertionBuilder", () => {
140
110
  tsaUrl: undefined,
141
111
  tsaHeaders: undefined,
142
112
  tsaBody: undefined,
143
- };
144
- const cawgConfig = {
145
- alg: "ed25519",
146
- certs: [cawgPublicKey],
147
- reserveSize: 10000,
148
- tsaUrl: undefined,
149
- tsaHeaders: undefined,
150
- tsaBody: undefined,
113
+ directCoseHandling: true,
151
114
  };
152
115
  // Create signers
153
116
  const c2paTestSigner = new TestSigner(c2paPrivateKey);
154
- const cawgTestSigner = new TestSigner(cawgPrivateKey);
155
- const c2paSigner = Signer_1.CallbackSigner.newSigner(c2paConfig, c2paTestSigner.sign);
156
- const cawgSigner = Signer_1.CallbackSigner.newSigner(cawgConfig, cawgTestSigner.sign);
117
+ const c2paSigner = CallbackSigner.newSigner(c2paConfig, c2paTestSigner.sign);
118
+ const cawgTestSigner = new TestCawgSigner(c2paTestSigner);
119
+ const cawgSigner = CallbackCredentialHolder.newCallbackCredentialHolder(10000, "cawg.x509.cose", cawgTestSigner.sign);
157
120
  const source = {
158
121
  buffer: yield fs.readFile("./tests/fixtures/CA.jpg"),
159
- mimeType: "jpeg",
122
+ mimeType: "image/jpeg",
160
123
  };
161
124
  const dest = {
162
125
  buffer: null,
163
126
  };
164
127
  // Create the manifest builder
165
- const builder = Builder_1.Builder.withJson(manifestDefinition);
128
+ const builder = Builder.withJson(manifestDefinition);
129
+ // Add the required resources
130
+ yield builder.addResource("thumbnail.jpg", {
131
+ mimeType: "image/jpeg",
132
+ buffer: yield fs.readFile("./tests/fixtures/thumbnail.jpg"),
133
+ });
134
+ yield builder.addResource("ingredient-thumb.jpg", {
135
+ mimeType: "image/jpeg",
136
+ buffer: yield fs.readFile("./tests/fixtures/thumbnail.jpg"),
137
+ });
166
138
  // Create and configure the identity assertion
167
- const ia_signer = IdentityAssertion_1.IdentityAssertionSigner.new(c2paSigner);
168
- const iab = yield IdentityAssertion_1.IdentityAssertionBuilder.identityBuilderForCredentialHolder(cawgSigner);
139
+ const iaSigner = IdentityAssertionSigner.new(c2paSigner.signer());
140
+ const iab = yield IdentityAssertionBuilder.identityBuilderForCredentialHolder(cawgSigner);
169
141
  iab.addReferencedAssertions(["cawg.training-mining"]);
170
- ia_signer.addIdentityAssertion(iab);
171
- // Sign the manifest
172
- yield builder.signAsync(c2paSigner, source, dest);
142
+ iaSigner.addIdentityAssertion(iab);
143
+ // Sign the manifest (standard async flow)
144
+ yield builder.signAsync(iaSigner, source, dest);
173
145
  // Verify the manifest
174
- const reader = yield Reader_1.Reader.fromAsset({
146
+ const reader = yield Reader.fromAsset({
175
147
  buffer: dest.buffer,
176
- mimeType: "jpeg",
148
+ mimeType: "image/jpeg",
177
149
  });
178
150
  yield reader.postValidateCawg();
179
151
  }));
package/dist/Reader.js CHANGED
@@ -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.Reader = 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 Reader {
21
+ const neon = require("./index.node");
22
+ export class Reader {
59
23
  constructor(reader) {
60
24
  this.reader = reader;
61
25
  }
@@ -105,4 +69,3 @@ class Reader {
105
69
  });
106
70
  }
107
71
  }
108
- exports.Reader = Reader;
@@ -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,20 +12,16 @@ 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
15
  // Unless required by applicable law or agreed to in writing,
54
16
  // this software is distributed on an "AS IS" BASIS, WITHOUT
55
17
  // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
56
18
  // implied. See the LICENSE-MIT and LICENSE-APACHE files for the
57
19
  // specific language governing permissions and limitations under
58
20
  // each license.
59
- const Reader_1 = require("./Reader");
60
- const path_1 = __importDefault(require("path"));
61
- const fs = __importStar(require("fs-extra"));
62
- const tempDir = path_1.default.join(__dirname, "tmp");
21
+ import { Reader } from "./Reader";
22
+ import path from "path";
23
+ import * as fs from "fs-extra";
24
+ const tempDir = path.join(__dirname, "tmp");
63
25
  describe("Reader", () => {
64
26
  const manifestStore = JSON.parse(`{
65
27
  "active_manifest": "contentauth:urn:uuid:e8ce52bc-df72-4ba5-ba37-52c360e76ba4",
@@ -169,6 +131,7 @@ describe("Reader", () => {
169
131
  "signature_info": {
170
132
  "alg": "Ps256",
171
133
  "issuer": "C2PA Test Signing Cert",
134
+ "common_name": "C2PA Signer",
172
135
  "cert_serial_number": "720724073027128164015125666832722375746636448153",
173
136
  "time": "2025-02-04T17:38:01+00:00"
174
137
  },
@@ -184,7 +147,7 @@ describe("Reader", () => {
184
147
  }));
185
148
  it("should read from an ArrayBuffer", () => __awaiter(void 0, void 0, void 0, function* () {
186
149
  const buffer = yield fs.readFile("./tests/fixtures/CAI.jpg");
187
- const reader = yield Reader_1.Reader.fromAsset({
150
+ const reader = yield Reader.fromAsset({
188
151
  buffer,
189
152
  mimeType: "jpeg",
190
153
  });
@@ -193,7 +156,7 @@ describe("Reader", () => {
193
156
  expect(json.active_manifest).toEqual(manifestStore.active_manifest);
194
157
  }));
195
158
  it("should read from a file", () => __awaiter(void 0, void 0, void 0, function* () {
196
- const reader = yield Reader_1.Reader.fromAsset({
159
+ const reader = yield Reader.fromAsset({
197
160
  path: "./tests/fixtures/CAI.jpg",
198
161
  });
199
162
  const json = reader.json();
@@ -203,7 +166,7 @@ describe("Reader", () => {
203
166
  it("should read from manifest data and buffer", () => __awaiter(void 0, void 0, void 0, function* () {
204
167
  const manifestData = yield fs.readFile("./tests/fixtures/CAI/manifest_data.c2pa");
205
168
  const buffer = yield fs.readFile("./tests/fixtures/CAI.jpg");
206
- const reader = yield Reader_1.Reader.fromManifestDataAndAsset(manifestData, {
169
+ const reader = yield Reader.fromManifestDataAndAsset(manifestData, {
207
170
  buffer,
208
171
  mimeType: "jpeg",
209
172
  });
@@ -213,7 +176,7 @@ describe("Reader", () => {
213
176
  }));
214
177
  it("should read from manifest data and file", () => __awaiter(void 0, void 0, void 0, function* () {
215
178
  const manifestData = yield fs.readFile("./tests/fixtures/CAI/manifest_data.c2pa");
216
- const reader = yield Reader_1.Reader.fromManifestDataAndAsset(manifestData, {
179
+ const reader = yield Reader.fromManifestDataAndAsset(manifestData, {
217
180
  path: "./tests/fixtures/CAI.jpg",
218
181
  });
219
182
  const json = reader.json();
@@ -222,8 +185,8 @@ describe("Reader", () => {
222
185
  it("should write to a file", () => __awaiter(void 0, void 0, void 0, function* () {
223
186
  var _a;
224
187
  let bytesWritten = 0;
225
- const outputPath = path_1.default.join(tempDir, "thumbnail.jpg");
226
- const reader = yield Reader_1.Reader.fromAsset({
188
+ const outputPath = path.join(tempDir, "thumbnail.jpg");
189
+ const reader = yield Reader.fromAsset({
227
190
  path: "./tests/fixtures/CAI.jpg",
228
191
  });
229
192
  const activeManifest = reader.getActive();
@@ -237,14 +200,14 @@ describe("Reader", () => {
237
200
  expect(fs.existsSync(outputPath));
238
201
  }));
239
202
  it("should report manifest is embedded", () => __awaiter(void 0, void 0, void 0, function* () {
240
- const reader = yield Reader_1.Reader.fromAsset({
203
+ const reader = yield Reader.fromAsset({
241
204
  path: "./tests/fixtures/CAI.jpg",
242
205
  });
243
206
  expect(reader.remoteUrl()).toEqual("");
244
207
  expect(reader.isEmbedded()).toBeTruthy();
245
208
  }));
246
209
  it("should report manifest is not embedded", () => __awaiter(void 0, void 0, void 0, function* () {
247
- const reader = yield Reader_1.Reader.fromAsset({
210
+ const reader = yield Reader.fromAsset({
248
211
  path: "./tests/fixtures/cloud.jpg",
249
212
  });
250
213
  expect(reader.remoteUrl()).toEqual("https://cai-manifests.adobe.com/manifests/adobe-urn-uuid-5f37e182-3687-462e-a7fb-573462780391");
@@ -0,0 +1,203 @@
1
+ // Copyright 2025 Adobe. All rights reserved.
2
+ // This file is licensed to you under the Apache License,
3
+ // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
4
+ // or the MIT license (http://opensource.org/licenses/MIT),
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
+ // Unless required by applicable law or agreed to in writing,
16
+ // this software is distributed on an "AS IS" BASIS, WITHOUT
17
+ // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
18
+ // implied. See the LICENSE-MIT and LICENSE-APACHE files for the
19
+ // specific language governing permissions and limitations under
20
+ // each license.
21
+ const neon = require("./index.node");
22
+ import * as fs from "fs-extra";
23
+ import path from "path";
24
+ import fetch from "node-fetch";
25
+ /**
26
+ * Load settings from a JSON string and apply them globally.
27
+ * @param json The JSON string containing the settings configuration
28
+ */
29
+ export function loadC2paSettings(json) {
30
+ neon.loadSettings(json);
31
+ }
32
+ /**
33
+ * Load settings from a TOML string and apply them globally.
34
+ * @param toml The TOML string containing the settings configuration
35
+ */
36
+ export function loadC2paSettingsToml(toml) {
37
+ neon.loadSettingsToml(toml);
38
+ }
39
+ /**
40
+ * Load settings from a TOML or JSON file path and apply them globally.
41
+ * The file format is determined by the file extension (.toml for TOML, otherwise JSON).
42
+ * @param filePath The path to the settings file
43
+ */
44
+ export function loadSettingsFromFile(filePath) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ const ext = path.extname(filePath).toLowerCase();
47
+ const content = yield fs.readFile(filePath, "utf8");
48
+ if (ext === ".toml") {
49
+ loadC2paSettingsToml(content);
50
+ return;
51
+ }
52
+ // Assume JSON (or JSON5 that is valid JSON) otherwise
53
+ // If needed, callers can ensure the content is valid JSON.
54
+ loadC2paSettings(content);
55
+ });
56
+ }
57
+ /**
58
+ * Load settings from a URL and apply them globally.
59
+ * The format is determined by the content-type header or file extension (.toml for TOML, otherwise JSON).
60
+ * @param url The URL to fetch the settings from
61
+ */
62
+ export function loadSettingsFromUrl(url) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ const res = yield fetch(url);
65
+ if (!res.ok) {
66
+ throw new Error(`Failed to fetch settings from URL: ${res.status} ${res.statusText}`);
67
+ }
68
+ const contentType = res.headers.get("content-type") || "";
69
+ const text = yield res.text();
70
+ const isToml = contentType.includes("toml") || url.toLowerCase().endsWith(".toml");
71
+ if (isToml) {
72
+ loadC2paSettingsToml(text);
73
+ return;
74
+ }
75
+ // Treat as JSON by default
76
+ loadC2paSettings(text);
77
+ });
78
+ }
79
+ /**
80
+ * Get the current settings as a JSON string.
81
+ * @returns The JSON representation of the current settings
82
+ */
83
+ export function getSettingsJson() {
84
+ return neon.getSettingsJson();
85
+ }
86
+ /**
87
+ * Load trust configuration into the main trust settings.
88
+ * @param trustConfig The trust configuration object
89
+ */
90
+ export function loadTrustConfig(trustConfig) {
91
+ var _a, _b, _c, _d;
92
+ // Convert settings to snake_case for the neon Rust code.
93
+ // Explicitly set undefined fields to null to ensure they are cleared
94
+ const config = {
95
+ verify_trust_list: trustConfig.verifyTrustList,
96
+ user_anchors: (_a = trustConfig.userAnchors) !== null && _a !== void 0 ? _a : null,
97
+ trust_anchors: (_b = trustConfig.trustAnchors) !== null && _b !== void 0 ? _b : null,
98
+ trust_config: (_c = trustConfig.trustConfig) !== null && _c !== void 0 ? _c : null,
99
+ allowed_list: (_d = trustConfig.allowedList) !== null && _d !== void 0 ? _d : null,
100
+ };
101
+ neon.loadTrustConfig(JSON.stringify(config));
102
+ }
103
+ /**
104
+ * Load trust configuration into the CAWG trust settings.
105
+ * @param trustConfig The trust configuration object
106
+ */
107
+ export function loadCawgTrustConfig(trustConfig) {
108
+ var _a, _b, _c, _d;
109
+ // Convert settings to snake_case for the neon Rust code
110
+ // Explicitly set undefined fields to null to ensure they are cleared
111
+ const config = {
112
+ verify_trust_list: trustConfig.verifyTrustList,
113
+ user_anchors: (_a = trustConfig.userAnchors) !== null && _a !== void 0 ? _a : null,
114
+ trust_anchors: (_b = trustConfig.trustAnchors) !== null && _b !== void 0 ? _b : null,
115
+ trust_config: (_c = trustConfig.trustConfig) !== null && _c !== void 0 ? _c : null,
116
+ allowed_list: (_d = trustConfig.allowedList) !== null && _d !== void 0 ? _d : null,
117
+ };
118
+ neon.loadCawgTrustConfig(JSON.stringify(config));
119
+ }
120
+ /**
121
+ * Get the current trust configuration.
122
+ * @returns The current trust configuration object
123
+ */
124
+ export function getTrustConfig() {
125
+ var _a, _b, _c, _d;
126
+ const json = neon.getTrustConfig();
127
+ const parsed = JSON.parse(json);
128
+ // Convert snake_case to camelCase to match TrustConfig interface
129
+ // Handle undefined values by converting them to null
130
+ return {
131
+ verifyTrustList: parsed.verify_trust_list,
132
+ userAnchors: (_a = parsed.user_anchors) !== null && _a !== void 0 ? _a : null,
133
+ trustAnchors: (_b = parsed.trust_anchors) !== null && _b !== void 0 ? _b : null,
134
+ trustConfig: (_c = parsed.trust_config) !== null && _c !== void 0 ? _c : null,
135
+ allowedList: (_d = parsed.allowed_list) !== null && _d !== void 0 ? _d : null,
136
+ };
137
+ }
138
+ /**
139
+ * Get the current CAWG trust configuration.
140
+ * @returns The current CAWG trust configuration object
141
+ */
142
+ export function getCawgTrustConfig() {
143
+ var _a, _b, _c, _d;
144
+ const json = neon.getCawgTrustConfig();
145
+ const parsed = JSON.parse(json);
146
+ // Convert snake_case to camelCase to match TrustConfig interface
147
+ // Handle undefined values by converting them to null
148
+ return {
149
+ verifyTrustList: parsed.verify_trust_list,
150
+ userAnchors: (_a = parsed.user_anchors) !== null && _a !== void 0 ? _a : null,
151
+ trustAnchors: (_b = parsed.trust_anchors) !== null && _b !== void 0 ? _b : null,
152
+ trustConfig: (_c = parsed.trust_config) !== null && _c !== void 0 ? _c : null,
153
+ allowedList: (_d = parsed.allowed_list) !== null && _d !== void 0 ? _d : null,
154
+ };
155
+ }
156
+ /**
157
+ * Load verify configuration into the verify settings.
158
+ * @param verifyConfig The verify configuration object
159
+ */
160
+ export function loadVerifyConfig(verifyConfig) {
161
+ // Convert camelCase to snake_case for the neon Rust code
162
+ const config = {
163
+ verify_after_reading: verifyConfig.verifyAfterReading,
164
+ verify_after_sign: verifyConfig.verifyAfterSign,
165
+ verify_trust: verifyConfig.verifyTrust,
166
+ verify_timestamp_trust: verifyConfig.verifyTimestampTrust,
167
+ ocsp_fetch: verifyConfig.ocspFetch,
168
+ remote_manifest_fetch: verifyConfig.remoteManifestFetch,
169
+ check_ingredient_trust: verifyConfig.checkIngredientTrust,
170
+ skip_ingredient_conflict_resolution: verifyConfig.skipIngredientConflictResolution,
171
+ strict_v1_validation: verifyConfig.strictV1Validation,
172
+ };
173
+ neon.loadVerifyConfig(JSON.stringify(config));
174
+ }
175
+ /**
176
+ * Patch the verify configuration with any subset of fields.
177
+ * @param patch Partial verify config fields to update
178
+ */
179
+ export function patchVerifyConfig(patch) {
180
+ const current = getVerifyConfig();
181
+ const updated = Object.assign(Object.assign({}, current), patch);
182
+ loadVerifyConfig(updated);
183
+ }
184
+ /**
185
+ * Get the current verify configuration.
186
+ * @returns The current verify configuration object
187
+ */
188
+ export function getVerifyConfig() {
189
+ const json = neon.getVerifyConfig();
190
+ const parsed = JSON.parse(json);
191
+ // Convert snake_case to camelCase to match VerifyConfig interface
192
+ return {
193
+ verifyAfterReading: parsed.verify_after_reading,
194
+ verifyAfterSign: parsed.verify_after_sign,
195
+ verifyTrust: parsed.verify_trust,
196
+ verifyTimestampTrust: parsed.verify_timestamp_trust,
197
+ ocspFetch: parsed.ocsp_fetch,
198
+ remoteManifestFetch: parsed.remote_manifest_fetch,
199
+ checkIngredientTrust: parsed.check_ingredient_trust,
200
+ skipIngredientConflictResolution: parsed.skip_ingredient_conflict_resolution,
201
+ strictV1Validation: parsed.strict_v1_validation,
202
+ };
203
+ }