|
1 | 1 | import { IDLogManager } from "./logs/log-manager";
|
2 |
| -import type { LogEvent, Signer } from "./logs/log.types"; |
| 2 | +import type { LogEvent, Signer, JWTPayload, JWTHeader } from "./logs/log.types"; |
3 | 3 | import type { StorageSpec } from "./logs/storage/storage-spec";
|
4 | 4 | import { generateRandomAlphaNum } from "./utils/rand";
|
5 | 5 | import { v4 as uuidv4 } from "uuid";
|
6 | 6 | import { generateUuid } from "./utils/uuid";
|
| 7 | +import { signJWT } from "./utils/jwt"; |
7 | 8 |
|
8 | 9 | export class W3ID {
|
9 |
| - constructor( |
10 |
| - public id: string, |
11 |
| - public logs?: IDLogManager, |
12 |
| - ) {} |
| 10 | + constructor( |
| 11 | + public id: string, |
| 12 | + public logs?: IDLogManager, |
| 13 | + ) {} |
| 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 | + } |
13 | 30 | }
|
14 | 31 |
|
15 | 32 | export class W3IDBuilder {
|
16 |
| - private signer?: Signer; |
17 |
| - private repository?: StorageSpec<LogEvent, LogEvent>; |
18 |
| - private entropy?: string; |
19 |
| - private namespace?: string; |
20 |
| - private nextKeyHash?: string; |
21 |
| - 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; |
22 | 39 |
|
23 |
| - /** |
24 |
| - * Specify entropy to create the identity with |
25 |
| - * |
26 |
| - * @param {string} str |
27 |
| - */ |
28 |
| - public withEntropy(str: string): W3IDBuilder { |
29 |
| - this.entropy = str; |
30 |
| - return this; |
31 |
| - } |
| 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 | + } |
32 | 49 |
|
33 |
| - /** |
34 |
| - * Specify namespace to use to generate the UUIDv5 |
35 |
| - * |
36 |
| - * @param {string} uuid |
37 |
| - */ |
38 |
| - public withNamespace(uuid: string): W3IDBuilder { |
39 |
| - this.namespace = uuid; |
40 |
| - return this; |
41 |
| - } |
| 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 | + } |
42 | 59 |
|
43 |
| - /** |
44 |
| - * Specify whether to create a global identifier or a local identifer |
45 |
| - * |
46 |
| - * According to the project specification there are supposed to be 2 main types of |
47 |
| - * W3ID's ones which are tied to more permanent entities |
48 |
| - * |
49 |
| - * A global identifer is expected to live at the registry and starts with an \`@\` |
50 |
| - * |
51 |
| - * @param {boolean} isGlobal |
52 |
| - */ |
53 |
| - public withGlobal(isGlobal: boolean): W3IDBuilder { |
54 |
| - this.global = isGlobal; |
55 |
| - return this; |
56 |
| - } |
| 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 | + } |
57 | 74 |
|
58 |
| - /** |
59 |
| - * Add a logs repository to the W3ID, a rotateble key attached W3ID would need a |
60 |
| - * repository in which the logs would be stored |
61 |
| - * |
62 |
| - * @param {StorageSpec<LogEvent, LogEvent>} storage |
63 |
| - */ |
64 |
| - public withRepository(storage: StorageSpec<LogEvent, LogEvent>): W3IDBuilder { |
65 |
| - this.repository = storage; |
66 |
| - return this; |
67 |
| - } |
| 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 | + } |
68 | 87 |
|
69 |
| - /** |
70 |
| - * Attach a keypair to the W3ID, a key attached W3ID would also need a repository |
71 |
| - * to be added. |
72 |
| - * |
73 |
| - * @param {Signer} signer |
74 |
| - */ |
75 |
| - public withSigner(signer: Signer): W3IDBuilder { |
76 |
| - this.signer = signer; |
77 |
| - return this; |
78 |
| - } |
| 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 | + } |
79 | 98 |
|
80 |
| - /** |
81 |
| - * Specify the SHA256 hash of the next key which will sign the next log entry after |
82 |
| - * rotation of keys |
83 |
| - * |
84 |
| - * @param {string} hash |
85 |
| - */ |
86 |
| - public withNextKeyHash(hash: string): W3IDBuilder { |
87 |
| - this.nextKeyHash = hash; |
88 |
| - return this; |
89 |
| - } |
| 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 | + } |
90 | 109 |
|
91 |
| - /** |
92 |
| - * Build the W3ID with provided builder options |
93 |
| - * |
94 |
| - * @returns Promise<W3ID> |
95 |
| - */ |
96 |
| - public async build(): Promise<W3ID> { |
97 |
| - this.entropy = this.entropy ?? generateRandomAlphaNum(); |
98 |
| - this.namespace = this.namespace ?? uuidv4(); |
99 |
| - const id = `${ |
100 |
| - this.global ? "@" : "" |
101 |
| - }${generateUuid(this.entropy, this.namespace)}`; |
102 |
| - if (!this.signer) { |
103 |
| - return new W3ID(id); |
104 |
| - } |
105 |
| - if (!this.repository) |
106 |
| - throw new Error( |
107 |
| - "Repository is required, pass with `withRepository` method", |
108 |
| - ); |
| 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 | + ); |
109 | 128 |
|
110 |
| - if (!this.nextKeyHash) |
111 |
| - throw new Error( |
112 |
| - "NextKeyHash is required pass with `withNextKeyHash` method", |
113 |
| - ); |
114 |
| - const logs = new IDLogManager(this.repository, this.signer); |
115 |
| - await logs.createLogEvent({ |
116 |
| - id, |
117 |
| - nextKeyHashes: [this.nextKeyHash], |
118 |
| - }); |
119 |
| - return new W3ID(id, logs); |
120 |
| - } |
| 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 | + } |
121 | 140 | }
|
0 commit comments