Skip to content

Commit 11453fe

Browse files
committed
chore: add new error for bad options
1 parent bdf3bd1 commit 11453fe

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

infrastructure/w3id/src/errors/errors.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export class MalformedHashChainError extends Error {}
55
export class BadSignatureError extends Error {}
66

77
export class BadNextKeySpecifiedError extends Error {}
8+
9+
export class BadOptionsSpecifiedError extends Error {}

infrastructure/w3id/src/logs/log-manager.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import canonicalize from "canonicalize";
22
import {
33
BadNextKeySpecifiedError,
4+
BadOptionsSpecifiedError,
45
BadSignatureError,
56
MalformedHashChainError,
67
MalformedIndexChainError,
78
} from "../errors/errors";
89
import { isSubsetOf } from "../utils/array";
910
import { hash } from "../utils/hash";
1011
import {
12+
isGenesisOptions,
13+
isRotationOptions,
1114
type CreateLogEventOptions,
1215
type GenesisLogOptions,
1316
type LogEvent,
@@ -135,8 +138,11 @@ export class IDLogManager {
135138

136139
async createLogEvent(options: CreateLogEventOptions) {
137140
const entries = await this.repository.findMany({});
138-
if (entries.length > 0)
139-
return this.appendEntry(entries, options as RotationLogOptions);
140-
return this.createGenesisEntry(options as GenesisLogOptions);
141+
if (entries.length > 0) {
142+
if (!isRotationOptions(options)) throw new BadOptionsSpecifiedError();
143+
return this.appendEntry(entries, options);
144+
}
145+
if (!isGenesisOptions(options)) throw new BadOptionsSpecifiedError();
146+
return this.createGenesisEntry(options);
141147
}
142148
}

infrastructure/w3id/src/logs/log.types.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export type VerifierCallback = (
1414
pubKey: string,
1515
) => Promise<boolean>;
1616

17+
export type Signer = {
18+
sign: (string: string) => Promise<string> | string;
19+
pubKey: string;
20+
};
21+
1722
export type RotationLogOptions = {
1823
nextKeyHashes: string[];
1924
signer: Signer;
@@ -26,9 +31,15 @@ export type GenesisLogOptions = {
2631
signer: Signer;
2732
};
2833

29-
export type Signer = {
30-
sign: (string: string) => Promise<string> | string;
31-
pubKey: string;
32-
};
34+
export function isGenesisOptions(
35+
options: CreateLogEventOptions,
36+
): options is GenesisLogOptions {
37+
return "id" in options;
38+
}
39+
export function isRotationOptions(
40+
options: CreateLogEventOptions,
41+
): options is RotationLogOptions {
42+
return "nextKeySigner" in options;
43+
}
3344

3445
export type CreateLogEventOptions = GenesisLogOptions | RotationLogOptions;

0 commit comments

Comments
 (0)