fix(identity): catching sig verification errors

This commit is contained in:
Shibo Lyu 2024-09-30 16:17:19 +08:00
parent 5761b308a4
commit c6378ad797
2 changed files with 35 additions and 4 deletions

View file

@ -3,8 +3,8 @@ import { BlahKeyPair } from "../crypto/mod.ts";
import { BlahIdentity } from "./identity.ts";
import type { BlahIdentityFile, BlahProfile } from "./mod.ts";
const idKeyPair = await BlahKeyPair.generate();
const actKeyPair = await BlahKeyPair.generate();
let idKeyPair: BlahKeyPair;
let actKeyPair: BlahKeyPair;
const profile: BlahProfile = {
typ: "profile",
name: "Shibo Lyu",
@ -17,6 +17,8 @@ let identityFile: BlahIdentityFile;
let identityFromFile: BlahIdentity;
Deno.test("create identity", async () => {
idKeyPair = await BlahKeyPair.generate();
actKeyPair = await BlahKeyPair.generate();
identity = await BlahIdentity.create(idKeyPair, actKeyPair, profile);
});
@ -48,6 +50,35 @@ Deno.test("parse identity file", async () => {
identityFromFile = await BlahIdentity.fromIdentityFile(identityFile);
});
Deno.test("identity file profile sigs are properly verfied", async () => {
const identityFileWithProfileInvalidProfileSig: BlahIdentityFile = {
...identityFile,
profile: { ...identityFile.profile, sig: "_ obviously not a valid sig _" },
};
const identityWithProfileInvalidProfileSig = await BlahIdentity
.fromIdentityFile(
identityFileWithProfileInvalidProfileSig,
);
expect(identityWithProfileInvalidProfileSig.profileSigValid).toBe(false);
});
Deno.test("identity file act key sigs are properly verfied", async () => {
const identityFileWithActKeyInvalidActKeySig: BlahIdentityFile = {
...identityFile,
act_keys: [
{
...identityFile.act_keys[0],
sig: "_ obviously not a valid sig _",
},
],
};
const identityWithActKeyInvalidActKeySig = await BlahIdentity
.fromIdentityFile(
identityFileWithActKeyInvalidActKeySig,
);
expect(identityWithActKeyInvalidActKeySig.actKeys[0].sigValid).toBe(false);
});
Deno.test("add a second act key", async () => {
const actKeyPair2 = await BlahKeyPair.generate();
await identity.addActKey(actKeyPair2, { comment: "test" });

View file

@ -31,7 +31,7 @@ async function constructActKeyFromRaw(
const publicKey = idKey instanceof BlahKeyPair ? idKey.publicKey : idKey;
let sigValid = false;
try {
publicKey.verifyPayload(raw);
await publicKey.verifyPayload(raw);
sigValid = true;
} catch {
sigValid = false;
@ -151,7 +151,7 @@ export class BlahIdentity {
}
let profileSigValid = false;
try {
profileSigningKey.verifyPayload(rawProfile);
await profileSigningKey.verifyPayload(rawProfile);
profileSigValid = true;
} catch {
profileSigValid = false;