Skip to content

Commit 7230550

Browse files
committed
chore: some things from coderabbit
1 parent 1b19583 commit 7230550

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

core/device-id/certor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { decodeJwt } from "jose";
44
import { base58btc } from "multiformats/bases/base58";
55
import { sha1 } from "multiformats/hashes/sha1";
66
import { sha256 } from "multiformats/hashes/sha2";
7+
import { deepFreeze } from "@fireproof/core-runtime";
78
import { CertificatePayload, CertificatePayloadSchema } from "@fireproof/core-types-base/fp-ca-cert-payload.zod.js";
89

910
export class Certor {
@@ -25,7 +26,8 @@ export class Certor {
2526
}
2627

2728
constructor(base64: Base64EndeCoder, cert: CertificatePayload) {
28-
this.#cert = cert;
29+
// this.#cert = cert;
30+
this.#cert = deepFreeze(toSortedObject(cert)) as CertificatePayload;
2931
this.base64 = base64;
3032
}
3133

core/device-id/device-id-key.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ export class DeviceIdKey {
2121
},
2222
): Promise<Result<DeviceIdKey>> {
2323
const parsed = JWKPrivateSchema.safeParse(jwk);
24-
if (parsed.success && (parsed.data.kty !== "EC" || parsed.data.crv !== "P-256" || !("d" in parsed.data))) {
25-
return Result.Err("Invalid JWK for ES256");
24+
if (!parsed.success) {
25+
return Result.Err(new Error(`Invalid JWK: ${parsed.error.message}`));
2626
}
27-
const pair = await importJWK(jwk, "ES256", opts);
28-
if (pair instanceof Uint8Array) {
29-
return Result.Err("Invalid JWK");
27+
const j = parsed.data;
28+
if (j.kty !== "EC" || j.crv !== "P-256" || !("d" in j)) {
29+
return Result.Err(new Error("Invalid JWK for ES256"));
3030
}
31-
return Result.Ok(new DeviceIdKey(pair));
31+
const key = await importJWK(j, "ES256", opts);
32+
return Result.Ok(new DeviceIdKey(key as CryptoKey));
3233
}
3334

3435
private constructor(pair: CryptoKey) {
@@ -51,9 +52,9 @@ export class DeviceIdKey {
5152

5253
async publicKey(): Promise<JWKPublic> {
5354
const privateJWK = await exportJWK(this.#privateKey);
54-
const { success, data } = JWKPublicSchema.safeParse(privateJWK);
55+
const { success, data, error } = JWKPublicSchema.safeParse(privateJWK);
5556
if (!success || !data) {
56-
throw new Error("Invalid JWK");
57+
throw new Error(`Invalid public JWK: ${error.message}`);
5758
}
5859
return data;
5960
}

core/runtime/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,3 +594,12 @@ export function timerStart(loggerOrHasLogger: Logger | HasLogger, tag: string) {
594594
export function timerEnd(loggerOrHasLogger: Logger | HasLogger, tag: string) {
595595
coerceLogger(loggerOrHasLogger).Debug().TimerEnd(tag).Msg("Timing ended");
596596
}
597+
598+
export function deepFreeze<T extends object>(o?: T): T | undefined {
599+
if (!o) return undefined;
600+
Object.freeze(o);
601+
for (const v of Object.values(o)) {
602+
if (v && typeof v === "object" && !Object.isFrozen(v)) deepFreeze(v as object);
603+
}
604+
return o;
605+
}

core/types/base/fp-device-id-payload.zod.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,11 @@ export const FPDeviceIDPayloadSchema = JWTPayloadSchema.extend({
9898
publicKey: JWKPublicSchema,
9999
extensions: ExtensionsSchema.optional(),
100100
})
101+
.strict()
101102
.readonly(),
102-
}).readonly();
103+
})
104+
.strict()
105+
.readonly();
103106

104107
// Type inference
105108
export type FPDeviceIDPayload = z.infer<typeof FPDeviceIDPayloadSchema>;

0 commit comments

Comments
 (0)