Skip to content

Commit 485f9e0

Browse files
committed
feat: Refactor NeonTypes
Interfaces for classes should not be used for Neon Objects.
1 parent 0262c27 commit 485f9e0

14 files changed

+148
-85
lines changed

js-src/Builder.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ import type {
2323
LocalSignerInterface,
2424
ManifestAssertionKind,
2525
SourceAsset,
26+
NeonBuilderHandle,
2627
} from "./types";
28+
import { IdentityAssertionSigner } from "./IdentityAssertion";
2729
import type { Manifest } from "@contentauth/c2pa-types";
2830

2931
export class Builder implements BuilderInterface {
30-
private constructor(private builder: BuilderInterface) {}
32+
private constructor(private builder: NeonBuilderHandle) {}
3133

3234
static new(): Builder {
33-
const builder = neon.builderNew();
35+
const builder: NeonBuilderHandle = neon.builderNew();
3436
return new Builder(builder);
3537
}
3638

@@ -49,7 +51,7 @@ export class Builder implements BuilderInterface {
4951
"Failed to stringify JSON Manifest Definition: Unknown error",
5052
);
5153
}
52-
const builder: BuilderInterface = neon.builderWithJson(jsonString);
54+
const builder: NeonBuilderHandle = neon.builderWithJson(jsonString);
5355
return new Builder(builder);
5456
}
5557

