@contentauth/c2pa-node 0.9.12 → 0.10.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.
@@ -10,6 +10,26 @@
10
10
  // specific language governing permissions and limitations under
11
11
  // each license.
12
12
  import { getNeonBinary } from "./binary.js";
13
+ /**
14
+ * Encode a CAWG SignerPayload using c2pa_cbor. Hashes become CBOR byte strings;
15
+ * the wire keys are referenced_assertions, sig_type, and (when nonempty) role.
16
+ *
17
+ * Input names are case-sensitive: referencedAssertions, sigType, roles, and each
18
+ * reference's url, hash, and alg. Wire-format names are not aliases. Missing or
19
+ * incorrectly cased required fields are rejected; unknown data properties are
20
+ * ignored, including incorrectly cased optional fields. Omitted, undefined, or
21
+ * empty roles are omitted on the wire; null roles are rejected.
22
+ *
23
+ * Use plain data objects with Uint8Array hashes for portability between bindings
24
+ * (Node Buffer hashes are also supported here). Validation failures throw
25
+ * synchronously. The Web counterpart rejects its Promise and additionally
26
+ * requires the entire payload, including unknown properties, to be
27
+ * structured-cloneable: an unknown function property is ignored here but cannot
28
+ * be sent to the Web worker. Error types and messages are binding-specific.
29
+ */
30
+ export function encodeSignerPayload(payload) {
31
+ return getNeonBinary().encodeSignerPayload(payload);
32
+ }
13
33
  export class IdentityAssertionBuilder {
14
34
  _builder;
15
35
  constructor(_builder) {
@@ -6,7 +6,7 @@
6
6
  import fs from "fs-extra";
7
7
  import * as crypto from "crypto";
8
8
  import { spawnSync } from "child_process";
9
- import { encode } from "cbor2";
9
+ import { encodeSignerPayload } from "./index.js";
10
10
  class TestSigner {
11
11
  privateKey;
12
12
  constructor(privateKey) {
@@ -28,7 +28,7 @@ class TestCawgSigner {
28
28
  this.manifestSigner = manifestSigner;
29
29
  }
30
30
  sign = async (payload) => {
31
- const cborBytes = Buffer.from(encode(payload));
31
+ const cborBytes = Buffer.from(encodeSignerPayload(payload));
32
32
  return this.manifestSigner.sign(cborBytes);
33
33
  };
34
34
  }
@@ -0,0 +1,180 @@
1
+ // Copyright 2026 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
+ // Unless required by applicable law or agreed to in writing,
7
+ // this software is distributed on an "AS IS" BASIS, WITHOUT
8
+ // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
9
+ // implied. See the LICENSE-MIT and LICENSE-APACHE files for the
10
+ // specific language governing permissions and limitations under
11
+ // each license.
12
+ import { encodeSignerPayload } from "./index.js";
13
+ const toHex = (bytes) => Buffer.from(bytes).toString("hex");
14
+ describe("encodeSignerPayload", () => {
15
+ it("encodes roles, alg, and non-ASCII URLs to the golden CBOR bytes", () => {
16
+ const hash = Uint8Array.of(1, 2, 3);
17
+ const payload = {
18
+ referencedAssertions: [
19
+ {
20
+ url: "self#jumbf=/c2pa/assertions/c2pa.actions",
21
+ hash,
22
+ alg: "sha256",
23
+ },
24
+ { url: "second/日本語", hash: Uint8Array.of(0, 127, 128, 255) },
25
+ ],
26
+ sigType: "cawg.x509.cose",
27
+ roles: ["cawg.creator", "cawg.publisher"],
28
+ };
29
+ const bytes = encodeSignerPayload(payload);
30
+ expect(bytes).toBeInstanceOf(Uint8Array);
31
+ expect(toHex(bytes)).toBe("a3757265666572656e6365645f617373657274696f6e7382a36375726c782873656c66236a756d62663d2f633270612f617373657274696f6e732f633270612e616374696f6e7363616c6766736861323536646861736843010203a26375726c707365636f6e642fe697a5e69cace8aa9e646861736844007f80ff687369675f747970656e636177672e783530392e636f736564726f6c65826c636177672e63726561746f726e636177672e7075626c6973686572");
32
+ expect(payload.referencedAssertions[0].hash).toEqual(hash);
33
+ });
34
+ it.each([undefined, []])("omits an absent or empty role (%j) and encodes empty assertions", (roles) => {
35
+ const bytes = encodeSignerPayload({
36
+ referencedAssertions: [],
37
+ sigType: "cawg.x509.cose",
38
+ roles,
39
+ });
40
+ expect(toHex(bytes)).toBe("a2757265666572656e6365645f617373657274696f6e7380687369675f747970656e636177672e783530392e636f7365");
41
+ });
42
+ it("accepts explicit undefined roles and rejects null roles", () => {
43
+ const base = { referencedAssertions: [], sigType: "test" };
44
+ expect(toHex(encodeSignerPayload({ ...base, roles: undefined }))).toBe(toHex(encodeSignerPayload(base)));
45
+ expect(() => encodeSignerPayload({ ...base, roles: null })).toThrow();
46
+ });
47
+ it.each([
48
+ [0, "40"],
49
+ [1, "41"],
50
+ [23, "57"],
51
+ [24, "5818"],
52
+ [255, "58ff"],
53
+ [256, "590100"],
54
+ [65535, "59ffff"],
55
+ [65536, "5a00010000"],
56
+ ])("encodes a %i-byte hash with the correct header and respects typed array offsets", (length, header) => {
57
+ const backing = new Uint8Array(length + 4).fill(255);
58
+ const hash = backing.subarray(2, length + 2);
59
+ hash.fill(128);
60
+ const bytes = encodeSignerPayload({
61
+ referencedAssertions: [{ url: "a", hash }],
62
+ sigType: "test",
63
+ });
64
+ expect(toHex(bytes)).toBe("a2757265666572656e6365645f617373657274696f6e7381a26375726c61616468617368" +
65
+ header +
66
+ "80".repeat(length) +
67
+ "687369675f747970656474657374");
68
+ expect(backing[0]).toBe(255);
69
+ });
70
+ it("accepts a Node Buffer hash", () => {
71
+ const payload = {
72
+ referencedAssertions: [{ url: "a", hash: Buffer.from([1, 2]) }],
73
+ sigType: "test",
74
+ };
75
+ expect(encodeSignerPayload(payload)).toEqual(encodeSignerPayload({
76
+ ...payload,
77
+ referencedAssertions: [{ url: "a", hash: Uint8Array.of(1, 2) }],
78
+ }));
79
+ });
80
+ it("rejects malformed payloads instead of encoding a partial value", () => {
81
+ expect(() => encodeSignerPayload({ sigType: "test" })).toThrow();
82
+ expect(() => encodeSignerPayload({ referencedAssertions: [] })).toThrow();
83
+ expect(() => encodeSignerPayload({
84
+ referencedAssertions: [{ url: "a", hash: "not bytes" }],
85
+ sigType: "test",
86
+ })).toThrow();
87
+ });
88
+ describe("input contract", () => {
89
+ // Keep these cases aligned with c2pa-web's signer.spec.ts. The JS-to-Rust
90
+ // adapters differ even though both bindings use the same Rust serializer.
91
+ const hash = Uint8Array.of(1, 2);
92
+ const uri = { url: "a", hash };
93
+ const payload = { referencedAssertions: [uri], sigType: "test" };
94
+ const rejected = [
95
+ ["an undefined payload", undefined],
96
+ ["a null payload", null],
97
+ ["missing referencedAssertions", { sigType: "test" }],
98
+ ["missing sigType", { referencedAssertions: [uri] }],
99
+ [
100
+ "snake_case referenced_assertions",
101
+ { referenced_assertions: [uri], sigType: "test" },
102
+ ],
103
+ [
104
+ "snake_case sig_type",
105
+ { referencedAssertions: [uri], sig_type: "test" },
106
+ ],
107
+ [
108
+ "case-mismatched SigType",
109
+ { referencedAssertions: [uri], SigType: "test" },
110
+ ],
111
+ [
112
+ "non-array referencedAssertions",
113
+ { ...payload, referencedAssertions: {} },
114
+ ],
115
+ ["a non-string sigType", { ...payload, sigType: 42 }],
116
+ ["a null assertion", { ...payload, referencedAssertions: [null] }],
117
+ ["a missing url", { ...payload, referencedAssertions: [{ hash }] }],
118
+ [
119
+ "case-mismatched Url",
120
+ { ...payload, referencedAssertions: [{ Url: "a", hash }] },
121
+ ],
122
+ [
123
+ "a non-string url",
124
+ { ...payload, referencedAssertions: [{ ...uri, url: 42 }] },
125
+ ],
126
+ ["a missing hash", { ...payload, referencedAssertions: [{ url: "a" }] }],
127
+ [
128
+ "case-mismatched Hash",
129
+ { ...payload, referencedAssertions: [{ url: "a", Hash: hash }] },
130
+ ],
131
+ [
132
+ "a string hash",
133
+ { ...payload, referencedAssertions: [{ ...uri, hash: "not bytes" }] },
134
+ ],
135
+ [
136
+ "a non-string alg",
137
+ { ...payload, referencedAssertions: [{ ...uri, alg: 42 }] },
138
+ ],
139
+ ["null roles", { ...payload, roles: null }],
140
+ ["non-array roles", { ...payload, roles: "creator" }],
141
+ ["a non-string role", { ...payload, roles: [42] }],
142
+ ];
143
+ it.each(rejected)("rejects %s", (_name, input) => {
144
+ expect(() => encodeSignerPayload(input)).toThrow();
145
+ });
146
+ const ignored = [
147
+ ["extra payload properties", { ...payload, extra: { version: 1 } }],
148
+ [
149
+ "wire-format field names alongside camelCase fields",
150
+ {
151
+ ...payload,
152
+ referenced_assertions: [],
153
+ sig_type: "ignored",
154
+ role: ["ignored"],
155
+ },
156
+ ],
157
+ [
158
+ "extra assertion properties",
159
+ { ...payload, referencedAssertions: [{ ...uri, extra: true }] },
160
+ ],
161
+ [
162
+ "case-mismatched optional Roles and Alg",
163
+ {
164
+ ...payload,
165
+ Roles: ["ignored"],
166
+ referencedAssertions: [{ ...uri, Alg: "ignored" }],
167
+ },
168
+ ],
169
+ ];
170
+ it.each(ignored)("ignores %s", (_name, input) => {
171
+ expect(encodeSignerPayload(input)).toEqual(encodeSignerPayload(payload));
172
+ });
173
+ it("ignores an unknown function property without cloning the payload", () => {
174
+ expect(encodeSignerPayload({
175
+ ...payload,
176
+ extra: () => undefined,
177
+ })).toEqual(encodeSignerPayload(payload));
178
+ });
179
+ });
180
+ });
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  export { Builder } from "./Builder.js";
7
7
  export { Reader } from "./Reader.js";
8
8
  export { LocalSigner, CallbackSigner } from "./Signer.js";
9
- export { IdentityAssertionBuilder, IdentityAssertionSigner, CallbackCredentialHolder, } from "./IdentityAssertion.js";
9
+ export { IdentityAssertionBuilder, IdentityAssertionSigner, CallbackCredentialHolder, encodeSignerPayload, } from "./IdentityAssertion.js";
10
10
  export { Trustmark } from "./Trustmark.js";
11
11
  export { isActionsAssertion } from "./assertions.js";
12
12
  export { loadSettingsFromFile } from "./Settings.js";
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: 52231912 bytes
4
- sha256: 454157566c12a23b7284bce2d66644c0db33f214351c55d28438dbe832a6203c
3
+ size: 52232544 bytes
4
+ sha256: 700dc336e16341cfabe26173ab8611648a599c33b8ad163602f7c749e7da4191
@@ -1,4 +1,22 @@
1
- import type { CallbackCredentialHolderInterface, IdentityAssertionBuilderInterface, IdentityAssertionSignerInterface, NeonCallbackCredentialHolderHandle, NeonIdentityAssertionSignerHandle, NeonIdentityAssertionBuilderHandle, SignerPayload, NeonCallbackSignerHandle } from "./types.d.ts";
1
+ import type { CallbackCredentialHolderInterface, IdentityAssertionBuilderInterface, IdentityAssertionSignerInterface, NeonCallbackCredentialHolderHandle, NeonIdentityAssertionSignerHandle, NeonIdentityAssertionBuilderHandle, SignerPayload, SignerPayloadInput, NeonCallbackSignerHandle } from "./types.d.ts";
2
+ /**
3
+ * Encode a CAWG SignerPayload using c2pa_cbor. Hashes become CBOR byte strings;
4
+ * the wire keys are referenced_assertions, sig_type, and (when nonempty) role.
5
+ *
6
+ * Input names are case-sensitive: referencedAssertions, sigType, roles, and each
7
+ * reference's url, hash, and alg. Wire-format names are not aliases. Missing or
8
+ * incorrectly cased required fields are rejected; unknown data properties are
9
+ * ignored, including incorrectly cased optional fields. Omitted, undefined, or
10
+ * empty roles are omitted on the wire; null roles are rejected.
11
+ *
12
+ * Use plain data objects with Uint8Array hashes for portability between bindings
13
+ * (Node Buffer hashes are also supported here). Validation failures throw
14
+ * synchronously. The Web counterpart rejects its Promise and additionally
15
+ * requires the entire payload, including unknown properties, to be
16
+ * structured-cloneable: an unknown function property is ignored here but cannot
17
+ * be sent to the Web worker. Error types and messages are binding-specific.
18
+ */
19
+ export declare function encodeSignerPayload(payload: SignerPayloadInput): Uint8Array;
2
20
  export declare class IdentityAssertionBuilder implements IdentityAssertionBuilderInterface {
3
21
  private _builder;
4
22
  constructor(_builder: NeonIdentityAssertionBuilderHandle);
@@ -0,0 +1 @@
1
+ export {};
@@ -2,7 +2,7 @@ export type * from "./types.d.ts";
2
2
  export { Builder } from "./Builder.js";
3
3
  export { Reader } from "./Reader.js";
4
4
  export { LocalSigner, CallbackSigner } from "./Signer.js";
5
- export { IdentityAssertionBuilder, IdentityAssertionSigner, CallbackCredentialHolder, } from "./IdentityAssertion.js";
5
+ export { IdentityAssertionBuilder, IdentityAssertionSigner, CallbackCredentialHolder, encodeSignerPayload, } from "./IdentityAssertion.js";
6
6
  export { Trustmark } from "./Trustmark.js";
7
7
  export { isActionsAssertion } from "./assertions.js";
8
8
  export { loadSettingsFromFile } from "./Settings.js";
@@ -183,6 +183,11 @@ export interface SignerPayload {
183
183
  roles: string[];
184
184
  }
185
185
 
186
+ /** Input to `encodeSignerPayload`; absent or empty roles are omitted. */
187
+ export type SignerPayloadInput = Omit<SignerPayload, "roles"> & {
188
+ roles?: string[];
189
+ };
190
+
186
191
  export interface HashedUri {
187
192
  url: string;
188
193
  hash: Uint8Array;
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "url": "git+https://github.com/contentauth/c2pa-js.git",
6
6
  "directory": "packages/c2pa-node"
7
7
  },
8
- "version": "0.9.12",
8
+ "version": "0.10.1",
9
9
  "description": "Node.js bindings for C2PA",
10
10
  "main": "dist/index.js",
11
11
  "type": "module",
@@ -65,7 +65,7 @@
65
65
  "pretty-bytes": "^6.1.1",
66
66
  "unzipper": "^0.10.14",
67
67
  "@contentauth/c2pa-types": "0.7.8",
68
- "@contentauth/c2pa-utilities": "0.4.0"
68
+ "@contentauth/c2pa-utilities": "0.5.0"
69
69
  },
70
70
  "nx": {
71
71
  "tags": [
package/src/lib.rs CHANGED
@@ -166,6 +166,10 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
166
166
  )?;
167
167
 
168
168
  // Identity Assertions
169
+ cx.export_function(
170
+ "encodeSignerPayload",
171
+ neon_credential_holder::encode_signer_payload,
172
+ )?;
169
173
  cx.export_function(
170
174
  "identitySignerNew",
171
175
  neon_identity_assertion_signer::NeonIdentityAssertionSigner::new,
@@ -32,7 +32,7 @@ struct JsSignerPayload {
32
32
  referenced_assertions: Vec<JsHashedUri>,
33
33
  sig_type: String,
34
34
  #[serde(default)]
35
- roles: Vec<String>,
35
+ roles: Option<Vec<String>>,
36
36
  }
37
37
 
38
38
  #[derive(Deserialize, Serialize)]
@@ -58,7 +58,7 @@ impl From<&SignerPayload> for JsSignerPayload {
58
58
  })
59
59
  .collect(),
60
60
  sig_type: payload.sig_type.clone(),
61
- roles: payload.roles.clone(),
61
+ roles: Some(payload.roles.clone()),
62
62
  }
63
63
  }
64
64
  }
@@ -72,11 +72,33 @@ impl From<JsSignerPayload> for SignerPayload {
72
72
  .map(|uri| HashedUri::new(uri.url, uri.alg, &uri.hash))
73
73
  .collect(),
74
74
  sig_type: payload.sig_type,
75
- roles: payload.roles,
75
+ roles: payload.roles.unwrap_or_default(),
76
76
  }
77
77
  }
