Skip to content

Commit da302fb

Browse files
committed
chore: move exports
1 parent 4b6a39d commit da302fb

File tree

4 files changed

+179
-141
lines changed

4 files changed

+179
-141
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { W3ID as W3IDClass, W3IDBuilder } from "w3id";
2+
3+
export class W3ID {
4+
private static instance: W3IDClass;
5+
6+
private constructor() {}
7+
8+
static async get(options?: { id: string }) {
9+
if (W3ID.instance) return W3ID.instance;
10+
if (!options)
11+
throw new Error(
12+
"No instance of W3ID exists yet, please create it by passing options",
13+
);
14+
W3ID.instance = await new W3IDBuilder().build();
15+
}
16+
}

infrastructure/evault-provisioner/src/templates/evault.nomad.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export function generateNomadJob(w3id: string, eVaultId: string) {
7878
NEO4J_USER: neo4jUser,
7979
NEO4J_PASSWORD: neo4jPassword,
8080
PORT: "${NOMAD_PORT_http}",
81+
W3ID: w3id,
8182
},
8283
Resources: {
8384
CPU: 300,

infrastructure/w3id/src/index.ts

Lines changed: 4 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,6 @@
1-
import { v4 as uuidv4 } from "uuid";
2-
import { IDLogManager } from "./logs/log-manager";
3-
import type { JWTHeader, JWTPayload, LogEvent, Signer } from "./logs/log.types";
4-
import type { StorageSpec } from "./logs/storage/storage-spec";
5-
import { signJWT } from "./utils/jwt";
6-
import { generateRandomAlphaNum } from "./utils/rand";
7-
import { generateUuid } from "./utils/uuid";
8-
9-
export class W3ID {
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-
}
30-
}
31-
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;
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-
}
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-
}
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-
}
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-
}
87-
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-
}
98-
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-
}
109-
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-
);
128-
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-
}
140-
}
141-
1+
export * from "./w3id";
1422
export * from "./utils/jwt";
1433
export * from "./logs/storage/storage-spec";
4+
export * from "./logs/log.types";
5+
export * from "./logs/log-manager";
6+
export * from "./utils/hash";

infrastructure/w3id/src/w3id.ts

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import { v4 as uuidv4 } from "uuid";
2+
import { IDLogManager } from "./logs/log-manager";
3+
import type { JWTHeader, JWTPayload, LogEvent, Signer } from "./logs/log.types";
4+
import type { StorageSpec } from "./logs/storage/storage-spec";
5+
import { signJWT } from "./utils/jwt";
6+
import { generateRandomAlphaNum } from "./utils/rand";
7+
import { generateUuid } from "./utils/uuid";
8+
9+
export class W3ID {
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+
}
30+
}
31+
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;
39+
private id?: string;
40+
41+
/**
42+
* Specify entropy to create the identity with
43+
*
44+
* @param {string} str
45+
*/
46+
public withEntropy(str: string): W3IDBuilder {
47+
this.entropy = str;
48+
return this;
49+
}
50+
51+
/**
52+
* Specify namespace to use to generate the UUIDv5
53+
*
54+
* @param {string} uuid
55+
*/
56+
public withNamespace(uuid: string): W3IDBuilder {
57+
this.namespace = uuid;
58+
return this;
59+
}
60+
61+
/**
62+
* Specify whether to create a global identifier or a local identifer
63+
*
64+
* According to the project specification there are supposed to be 2 main types of
65+
* W3ID's ones which are tied to more permanent entities
66+
*
67+
* A global identifer is expected to live at the registry and starts with an \`@\`
68+
*
69+
* @param {boolean} isGlobal
70+
*/
71+
public withGlobal(isGlobal: boolean): W3IDBuilder {
72+
this.global = isGlobal;
73+
return this;
74+
}
75+
76+
/**
77+
* Add a logs repository to the W3ID, a rotateble key attached W3ID would need a
78+
* repository in which the logs would be stored
79+
*
80+
* @param {StorageSpec<LogEvent, LogEvent>} storage
81+
*/
82+
public withRepository(
83+
storage: StorageSpec<LogEvent, LogEvent>,
84+
): W3IDBuilder {
85+
this.repository = storage;
86+
return this;
87+
}
88+
89+
/**
90+
* Pre-specify a UUID to use as the W3ID
91+
*
92+
* @param {string} id
93+
*/
94+
public withId(id: string): W3IDBuilder {
95+
this.id = id;
96+
return this;
97+
}
98+
99+
/**
100+
* Attach a keypair to the W3ID, a key attached W3ID would also need a repository
101+
* to be added.
102+
*
103+
* @param {Signer} signer
104+
*/
105+
public withSigner(signer: Signer): W3IDBuilder {
106+
this.signer = signer;
107+
return this;
108+
}
109+
110+
/**
111+
* Specify the SHA256 hash of the next key which will sign the next log entry after
112+
* rotation of keys
113+
*
114+
* @param {string} hash
115+
*/
116+
public withNextKeyHash(hash: string): W3IDBuilder {
117+
this.nextKeyHash = hash;
118+
return this;
119+
}
120+
121+
/**
122+
* Build the W3ID with provided builder options
123+
*
124+
* @returns Promise<W3ID>
125+
*/
126+
public async build(): Promise<W3ID> {
127+
if ((this.id && this.namespace) || (this.id && this.entropy))
128+
throw new Error(
129+
"Namespace and Entropy can't be specified when using pre-defined ID",
130+
);
131+
this.entropy = this.entropy ?? generateRandomAlphaNum();
132+
this.namespace = this.namespace ?? uuidv4();
133+
this.id =
134+
this.id && this.id?.includes("@") ? this.id.split("@")[1] : this.id;
135+
const id = `${
136+
this.global ? "@" : ""
137+
}${this.id ?? generateUuid(this.entropy, this.namespace)}`;
138+
if (!this.signer) {
139+
return new W3ID(id);
140+
}
141+
if (!this.repository)
142+
throw new Error(
143+
"Repository is required, pass with `withRepository` method",
144+
);
145+
,
146+
if (!this.nextKeyHash)
147+
throw new Error(
148+
"NextKeyHash is required pass with `withNextKeyHash` method",
149+
);
150+
const logs = new IDLogManager(this.repository, this.signer);
151+
await logs.createLogEvent({
152+
id,
153+
nextKeyHashes: [this.nextKeyHash],
154+
});
155+
return new W3ID(id, logs);
156+
}
157+
}
158+

0 commit comments

Comments
 (0)