@@ -146,8 +148,13 @@ export class Builder implements BuilderInterface {
146148
input: SourceAsset,
147149
output: DestinationAsset,
148150
): Promise<Buffer> {
149-
return neon.builderSignAsync
150-
.call(this.builder, signer.signer(), input, output)
151+
const neonHandle = signer.signer();
152+
const isIdentity = signer instanceof IdentityAssertionSigner;
153+
const neonFn = isIdentity
154+
? neon.builderIdentitySignAsync
155+
: neon.builderSignAsync;
156+
return neonFn
157+
.call(this.builder, neonHandle, input, output)
151158
.then((result: Buffer | { manifest: Buffer; signedAsset: Buffer }) => {
152159
// output is a buffer and result is the manifest and the signed asset.
153160
if ("buffer" in output) {

js-src/IdentityAssertion.spec.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,17 @@
1313

1414
/* eslint-disable @typescript-eslint/no-non-null-assertion */
1515

16-
import { CallbackSigner } from "./Signer";
17-
import { Reader } from "./Reader";
18-
import { Builder } from "./Builder";
19-
import {
20-
IdentityAssertionBuilder,
21-
IdentityAssertionSigner,
22-
CallbackCredentialHolder,
23-
} from "./IdentityAssertion";
16+
// Note: We will dynamically import modules after establishing mocks
2417
import type {
2518
JsCallbackSignerConfig,
2619
DestinationBufferAsset,
2720
SigningAlg,
21+
SignerPayload,
2822
} from "./types";
2923
import type { Manifest } from "@contentauth/c2pa-types";
3024
import * as fs from "fs-extra";
3125
import * as crypto from "crypto";
26+
import { encode } from "cbor2";
3227

3328
class TestSigner {
3429
private privateKey: crypto.KeyObject;
@@ -41,7 +36,19 @@ class TestSigner {
4136
}
4237

4338
sign = async (bytes: Buffer): Promise<Buffer> => {
44-
return crypto.sign(null, bytes, this.privateKey);
39+
const sign = crypto.createSign("SHA256");
40+
sign.update(bytes);
41+
sign.end();
42+
return sign.sign(this.privateKey);
43+
};
44+
}
45+
46+
class TestCawgSigner {
47+
constructor(private manifestSigner: TestSigner) {}
48+
49+
sign = async (payload: SignerPayload): Promise<Buffer> => {
50+
const cborBytes = Buffer.from(encode(payload));
51+
return this.manifestSigner.sign(cborBytes);
4552
};
4653
}
4754

@@ -103,17 +110,20 @@ describe("IdentityAssertionBuilder", () => {
103110
};
104111

105112
it("should build an Identity Assertion Signer", async () => {
113+
const { CallbackSigner } = await import("./Signer");
114+
const { Reader } = await import("./Reader");
115+
const { Builder } = await import("./Builder");
116+
const {
117+
IdentityAssertionBuilder,
118+
IdentityAssertionSigner,
119+
CallbackCredentialHolder,
120+
} = await import("./IdentityAssertion");
106121
// Read certificate files once
107122
const c2paPrivateKey = await fs.readFile(
108123
"./tests/fixtures/certs/es256.pem",
109124
);
110125
const c2paPublicKey = await fs.readFile("./tests/fixtures/certs/es256.pub");
111-
const cawgPrivateKey = await fs.readFile(
112-
"./tests/fixtures/certs/ed25519.pem",
113-
);
114-
const cawgPublicKey = await fs.readFile(
115-
"./tests/fixtures/certs/ed25519.pub",
116-
);
126+
// Use the same signer for both C2PA manifest and COSE signing
117127

118128
// Create signer configurations
119129
const c2paConfig: JsCallbackSignerConfig = {
@@ -128,11 +138,11 @@ describe("IdentityAssertionBuilder", () => {
128138

129139
// Create signers
130140
const c2paTestSigner = new TestSigner(c2paPrivateKey);
131-
const cawgTestSigner = new TestSigner(cawgPrivateKey);
132141
const c2paSigner = CallbackSigner.newSigner(
133142
c2paConfig,
134143
c2paTestSigner.sign,
135144
);
145+
const cawgTestSigner = new TestCawgSigner(c2paTestSigner);
136146
const cawgSigner = CallbackCredentialHolder.newCallbackCredentialHolder(
137147
10000,
138148
"cawg.x509.cose",
@@ -164,13 +174,13 @@ describe("IdentityAssertionBuilder", () => {
164174
const iaSigner = IdentityAssertionSigner.new(c2paSigner.signer());
165175
const iab =
166176
await IdentityAssertionBuilder.identityBuilderForCredentialHolder(
167-
cawgSigner.signer(),
177+
cawgSigner,
168178
);
169179
iab.addReferencedAssertions(["cawg.training-mining"]);
170180
iaSigner.addIdentityAssertion(iab);
171181

172182
// Sign the manifest (standard async flow)
173-
await builder.signAsync(iaSigner.signer(), source, dest);
183+
await builder.signAsync(iaSigner, source, dest);
174184

175185
// Verify the manifest
176186
const reader = await Reader.fromAsset({

js-src/IdentityAssertion.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@
1414
const neon = require("./index.node");
1515
import type {
1616
CallbackCredentialHolderInterface,
17-
CallbackSignerInterface,
1817
IdentityAssertionBuilderInterface,
1918
IdentityAssertionSignerInterface,
19+
NeonCallbackCredentialHolderHandle,
20+
NeonIdentityAssertionSignerHandle,
21+
NeonIdentityAssertionBuilderHandle,
2022
SignerPayload,
23+
NeonCallbackSignerHandle,
2124
} from "./types";
2225

2326
export class IdentityAssertionBuilder
2427
implements IdentityAssertionBuilderInterface
2528
{
26-
constructor(private _builder: IdentityAssertionBuilderInterface) {}
29+
constructor(private _builder: NeonIdentityAssertionBuilderHandle) {}
2730

2831
static async identityBuilderForCredentialHolder(
2932
credentialHolder: CallbackCredentialHolderInterface,
@@ -45,18 +48,18 @@ export class IdentityAssertionBuilder
4548
neon.identityBuilderAddRoles.call(this._builder, roles);
4649
}
4750

48-
builder(): IdentityAssertionBuilderInterface {
51+
builder(): NeonIdentityAssertionBuilderHandle {
4952
return this._builder;
5053
}
5154
}
5255

5356
export class IdentityAssertionSigner
5457
implements IdentityAssertionSignerInterface
5558
{
56-
constructor(private _signer: IdentityAssertionSignerInterface) {}
59+
constructor(private _signer: NeonIdentityAssertionSignerHandle) {}
5760

58-
static new(signer: CallbackSignerInterface): IdentityAssertionSigner {
59-
const identitySigner = neon.identitySignerNew(signer.signer());
61+
static new(signer: NeonCallbackSignerHandle): IdentityAssertionSigner {
62+
const identitySigner = neon.identitySignerNew(signer);
6063
return new IdentityAssertionSigner(identitySigner);
6164
}
6265

@@ -69,7 +72,7 @@ export class IdentityAssertionSigner
6972
);
7073
}
7174

72-
signer(): IdentityAssertionSignerInterface {
75+
signer(): NeonIdentityAssertionSignerHandle {
7376
return this._signer;
7477
}
7578
}
@@ -78,10 +81,10 @@ export class CallbackCredentialHolder
7881
implements CallbackCredentialHolderInterface
7982
{
8083
constructor(
81-
private callbackCredentialHolder: CallbackCredentialHolderInterface,
84+
private callbackCredentialHolder: NeonCallbackCredentialHolderHandle,
8285
) {}
8386

84-
signer(): CallbackCredentialHolderInterface {
87+
signer(): NeonCallbackCredentialHolderHandle {
8588
return this.callbackCredentialHolder;
8689
}
8790

js-src/Reader.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
// each license.
1313

1414
const neon = require("./index.node");
15-
import type { DestinationAsset, ReaderInterface, SourceAsset } from "./types";
15+
import type {
16+
DestinationAsset,
17+
ReaderInterface,
18+
SourceAsset,
19+
NeonReaderHandle,
20+
} from "./types";
1621

1722
import type { Manifest, ManifestStore } from "@contentauth/c2pa-types";
1823

1924
export class Reader implements ReaderInterface {
20-
constructor(private reader: ReaderInterface) {}
25+
constructor(private reader: NeonReaderHandle) {}
2126

2227
json(): ManifestStore {
2328
return JSON.parse(neon.readerJson.call(this.reader));
@@ -36,15 +41,15 @@ export class Reader implements ReaderInterface {
3641
}
3742

3843
static async fromAsset(asset: SourceAsset): Promise<Reader> {
39-
const reader = await neon.readerFromAsset(asset);
44+
const reader: NeonReaderHandle = await neon.readerFromAsset(asset);
4045
return new Reader(reader);
4146
}
4247

4348
static async fromManifestDataAndAsset(
4449
manifestData: Buffer,
4550
asset: SourceAsset,
4651
): Promise<Reader> {
47-
const reader = await neon.readerFromManifestDataAndAsset(
52+
const reader: NeonReaderHandle = await neon.readerFromManifestDataAndAsset(
4853
manifestData,
4954
asset,
5055
);

js-src/Signer.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ const neon = require("./index.node");
1515
import type {
1616
CallbackSignerInterface,
1717
JsCallbackSignerConfig,
18+
NeonCallbackSignerHandle,
19+
NeonLocalSignerHandle,
1820
LocalSignerInterface,
1921
SigningAlg,
2022
} from "./types";
2123

2224
export class LocalSigner implements LocalSignerInterface {
23-
constructor(private localSigner: LocalSignerInterface) {}
25+
constructor(private localSigner: NeonLocalSignerHandle) {}
2426

2527
static newSigner(
2628
certificate: Buffer,
@@ -57,15 +59,15 @@ export class LocalSigner implements LocalSignerInterface {
5759
return neon.localSignerTimeAuthorityUrl.call(this.localSigner);
5860
}
5961

60-
signer(): LocalSignerInterface {
62+
signer(): NeonLocalSignerHandle {
6163
return this.localSigner;
6264
}
6365
}
6466

6567
export class CallbackSigner implements CallbackSignerInterface {
66-
constructor(private callbackSigner: CallbackSignerInterface) {}
68+
constructor(private callbackSigner: NeonCallbackSignerHandle) {}
6769

68-
signer(): CallbackSignerInterface {
70+
signer(): NeonCallbackSignerHandle {
6971
return this.callbackSigner;
7072
}
7173

js-src/Trustmark.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
// each license.
1313

1414
const neon = require("./index.node");
15-
import type { TrustmarkInterface, TrustmarkConfig } from "./types";
15+
import type {
16+
TrustmarkInterface,
17+
TrustmarkConfig,
18+
NeonTrustmarkHandle,
19+
} from "./types";
1620

1721
export class Trustmark implements TrustmarkInterface {
18-
constructor(private trustmark: TrustmarkInterface) {}
22+
constructor(private trustmark: NeonTrustmarkHandle) {}
1923

2024
static async newTrustmark(config: TrustmarkConfig): Promise<Trustmark> {
21-
const trustmark = await neon.trustmarkNew(config);
25+
const trustmark: NeonTrustmarkHandle = await neon.trustmarkNew(config);
2226
return new Trustmark(trustmark);
2327
}
2428

0 commit comments

Comments
 (0)