@contentauth/c2pa-node 0.4.0 → 0.4.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/dist/Builder.js CHANGED
@@ -68,6 +68,11 @@ export class Builder {
68
68
  }
69
69
  });
70
70
  }
71
+ addIngredientFromReader(reader) {
72
+ const readerHandle = reader.getHandle();
73
+ const result = getNeonBinary().builderAddIngredientFromReader.call(this.builder, readerHandle);
74
+ return JSON.parse(result);
75
+ }
71
76
  toArchive(asset) {
72
77
  return __awaiter(this, void 0, void 0, function* () {
73
78
  return getNeonBinary().builderToArchive.call(this.builder, asset);
@@ -79,11 +84,11 @@ export class Builder {
79
84
  });
80
85
  }
81
86
  sign(signer, input, output) {
82
- return getNeonBinary().builderSign.call(this.builder, signer.signer(), input, output);
87
+ return getNeonBinary().builderSign.call(this.builder, signer.getHandle(), input, output);
83
88
  }
84
89
  signFile(signer, filePath, output) {
85
90
  const input = { path: filePath };
86
- return getNeonBinary().builderSign.call(this.builder, signer.signer(), input, output);
91
+ return getNeonBinary().builderSign.call(this.builder, signer.getHandle(), input, output);
87
92
  }
88
93
  signConfigAsync(callback, signerConfig, input, output) {
89
94
  return __awaiter(this, void 0, void 0, function* () {
@@ -112,7 +117,7 @@ export class Builder {
112
117
  }
113
118
  signAsync(signer, input, output) {
114
119
  return __awaiter(this, void 0, void 0, function* () {
115
- const neonHandle = signer.signer();
120
+ const neonHandle = signer.getHandle();
116
121
  const isIdentity = signer instanceof IdentityAssertionSigner;
117
122
  const neonFn = isIdentity
118
123
  ? getNeonBinary().builderIdentitySignAsync
@@ -634,5 +634,43 @@ describe("Builder", () => {
634
634
  expect(addedIngredient === null || addedIngredient === void 0 ? void 0 : addedIngredient.instance_id).toBe("ingredient-12345");
635
635
  expect(addedIngredient === null || addedIngredient === void 0 ? void 0 : addedIngredient.relationship).toBe("componentOf");
636
636
  }));
637
+ it("should add ingredient from reader", () => __awaiter(void 0, void 0, void 0, function* () {
638
+ var _a;
639
+ const builder1 = Builder.new();
640
+ builder1.setIntent("edit");
641
+ const signer = LocalSigner.newSigner(publicKey, privateKey, "es256");
642
+ const dest1 = {
643
+ buffer: null,
644
+ };
645
+ builder1.sign(signer, source, dest1);
646
+ // Read the signed file back with Reader
647
+ const reader = yield Reader.fromAsset({
648
+ buffer: dest1.buffer,
649
+ mimeType: "image/jpeg",
650
+ });
651
+ expect(reader).not.toBeNull();
652
+ // Create a new builder and add ingredient from the reader
653
+ const builder2 = Builder.new();
654
+ const ingredient = builder2.addIngredientFromReader(reader);
655
+ expect(ingredient).toBeDefined();
656
+ // Verify the ingredient was added to the builder
657
+ const definition = builder2.getManifestDefinition();
658
+ expect(definition.ingredients).toBeDefined();
659
+ expect(definition.ingredients.length).toBeGreaterThan(0);
660
+ // Sign again with the new builder
661
+ const dest2 = {
662
+ buffer: null,
663
+ };
664
+ builder2.sign(signer, source, dest2);
665
+ // Verify the ingredient is in the signed manifest
666
+ const reader2 = yield Reader.fromAsset({
667
+ buffer: dest2.buffer,
668
+ mimeType: "image/jpeg",
669
+ });
670
+ expect(reader2).not.toBeNull();
671
+ const activeManifest = reader2.getActive();
672
+ expect(activeManifest === null || activeManifest === void 0 ? void 0 : activeManifest.ingredients).toBeDefined();
673
+ expect((_a = activeManifest === null || activeManifest === void 0 ? void 0 : activeManifest.ingredients) === null || _a === void 0 ? void 0 : _a.length).toBe(1);
674
+ }));
637
675
  });
638
676
  });
@@ -25,7 +25,7 @@ export class IdentityAssertionBuilder {
25
25
  }
26
26
  static identityBuilderForCredentialHolder(credentialHolder) {
27
27
  return __awaiter(this, void 0, void 0, function* () {
28
- const builder = getNeonBinary().identityBuilderForCredentialHolder(credentialHolder.signer());
28
+ const builder = getNeonBinary().identityBuilderForCredentialHolder(credentialHolder.getHandle());
29
29
  return new IdentityAssertionBuilder(builder);
30
30
  });
31
31
  }
@@ -50,7 +50,7 @@ export class IdentityAssertionSigner {
50
50
  addIdentityAssertion(identityAssertionBuilder) {
51
51
  getNeonBinary().identitySignerAddIdentityAssertion.call(this._signer, identityAssertionBuilder.builder());
52
52
  }
53
- signer() {
53
+ getHandle() {
54
54
  return this._signer;
55
55
  }
56
56
  }
@@ -58,7 +58,7 @@ export class CallbackCredentialHolder {
58
58
  constructor(callbackCredentialHolder) {
59
59
  this.callbackCredentialHolder = callbackCredentialHolder;
60
60
  }
61
- signer() {
61
+ getHandle() {
62
62
  return this.callbackCredentialHolder;
63
63
  }
64
64
  static newCallbackCredentialHolder(reserveSize, sigType, callback) {
@@ -136,7 +136,7 @@ describe("IdentityAssertionBuilder", () => {
136
136
  buffer: yield fs.readFile("./tests/fixtures/thumbnail.jpg"),
137
137
  });
138
138
  // Create and configure the identity assertion
139
- const iaSigner = IdentityAssertionSigner.new(c2paSigner.signer());
139
+ const iaSigner = IdentityAssertionSigner.new(c2paSigner.getHandle());
140
140
  const iab = yield IdentityAssertionBuilder.identityBuilderForCredentialHolder(cawgSigner);
141
141
  iab.addReferencedAssertions(["cawg.training-mining"]);
142
142
  iaSigner.addIdentityAssertion(iab);
package/dist/Reader.js CHANGED
@@ -57,4 +57,7 @@ export class Reader {
57
57
  }
58
58
  return manifestStore.manifests[activeManifest];
59
59
  }
60
+ getHandle() {
61
+ return this.reader;
62
+ }
60
63
  }
package/dist/Signer.js CHANGED
@@ -42,7 +42,7 @@ export class LocalSigner {
42
42
  timeAuthorityUrl() {
43
43
  return getNeonBinary().localSignerTimeAuthorityUrl.call(this.localSigner);
44
44
  }
45
- signer() {
45
+ getHandle() {
46
46
  return this.localSigner;
47
47
  }
48
48
  }
@@ -50,7 +50,7 @@ export class CallbackSigner {
50
50
  constructor(callbackSigner) {
51
51
  this.callbackSigner = callbackSigner;
52
52
  }
53
- signer() {
53
+ getHandle() {
54
54
  return this.callbackSigner;
55
55
  }
56
56
  static newSigner(config, callback) {
package/dist/index.node CHANGED
Binary file
@@ -1,5 +1,5 @@
1
- import type { BuilderIntent, Manifest } from "@contentauth/c2pa-types";
2
- import type { BuilderInterface, CallbackSignerInterface, ClaimVersion, DestinationAsset, IdentityAssertionSignerInterface, JsCallbackSignerConfig, LocalSignerInterface, ManifestAssertionKind, SourceAsset } from "./types.d.ts";
1
+ import type { BuilderIntent, Ingredient, Manifest } from "@contentauth/c2pa-types";
2
+ import type { BuilderInterface, CallbackSignerInterface, ClaimVersion, DestinationAsset, IdentityAssertionSignerInterface, JsCallbackSignerConfig, LocalSignerInterface, ManifestAssertionKind, ReaderInterface, SourceAsset } from "./types.d.ts";
3
3
  export declare class Builder implements BuilderInterface {
4
4
  private builder;
5
5
  private constructor();
@@ -12,6 +12,7 @@ export declare class Builder implements BuilderInterface {
12
12
  addAssertion(label: string, assertion: unknown, assertionKind?: ManifestAssertionKind): void;
13
13
  addResource(uri: string, resource: SourceAsset): Promise<void>;
14
14
  addIngredient(ingredientJson: string, ingredient?: SourceAsset): Promise<void>;
15
+ addIngredientFromReader(reader: ReaderInterface): Ingredient;
15
16
  toArchive(asset: DestinationAsset): Promise<void>;
16
17
  static fromArchive(asset: SourceAsset): Promise<Builder>;
17
18
  sign(signer: LocalSignerInterface, input: SourceAsset, output: DestinationAsset): Buffer;
@@ -12,12 +12,12 @@ export declare class IdentityAssertionSigner implements IdentityAssertionSignerI
12
12
  constructor(_signer: NeonIdentityAssertionSignerHandle);
13
13
  static new(signer: NeonCallbackSignerHandle): IdentityAssertionSigner;
14
14
  addIdentityAssertion(identityAssertionBuilder: IdentityAssertionBuilder): void;
15
- signer(): NeonIdentityAssertionSignerHandle;
15
+ getHandle(): NeonIdentityAssertionSignerHandle;
16
16
  }
17
17
  export declare class CallbackCredentialHolder implements CallbackCredentialHolderInterface {
18
18
  private callbackCredentialHolder;
19
19
  constructor(callbackCredentialHolder: NeonCallbackCredentialHolderHandle);
20
- signer(): NeonCallbackCredentialHolderHandle;
20
+ getHandle(): NeonCallbackCredentialHolderHandle;
21
21
  static newCallbackCredentialHolder(reserveSize: number, sigType: string, callback: (signerPayload: SignerPayload) => Promise<Buffer>): CallbackCredentialHolder;
22
22
  sign(payload: SignerPayload): Promise<Buffer>;
23
23
  reserveSize(): number;
@@ -11,4 +11,5 @@ export declare class Reader implements ReaderInterface {
11
11
  static fromManifestDataAndAsset(manifestData: Buffer, asset: SourceAsset): Promise<Reader>;
12
12
  activeLabel(): string | undefined;
13
13
  getActive(): Manifest | undefined;
14
+ getHandle(): NeonReaderHandle;
14
15
  }
@@ -8,12 +8,12 @@ export declare class LocalSigner implements LocalSignerInterface {
8
8
  certs(): Array<Buffer>;
9
9
  reserveSize(): number;
10
10
  timeAuthorityUrl(): string | undefined;
11
- signer(): NeonLocalSignerHandle;
11
+ getHandle(): NeonLocalSignerHandle;
12
12
  }
13
13
  export declare class CallbackSigner implements CallbackSignerInterface {
14
14
  private callbackSigner;
15
15
  constructor(callbackSigner: NeonCallbackSignerHandle);
16
- signer(): NeonCallbackSignerHandle;
16
+ getHandle(): NeonCallbackSignerHandle;
17
17
  static newSigner(config: JsCallbackSignerConfig, callback: (data: Buffer) => Promise<Buffer>): CallbackSigner;
18
18
  sign(data: Buffer): Promise<Buffer>;
19
19
  alg(): SigningAlg;
@@ -15,6 +15,7 @@
15
15
  import { Buffer } from "buffer";
16
16
  import type {
17
17
  BuilderIntent,
18
+ Ingredient,
18
19
  Manifest,
19
20
  ManifestStore,
20
21
  } from "@contentauth/c2pa-types";
@@ -110,7 +111,7 @@ export interface LocalSignerInterface {
110
111
  certs(): Array<Buffer>;
111
112
  reserveSize(): number;
112
113
  timeAuthorityUrl(): string | undefined;
113
- signer(): NeonLocalSignerHandle;
114
+ getHandle(): NeonLocalSignerHandle;
114
115
  }
115
116
 
116
117
  /**
@@ -123,14 +124,14 @@ export interface CallbackSignerInterface {
123
124
  reserveSize(): number;
124
125
  timeAuthorityUrl(): string | undefined;
125
126
  directCoseHandling(): boolean;
126
- signer(): NeonCallbackSignerHandle;
127
+ getHandle(): NeonCallbackSignerHandle;
127
128
  }
128
129
 
129
130
  export interface CallbackCredentialHolderInterface {
130
131
  sigType(): string;
131
132
  reserveSize(): number;
132
133
  sign(payload: SignerPayload): Promise<Buffer>;
133
- signer(): NeonCallbackCredentialHolderHandle;
134
+ getHandle(): NeonCallbackCredentialHolderHandle;
134
135
  }
135
136
 
136
137
  /**
@@ -235,6 +236,12 @@ export interface BuilderInterface {
235
236
  ingredient?: SourceAsset,
236
237
  ): Promise<void>;
237
238
 
239
+ /**
240
+ * Add an ingredient to the manifest from a Reader
241
+ * @param reader The Reader object of the ingredient
242
+ */
243
+ addIngredientFromReader(reader: ReaderInterface): Ingredient;
244
+
238
245
  /**
239
246
  * Convert the Builder into a archive formatted buffer or file
240
247
  * @param asset The file or buffer for the archive
@@ -332,6 +339,11 @@ export interface ReaderInterface {
332
339
  * @param filePath The path to the file
333
340
  */
334
341
  resourceToAsset(uri: string, output: DestinationAsset): Promise<number>;
342
+
343
+ /**
344
+ * Get the internal handle for use with Neon bindings
345
+ */
346
+ getHandle(): NeonReaderHandle;
335
347
  }
336
348
 
337
349
  export interface IdentityAssertionSignerInterface {
@@ -345,7 +357,7 @@ export interface IdentityAssertionSignerInterface {
345
357
  identityAssertionBuilder: IdentityAssertionBuilderInterface,
346
358
  ): void;
347
359
 
348
- signer(): NeonIdentityAssertionSignerHandle;
360
+ getHandle(): NeonIdentityAssertionSignerHandle;
349
361
  }
350
362
 
351
363
  export interface IdentityAssertionBuilderInterface {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "git+https://github.com/contentauth/c2pa-node-v2.git"
6
6
  },
7
- "version": "0.4.0",
7
+ "version": "0.4.1",
8
8
  "description": "Node.js bindings for C2PA",
9
9
  "main": "dist/index.js",
10
10
  "type": "module",
package/src/lib.rs CHANGED
@@ -55,6 +55,10 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
55
55
  "builderAddIngredientFromAsset",
56
56
  neon_builder::NeonBuilder::add_ingredient_from_asset,
57
57
  )?;
58
+ cx.export_function(
59
+ "builderAddIngredientFromReader",
60
+ neon_builder::NeonBuilder::add_ingredient_from_reader,
61
+ )?;
58
62
  cx.export_function("builderToArchive", neon_builder::NeonBuilder::to_archive)?;
59
63
  cx.export_function(
60
64
  "builderFromArchive",
@@ -14,6 +14,7 @@
14
14
  use crate::asset::parse_asset;
15
15
  use crate::error::{as_js_error, Error};
16
16
  use crate::neon_identity_assertion_signer::NeonIdentityAssertionSigner;
17
+ use crate::neon_reader::NeonReader;
17
18
  use crate::neon_signer::{CallbackSignerConfig, NeonCallbackSigner, NeonLocalSigner};
18
19
  use crate::runtime::runtime;
19
20
  use c2pa::{Builder, BuilderIntent, Ingredient};
@@ -92,8 +93,8 @@ impl NeonBuilder {
92
93
  let rt = runtime();
93
94
  let this = cx.this::<JsBox<Self>>()?;
94
95
  let action_json = cx.argument::<JsString>(0)?.value(&mut cx);
95
- let action: c2pa::assertions::Action = serde_json::from_str(&action_json)
96
- .or_else(|err| cx.throw_error(err.to_string()))?;
96
+ let action: c2pa::assertions::Action =
97
+ serde_json::from_str(&action_json).or_else(|err| cx.throw_error(err.to_string()))?;
97
98
  let mut builder = rt.block_on(async { this.builder.lock().await });
98
99
  builder
99
100
  .add_action(action)
@@ -217,6 +218,21 @@ impl NeonBuilder {
217
218
  Ok(promise)
218
219
  }
219
220
 
221
+ pub fn add_ingredient_from_reader(mut cx: FunctionContext) -> JsResult<JsString> {
222
+ let rt = runtime();
223
+ let this = cx.this::<JsBox<Self>>()?;
224
+ let reader = cx.argument::<JsBox<NeonReader>>(0)?.reader();
225
+
226
+ let mut builder = rt.block_on(async { this.builder.lock().await });
227
+ let reader = rt.block_on(async { reader.lock().await });
228
+ let ingredient = builder
229
+ .add_ingredient_from_reader(&reader)
230
+ .or_else(|err| cx.throw_error(err.to_string()))?;
231
+ let json =
232
+ serde_json::to_string(&ingredient).or_else(|err| cx.throw_error(err.to_string()))?;
233
+ Ok(cx.string(json))
234
+ }
235
+
220
236
  pub fn to_archive(mut cx: FunctionContext) -> JsResult<JsPromise> {
221
237
  let this = cx.this::<JsBox<Self>>()?;
222
238
  let dest = cx
@@ -34,6 +34,11 @@ impl NeonReader {
34
34
  }))
35
35
  }
36
36
 
37
+ #[allow(clippy::borrowed_box)]
38
+ pub(crate) fn reader(&self) -> Arc<Mutex<Reader>> {
39
+ Arc::clone(&self.reader)
40
+ }
41
+
37
42
  pub fn from_stream(mut cx: FunctionContext) -> JsResult<JsPromise> {
38
43
  let rt = runtime();
39
44
  let channel = cx.channel();
@@ -69,9 +74,7 @@ impl NeonReader {
69
74
  // Return null instead of throwing for these specific cases
70
75
  match &err {
71
76
  Error::C2pa(c2pa_err) => match c2pa_err {
72
- c2pa::Error::JumbfNotFound => {
73
- Ok(cx.null().upcast::<JsValue>())
74
- }
77
+ c2pa::Error::JumbfNotFound => Ok(cx.null().upcast::<JsValue>()),
75
78
  _ => as_js_error(&mut cx, err).and_then(|err| cx.throw(err)),
76
79
  },
77
80
  _ => as_js_error(&mut cx, err).and_then(|err| cx.throw(err)),