|
1 |
| -/** |
2 |
| - * According to the project specification there are supposed to be 2 main types of |
3 |
| - * W3ID's ones which are tied to more tangible entities and hence need rotation |
4 |
| - * and others which are just UUIDs. Hence the approach to do this in a builder pattern |
5 |
| - */ |
6 |
| - |
7 | 1 | import { IDLogManager } from "./logs/log-manager";
|
8 |
| -import { LogEvent, Signer } from "./logs/log.types"; |
9 |
| -import { StorageSpec } from "./logs/storage/storage-spec"; |
| 2 | +import type { LogEvent, Signer } from "./logs/log.types"; |
| 3 | +import type { StorageSpec } from "./logs/storage/storage-spec"; |
10 | 4 | import { generateRandomAlphaNum } from "./utils/rand";
|
11 | 5 | import { v4 as uuidv4 } from "uuid";
|
12 | 6 | import { generateUuid } from "./utils/uuid";
|
13 | 7 |
|
14 | 8 | export class W3ID {
|
15 |
| - constructor( |
16 |
| - public id: string, |
17 |
| - public logs?: IDLogManager, |
18 |
| - ) {} |
| 9 | + constructor( |
| 10 | + public id: string, |
| 11 | + public logs?: IDLogManager, |
| 12 | + ) {} |
19 | 13 | }
|
20 | 14 |
|
21 | 15 | export class W3IDBuilder {
|
22 |
| - private signer?: Signer; |
23 |
| - private repository?: StorageSpec<LogEvent, LogEvent>; |
24 |
| - private entropy?: string; |
25 |
| - private namespace?: string; |
26 |
| - private nextKeyHash?: string; |
27 |
| - private global?: boolean = false; |
| 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; |
28 | 22 |
|
29 |
| - public withEntropy(str: string): W3IDBuilder { |
30 |
| - this.entropy = str; |
31 |
| - return this; |
32 |
| - } |
| 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 | + } |
33 | 32 |
|
34 |
| - public withGlobal(isGlobal: boolean): W3IDBuilder { |
35 |
| - this.global = isGlobal; |
36 |
| - return this; |
37 |
| - } |
| 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 | + } |
38 | 42 |
|
39 |
| - public withNamespace(uuid: string): W3IDBuilder { |
40 |
| - this.namespace = uuid; |
41 |
| - return this; |
42 |
| - } |
| 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 | + } |
43 | 57 |
|
44 |
| - public withRepository( |
45 |
| - storage: StorageSpec<LogEvent, LogEvent>, |
46 |
| - ): W3IDBuilder { |
47 |
| - this.repository = storage; |
48 |
| - return this; |
49 |
| - } |
| 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 | + } |
50 | 68 |
|
51 |
| - public withSigner(signer: Signer): W3IDBuilder { |
52 |
| - this.signer = signer; |
53 |
| - return this; |
54 |
| - } |
| 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 | + } |
55 | 79 |
|
56 |
| - public withNextKeyHash(hash: string): W3IDBuilder { |
57 |
| - this.nextKeyHash = hash; |
58 |
| - return this; |
59 |
| - } |
| 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 | + } |
60 | 90 |
|
61 |
| - public async build(): Promise<W3ID> { |
62 |
| - this.entropy = this.entropy ?? generateRandomAlphaNum(); |
63 |
| - this.namespace = this.namespace ?? uuidv4(); |
64 |
| - const id = this.global |
65 |
| - ? "@" |
66 |
| - : "" + generateUuid(this.entropy, this.namespace); |
67 |
| - if (!this.signer) { |
68 |
| - return new W3ID(id); |
69 |
| - } else { |
70 |
| - if (!this.repository) |
71 |
| - throw new Error( |
72 |
| - "Repository is required, pass with \`withRepository\` method", |
73 |
| - ); |
| 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 | + ); |
74 | 109 |
|
75 |
| - if (!this.nextKeyHash) |
76 |
| - throw new Error( |
77 |
| - "NextKeyHash is required pass with \`withNextKeyHash\` method", |
78 |
| - ); |
79 |
| - const logs = new IDLogManager(this.repository, this.signer); |
80 |
| - await logs.createLogEvent({ |
81 |
| - id, |
82 |
| - nextKeyHashes: [this.nextKeyHash], |
83 |
| - }); |
84 |
| - return new W3ID(id, logs); |
85 |
| - } |
86 |
| - } |
| 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 | + } |
87 | 121 | }
|
0 commit comments