Skip to content

Commit b1ccbb5

Browse files
committed
chore: format
1 parent 35dee92 commit b1ccbb5

File tree

4 files changed

+192
-194
lines changed

4 files changed

+192
-194
lines changed

infrastructure/w3id/src/index.ts

Lines changed: 119 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,140 @@
1+
import { v4 as uuidv4 } from "uuid";
12
import { IDLogManager } from "./logs/log-manager";
2-
import type { LogEvent, Signer, JWTPayload, JWTHeader } from "./logs/log.types";
3+
import type { JWTHeader, JWTPayload, LogEvent, Signer } from "./logs/log.types";
34
import type { StorageSpec } from "./logs/storage/storage-spec";
5+
import { signJWT } from "./utils/jwt";
46
import { generateRandomAlphaNum } from "./utils/rand";
5-
import { v4 as uuidv4 } from "uuid";
67
import { generateUuid } from "./utils/uuid";
7-
import { signJWT } from "./utils/jwt";
88

99
export class W3ID {
10-
constructor(
11-
public id: string,
12-
public logs?: IDLogManager,
13-
) {}
10+
constructor(
11+
public id: string,
12+
public logs?: IDLogManager,
13+
) {}
1414

15-
/**
16-
* Signs a JWT with the W3ID's signer
17-
* @param payload - The JWT payload
18-
* @param header - Optional JWT header (defaults to using the signer's alg and W3ID's id as kid)
19-
* @returns The signed JWT
20-
*/
21-
public async signJWT(
22-
payload: JWTPayload,
23-
header?: JWTHeader,
24-
): Promise<string> {
25-
if (!this.logs?.signer) {
26-
throw new Error("W3ID must have a signer to sign JWTs");
27-
}
28-
return signJWT(this.logs.signer, payload, `@${this.id}#0`, header);
29-
}
15+
/**
16+
* Signs a JWT with the W3ID's signer
17+
* @param payload - The JWT payload
18+
* @param header - Optional JWT header (defaults to using the signer's alg and W3ID's id as kid)
19+
* @returns The signed JWT
20+
*/
21+
public async signJWT(
22+
payload: JWTPayload,
23+
header?: JWTHeader,
24+
): Promise<string> {
25+
if (!this.logs?.signer) {
26+
throw new Error("W3ID must have a signer to sign JWTs");
27+
}
28+
return signJWT(this.logs.signer, payload, `@${this.id}#0`, header);
29+
}
3030
}
3131

