@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.
package/README.md CHANGED
@@ -1,23 +1,10 @@
1
1
  # C2PA Node.js library
2
2
 
3
- The [c2pa-node](https://github.com/contentauth/c2pa-node-v2) repository implements a Node.js API that can:
3
+ The [c2pa-node-v2](https://github.com/contentauth/c2pa-node-v2) repository implements a Node.js API 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
- For more information on using the library in an application, see [Using the CAI Node library](docs/usage.md).
8
-
9
- **WARNING**: This is an early prerelease version of this library. There may be bugs and unimplemented features, and the API is subject to change.
10
-
11
- <div style={{display: 'none'}}>
12
-
13
- For the best experience, read the docs on the [CAI Open Source SDK documentation website](https://opensource.contentauthenticity.org/docs/c2pa-node/). Some additional documentation for this repository is also available on GitHub:
14
-
15
- - [Using the Node library](docs/usage.md)
16
- - [Supported media file formats](https://opensource.contentauthenticity.org/docs/c2pa-node/supported-formats)
17
- - [Release notes](docs/release-notes.md)
18
- - [Contributing to the project](docs/project-contributions.md)
19
-
20
- </div>
7
+ **WARNING**: This is an early version of this library, and there may be bugs and unimplemented features.
21
8
 
22
9
  ## Prerequisites
23
10
 
@@ -29,24 +16,24 @@ If you need to manage multiple versions of Node on your machine, use a tool such
29
16
 
30
17
  ## Installation
31
18
 
32
- ### Installing for use in a client app
19
+ ### Installing for use in an app
33
20
 
34
21
  Using npm:
35
22
 
36
23
  ```sh
37
- $ npm install c2pa-node
24
+ $ npm install @contentauth/c2pa-node
38
25
  ```
39
26
 
40
27
  Using Yarn:
41
28
 
42
29
  ```sh
43
- $ yarn add c2pa-node
30
+ $ yarn add @contentauth/c2pa-node
44
31
  ```
45
32
 
46
33
  Using pnpm:
47
34
 
48
35
  ```sh
49
- $ pnpm add c2pa-node
36
+ $ pnpm add @contentauth/c2pa-node
50
37
  ```
51
38
 
52
39
  This command will download precompiled binaries for the following systems:
@@ -58,30 +45,209 @@ This command will download precompiled binaries for the following systems:
58
45
  - Windows x86
59
46
  - Windows ARM
60
47
 
61
- All other platforms require building a custom binary as described below, since the `postinstall` step builds the Rust library into a native Node.js module on your machine.
62
-
63
- ### Building custom binaries
64
-
65
- For a platform or architecture that does not have a precompiled binary, you must pre-build a custom binary by following these steps:
66
-
67
- 1. [Install the Rust toolchain](https://www.rust-lang.org/tools/install).
68
- 1. Run the following commands on the target system or VM:
69
- ```sh
70
- cd c2pa-node
71
- pnpm install
72
- pnpm build:rust
73
- ```
74
- 1. Copy the binary to a place that is accessible by your application (in this example, it is `/path/to/my/application/resources`):
75
- ```sh
76
- cd /path/to/my/application
77
- mkdir resources
78
- cp /path/to/c2pa-node/generated/c2pa.node resources/c2pa.node
79
- ```
80
- 1. Set the the `C2PA_LIBRARY_PATH` environment variable to the path to the `c2pa.node` module by entering these commands:
81
- ```sh
82
- export C2PA_LIBRARY_PATH=resources/c2pa.node
83
- npm install c2pa-node
84
- npm start
85
- ```
86
-
87
- **Important:** `C2PA_LIBRARY_PATH` _must_ be set while both **installing** or **adding** `c2pa-node` to your app to avoid building the Rust code. It must _also_ be set while **running** your app so that it loads the bindings from the correct location.
48
+ ## Components
49
+
50
+ ### Reader
51
+
52
+ The `Reader` class is used to read and validate C2PA manifests from media files. It can parse embedded manifests or fetch remote manifests.
53
+
54
+ ```javascript
55
+ import { Reader } from '@contentauth/c2pa-node';
56
+
57
+ // Read from an asset file
58
+ const reader = await Reader.fromAsset(inputAsset);
59
+
60
+ // Read from manifest data and asset
61
+ const reader = await Reader.fromManifestDataAndAsset(manifestData, asset);
62
+
63
+ // Get the manifest store as JSON
64
+ const manifestStore = reader.json();
65
+
66
+ // Get the active manifest
67
+ const activeManifest = reader.getActive();
68
+
69
+ // Check if manifest is embedded
70
+ const isEmbedded = reader.isEmbedded();
71
+
72
+ // Get remote URL if applicable
73
+ const remoteUrl = reader.remoteUrl();
74
+ ```
75
+
76
+ ### Builder
77
+
78
+ The `Builder` class is the main component for creating and signing C2PA manifests. It provides methods to add assertions, resources, and ingredients to manifests, and handles the signing process. Use the `Signer` class to sign the manifests.
79
+
80
+ ```javascript
81
+ import { Builder } from '@contentauth/c2pa-node';
82
+
83
+ // Create a new builder
84
+ const builder = Builder.new();
85
+
86
+ // Or create from an existing manifest definition
87
+ const builder = Builder.withJson(manifestDefinition);
88
+
89
+ // Add assertions to the manifest
90
+ builder.addAssertion('c2pa.actions', actionsAssertion);
91
+
92
+ // Add resources
93
+ await builder.addResource('resource://example', resourceAsset);
94
+
95
+ // Sign the manifest
96
+ const manifest = builder.sign(signer, inputAsset, outputAsset);
97
+ ```
98
+
99
+ ### Signers
100
+
101
+ The library provides several types of signers for different use cases:
102
+
103
+ #### LocalSigner
104
+
105
+ For local signing with certificates and private keys:
106
+
107
+ ```javascript
108
+ import { LocalSigner } from '@contentauth/c2pa-node';
109
+
110
+ // Create a local signer with certificate and private key
111
+ const signer = LocalSigner.newSigner(
112
+ certificateBuffer,
113
+ privateKeyBuffer,
114
+ 'es256', // signing algorithm
115
+ 'https://timestamp.example.com' // optional TSA URL
116
+ );
117
+
118
+ // Sign data
119
+ const signature = signer.sign(dataBuffer);
120
+ ```
121
+
122
+ #### CallbackSigner
123
+
124
+ For custom signing implementations using callbacks:
125
+
126
+ ```javascript
127
+ import { CallbackSigner } from '@contentauth/c2pa-node';
128
+
129
+ // Create a callback signer
130
+ const signer = CallbackSigner.newSigner(
131
+ {
132
+ alg: 'es256',
133
+ certs: [certificateBuffer],
134
+ reserveSize: 1024, // Reserved size in bytes for the C2PA Claim Signature box.
135
+ tsaUrl: 'https://timestamp.example.com'
136
+ },
137
+ async (data) => {
138
+ // Custom signing logic
139
+ return await customSigningFunction(data);
140
+ }
141
+ );
142
+ ```
143
+
144
+ ### Identity Assertion Components
145
+
146
+ For working with identity assertions and CAWG (Content Authenticity Working Group) identities:
147
+
148
+ #### IdentityAssertionBuilder
149
+
150
+ Builds identity assertions with roles and referenced assertions:
151
+
152
+ ```javascript
153
+ import { IdentityAssertionBuilder, CallbackCredentialHolder } from '@contentauth/c2pa-node';
154
+
155
+ // Create a credential holder
156
+ const credentialHolder = CallbackCredentialHolder.newCallbackCredentialHolder(
157
+ 1024, // reserveSize
158
+ 'es256', // sigType
159
+ async (payload) => {
160
+ // Custom signing logic for identity assertions
161
+ return await signIdentityPayload(payload);
162
+ }
163
+ );
164
+
165
+ // Create an identity assertion builder
166
+ const identityBuilder = await IdentityAssertionBuilder.identityBuilderForCredentialHolder(
167
+ credentialHolder
168
+ );
169
+
170
+ // Add roles and referenced assertions
171
+ identityBuilder.addRoles(['photographer', 'editor']);
172
+ identityBuilder.addReferencedAssertions(['c2pa.actions']);
173
+ ```
174
+
175
+ #### IdentityAssertionSigner
176
+
177
+ Signs manifests with identity assertions:
178
+
179
+ ```javascript
180
+ import { IdentityAssertionSigner } from '@contentauth/c2pa-node';
181
+
182
+ // Create an identity assertion signer
183
+ const identitySigner = IdentityAssertionSigner.new(callbackSigner);
184
+
185
+ // Add identity assertion
186
+ identitySigner.addIdentityAssertion(identityBuilder);
187
+
188
+ // Use with Builder for signing
189
+ const manifest = await builder.signAsync(identitySigner, inputAsset, outputAsset);
190
+ ```
191
+
192
+ ### Trustmark
193
+
194
+ The `Trustmark` class provides functionality for encoding and decoding trustmarks in images:
195
+
196
+ ```javascript
197
+ import { Trustmark } from '@contentauth/c2pa-node';
198
+
199
+ // Create a trustmark instance
200
+ const trustmark = await Trustmark.newTrustmark({
201
+ // trustmark configuration
202
+ });
203
+
204
+ // Encode a trustmark into an image
205
+ const encodedImage = await trustmark.encode(
206
+ imageBuffer,
207
+ 0.5, // strength
208
+ 'watermark-text' // optional watermark
209
+ );
210
+
211
+ // Decode a trustmark from an image
212
+ const decodedData = await trustmark.decode(imageBuffer);
213
+ ```
214
+
215
+ ### Settings and Configuration
216
+
217
+ The library provides comprehensive settings management for trust configuration, verification settings, and global C2PA settings:
218
+
219
+ ```javascript
220
+ import {
221
+ loadC2paSettings,
222
+ loadTrustConfig,
223
+ loadVerifyConfig,
224
+ loadSettingsFromFile,
225
+ loadSettingsFromUrl
226
+ } from '@contentauth/c2pa-node';
227
+
228
+ // Load settings from JSON string
229
+ loadC2paSettings('{"trust": {"verify_trust_list": true}}');
230
+
231
+ // Load settings from file
232
+ await loadSettingsFromFile('./c2pa-settings.json');
233
+
234
+ // Load settings from URL
235
+ await loadSettingsFromUrl('https://example.com/c2pa-settings.json');
236
+
237
+ // Configure trust settings
238
+ loadTrustConfig({
239
+ verifyTrustList: true,
240
+ userAnchors: ['anchor1', 'anchor2'],
241
+ trustAnchors: ['trust-anchor1'],
242
+ allowedList: ['allowed-cert1']
243
+ });
244
+
245
+ // Configure verification settings
246
+ loadVerifyConfig({
247
+ verifyAfterReading: true,
248
+ verifyAfterSign: true,
249
+ verifyTrust: true,
250
+ ocspFetch: true,
251
+ remoteManifestFetch: true
252
+ });
253
+ ```
package/dist/Builder.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,15 @@ 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.Builder = 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 Builder {
21
+ const neon = require("./index.node");
22
+ import { IdentityAssertionSigner } from "./IdentityAssertion";
23
+ export class Builder {
59
24
  constructor(builder) {
60
25
  this.builder = builder;
61
26
  }
@@ -141,33 +106,13 @@ class Builder {
141
106
  }
142
107
  signAsync(signer, input, output) {
143
108
  return __awaiter(this, void 0, void 0, function* () {
144
- return neon.builderSignAsync
145
- .call(this.builder, signer.signer(), input, output)
146
- .then((result) => {
147
- // output is a buffer and result is the manifest and the signed asset.
148
- if ("buffer" in output) {
149
- if ("signedAsset" in result && "manifest" in result) {
150
- output.buffer = result.signedAsset;
151
- return result.manifest;
152
- }
153
- else {
154
- throw new Error("Unexpected result for DestinationBuffer");
155
- }
156
- }
157
- else {
158
- // output is a file and result is the bytes of the manifest.
159
- return result;
160
- }
161
- })
162
- .catch((error) => {
163
- throw error;
164
- });
165
- });
166
- }
167
- identitySignAsync(signer, input, output) {
168
- return __awaiter(this, void 0, void 0, function* () {
169
- return neon.builderIdentitySignAsync
170
- .call(this.builder, signer.signer(), input, output)
109
+ const neonHandle = signer.signer();
110
+ const isIdentity = signer instanceof IdentityAssertionSigner;
111
+ const neonFn = isIdentity
112
+ ? neon.builderIdentitySignAsync
113
+ : neon.builderSignAsync;
114
+ return neonFn
115
+ .call(this.builder, neonHandle, input, output)
171
116
  .then((result) => {
172
117
  // output is a buffer and result is the manifest and the signed asset.
173
118
  if ("buffer" in output) {
@@ -196,4 +141,3 @@ class Builder {
196
141
  neon.builderUpdateManifestProperty.call(this.builder, property, value);
197
142
  }
198
143
  }
199
- exports.Builder = Builder;