|
| 1 | +import { v4 as uuidv4 } from "uuid"; |
1 | 2 | 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"; |
3 | 4 | import type { StorageSpec } from "./logs/storage/storage-spec";
|
| 5 | +import { signJWT } from "./utils/jwt"; |
4 | 6 | import { generateRandomAlphaNum } from "./utils/rand";
|
5 |
| -import { v4 as uuidv4 } from "uuid"; |
6 | 7 | import { generateUuid } from "./utils/uuid";
|
7 |
| -import { signJWT } from "./utils/jwt"; |
8 | 8 |
|
9 | 9 | 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 | + ) {} |
14 | 14 |
|
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 | + } |
30 | 30 | }
|
31 | 31 |
|
32 | 32 | 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; |
39 | 39 |
|
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 | + } |
49 | 49 |
|
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 | + } |
59 | 59 |
|
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 | + } |
74 | 74 |
|
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 | + } |
87 | 85 |
|
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 | + } |
98 | 96 |
|
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 | + } |
109 | 107 |
|
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 | + ); |
128 | 126 |
|
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 | + } |
140 | 138 | }
|
141 | 139 |
|
142 | 140 | export * from "./utils/jwt";
|
0 commit comments