Skip to content

Commit

Permalink
Export C2PA types (#5)
Browse files Browse the repository at this point in the history
* Export C2PA types

* Add changeset
  • Loading branch information
Dave Kozma committed Jun 29, 2023
1 parent 46975b6 commit 8f4a321
Show file tree
Hide file tree
Showing 40 changed files with 1,377 additions and 465 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-bears-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'c2pa-node': patch
---

Added C2PA types to exports
200 changes: 200 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
c2pa-node / [Exports](modules.md)

# c2pa-node

**c2pa-node:** Node.js bindings for C2PA

## Installing c2pa-node

Installing c2pa-node requires a [supported version of Node and Rust](https://github.com/neon-bindings/neon#platform-support).
[nvm](https://github.com/nvm-sh/nvm) is a good tool for managing multiple versions of Node on your machine, and you can install
Rust by [visiting this link](https://www.rust-lang.org/tools/install).

You can install the project with npm. In the project directory, run:

```sh
# Switch to the supported version of Node.js for building
$ nvm use
# Install pnpm
$ npm install -g pnpm
# Install dependencies
$ pnpm install
# Build the SDK
$ pnpm run build
```

## Quick start

### Creating a `c2pa` object

You will need to instantiate a `c2pa` object by using [`createC2pa()`](docs/modules.md#createc2pa):

```ts
import { createC2pa } from 'c2pa-node';

const c2pa = createC2pa();
```

### Reading a manifest

You can read a manifest by using the `c2pa.read()` function:

```ts
import { readFile } from 'node:fs/promises';

const buffer = await readFile('my-c2pa-file.jpg');
const result = await c2pa.read({ mimeType: 'image/jpeg', buffer });

if (result) {
const { active_manifest, manifests, validation_status } = result;
console.log(active_manifest.claim_generator);
}
```

### Creating a manifest

To create a manifest, you can pass in the claim information to a [`ManifestBuilder`](docs/classes//ManifestBuilder.md).

```ts
import { ManifestBuilder } from 'c2pa-node';

const manifest = new ManifestBuilder({
claim_generator: 'my-app/1.0.0',
format: 'image/jpeg',
title: 'node_test_local_signer.jpg',
assertions: [
{
label: 'c2pa.actions',
data: {
actions: [
{
action: 'c2pa.created',
},
],
},
},
{
label: 'com.custom.my-assertion',
data: {
description: 'My custom test assertion',
version: '1.0.0',
},
},
],
});
```

### Adding an ingredient

You can use `c2pa.createIngredient()` to load ingredient data for inclusion into a manifest. This can be stored on a backend
if necessary and loaded in at signing time without the need for the original ingredient if it is no longer available.

```ts
// Create the ingredient
const ingredient = await c2pa.createIngredient({
asset: ingredientAsset,
title: 'ingredient.jpg',
});
// Add it to the manifest
manifest.addIngredient(ingredient);
```

### Signing a manifest

You can use the `c2pa.sign()` method to sign an ingredient, either locally if you have a signing certificate and key
available, or by using a remote signing API.

#### Local signing

If you have a signing certificate and key, you can sign locally using a local signer:

```ts
import { readFile } from 'node:fs/promises';
import { SigningAlgorithm } from 'c2pa-node';

async function createLocalSigner() {
const [certificate, privateKey] = await Promise.all([
readFile('tests/fixtures/es256_certs.pem'),
readFile('tests/fixtures/es256_private.key'),
]);

return {
type: 'local',
certificate,
privateKey,
algorithm: SigningAlgorithm.ES256,
tsaUrl: 'http://timestamp.digicert.com',
};
}

async function sign(asset, manifest) {
const buffer = await readFile('to-be-signed.jpg');
const asset: Asset = { mimeType: 'image/jpeg', buffer };
const signer = await createLocalSigner();
const c2pa = createC2pa({
signer,
});

const { signedAsset, signedManifest } = await c2pa.sign({ asset, manifest });
}

sign(asset, manifest);
```

#### Remote signing

If you have a service that you want to use for signing, you can use that to sign remotely:

```ts
import { readFile } from 'node:fs/promises';
import { fetch, Headers } from 'node-fetch';
import { SigningAlgorithm } from 'c2pa-node';

function createRemoteSigner() {
return {
type: 'remote',
async reserveSize() {
const url = `https://my.signing.service/box-size`;
const res = await fetch(url);
const data = (await res.json()) as { boxSize: number };
return data.boxSize;
},
async sign({ reserveSize, toBeSigned }) {
const url = `https://my.signing.service/sign?boxSize=${reserveSize}`;
const res = await fetch(url, {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/octet-stream',
}),
body: toBeSigned,
});
return res.buffer();
},
};
}

async function sign(asset, manifest) {
const buffer = await readFile('to-be-signed.jpg');
const asset: Asset = { mimeType: 'image/jpeg', buffer };
const signer = createRemoteSigner();
const c2pa = createC2pa({
signer,
});

const { signedAsset, signedManifest } = await c2pa.sign({ asset, manifest });
}

sign(asset, manifest);
```

## API documentation

[Click here](docs/modules.md) to view the API documentation.

## Testing

After installation, you can run the test suite by running:

```sh
$ pnpm test
```
24 changes: 12 additions & 12 deletions docs/classes/ManifestBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

#### Defined in

[lib/manifestBuilder.ts:37](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/manifestBuilder.ts#L37)
[lib/manifestBuilder.ts:37](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/manifestBuilder.ts#L37)

## Properties

Expand All @@ -53,7 +53,7 @@

#### Defined in

[lib/manifestBuilder.ts:27](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/manifestBuilder.ts#L27)
[lib/manifestBuilder.ts:27](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/manifestBuilder.ts#L27)

___

Expand All @@ -63,7 +63,7 @@ ___

#### Defined in

[lib/manifestBuilder.ts:31](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/manifestBuilder.ts#L31)
[lib/manifestBuilder.ts:31](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/manifestBuilder.ts#L31)

___

Expand All @@ -73,7 +73,7 @@ ___

#### Defined in

[lib/manifestBuilder.ts:29](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/manifestBuilder.ts#L29)
[lib/manifestBuilder.ts:29](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/manifestBuilder.ts#L29)

___

Expand All @@ -83,7 +83,7 @@ ___

#### Defined in

[lib/manifestBuilder.ts:25](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/manifestBuilder.ts#L25)
[lib/manifestBuilder.ts:25](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/manifestBuilder.ts#L25)

## Accessors

Expand All @@ -97,7 +97,7 @@ ___

#### Defined in

[lib/manifestBuilder.ts:96](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/manifestBuilder.ts#L96)
[lib/manifestBuilder.ts:96](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/manifestBuilder.ts#L96)

___

Expand All @@ -111,7 +111,7 @@ ___

#### Defined in

[lib/manifestBuilder.ts:100](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/manifestBuilder.ts#L100)
[lib/manifestBuilder.ts:100](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/manifestBuilder.ts#L100)

___

Expand All @@ -125,7 +125,7 @@ ___

#### Defined in

[lib/manifestBuilder.ts:33](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/manifestBuilder.ts#L33)
[lib/manifestBuilder.ts:33](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/manifestBuilder.ts#L33)

## Methods

Expand All @@ -145,7 +145,7 @@ ___

#### Defined in

[lib/manifestBuilder.ts:66](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/manifestBuilder.ts#L66)
[lib/manifestBuilder.ts:66](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/manifestBuilder.ts#L66)

___

Expand All @@ -165,7 +165,7 @@ ___

#### Defined in

[lib/manifestBuilder.ts:80](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/manifestBuilder.ts#L80)
[lib/manifestBuilder.ts:80](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/manifestBuilder.ts#L80)

___

Expand All @@ -185,7 +185,7 @@ ___

#### Defined in

[lib/manifestBuilder.ts:109](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/manifestBuilder.ts#L109)
[lib/manifestBuilder.ts:109](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/manifestBuilder.ts#L109)

___

Expand All @@ -205,4 +205,4 @@ ___

#### Defined in

[lib/manifestBuilder.ts:86](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/manifestBuilder.ts#L86)
[lib/manifestBuilder.ts:86](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/manifestBuilder.ts#L86)
14 changes: 7 additions & 7 deletions docs/enums/SigningAlgorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#### Defined in

[lib/signer.ts:5](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/signer.ts#L5)
[lib/signer.ts:5](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/signer.ts#L5)

___

Expand All @@ -32,7 +32,7 @@ ___

#### Defined in

[lib/signer.ts:7](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/signer.ts#L7)
[lib/signer.ts:7](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/signer.ts#L7)

___

Expand All @@ -42,7 +42,7 @@ ___

#### Defined in

[lib/signer.ts:9](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/signer.ts#L9)
[lib/signer.ts:9](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/signer.ts#L9)

___

Expand All @@ -52,7 +52,7 @@ ___

#### Defined in

[lib/signer.ts:17](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/signer.ts#L17)
[lib/signer.ts:17](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/signer.ts#L17)

___

Expand All @@ -62,7 +62,7 @@ ___

#### Defined in

[lib/signer.ts:11](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/signer.ts#L11)
[lib/signer.ts:11](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/signer.ts#L11)

___

Expand All @@ -72,7 +72,7 @@ ___

#### Defined in

[lib/signer.ts:13](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/signer.ts#L13)
[lib/signer.ts:13](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/signer.ts#L13)

___

Expand All @@ -82,4 +82,4 @@ ___

#### Defined in

[lib/signer.ts:15](https://github.com/contentauth/c2pa-node/blob/7225e97/js-src/lib/signer.ts#L15)
[lib/signer.ts:15](https://github.com/contentauth/c2pa-node/blob/46975b6/js-src/lib/signer.ts#L15)
Loading

0 comments on commit 8f4a321

Please sign in to comment.