-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsetup.ts
More file actions
157 lines (136 loc) · 4.58 KB
/
setup.ts
File metadata and controls
157 lines (136 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { EllipticPoint, secp256k1 } from "@tkey/common-types";
import { tssLib } from "@toruslabs/tss-dkls-lib";
import BN from "bn.js";
import jwt, { Algorithm } from "jsonwebtoken";
import { tssLib as tssLibDKLS } from "@toruslabs/tss-dkls-lib";
import { IAsyncStorage, IStorage, parseToken, TssLibType, WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit, Web3AuthOptions } from "../src";
export const mockLogin2 = async (email: string) => {
const req = new Request("https://li6lnimoyrwgn2iuqtgdwlrwvq0upwtr.lambda-url.eu-west-1.on.aws/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ verifier: "torus-key-test", scope: "email", extraPayload: { email }, alg: "ES256" }),
});
const resp = await fetch(req);
const bodyJson = (await resp.json()) as { token: string };
const idToken = bodyJson.token;
const parsedToken = parseToken(idToken);
return { idToken, parsedToken };
};
export const criticalResetAccount = async (coreKitInstance: Web3AuthMPCCoreKit): Promise<void> => {
// This is a critical function that should only be used for testing purposes
// Resetting your account means clearing all the metadata associated with it from the metadata server
// The key details will be deleted from our server and you will not be able to recover your account
if (!coreKitInstance) {
throw new Error("coreKitInstance is not set");
}
if (coreKitInstance.tKey.secp256k1Key) {
await coreKitInstance.tKey.CRITICAL_deleteTkey();
} else {
await coreKitInstance.tKey.storageLayer.setMetadata({
privKey: new BN(coreKitInstance.state.postBoxKey!, "hex"),
input: { message: "KEY_NOT_FOUND" },
});
}
};
const privateKey = "MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCCD7oLrcKae+jVZPGx52Cb/lKhdKxpXjl9eGNa1MlY57A==";
const jwtPrivateKey = `-----BEGIN PRIVATE KEY-----\n${privateKey}\n-----END PRIVATE KEY-----`;
const alg: Algorithm = "ES256";
export function stringGen(len: number) {
let text = "";
const charset = "abcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < len; i++) {
text += charset.charAt(Math.floor(Math.random() * charset.length));
}
return text;
}
export const mockLogin = async (email?: string) => {
// if email is not passed generate a random email
if (!email) {
email = `${stringGen(10)}@${stringGen(5)}.${stringGen(3)}`;
}
const iat = Math.floor(Date.now() / 1000);
const payload = {
iss: "torus-key-test",
aud: "torus-key-test",
name: email,
email,
scope: "email",
iat,
eat: iat + 120,
};
const algo = {
expiresIn: 120,
algorithm: alg,
};
const token = jwt.sign(payload, jwtPrivateKey, algo);
const idToken = token;
const parsedToken = parseToken(idToken);
return { idToken, parsedToken };
};
export type LoginFunc = (email: string) => Promise<{ idToken: string, parsedToken: any }>;
export const defaultTestOptions = (params: {
network: WEB3AUTH_NETWORK_TYPE;
manualSync: boolean;
storageInstance: IStorage | IAsyncStorage;
tssLib?: TssLibType;
}) : Web3AuthOptions => {
const { network, manualSync, storageInstance, tssLib } = params;
return {
web3AuthClientId: "torus-key-test",
web3AuthNetwork: network,
baseUrl: "http://localhost:3000",
uxMode: "nodejs",
tssLib: tssLib || tssLibDKLS,
storage: storageInstance,
manualSync,
}
}
export const newCoreKitLogInInstance = async ({
network,
manualSync,
email,
storageInstance,
importTssKey,
login,
}: {
network: WEB3AUTH_NETWORK_TYPE;
manualSync: boolean;
email: string;
storageInstance: IStorage | IAsyncStorage;
tssLib?: TssLibType;
importTssKey?: string;
login?: LoginFunc;
}) => {
const instance = new Web3AuthMPCCoreKit({
web3AuthClientId: "torus-key-test",
web3AuthNetwork: network,
baseUrl: "http://localhost:3000",
uxMode: "nodejs",
tssLib: tssLib || tssLibDKLS,
storage: storageInstance,
manualSync,
});
const { idToken, parsedToken } = login ? await login(email) : await mockLogin(email);
await instance.init();
await instance.loginWithJWT({
verifier: "torus-test-health",
verifierId: parsedToken.email,
idToken,
importTssKey,
});
return instance;
};
export class AsyncMemoryStorage implements IAsyncStorage {
private _store: Record<string, string> = {};
async getItem(key: string): Promise<string | null> {
return this._store[key] || null;
}
async setItem(key: string, value: string): Promise<void> {
this._store[key] = value;
}
}
export function bufferToElliptic(p: Buffer, ec = secp256k1): EllipticPoint {
return ec.keyFromPublic(p).getPublic();
}