3232
export class W3IDBuilder {
33-
private signer?: Signer;
34-
private repository?: StorageSpec<LogEvent, LogEvent>;
35-
private entropy?: string;
36-
private namespace?: string;
37-
private nextKeyHash?: string;
38-
private global?: boolean = false;
33+
private signer?: Signer;
34+
private repository?: StorageSpec<LogEvent, LogEvent>;
35+
private entropy?: string;
36+
private namespace?: string;
37+
private nextKeyHash?: string;
38+
private global?: boolean = false;
3939

40-
/**
41-
* Specify entropy to create the identity with
42-
*
43-
* @param {string} str
44-
*/
45-
public withEntropy(str: string): W3IDBuilder {
46-
this.entropy = str;
47-
return this;
48-
}
40+
/**
41+
* Specify entropy to create the identity with
42+
*
43+
* @param {string} str
44+
*/
45+
public withEntropy(str: string): W3IDBuilder {
46+
this.entropy = str;
47+
return this;
48+
}
4949

50-
/**
51-
* Specify namespace to use to generate the UUIDv5
52-
*
53-
* @param {string} uuid
54-
*/
55-
public withNamespace(uuid: string): W3IDBuilder {
56-
this.namespace = uuid;
57-
return this;
58-
}
50+
/**
51+
* Specify namespace to use to generate the UUIDv5
52+
*
53+
* @param {string} uuid
54+
*/
55+
public withNamespace(uuid: string): W3IDBuilder {
56+
this.namespace = uuid;
57+
return this;
58+
}
5959

60-
/**
61-
* Specify whether to create a global identifier or a local identifer
62-
*
63-
* According to the project specification there are supposed to be 2 main types of
64-
* W3ID's ones which are tied to more permanent entities
65-
*
66-
* A global identifer is expected to live at the registry and starts with an \`@\`
67-
*
68-
* @param {boolean} isGlobal
69-
*/
70-
public withGlobal(isGlobal: boolean): W3IDBuilder {
71-
this.global = isGlobal;
72-
return this;
73-
}
60+
/**
61+
* Specify whether to create a global identifier or a local identifer
62+
*
63+
* According to the project specification there are supposed to be 2 main types of
64+
* W3ID's ones which are tied to more permanent entities
65+
*
66+
* A global identifer is expected to live at the registry and starts with an \`@\`
67+
*
68+
* @param {boolean} isGlobal
69+
*/
70+
public withGlobal(isGlobal: boolean): W3IDBuilder {
71+
this.global = isGlobal;
72+
return this;
73+
}
7474

75-
/**
76-
* Add a logs repository to the W3ID, a rotateble key attached W3ID would need a
77-
* repository in which the logs would be stored
78-
*
79-
* @param {StorageSpec<LogEvent, LogEvent>} storage
80-
*/
81-
public withRepository(
82-
storage: StorageSpec<LogEvent, LogEvent>,
83-
): W3IDBuilder {
84-
this.repository = storage;
85-
return this;
86-
}
75+
/**
76+
* Add a logs repository to the W3ID, a rotateble key attached W3ID would need a
77+
* repository in which the logs would be stored
78+
*
79+
* @param {StorageSpec<LogEvent, LogEvent>} storage
80+
*/
81+
public withRepository(storage: StorageSpec<LogEvent, LogEvent>): W3IDBuilder {
82+
this.repository = storage;
83+
return this;
84+
}
8785

88-
/**
89-
* Attach a keypair to the W3ID, a key attached W3ID would also need a repository
90-
* to be added.
91-
*
92-
* @param {Signer} signer
93-
*/
94-
public withSigner(signer: Signer): W3IDBuilder {
95-
this.signer = signer;
96-
return this;
97-
}
86+
/**
87+
* Attach a keypair to the W3ID, a key attached W3ID would also need a repository
88+
* to be added.
89+
*
90+
* @param {Signer} signer
91+
*/
92+
public withSigner(signer: Signer): W3IDBuilder {
93+
this.signer = signer;
94+
return this;
95+
}
9896

99-
/**
100-
* Specify the SHA256 hash of the next key which will sign the next log entry after
101-
* rotation of keys
102-
*
103-
* @param {string} hash
104-
*/
105-
public withNextKeyHash(hash: string): W3IDBuilder {
106-
this.nextKeyHash = hash;
107-
return this;
108-
}
97+
/**
98+
* Specify the SHA256 hash of the next key which will sign the next log entry after
99+
* rotation of keys
100+
*
101+
* @param {string} hash
102+
*/
103+
public withNextKeyHash(hash: string): W3IDBuilder {
104+
this.nextKeyHash = hash;
105+
return this;
106+
}
109107

110-
/**
111-
* Build the W3ID with provided builder options
112-
*
113-
* @returns Promise<W3ID>
114-
*/
115-
public async build(): Promise<W3ID> {
116-
this.entropy = this.entropy ?? generateRandomAlphaNum();
117-
this.namespace = this.namespace ?? uuidv4();
118-
const id = `${
119-
this.global ? "@" : ""
120-
}${generateUuid(this.entropy, this.namespace)}`;
121-
if (!this.signer) {
122-
return new W3ID(id);
123-
}
124-
if (!this.repository)
125-
throw new Error(
126-
"Repository is required, pass with `withRepository` method",
127-
);
108+
/**
109+
* Build the W3ID with provided builder options
110+
*
111+
* @returns Promise<W3ID>
112+
*/
113+
public async build(): Promise<W3ID> {
114+
this.entropy = this.entropy ?? generateRandomAlphaNum();
115+
this.namespace = this.namespace ?? uuidv4();
116+
const id = `${
117+
this.global ? "@" : ""
118+
}${generateUuid(this.entropy, this.namespace)}`;
119+
if (!this.signer) {
120+
return new W3ID(id);
121+
}
122+
if (!this.repository)
123+
throw new Error(
124+
"Repository is required, pass with `withRepository` method",
125+
);
128126

129-
if (!this.nextKeyHash)
130-
throw new Error(
131-
"NextKeyHash is required pass with `withNextKeyHash` method",
132-
);
133-
const logs = new IDLogManager(this.repository, this.signer);
134-
await logs.createLogEvent({
135-
id,
136-
nextKeyHashes: [this.nextKeyHash],
137-
});
138-
return new W3ID(id, logs);
139-
}
127+
if (!this.nextKeyHash)
128+
throw new Error(
129+
"NextKeyHash is required pass with `withNextKeyHash` method",
130+
);
131+
const logs = new IDLogManager(this.repository, this.signer);
132+
await logs.createLogEvent({
133+
id,
134+
nextKeyHashes: [this.nextKeyHash],
135+
});
136+
return new W3ID(id, logs);
137+
}
140138
}
141139

