@contentauth/c2pa-node 0.5.4 → 0.6.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 +2 -1
- package/README.md +145 -7
- package/dist/Builder.js +63 -80
- package/dist/Builder.spec.js +251 -202
- package/dist/IdentityAssertion.js +8 -18
- package/dist/IdentityAssertion.spec.js +28 -35
- package/dist/Reader.js +13 -28
- package/dist/Reader.spec.js +53 -64
- package/dist/Settings.js +16 -29
- package/dist/Settings.spec.js +47 -62
- package/dist/Signer.js +4 -13
- package/dist/Signer.spec.js +35 -43
- package/dist/Trustmark.js +8 -22
- package/dist/Trustmark.spec.js +50 -65
- package/dist/binary.js +2 -1
- package/dist/index.node +0 -0
- package/dist/types/Builder.d.ts +2 -1
- package/dist/types/types.d.ts +11 -1
- package/package.json +54 -51
- package/scripts/postinstall.cjs +18 -1
- package/src/lib.rs +4 -0
- package/src/neon_builder.rs +30 -6
- package/src/neon_reader.rs +4 -2
- package/src/neon_trustmark.rs +74 -30
- package/src/utils.rs +8 -6
- package/Cargo.lock +0 -5050
- package/LICENSE +0 -21
package/Cargo.toml
CHANGED
|
@@ -3,6 +3,7 @@ name = "c2pa-node"
|
|
|
3
3
|
version = "0.1.0"
|
|
4
4
|
license = "MIT"
|
|
5
5
|
edition = "2021"
|
|
6
|
+
rust-version.workspace = true
|
|
6
7
|
exclude = ["index.node"]
|
|
7
8
|
|
|
8
9
|
[lib]
|
|
@@ -11,7 +12,7 @@ crate-type = ["cdylib"]
|
|
|
11
12
|
[dependencies]
|
|
12
13
|
async-trait = "0.1.77"
|
|
13
14
|
ciborium = "0.2.2"
|
|
14
|
-
c2pa = {
|
|
15
|
+
c2pa = { workspace = true, features = ["file_io", "add_thumbnails"] }
|
|
15
16
|
futures = "0.3"
|
|
16
17
|
image = "0.25.6"
|
|
17
18
|
neon = { version = "1.0.0", default-features = false, features = [
|
package/README.md
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
# C2PA Node.js library
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@contentauth/c2pa-node` is a Node.js library in the [c2pa-js](https://github.com/contentauth/c2pa-js) monorepo that can:
|
|
4
4
|
- Read and validate C2PA data from media files in supported formats.
|
|
5
5
|
- Add signed manifests to media files in supported formats.
|
|
6
6
|
|
|
7
|
-
**WARNING**: This is an early version of this library, and there may be bugs and unimplemented features.
|
|
8
|
-
|
|
9
7
|
## Prerequisites
|
|
10
8
|
|
|
11
|
-
To use the C2PA Node library
|
|
12
|
-
- A [supported version of Node](https://github.com/neon-bindings/neon#platform-support).
|
|
13
|
-
- [Rust](https://www.rust-lang.org/tools/install).
|
|
9
|
+
To use the C2PA Node library you need Node.js **>=22**. If you need to manage multiple versions of Node on your machine, use a tool such as [nvm](https://github.com/nvm-sh/nvm).
|
|
14
10
|
|
|
15
|
-
|
|
11
|
+
To **build from source** you also need [Rust](https://www.rust-lang.org/tools/install). This is not required when installing the package normally — precompiled binaries are downloaded automatically during installation.
|
|
16
12
|
|
|
17
13
|
## Installation
|
|
18
14
|
|
|
@@ -45,6 +41,18 @@ This command will download precompiled binaries for the following systems:
|
|
|
45
41
|
- Windows x86
|
|
46
42
|
- Windows ARM
|
|
47
43
|
|
|
44
|
+
## Documentation
|
|
45
|
+
|
|
46
|
+
Complete API documentation is generated from TypeScript source using [TypeDoc](https://typedoc.org/) and published to GitHub Pages at [https://contentauth.github.io/c2pa-js/](https://contentauth.github.io/c2pa-js/).
|
|
47
|
+
|
|
48
|
+
To generate documentation locally, run the following from the monorepo root:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pnpm ci:docs
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
This generates HTML documentation in the `docs/` directory at the repo root. It is not checked into version control and is automatically published to GitHub Pages whenever a new release is published.
|
|
55
|
+
|
|
48
56
|
## Components
|
|
49
57
|
|
|
50
58
|
### Reader
|
|
@@ -241,6 +249,125 @@ const ingredient = builder.addIngredientFromReader(sourceReader);
|
|
|
241
249
|
console.log(ingredient.title); // Contains ingredient metadata
|
|
242
250
|
```
|
|
243
251
|
|
|
252
|
+
#### Adding Ingredients from Archives (.c2pa files)
|
|
253
|
+
|
|
254
|
+
You can add ingredients from `.c2pa` archive files. Archives are binary files that contain a manifest store with ingredients and their associated resources (thumbnails, manifest data, etc.). To work with them, read the archive with `Reader` using the `application/c2pa` MIME type, then extract the ingredients and transfer their binary resources to a new `Builder`.
|
|
255
|
+
|
|
256
|
+
There are two types of archives sharing the same binary format:
|
|
257
|
+
|
|
258
|
+
- **Builder archives** (working store archives): Serialized snapshots of a `Builder`, created by `builder.toArchive()`. They can contain multiple ingredients.
|
|
259
|
+
- **Ingredient archives**: Contain exactly one ingredient from a source asset. They carry the provenance history for reuse in other manifests.
|
|
260
|
+
|
|
261
|
+
##### Reading an archive and adding its ingredients
|
|
262
|
+
|
|
263
|
+
```javascript
|
|
264
|
+
import { Reader, Builder } from '@contentauth/c2pa-node';
|
|
265
|
+
import * as fs from 'node:fs/promises';
|
|
266
|
+
|
|
267
|
+
// Read the archive using the application/c2pa MIME type
|
|
268
|
+
const archiveBuffer = await fs.readFile('ingredients.c2pa');
|
|
269
|
+
const reader = await Reader.fromAsset(
|
|
270
|
+
{ buffer: archiveBuffer, mimeType: 'application/c2pa' },
|
|
271
|
+
{ verify: { verify_after_reading: false } }
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
// Get the ingredients from the active manifest
|
|
275
|
+
const activeManifest = reader.getActive();
|
|
276
|
+
const ingredients = activeManifest.ingredients;
|
|
277
|
+
|
|
278
|
+
// Create a new builder with the ingredients from the archive
|
|
279
|
+
const builder = Builder.withJson({
|
|
280
|
+
claim_generator_info: [{ name: 'my-app', version: '1.0.0' }],
|
|
281
|
+
ingredients: ingredients,
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// Transfer binary resources (thumbnails, manifest_data) for each ingredient
|
|
285
|
+
for (const ingredient of ingredients) {
|
|
286
|
+
if (ingredient.thumbnail) {
|
|
287
|
+
const resource = await reader.resourceToAsset(ingredient.thumbnail.identifier, { buffer: null });
|
|
288
|
+
await builder.addResource(ingredient.thumbnail.identifier, {
|
|
289
|
+
buffer: resource.buffer,
|
|
290
|
+
mimeType: ingredient.thumbnail.format,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
if (ingredient.manifest_data) {
|
|
294
|
+
const resource = await reader.resourceToAsset(ingredient.manifest_data.identifier, { buffer: null });
|
|
295
|
+
await builder.addResource(ingredient.manifest_data.identifier, {
|
|
296
|
+
buffer: resource.buffer,
|
|
297
|
+
mimeType: 'application/c2pa',
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Sign the manifest
|
|
303
|
+
const signer = LocalSigner.newSigner(cert, key, 'es256');
|
|
304
|
+
builder.sign(signer, inputAsset, outputAsset);
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
##### Selecting specific ingredients from an archive
|
|
308
|
+
|
|
309
|
+
When an archive contains multiple ingredients, you can filter to include only the ones you need:
|
|
310
|
+
|
|
311
|
+
```javascript
|
|
312
|
+
const reader = await Reader.fromAsset(
|
|
313
|
+
{ buffer: archiveBuffer, mimeType: 'application/c2pa' },
|
|
314
|
+
{ verify: { verify_after_reading: false } }
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
const activeManifest = reader.getActive();
|
|
318
|
+
const allIngredients = activeManifest.ingredients;
|
|
319
|
+
|
|
320
|
+
// Select only the ingredients you want (e.g., by title or instance_id)
|
|
321
|
+
const selected = allIngredients.filter(
|
|
322
|
+
(ing) => ing.title === 'photo_1.jpg' || ing.instance_id === 'catalog:logo'
|
|
323
|
+
);
|
|
324
|
+
|
|
325
|
+
// Build with only the selected ingredients
|
|
326
|
+
const builder = Builder.withJson({
|
|
327
|
+
claim_generator_info: [{ name: 'my-app', version: '1.0.0' }],
|
|
328
|
+
ingredients: selected,
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
// Transfer resources only for selected ingredients
|
|
332
|
+
for (const ingredient of selected) {
|
|
333
|
+
if (ingredient.thumbnail) {
|
|
334
|
+
const resource = await reader.resourceToAsset(ingredient.thumbnail.identifier, { buffer: null });
|
|
335
|
+
await builder.addResource(ingredient.thumbnail.identifier, {
|
|
336
|
+
buffer: resource.buffer,
|
|
337
|
+
mimeType: ingredient.thumbnail.format,
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
if (ingredient.manifest_data) {
|
|
341
|
+
const resource = await reader.resourceToAsset(ingredient.manifest_data.identifier, { buffer: null });
|
|
342
|
+
await builder.addResource(ingredient.manifest_data.identifier, {
|
|
343
|
+
buffer: resource.buffer,
|
|
344
|
+
mimeType: 'application/c2pa',
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
##### Building an ingredient archive
|
|
351
|
+
|
|
352
|
+
To create an ingredient archive, add ingredients to a `Builder` and save it as an archive:
|
|
353
|
+
|
|
354
|
+
```javascript
|
|
355
|
+
const builder = Builder.new();
|
|
356
|
+
|
|
357
|
+
// Add ingredients with stable instance_id for later catalog lookups
|
|
358
|
+
await builder.addIngredient(
|
|
359
|
+
JSON.stringify({
|
|
360
|
+
title: 'photo-A.jpg',
|
|
361
|
+
relationship: 'componentOf',
|
|
362
|
+
instance_id: 'catalog:photo-A',
|
|
363
|
+
}),
|
|
364
|
+
{ path: 'photo-A.jpg' }
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
// Save as a .c2pa archive
|
|
368
|
+
await builder.toArchive({ path: 'ingredient-catalog.c2pa' });
|
|
369
|
+
```
|
|
370
|
+
|
|
244
371
|
#### Creating and Reusing Builder Archives
|
|
245
372
|
|
|
246
373
|
Builder archives allow you to save a builder's state (including ingredients) and reuse it later:
|
|
@@ -506,3 +633,14 @@ const builder = Builder.new(urlSettings);
|
|
|
506
633
|
- `generate_c2pa_archive` - Whether to generate C2PA archive format
|
|
507
634
|
|
|
508
635
|
**Note:** Settings are passed as per-instance configuration. There are no global settings that affect all Readers and Builders.
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
### Build and use custom binary
|
|
639
|
+
**Build Rust binary:**
|
|
640
|
+
```bash
|
|
641
|
+
pnpm run build:rust
|
|
642
|
+
```
|
|
643
|
+
**Use custom Rust binary:**
|
|
644
|
+
```bash
|
|
645
|
+
export C2PA_LIBRARY_PATH=<path-to-binary-folder>/index.node
|
|
646
|
+
```
|
package/dist/Builder.js
CHANGED
|
@@ -3,18 +3,10 @@
|
|
|
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
|
-
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
6
|
import { getNeonBinary } from "./binary.js";
|
|
16
7
|
import { IdentityAssertionSigner } from "./IdentityAssertion.js";
|
|
17
8
|
export class Builder {
|
|
9
|
+
builder;
|
|
18
10
|
constructor(builder) {
|
|
19
11
|
this.builder = builder;
|
|
20
12
|
}
|
|
@@ -63,40 +55,32 @@ export class Builder {
|
|
|
63
55
|
addAssertion(label, assertion, assertionKind) {
|
|
64
56
|
return getNeonBinary().builderAddAssertion.call(this.builder, label, assertion, assertionKind);
|
|
65
57
|
}
|
|
66
|
-
addResource(uri, resource) {
|
|
67
|
-
return
|
|
68
|
-
return getNeonBinary().builderAddResource.call(this.builder, uri, resource);
|
|
69
|
-
});
|
|
58
|
+
async addResource(uri, resource) {
|
|
59
|
+
return getNeonBinary().builderAddResource.call(this.builder, uri, resource);
|
|
70
60
|
}
|
|
71
|
-
addIngredient(ingredientJson, ingredient) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
});
|
|
61
|
+
async addIngredient(ingredientJson, ingredient) {
|
|
62
|
+
if (ingredient) {
|
|
63
|
+
return getNeonBinary().builderAddIngredientFromAsset.call(this.builder, ingredientJson, ingredient);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return getNeonBinary().builderAddIngredient.call(this.builder, ingredientJson);
|
|
67
|
+
}
|
|
80
68
|
}
|
|
81
69
|
addIngredientFromReader(reader) {
|
|
82
70
|
const readerHandle = reader.getHandle();
|
|
83
71
|
const result = getNeonBinary().builderAddIngredientFromReader.call(this.builder, readerHandle);
|
|
84
72
|
return JSON.parse(result);
|
|
85
73
|
}
|
|
86
|
-
toArchive(asset) {
|
|
87
|
-
return
|
|
88
|
-
return getNeonBinary().builderToArchive.call(this.builder, asset);
|
|
89
|
-
});
|
|
74
|
+
async toArchive(asset) {
|
|
75
|
+
return getNeonBinary().builderToArchive.call(this.builder, asset);
|
|
90
76
|
}
|
|
91
|
-
static fromArchive(asset, settings) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
?
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return new Builder(yield getNeonBinary().builderFromArchive(asset, settingsStr));
|
|
99
|
-
});
|
|
77
|
+
static async fromArchive(asset, settings) {
|
|
78
|
+
const settingsStr = settings
|
|
79
|
+
? typeof settings === "string"
|
|
80
|
+
? settings
|
|
81
|
+
: JSON.stringify(settings)
|
|
82
|
+
: undefined;
|
|
83
|
+
return new Builder(await getNeonBinary().builderFromArchive(asset, settingsStr));
|
|
100
84
|
}
|
|
101
85
|
sign(signer, input, output) {
|
|
102
86
|
return getNeonBinary().builderSign.call(this.builder, signer.getHandle(), input, output);
|
|
@@ -105,59 +89,55 @@ export class Builder {
|
|
|
105
89
|
const input = { path: filePath };
|
|
106
90
|
return getNeonBinary().builderSign.call(this.builder, signer.getHandle(), input, output);
|
|
107
91
|
}
|
|
108
|
-
signConfigAsync(callback, signerConfig, input, output) {
|
|
109
|
-
return
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if ("
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
return result.manifest;
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
throw new Error("Unexpected result for DestinationBuffer");
|
|
121
|
-
}
|
|
92
|
+
async signConfigAsync(callback, signerConfig, input, output) {
|
|
93
|
+
return getNeonBinary()
|
|
94
|
+
.builderSignConfigAsync.call(this.builder, callback, signerConfig, input, output)
|
|
95
|
+
.then((result) => {
|
|
96
|
+
// output is a buffer and result is the manifest and the signed asset.
|
|
97
|
+
if ("buffer" in output) {
|
|
98
|
+
if ("signedAsset" in result && "manifest" in result) {
|
|
99
|
+
output.buffer = result.signedAsset;
|
|
100
|
+
return result.manifest;
|
|
122
101
|
}
|
|
123
102
|
else {
|
|
124
|
-
|
|
125
|
-
return result;
|
|
103
|
+
throw new Error("Unexpected result for DestinationBuffer");
|
|
126
104
|
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
// output is a file and result is the bytes of the manifest.
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
.catch((error) => {
|
|
112
|
+
throw error;
|
|
131
113
|
});
|
|
132
114
|
}
|
|
133
|
-
signAsync(signer, input, output) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if ("
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
return result.manifest;
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
throw new Error("Unexpected result for DestinationBuffer");
|
|
151
|
-
}
|
|
115
|
+
async signAsync(signer, input, output) {
|
|
116
|
+
const neonHandle = signer.getHandle();
|
|
117
|
+
const isIdentity = signer instanceof IdentityAssertionSigner;
|
|
118
|
+
const neonFn = isIdentity
|
|
119
|
+
? getNeonBinary().builderIdentitySignAsync
|
|
120
|
+
: getNeonBinary().builderSignAsync;
|
|
121
|
+
return neonFn
|
|
122
|
+
.call(this.builder, neonHandle, input, output)
|
|
123
|
+
.then((result) => {
|
|
124
|
+
// output is a buffer and result is the manifest and the signed asset.
|
|
125
|
+
if ("buffer" in output) {
|
|
126
|
+
if ("signedAsset" in result && "manifest" in result) {
|
|
127
|
+
output.buffer = result.signedAsset;
|
|
128
|
+
return result.manifest;
|
|
152
129
|
}
|
|
153
130
|
else {
|
|
154
|
-
|
|
155
|
-
return result;
|
|
131
|
+
throw new Error("Unexpected result for DestinationBuffer");
|
|
156
132
|
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
// output is a file and result is the bytes of the manifest.
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
.catch((error) => {
|
|
140
|
+
throw error;
|
|
161
141
|
});
|
|
162
142
|
}
|
|
163
143
|
getManifestDefinition() {
|
|
@@ -166,6 +146,9 @@ export class Builder {
|
|
|
166
146
|
updateManifestProperty(property, value) {
|
|
167
147
|
getNeonBinary().builderUpdateManifestProperty.call(this.builder, property, value);
|
|
168
148
|
}
|
|
149
|
+
addRedaction(uri, reason) {
|
|
150
|
+
getNeonBinary().builderAddRedaction.call(this.builder, uri, reason);
|
|
151
|
+
}
|
|
169
152
|
getHandle() {
|
|
170
153
|
return this.builder;
|
|
171
154
|
}
|