78
78
  }
79
79
 
80
+ /// Encode the JavaScript payload using the canonical CAWG wire representation.
81
+ pub fn encode_signer_payload(mut cx: FunctionContext) -> JsResult<JsUint8Array> {
82
+ let value = cx.argument::<JsValue>(0)?;
83
+ if let Ok(object) = value.downcast::<JsObject, _>(&mut cx) {
84
+ let roles = object.get_value(&mut cx, "roles")?;
85
+ if roles.is_a::<JsNull, _>(&mut cx) {
86
+ return cx.throw_type_error("roles must be an array of strings or undefined");
87
+ }
88
+ }
89
+ let payload: JsSignerPayload = match neon_serde4::from_value(&mut cx, value) {
90
+ Ok(payload) => payload,
91
+ Err(neon_serde4::errors::Error::Js { throw }) => return Err(throw),
92
+ Err(err) => return cx.throw_type_error(err.to_string()),
93
+ };
94
+ let payload: SignerPayload = payload.into();
95
+ let mut bytes = Vec::new();
96
+ if let Err(err) = c2pa_cbor::to_writer(&mut bytes, &payload) {
97
+ return cx.throw_error(err.to_string());
98
+ }
99
+ JsUint8Array::from_slice(&mut cx, &bytes)
100
+ }
101
+
80
102
  /// NeonCallbackCredentialHolder allows JS to asynchronously sign a SignerPayload.
81
103
  #[derive(Clone)]
82
104
  pub struct NeonCallbackCredentialHolder {