142140
export * from "./utils/jwt";

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import {
99
import { isSubsetOf } from "../utils/array";
1010
import { hash } from "../utils/hash";
1111
import {
12-
isGenesisOptions,
13-
isRotationOptions,
14-
type Signer,
1512
type CreateLogEventOptions,
1613
type GenesisLogOptions,
1714
type LogEvent,
1815
type RotationLogOptions,
16+
type Signer,
1917
type VerifierCallback,
18+
isGenesisOptions,
19+
isRotationOptions,
2020
} from "./log.types";
2121
import type { StorageSpec } from "./storage/storage-spec";
2222

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
11
export type LogEvent = {
2-
id: string;
3-
versionId: string;
4-
versionTime: Date;
5-
updateKeys: string[];
6-
nextKeyHashes: string[];
7-
method: `w3id:v${string}`;
8-
proof?: string;
2+
id: string;
3+
versionId: string;
4+
versionTime: Date;
5+
updateKeys: string[];
6+
nextKeyHashes: string[];
7+
method: `w3id:v${string}`;
8+
proof?: string;
99
};
1010

1111
export type VerifierCallback = (
12-
message: string,
13-
signature: string,
14-
pubKey: string,
12+
message: string,
13+
signature: string,
14+
pubKey: string,
1515
) => Promise<boolean>;
1616

1717
export type JWTHeader = {
18-
alg: string;
19-
typ: "JWT";
20-
kid?: string;
18+
alg: string;
19+
typ: "JWT";
20+
kid?: string;
2121
};
2222

2323
export type JWTPayload = {
24-
[key: string]: any;
25-
iat?: number;
26-
exp?: number;
27-
nbf?: number;
28-
iss?: string;
29-
sub?: string;
30-
aud?: string;
31-
jti?: string;
24+
[key: string]: unknown;
25+
iat?: number;
26+
exp?: number;
27+
nbf?: number;
28+
iss?: string;
29+
sub?: string;
30+
aud?: string;
31+
jti?: string;
3232
};
3333

3434
export type Signer = {
35-
sign: (message: string) => Promise<string> | string;
36-
pubKey: string;
37-
alg: string;
35+
sign: (message: string) => Promise<string> | string;
36+
pubKey: string;
37+
alg: string;
3838
};
3939

4040
export type RotationLogOptions = {
41-
nextKeyHashes: string[];
42-
nextKeySigner: Signer;
41+
nextKeyHashes: string[];
42+
nextKeySigner: Signer;
4343
};
4444

4545
export type GenesisLogOptions = {
46-
nextKeyHashes: string[];
47-
id: string;
46+
nextKeyHashes: string[];
47+
id: string;
4848
};
4949

5050
export function isGenesisOptions(
51-
options: CreateLogEventOptions,
51+
options: CreateLogEventOptions,
5252
): options is GenesisLogOptions {
53-
return "id" in options;
53+
return "id" in options;
5454
}
5555
export function isRotationOptions(
56-
options: CreateLogEventOptions,
56+
options: CreateLogEventOptions,
5757
): options is RotationLogOptions {
58-
return "nextKeySigner" in options;
58+
return "nextKeySigner" in options;
5959
}
6060

6161
export type CreateLogEventOptions = GenesisLogOptions | RotationLogOptions;

0 commit comments

Comments
 (0)