@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.
- package/Cargo.toml +1 -1
- package/README.md +71 -55
- package/dist/Builder.js +6 -32
- package/dist/Builder.spec.js +74 -71
- package/dist/IdentityAssertion.spec.js +82 -4
- package/dist/Reader.js +9 -13
- package/dist/Reader.spec.js +4 -27
- package/dist/Settings.js +0 -39
- package/dist/Settings.spec.js +1 -79
- package/dist/Signer.spec.js +1 -1
- package/dist/Trustmark.js +14 -0
- package/dist/Trustmark.spec.js +247 -9
- package/dist/index.node +2 -2
- package/dist/types/Builder.d.ts +4 -20
- package/dist/types/Reader.d.ts +6 -10
- package/dist/types/Settings.d.ts +0 -21
- package/dist/types/Trustmark.d.ts +12 -0
- package/dist/types/types.d.ts +22 -30
- package/package.json +3 -3
- package/src/lib.rs +1 -0
- package/src/neon_identity_assertion_signer.rs +12 -3
- package/src/neon_trustmark.rs +126 -8
package/dist/Reader.spec.js
CHANGED
|
@@ -71,7 +71,7 @@ describe("Reader", () => {
|
|
|
71
71
|
"parameters": {
|
|
72
72
|
"ingredient": {
|
|
73
73
|
"url": "self#jumbf=c2pa.assertions/c2pa.ingredient",
|
|
74
|
-
"hash": "5dNlxTKe4afGAicpJa1hF1R3mBZKE+Bl0xmh0McXuO4="
|
|
74
|
+
"hash": "b64'5dNlxTKe4afGAicpJa1hF1R3mBZKE+Bl0xmh0McXuO4='"
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
},
|
|
@@ -175,27 +175,6 @@ describe("Reader", () => {
|
|
|
175
175
|
expect(json.manifests).toEqual(manifestStore.manifests);
|
|
176
176
|
expect(json.validation_status[0].code).toEqual("signingCredential.untrusted");
|
|
177
177
|
});
|
|
178
|
-
it("should read from manifest data and file with settings context", async () => {
|
|
179
|
-
const manifestData = await fs.readFile("./tests/fixtures/CA/manifest_data.c2pa");
|
|
180
|
-
const buffer = await fs.readFile("./tests/fixtures/CA.jpg");
|
|
181
|
-
const settings = {
|
|
182
|
-
verify: {
|
|
183
|
-
verify_after_reading: false,
|
|
184
|
-
verify_trust: false,
|
|
185
|
-
},
|
|
186
|
-
};
|
|
187
|
-
const reader = await Reader.fromManifestDataAndAsset(manifestData, {
|
|
188
|
-
buffer,
|
|
189
|
-
mimeType: "jpeg",
|
|
190
|
-
}, settings);
|
|
191
|
-
expect(reader).not.toBeNull();
|
|
192
|
-
const json = reader.json();
|
|
193
|
-
// Settings should affect validation behavior
|
|
194
|
-
// With verify_after_reading: false, validation_status should be undefined
|
|
195
|
-
expect(json.validation_status).toBeUndefined();
|
|
196
|
-
expect(json.manifests).toBeDefined();
|
|
197
|
-
expect(json.active_manifest).toEqual(manifestStore.active_manifest);
|
|
198
|
-
});
|
|
199
178
|
it("should read from manifest data and file with a Context", async () => {
|
|
200
179
|
const manifestData = await fs.readFile("./tests/fixtures/CA/manifest_data.c2pa");
|
|
201
180
|
const buffer = await fs.readFile("./tests/fixtures/CA.jpg");
|
|
@@ -211,8 +190,6 @@ describe("Reader", () => {
|
|
|
211
190
|
}, context);
|
|
212
191
|
expect(reader).not.toBeNull();
|
|
213
192
|
const json = reader.json();
|
|
214
|
-
// Same effect as the raw-settings equivalent above: verifyAfterReading: false means
|
|
215
|
-
// validation_status should be undefined.
|
|
216
193
|
expect(json.validation_status).toBeUndefined();
|
|
217
194
|
expect(json.manifests).toBeDefined();
|
|
218
195
|
expect(json.active_manifest).toEqual(manifestStore.active_manifest);
|
|
@@ -268,17 +245,17 @@ describe("Reader", () => {
|
|
|
268
245
|
expect(signatureInfo.alg).toBe("Ed25519");
|
|
269
246
|
expect(signatureInfo.issuer).toBe("C2PA Test Signing Cert");
|
|
270
247
|
expect(signatureInfo.cert_serial_number).toBe("638838410810235485828984295321338730070538954823");
|
|
271
|
-
expect(signatureInfo.revocation_status).
|
|
248
|
+
expect(signatureInfo.revocation_status).toBeUndefined();
|
|
272
249
|
// Verify signer_payload values
|
|
273
250
|
expect(signerPayload.sig_type).toBe("cawg.x509.cose");
|
|
274
251
|
expect(signerPayload.referenced_assertions).toHaveLength(2);
|
|
275
252
|
expect(signerPayload.referenced_assertions[0]).toEqual({
|
|
276
253
|
url: "self#jumbf=c2pa.assertions/cawg.training-mining",
|
|
277
|
-
hash: "rBBgURB+/0Bc2Uk3+blNpYTGQTxOwzXQ2xhjA3gsqI4=",
|
|
254
|
+
hash: Array.from(Buffer.from("rBBgURB+/0Bc2Uk3+blNpYTGQTxOwzXQ2xhjA3gsqI4=", "base64")),
|
|
278
255
|
});
|
|
279
256
|
expect(signerPayload.referenced_assertions[1]).toEqual({
|
|
280
257
|
url: "self#jumbf=c2pa.assertions/c2pa.hash.data",
|
|
281
|
-
hash: "sASozh9KFSkW+cyMI0Pw5KYoD2qn7MkUEq9jUUhe/sM=",
|
|
258
|
+
hash: Array.from(Buffer.from("sASozh9KFSkW+cyMI0Pw5KYoD2qn7MkUEq9jUUhe/sM=", "base64")),
|
|
282
259
|
});
|
|
283
260
|
});
|
|
284
261
|
it("should return null when reading an asset without C2PA metadata", async () => {
|
package/dist/Settings.js
CHANGED
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
// specific language governing permissions and limitations under
|
|
11
11
|
// each license.
|
|
12
12
|
import fs from "fs-extra";
|
|
13
|
-
import { Context } from "@contentauth/c2pa-utilities";
|
|
14
13
|
/**
|
|
15
14
|
* This file contains only Settings functions that are unique to the Node SDK.
|
|
16
15
|
*
|
|
@@ -26,41 +25,3 @@ export async function loadSettingsFromFile(filePath) {
|
|
|
26
25
|
const content = await fs.readFile(filePath, "utf8");
|
|
27
26
|
return content;
|
|
28
27
|
}
|
|
29
|
-
/**
|
|
30
|
-
* Reads the SDK config out of a settings argument.
|
|
31
|
-
*
|
|
32
|
-
* Only a `Context` carries an SDK config so the deprecated raw `C2paSettings` forms always
|
|
33
|
-
* resolve to `undefined`, leaving this package's defaults in place.
|
|
34
|
-
*
|
|
35
|
-
* @param settingsOrContext A `Context`, or (@deprecated) a raw `C2paSettings` string/object.
|
|
36
|
-
* @returns The `Context`'s SDK config, or `undefined` if there is none.
|
|
37
|
-
*/
|
|
38
|
-
export function resolveSdkConfig(settingsOrContext) {
|
|
39
|
-
return settingsOrContext instanceof Context
|
|
40
|
-
? settingsOrContext.sdkConfig
|
|
41
|
-
: undefined;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Resolves Context settings, including any trust-anchor URLs, before passing them to the native
|
|
45
|
-
* library. Deprecated raw settings retain their existing pass-through behavior.
|
|
46
|
-
*/
|
|
47
|
-
export async function resolveSettingsForNeon(settingsOrContext) {
|
|
48
|
-
if (settingsOrContext == null) {
|
|
49
|
-
return undefined;
|
|
50
|
-
}
|
|
51
|
-
if (settingsOrContext instanceof Context) {
|
|
52
|
-
return settingsOrContext.toJson();
|
|
53
|
-
}
|
|
54
|
-
return typeof settingsOrContext === "string"
|
|
55
|
-
? settingsOrContext
|
|
56
|
-
: JSON.stringify(settingsOrContext);
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Resolves deprecated raw settings for the synchronous Builder factories.
|
|
60
|
-
*/
|
|
61
|
-
export function resolveRawSettingsForNeon(settings) {
|
|
62
|
-
if (settings == null) {
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|
|
65
|
-
return typeof settings === "string" ? settings : JSON.stringify(settings);
|
|
66
|
-
}
|
package/dist/Settings.spec.js
CHANGED
|
@@ -6,9 +6,7 @@
|
|
|
6
6
|
import fs from "fs-extra";
|
|
7
7
|
import * as path from "path";
|
|
8
8
|
import * as os from "os";
|
|
9
|
-
import {
|
|
10
|
-
import { vi } from "vitest";
|
|
11
|
-
import { loadSettingsFromFile, resolveRawSettingsForNeon, resolveSdkConfig, resolveSettingsForNeon, } from "./Settings.js";
|
|
9
|
+
import { loadSettingsFromFile } from "./Settings.js";
|
|
12
10
|
describe("Settings", () => {
|
|
13
11
|
describe("loadSettingsFromFile", () => {
|
|
14
12
|
let tempDir;
|
|
@@ -48,80 +46,4 @@ verify_after_sign = false`;
|
|
|
48
46
|
await expect(loadSettingsFromFile(filePath)).rejects.toThrow();
|
|
49
47
|
});
|
|
50
48
|
});
|
|
51
|
-
describe("resolveRawSettingsForNeon", () => {
|
|
52
|
-
it("returns undefined when omitted", () => {
|
|
53
|
-
expect(resolveRawSettingsForNeon(undefined)).toBeUndefined();
|
|
54
|
-
});
|
|
55
|
-
it("returns undefined when null is passed in", () => {
|
|
56
|
-
// A plain-JS caller can pass `null` explicitly.
|
|
57
|
-
// It must not fall through to JSON.stringify(null), which would send the
|
|
58
|
-
// native library the literal string "null" instead of "no settings".
|
|
59
|
-
expect(resolveRawSettingsForNeon(null)).toBeUndefined();
|
|
60
|
-
});
|
|
61
|
-
it("passes raw settings through unchanged", () => {
|
|
62
|
-
const rawSettings = { verify: { verify_trust: false } };
|
|
63
|
-
const result = resolveRawSettingsForNeon(rawSettings);
|
|
64
|
-
expect(JSON.parse(result)).toEqual(rawSettings);
|
|
65
|
-
});
|
|
66
|
-
it("passes raw settings JSON through unchanged", () => {
|
|
67
|
-
const json = JSON.stringify({ verify: { verify_trust: false } });
|
|
68
|
-
expect(resolveRawSettingsForNeon(json)).toBe(json);
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
describe("resolveSettingsForNeon", () => {
|
|
72
|
-
it("returns undefined when omitted", async () => {
|
|
73
|
-
expect(await resolveSettingsForNeon(undefined)).toBeUndefined();
|
|
74
|
-
});
|
|
75
|
-
it("returns undefined when null is passed in", async () => {
|
|
76
|
-
expect(await resolveSettingsForNeon(null)).toBeUndefined();
|
|
77
|
-
});
|
|
78
|
-
it("applies defaults for an empty Context", async () => {
|
|
79
|
-
const result = await resolveSettingsForNeon(new Context());
|
|
80
|
-
expect(JSON.parse(result)).toEqual(JSON.parse(settingsToJson(DEFAULT_SETTINGS)));
|
|
81
|
-
});
|
|
82
|
-
it("does not serialize a Context's SDK config", async () => {
|
|
83
|
-
const result = await resolveSettingsForNeon(new Context(undefined, { maxSizeInBytes: 42 }));
|
|
84
|
-
expect(JSON.parse(result)).toEqual(JSON.parse(settingsToJson(DEFAULT_SETTINGS)));
|
|
85
|
-
});
|
|
86
|
-
it("merges a Context's settings with defaults", async () => {
|
|
87
|
-
const settings = { verify: { verifyTrust: false } };
|
|
88
|
-
const result = await resolveSettingsForNeon(new Context(settings));
|
|
89
|
-
expect(JSON.parse(result)).toEqual(JSON.parse(settingsToJson(mergeSettings(DEFAULT_SETTINGS, settings))));
|
|
90
|
-
});
|
|
91
|
-
it("fetches trust-anchor URLs before returning Context settings", async () => {
|
|
92
|
-
const pem = "-----BEGIN CERTIFICATE-----\nanchor\n-----END CERTIFICATE-----";
|
|
93
|
-
const fetch = vi.fn(async () => new Response(pem));
|
|
94
|
-
vi.stubGlobal("fetch", fetch);
|
|
95
|
-
try {
|
|
96
|
-
const result = await resolveSettingsForNeon(new Context({
|
|
97
|
-
trust: { trustAnchors: "https://example.com/anchors.pem" },
|
|
98
|
-
}));
|
|
99
|
-
expect(fetch).toHaveBeenCalledOnce();
|
|
100
|
-
expect(JSON.parse(result).trust.anchors[0].trust_anchors).toBe(pem);
|
|
101
|
-
}
|
|
102
|
-
finally {
|
|
103
|
-
vi.unstubAllGlobals();
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
it("keeps deprecated raw settings unchanged", async () => {
|
|
107
|
-
const rawSettings = { verify: { verify_trust: false } };
|
|
108
|
-
const result = await resolveSettingsForNeon(rawSettings);
|
|
109
|
-
expect(JSON.parse(result)).toEqual(rawSettings);
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
describe("resolveSdkConfig", () => {
|
|
113
|
-
it("returns a Context's SDK config", () => {
|
|
114
|
-
const sdkConfig = { maxSizeInBytes: 42 };
|
|
115
|
-
expect(resolveSdkConfig(new Context(undefined, sdkConfig))).toEqual(sdkConfig);
|
|
116
|
-
});
|
|
117
|
-
it("returns undefined when a Context carries no SDK config", () => {
|
|
118
|
-
expect(resolveSdkConfig(new Context())).toBeUndefined();
|
|
119
|
-
});
|
|
120
|
-
it("returns undefined for the deprecated raw settings forms", () => {
|
|
121
|
-
expect(resolveSdkConfig(undefined)).toBeUndefined();
|
|
122
|
-
expect(resolveSdkConfig(null)).toBeUndefined();
|
|
123
|
-
expect(resolveSdkConfig({ verify: { verifyTrust: false } })).toBeUndefined();
|
|
124
|
-
expect(resolveSdkConfig('{"verify":{"verify_trust":false}}')).toBeUndefined();
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
49
|
});
|
package/dist/Signer.spec.js
CHANGED
|
@@ -121,7 +121,7 @@ describe("CallbackSigner", () => {
|
|
|
121
121
|
],
|
|
122
122
|
resources: { resources: {} },
|
|
123
123
|
};
|
|
124
|
-
const builder = Builder.
|
|
124
|
+
const builder = await Builder.withJsonAsync(manifestDefinition);
|
|
125
125
|
builder.setIntent({ create: "http://c2pa.org/digitalsourcetype/empty" });
|
|
126
126
|
const source = {
|
|
127
127
|
buffer: await fs.readFile("./tests/fixtures/CA.jpg"),
|
package/dist/Trustmark.js
CHANGED
|
@@ -25,4 +25,18 @@ export class Trustmark {
|
|
|
25
25
|
async decode(image) {
|
|
26
26
|
return getNeonBinary().trustmarkDecode.call(this.trustmark, image);
|
|
27
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Remove a P/Q watermark from an 8-bit JPEG or PNG, returning an encoded PNG
|
|
30
|
+
* with the same dimensions and alpha. Requires `loadRemover: true`.
|
|
31
|
+
* Removal is lossy and does not guarantee restoration of the original pixels.
|
|
32
|
+
* Input is limited to 64 MiB, 16,384 pixels per dimension, and 32 million
|
|
33
|
+
* pixels, with a best-effort 128 MiB decoder allocation budget. Limits are
|
|
34
|
+
* per call, not a cap on total process memory.
|
|
35
|
+
* Image metadata (EXIF, XMP, ICC profiles, and C2PA manifests) is not copied.
|
|
36
|
+
* Existing credentials do not remain valid for the modified pixels; sign a
|
|
37
|
+
* new manifest to attach valid Content Credentials to the output.
|
|
38
|
+
*/
|
|
39
|
+
async remove(image) {
|
|
40
|
+
return getNeonBinary().trustmarkRemove.call(this.trustmark, image);
|
|
41
|
+
}
|
|
28
42
|
}
|
package/dist/Trustmark.spec.js
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
// specific language governing permissions and limitations under
|
|
11
11
|
// each license.
|
|
12
12
|
import path from "path";
|
|
13
|
+
import os from "os";
|
|
13
14
|
import fs from "fs-extra";
|
|
14
15
|
import sharp from "sharp";
|
|
15
16
|
import { Trustmark } from "./Trustmark.js";
|
|
@@ -24,8 +25,24 @@ async function rawRgbToJpeg(rawRgbData, width, height) {
|
|
|
24
25
|
channels: 3, // RGB
|
|
25
26
|
},
|
|
26
27
|
});
|
|
27
|
-
//
|
|
28
|
-
return await image.jpeg().toBuffer();
|
|
28
|
+
// Use a high-quality JPEG round-trip for the perceptual P model.
|
|
29
|
+
return await image.jpeg({ quality: 95 }).toBuffer();
|
|
30
|
+
}
|
|
31
|
+
// Modify only the PNG header so oversized-image tests never allocate its pixels.
|
|
32
|
+
function pngWithDimensions(png, width, height) {
|
|
33
|
+
const image = Buffer.from(png);
|
|
34
|
+
expect(image.toString("ascii", 12, 16)).toBe("IHDR");
|
|
35
|
+
image.writeUInt32BE(width, 16);
|
|
36
|
+
image.writeUInt32BE(height, 20);
|
|
37
|
+
let crc = 0xffffffff;
|
|
38
|
+
for (const byte of image.subarray(12, 29)) {
|
|
39
|
+
crc ^= byte;
|
|
40
|
+
for (let bit = 0; bit < 8; bit++) {
|
|
41
|
+
crc = (crc >>> 1) ^ (crc & 1 ? 0xedb88320 : 0);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
image.writeUInt32BE((crc ^ 0xffffffff) >>> 0, 29);
|
|
45
|
+
return image;
|
|
29
46
|
}
|
|
30
47
|
describe("Trustmark", () => {
|
|
31
48
|
let trustmark;
|
|
@@ -33,14 +50,17 @@ describe("Trustmark", () => {
|
|
|
33
50
|
let testImageWidth;
|
|
34
51
|
let testImageHeight;
|
|
35
52
|
const trustmarkConfig = {
|
|
36
|
-
variant: "
|
|
37
|
-
version: "
|
|
53
|
+
variant: "P", // Perceptual model, cached in CI
|
|
54
|
+
version: "BCH_5", // Tolerates 5 bit flips
|
|
38
55
|
modelPath: path.join(tempDir, "trustmark_models"), // Use temporary directory for model path
|
|
39
56
|
};
|
|
40
57
|
beforeAll(async () => {
|
|
41
58
|
await fs.ensureDir(tempDir);
|
|
42
59
|
// Use an actual test image from fixtures directory
|
|
43
|
-
testImage = await
|
|
60
|
+
testImage = await sharp("./tests/fixtures/A.jpg")
|
|
61
|
+
.resize(512, 512)
|
|
62
|
+
.png()
|
|
63
|
+
.toBuffer();
|
|
44
64
|
// Get image dimensions for converting raw pixel data
|
|
45
65
|
const imageInfo = await sharp(testImage).metadata();
|
|
46
66
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
@@ -93,8 +113,8 @@ describe("Trustmark", () => {
|
|
|
93
113
|
});
|
|
94
114
|
describe("decode", () => {
|
|
95
115
|
it("should decode a watermark from an encoded image", async () => {
|
|
96
|
-
const strength = 0.
|
|
97
|
-
const customWatermark = "
|
|
116
|
+
const strength = 0.95;
|
|
117
|
+
const customWatermark = "1011011110011000111111000000011111011111011100000110110110111";
|
|
98
118
|
// First encode a watermark
|
|
99
119
|
const rawPixelData = await trustmark.encode(testImage, strength, customWatermark);
|
|
100
120
|
// Convert raw pixel data to JPEG format for decoding
|
|
@@ -105,10 +125,10 @@ describe("Trustmark", () => {
|
|
|
105
125
|
expect(typeof decodedWatermark).toBe("string");
|
|
106
126
|
expect(decodedWatermark.length).toBeGreaterThan(0);
|
|
107
127
|
// The decoded watermark should match the original
|
|
108
|
-
expect(decodedWatermark.
|
|
128
|
+
expect(decodedWatermark).toBe(customWatermark);
|
|
109
129
|
}, 30000);
|
|
110
130
|
it("should decode watermark from image with generated watermark", async () => {
|
|
111
|
-
const strength = 0.
|
|
131
|
+
const strength = 0.95;
|
|
112
132
|
// Encode with default watermark
|
|
113
133
|
const rawPixelData = await trustmark.encode(testImage, strength);
|
|
114
134
|
// Convert raw pixel data to JPEG format for decoding
|
|
@@ -128,4 +148,222 @@ describe("Trustmark", () => {
|
|
|
128
148
|
await expect(trustmark.decode(emptyBuffer)).rejects.toThrow("The image format could not be determined");
|
|
129
149
|
}, 30000);
|
|
130
150
|
});
|
|
151
|
+
it("rejects removal when not opted in", async () => {
|
|
152
|
+
await expect(trustmark.remove(testImage)).rejects.toThrow("Watermark remover not loaded");
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
describe("Trustmark removal configuration", () => {
|
|
156
|
+
it.each(["B", "C"])("rejects unsupported variant %s before creating the model directory", async (variant) => {
|
|
157
|
+
const root = await fs.mkdtemp(path.join(os.tmpdir(), "trustmark-config-"));
|
|
158
|
+
const modelPath = path.join(root, "models");
|
|
159
|
+
try {
|
|
160
|
+
await expect(Trustmark.newTrustmark({
|
|
161
|
+
variant,
|
|
162
|
+
version: "BCH_5",
|
|
163
|
+
loadRemover: true,
|
|
164
|
+
modelPath,
|
|
165
|
+
})).rejects.toThrow("removal supports only P/Q variants");
|
|
166
|
+
expect(await fs.pathExists(modelPath)).toBe(false);
|
|
167
|
+
}
|
|
168
|
+
finally {
|
|
169
|
+
await fs.remove(root);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
describe("Trustmark P removal", () => {
|
|
174
|
+
const variant = "P";
|
|
175
|
+
let runner;
|
|
176
|
+
let source;
|
|
177
|
+
const modelPath = path.join(tempDir, "trustmark_models");
|
|
178
|
+
const original = "1011011110011000111111000000011111011111011100000110110110111";
|
|
179
|
+
const replacement = "0011011110011000111111000000011111011111011100000110110110111";
|
|
180
|
+
beforeAll(async () => {
|
|
181
|
+
runner = await Trustmark.newTrustmark({
|
|
182
|
+
variant,
|
|
183
|
+
version: "BCH_5",
|
|
184
|
+
loadRemover: true,
|
|
185
|
+
modelPath,
|
|
186
|
+
});
|
|
187
|
+
source = await sharp("./tests/fixtures/A.jpg")
|
|
188
|
+
.resize(512, 512)
|
|
189
|
+
.png()
|
|
190
|
+
.toBuffer();
|
|
191
|
+
}, 180000);
|
|
192
|
+
it.each(["jpeg", "png"])("removes the old watermark and allows re-watermarking a %s", async (format) => {
|
|
193
|
+
const raw = await runner.encode(source, 0.95, original);
|
|
194
|
+
const encoded = await sharp(raw, {
|
|
195
|
+
raw: { width: 512, height: 512, channels: 3 },
|
|
196
|
+
})
|
|
197
|
+
.toFormat(format, { quality: 95 })
|
|
198
|
+
.toBuffer();
|
|
199
|
+
expect(await runner.decode(encoded)).toBe(original);
|
|
200
|
+
const recovered = await runner.remove(encoded);
|
|
201
|
+
expect(await sharp(recovered).metadata()).toMatchObject({
|
|
202
|
+
format: "png",
|
|
203
|
+
width: 512,
|
|
204
|
+
height: 512,
|
|
205
|
+
channels: 3,
|
|
206
|
+
hasAlpha: false,
|
|
207
|
+
});
|
|
208
|
+
const decoded = await runner.decode(recovered).catch((error) => {
|
|
209
|
+
expect(error).toBeInstanceOf(Error);
|
|
210
|
+
if (!(error instanceof Error))
|
|
211
|
+
throw error;
|
|
212
|
+
expect(error.message).toContain("watermark is corrupt or missing");
|
|
213
|
+
});
|
|
214
|
+
expect(decoded).not.toBe(original);
|
|
215
|
+
const reencoded = await runner.encode(recovered, 0.95, replacement);
|
|
216
|
+
const png = await sharp(reencoded, {
|
|
217
|
+
raw: { width: 512, height: 512, channels: 3 },
|
|
218
|
+
})
|
|
219
|
+
.png()
|
|
220
|
+
.toBuffer();
|
|
221
|
+
expect(await runner.decode(png)).toBe(replacement);
|
|
222
|
+
}, 60000);
|
|
223
|
+
it("preserves dimensions and exact alpha on unwatermarked images concurrently", async () => {
|
|
224
|
+
await Promise.all([
|
|
225
|
+
[17, 11],
|
|
226
|
+
[11, 17],
|
|
227
|
+
].map(async ([width, height]) => {
|
|
228
|
+
const rgba = Buffer.alloc(width * height * 4);
|
|
229
|
+
for (let i = 0; i < rgba.length; i += 4) {
|
|
230
|
+
rgba.set([120, 80, 160, (i / 4) % 256], i);
|
|
231
|
+
}
|
|
232
|
+
const input = await sharp(rgba, {
|
|
233
|
+
raw: { width, height, channels: 4 },
|
|
234
|
+
})
|
|
235
|
+
.png()
|
|
236
|
+
.toBuffer();
|
|
237
|
+
const output = await runner.remove(input);
|
|
238
|
+
const { data, info } = await sharp(output)
|
|
239
|
+
.raw()
|
|
240
|
+
.toBuffer({ resolveWithObject: true });
|
|
241
|
+
expect(info).toMatchObject({ width, height, channels: 4 });
|
|
242
|
+
for (let i = 3; i < rgba.length; i += 4) {
|
|
243
|
+
expect(data[i]).toBe(rgba[i]);
|
|
244
|
+
}
|
|
245
|
+
}));
|
|
246
|
+
}, 30000);
|
|
247
|
+
it("rejects invalid and unsupported input then remains usable", async () => {
|
|
248
|
+
const gif = await sharp(source).gif().toBuffer();
|
|
249
|
+
for (const invalid of [
|
|
250
|
+
Buffer.alloc(0),
|
|
251
|
+
Buffer.from("not an image"),
|
|
252
|
+
source.subarray(0, 40),
|
|
253
|
+
gif,
|
|
254
|
+
]) {
|
|
255
|
+
await expect(runner.remove(invalid)).rejects.toThrow();
|
|
256
|
+
}
|
|
257
|
+
expect(await sharp(await runner.remove(source)).metadata()).toMatchObject({
|
|
258
|
+
width: 512,
|
|
259
|
+
height: 512,
|
|
260
|
+
});
|
|
261
|
+
}, 30000);
|
|
262
|
+
it("rejects 16-bit PNG input", async () => {
|
|
263
|
+
const input = await sharp(source).toColourspace("rgb16").png().toBuffer();
|
|
264
|
+
expect(await sharp(input).metadata()).toMatchObject({ bitsPerSample: 16 });
|
|
265
|
+
await expect(runner.remove(input)).rejects.toThrow("removal supports only 8-bit JPEG/PNG images");
|
|
266
|
+
});
|
|
267
|
+
it("rejects encoded input larger than 64 MiB before reading its contents", async () => {
|
|
268
|
+
const oversized = Buffer.alloc(64 * 1024 * 1024 + 1);
|
|
269
|
+
await expect(runner.remove(oversized)).rejects.toThrow("removal input exceeds 64 MiB");
|
|
270
|
+
});
|
|
271
|
+
it.each([
|
|
272
|
+
[16385, 1],
|
|
273
|
+
[1, 16385],
|
|
274
|
+
])("rejects dimensions %i x %i before decoding pixels", async (width, height) => {
|
|
275
|
+
const input = pngWithDimensions(source, width, height);
|
|
276
|
+
await expect(runner.remove(input)).rejects.toThrow(/limit/i);
|
|
277
|
+
});
|
|
278
|
+
it("rejects more than 32 million pixels before decoding pixels", async () => {
|
|
279
|
+
const input = pngWithDimensions(source, 8000, 4001);
|
|
280
|
+
await expect(runner.remove(input)).rejects.toThrow("removal image exceeds 32000000 pixels");
|
|
281
|
+
});
|
|
282
|
+
it("accepts the dimension boundary and remains usable after rejected input", async () => {
|
|
283
|
+
const input = await sharp({
|
|
284
|
+
create: {
|
|
285
|
+
width: 16384,
|
|
286
|
+
height: 1,
|
|
287
|
+
channels: 3,
|
|
288
|
+
background: { r: 120, g: 80, b: 160 },
|
|
289
|
+
},
|
|
290
|
+
})
|
|
291
|
+
.png()
|
|
292
|
+
.toBuffer();
|
|
293
|
+
const output = await runner.remove(input);
|
|
294
|
+
expect(await sharp(output).metadata()).toMatchObject({
|
|
295
|
+
format: "png",
|
|
296
|
+
width: 16384,
|
|
297
|
+
height: 1,
|
|
298
|
+
channels: 3,
|
|
299
|
+
});
|
|
300
|
+
}, 30000);
|
|
301
|
+
it("does not download or load a remover by default or with loadRemover false", async () => {
|
|
302
|
+
const root = await fs.mkdtemp(path.join(os.tmpdir(), "trustmark-opt-in-"));
|
|
303
|
+
try {
|
|
304
|
+
for (const name of ["encoder", "decoder"]) {
|
|
305
|
+
const filename = `${name}_${variant}.onnx`;
|
|
306
|
+
await fs.copyFile(path.join(modelPath, filename), path.join(root, filename));
|
|
307
|
+
}
|
|
308
|
+
for (const loadRemover of [undefined, false]) {
|
|
309
|
+
const disabled = await Trustmark.newTrustmark({
|
|
310
|
+
variant,
|
|
311
|
+
version: "BCH_5",
|
|
312
|
+
modelPath: root,
|
|
313
|
+
loadRemover,
|
|
314
|
+
});
|
|
315
|
+
await expect(disabled.remove(source)).rejects.toThrow("Watermark remover not loaded");
|
|
316
|
+
}
|
|
317
|
+
expect(await fs.pathExists(path.join(root, `remover_${variant}.onnx`))).toBe(false);
|
|
318
|
+
}
|
|
319
|
+
finally {
|
|
320
|
+
await fs.remove(root);
|
|
321
|
+
}
|
|
322
|
+
}, 30000);
|
|
323
|
+
it("rejects initialization with an invalid remover instead of falling back", async () => {
|
|
324
|
+
const root = await fs.mkdtemp(path.join(os.tmpdir(), "trustmark-invalid-model-"));
|
|
325
|
+
try {
|
|
326
|
+
for (const name of ["encoder", "decoder"]) {
|
|
327
|
+
const filename = `${name}_${variant}.onnx`;
|
|
328
|
+
await fs.copyFile(path.join(modelPath, filename), path.join(root, filename));
|
|
329
|
+
}
|
|
330
|
+
await fs.writeFile(path.join(root, `remover_${variant}.onnx`), "invalid ONNX");
|
|
331
|
+
await expect(Trustmark.newTrustmark({
|
|
332
|
+
variant,
|
|
333
|
+
version: "BCH_5",
|
|
334
|
+
modelPath: root,
|
|
335
|
+
loadRemover: true,
|
|
336
|
+
})).rejects.toThrow(`unable to load remover model ${path.join(root, `remover_${variant}.onnx`)}`);
|
|
337
|
+
}
|
|
338
|
+
finally {
|
|
339
|
+
await fs.remove(root);
|
|
340
|
+
}
|
|
341
|
+
}, 30000);
|
|
342
|
+
it("rejects non-finite inference output instead of returning an image", async () => {
|
|
343
|
+
const root = await fs.mkdtemp(path.join(os.tmpdir(), "trustmark-inference-"));
|
|
344
|
+
try {
|
|
345
|
+
for (const name of ["encoder", "decoder"]) {
|
|
346
|
+
const filename = `${name}_${variant}.onnx`;
|
|
347
|
+
await fs.copyFile(path.join(modelPath, filename), path.join(root, filename));
|
|
348
|
+
}
|
|
349
|
+
// ONNX opset 13 ConstantOfShape graph: declares float32 [1, 3, 256, 256]
|
|
350
|
+
// input/output, but returns NaN pixels. Loading succeeds; inference fails
|
|
351
|
+
// TrustMark's output validation, exercising the native promise error path.
|
|
352
|
+
const model = Buffer.from("CAg6wwEKOAoFc2hhcGUSBm91dHB1dCIPQ29uc3RhbnRPZlNoYXBlKhYKBXZhbHVlKgoIARABSgQAAMB/oAEEEhFub25maW5pdGVfcmVtb3ZlciotCAQQB0IFc2hhcGVKIAEAAAAAAAAAAwAAAAAAAAAAAQAAAAAAAAABAAAAAAAAWiEKBWlucHV0EhgKFggBEhIKAggBCgIIAwoDCIACCgMIgAJiIgoGb3V0cHV0EhgKFggBEhIKAggBCgIIAwoDCIACCgMIgAJCAhAN", "base64");
|
|
353
|
+
await fs.writeFile(path.join(root, `remover_${variant}.onnx`), model);
|
|
354
|
+
const invalid = await Trustmark.newTrustmark({
|
|
355
|
+
variant,
|
|
356
|
+
version: "BCH_5",
|
|
357
|
+
modelPath: root,
|
|
358
|
+
loadRemover: true,
|
|
359
|
+
});
|
|
360
|
+
// Repeated failures must reject without poisoning the shared remover.
|
|
361
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
362
|
+
await expect(invalid.remove(source)).rejects.toThrow("invalid remover model");
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
finally {
|
|
366
|
+
await fs.remove(root);
|
|
367
|
+
}
|
|
368
|
+
}, 30000);
|
|
131
369
|
});
|
package/dist/index.node
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
[diffend] Oversized file quarantined before diffing.
|
|
2
2
|
name: package/dist/index.node
|
|
3
|
-
size:
|
|
4
|
-
sha256:
|
|
3
|
+
size: 52409088 bytes
|
|
4
|
+
sha256: dd151f6f6f5b4ccca5b9d5e754770ad977dca5f8dcef4bed3c3e0e78d6e8c118
|
package/dist/types/Builder.d.ts
CHANGED
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
import type { Action, BuilderIntent, C2paReason, Ingredient, Manifest, ManifestStore } from "@contentauth/c2pa-types";
|
|
2
2
|
import { Context } from "@contentauth/c2pa-utilities";
|
|
3
|
-
import type { BuilderInterface,
|
|
3
|
+
import type { BuilderInterface, CallbackSignerInterface, ClaimVersion, DestinationAsset, IdentityAssertionSignerInterface, JsCallbackSignerConfig, LocalSignerInterface, ManifestAssertionKind, ReaderInterface, SourceAsset, NeonBuilderHandle } from "./types.d.ts";
|
|
4
4
|
export declare class Builder implements BuilderInterface {
|
|
5
5
|
private builder;
|
|
6
6
|
constructor(builder: NeonBuilderHandle);
|
|
7
|
-
/**
|
|
8
|
-
* @deprecated Use {@link Builder.newAsync} instead, passing a `Context`. Will be removed in a
|
|
9
|
-
* future major version.
|
|
10
|
-
* @param settings A raw `C2paSettings` string/object.
|
|
11
|
-
*/
|
|
12
|
-
static new(settings?: C2paSettings): Builder;
|
|
13
7
|
/**
|
|
14
8
|
* Create a Builder with a minimal manifest definition, configured by a `Context`.
|
|
15
|
-
* Prefer this over the deprecated {@link Builder.new}.
|
|
16
9
|
*
|
|
17
10
|
* Resolves trust-anchor URLs in the Context before constructing the Builder.
|
|
18
11
|
*
|
|
@@ -21,14 +14,7 @@ export declare class Builder implements BuilderInterface {
|
|
|
21
14
|
*/
|
|
22
15
|
static newAsync(context?: Context): Promise<Builder>;
|
|
23
16
|
/**
|
|
24
|
-
*
|
|
25
|
-
* in a future version.
|
|
26
|
-
* @param settings A raw `C2paSettings` string/object.
|
|
27
|
-
*/
|
|
28
|
-
static withJson(json: Manifest, settings?: C2paSettings): Builder;
|
|
29
|
-
/**
|
|
30
|
-
* Create a Builder from a manifest definition, configured by a `Context`. Prefer this over
|
|
31
|
-
* the deprecated {@link Builder.withJson}.
|
|
17
|
+
* Create a Builder from a manifest definition, configured by a `Context`.
|
|
32
18
|
*
|
|
33
19
|
* Resolves trust-anchor URLs in the Context before constructing the Builder.
|
|
34
20
|
*
|
|
@@ -47,11 +33,9 @@ export declare class Builder implements BuilderInterface {
|
|
|
47
33
|
addIngredientFromReader(reader: ReaderInterface): Ingredient;
|
|
48
34
|
toArchive(asset: DestinationAsset): Promise<void>;
|
|
49
35
|
/**
|
|
50
|
-
* @param
|
|
51
|
-
* raw `C2paSettings` value is deprecated and will be removed in a future version. `null` is treated
|
|
52
|
-
* the same as `undefined`.
|
|
36
|
+
* @param context A `Context` configuring this Builder. Defaults to an empty `Context`.
|
|
53
37
|
*/
|
|
54
|
-
static fromArchive(asset: SourceAsset,
|
|
38
|
+
static fromArchive(asset: SourceAsset, context?: Context): Promise<Builder>;
|
|
55
39
|
sign(signer: LocalSignerInterface, input: SourceAsset, output: DestinationAsset): Buffer;
|
|
56
40
|
signFile(signer: LocalSignerInterface, filePath: string, output: DestinationAsset): Buffer;
|
|
57
41
|
signConfigAsync(callback: (data: Buffer) => Promise<Buffer>, signerConfig: JsCallbackSignerConfig, input: SourceAsset, output: DestinationAsset): Promise<Buffer>;
|
package/dist/types/Reader.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Manifest, ManifestStore } from "@contentauth/c2pa-types";
|
|
2
|
-
import
|
|
3
|
-
import type {
|
|
2
|
+
import { Context } from "@contentauth/c2pa-utilities";
|
|
3
|
+
import type { DestinationAsset, ReaderInterface, ResourceAsset, SourceAsset, NeonReaderHandle } from "./types.d.ts";
|
|
4
4
|
export declare class Reader implements ReaderInterface {
|
|
5
5
|
private reader;
|
|
6
6
|
constructor(reader: NeonReaderHandle);
|
|
@@ -9,17 +9,13 @@ export declare class Reader implements ReaderInterface {
|
|
|
9
9
|
isEmbedded(): boolean;
|
|
10
10
|
resourceToAsset(uri: string, asset: DestinationAsset): Promise<ResourceAsset>;
|
|
11
11
|
/**
|
|
12
|
-
* @param
|
|
13
|
-
* raw `C2paSettings` value is deprecated and will be removed in a future version. `null` is treated
|
|
14
|
-
* the same as `undefined`.
|
|
12
|
+
* @param context A `Context` configuring this Reader. Defaults to an empty `Context`.
|
|
15
13
|
*/
|
|
16
|
-
static fromAsset(asset: SourceAsset,
|
|
14
|
+
static fromAsset(asset: SourceAsset, context?: Context): Promise<Reader | null>;
|
|
17
15
|
/**
|
|
18
|
-
* @param
|
|
19
|
-
* raw `C2paSettings` value is deprecated and will be removed in a future version. `null` is treated
|
|
20
|
-
* the same as `undefined`.
|
|
16
|
+
* @param context A `Context` configuring this Reader. Defaults to an empty `Context`.
|
|
21
17
|
*/
|
|
22
|
-
static fromManifestDataAndAsset(manifestData: Buffer, asset: SourceAsset,
|
|
18
|
+
static fromManifestDataAndAsset(manifestData: Buffer, asset: SourceAsset, context?: Context): Promise<Reader>;
|
|
23
19
|
activeLabel(): string | undefined;
|
|
24
20
|
getActive(): Manifest | undefined;
|
|
25
21
|
getHandle(): NeonReaderHandle;
|
package/dist/types/Settings.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Context, type SdkConfig } from "@contentauth/c2pa-utilities";
|
|
2
|
-
import type { C2paSettings } from "./types.d.ts";
|
|
3
1
|
/**
|
|
4
2
|
* This file contains only Settings functions that are unique to the Node SDK.
|
|
5
3
|
*
|
|
@@ -12,22 +10,3 @@ import type { C2paSettings } from "./types.d.ts";
|
|
|
12
10
|
* @returns Settings as a string (TOML or JSON depending on file extension)
|
|
13
11
|
*/
|
|
14
12
|
export declare function loadSettingsFromFile(filePath: string): Promise<string>;
|
|
15
|
-
/**
|
|
16
|
-
* Reads the SDK config out of a settings argument.
|
|
17
|
-
*
|
|
18
|
-
* Only a `Context` carries an SDK config so the deprecated raw `C2paSettings` forms always
|
|
19
|
-
* resolve to `undefined`, leaving this package's defaults in place.
|
|
20
|
-
*
|
|
21
|
-
* @param settingsOrContext A `Context`, or (@deprecated) a raw `C2paSettings` string/object.
|
|
22
|
-
* @returns The `Context`'s SDK config, or `undefined` if there is none.
|
|
23
|
-
*/
|
|
24
|
-
export declare function resolveSdkConfig(settingsOrContext: C2paSettings | Context | null | undefined): SdkConfig | undefined;
|
|
25
|
-
/**
|
|
26
|
-
* Resolves Context settings, including any trust-anchor URLs, before passing them to the native
|
|
27
|
-
* library. Deprecated raw settings retain their existing pass-through behavior.
|
|
28
|
-
*/
|
|
29
|
-
export declare function resolveSettingsForNeon(settingsOrContext: C2paSettings | Context | null | undefined): Promise<string | undefined>;
|
|
30
|
-
/**
|
|
31
|
-
* Resolves deprecated raw settings for the synchronous Builder factories.
|
|
32
|
-
*/
|
|
33
|
-
export declare function resolveRawSettingsForNeon(settings: C2paSettings | null | undefined): string | undefined;
|