-
Notifications
You must be signed in to change notification settings - Fork 629
/
client.ts
263 lines (227 loc) · 7.48 KB
/
client.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { Core } from "@walletconnect/core";
import {
generateChildLogger,
getDefaultLoggerOptions,
getLoggerContext,
pino,
} from "@walletconnect/logger";
import { SignClientTypes, ISignClient, ISignClientEvents, EngineTypes } from "@walletconnect/types";
import { getAppMetadata } from "@walletconnect/utils";
import { EventEmitter } from "events";
import { SIGN_CLIENT_DEFAULT, SIGN_CLIENT_PROTOCOL, SIGN_CLIENT_VERSION } from "./constants";
import { AuthStore, Engine, PendingRequest, Proposal, Session } from "./controllers";
export class SignClient extends ISignClient {
public readonly protocol = SIGN_CLIENT_PROTOCOL;
public readonly version = SIGN_CLIENT_VERSION;
public readonly name: ISignClient["name"] = SIGN_CLIENT_DEFAULT.name;
public readonly metadata: ISignClient["metadata"];
public core: ISignClient["core"];
public logger: ISignClient["logger"];
public events: ISignClient["events"] = new EventEmitter();
public engine: ISignClient["engine"];
public session: ISignClient["session"];
public proposal: ISignClient["proposal"];
public pendingRequest: ISignClient["pendingRequest"];
public auth: ISignClient["auth"];
public signConfig?: ISignClient["signConfig"];
static async init(opts?: SignClientTypes.Options) {
const client = new SignClient(opts);
await client.initialize();
return client;
}
constructor(opts?: SignClientTypes.Options) {
super(opts);
this.name = opts?.name || SIGN_CLIENT_DEFAULT.name;
this.metadata = opts?.metadata || getAppMetadata();
this.signConfig = opts?.signConfig;
const logger =
typeof opts?.logger !== "undefined" && typeof opts?.logger !== "string"
? opts.logger
: pino(getDefaultLoggerOptions({ level: opts?.logger || SIGN_CLIENT_DEFAULT.logger }));
this.core = opts?.core || new Core(opts);
this.logger = generateChildLogger(logger, this.name);
this.session = new Session(this.core, this.logger);
this.proposal = new Proposal(this.core, this.logger);
this.pendingRequest = new PendingRequest(this.core, this.logger);
this.engine = new Engine(this);
this.auth = new AuthStore(this.core, this.logger);
}
get context() {
return getLoggerContext(this.logger);
}
get pairing() {
return this.core.pairing.pairings;
}
// ---------- Events ----------------------------------------------- //
public on: ISignClientEvents["on"] = (name, listener) => {
return this.events.on(name, listener);
};
public once: ISignClientEvents["once"] = (name, listener) => {
return this.events.once(name, listener);
};
public off: ISignClientEvents["off"] = (name, listener) => {
return this.events.off(name, listener);
};
public removeListener: ISignClientEvents["removeListener"] = (name, listener) => {
return this.events.removeListener(name, listener);
};
public removeAllListeners: ISignClientEvents["removeAllListeners"] = (name) => {
return this.events.removeAllListeners(name);
};
// ---------- Engine ----------------------------------------------- //
public connect: ISignClient["connect"] = async (params) => {
try {
return await this.engine.connect(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public pair: ISignClient["pair"] = async (params) => {
try {
return await this.engine.pair(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public approve: ISignClient["approve"] = async (params) => {
try {
return await this.engine.approve(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public reject: ISignClient["reject"] = async (params) => {
try {
return await this.engine.reject(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public update: ISignClient["update"] = async (params) => {
try {
return await this.engine.update(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public extend: ISignClient["extend"] = async (params) => {
try {
return await this.engine.extend(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public request: ISignClient["request"] = async <T>(params: EngineTypes.RequestParams) => {
try {
return await this.engine.request<T>(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public respond: ISignClient["respond"] = async (params) => {
try {
return await this.engine.respond(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public ping: ISignClient["ping"] = async (params) => {
try {
return await this.engine.ping(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public emit: ISignClient["emit"] = async (params) => {
try {
return await this.engine.emit(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public disconnect: ISignClient["disconnect"] = async (params) => {
try {
return await this.engine.disconnect(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public find: ISignClient["find"] = (params) => {
try {
return this.engine.find(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public getPendingSessionRequests: ISignClient["getPendingSessionRequests"] = () => {
try {
return this.engine.getPendingSessionRequests();
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public authenticate: ISignClient["authenticate"] = async (params) => {
try {
return await this.engine.authenticate(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public formatAuthMessage: ISignClient["formatAuthMessage"] = (params) => {
try {
return this.engine.formatAuthMessage(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public approveSessionAuthenticate: ISignClient["approveSessionAuthenticate"] = async (params) => {
try {
return await this.engine.approveSessionAuthenticate(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
public rejectSessionAuthenticate: ISignClient["rejectSessionAuthenticate"] = async (params) => {
try {
return await this.engine.rejectSessionAuthenticate(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};
// ---------- Private ----------------------------------------------- //
private async initialize() {
this.logger.trace(`Initialized`);
try {
await this.core.start();
await this.session.init();
await this.proposal.init();
await this.pendingRequest.init();
await this.engine.init();
await this.auth.init();
this.core.verify.init({ verifyUrl: this.metadata.verifyUrl });
this.logger.info(`SignClient Initialization Success`);
this.engine.processRelayMessageCache();
} catch (error: any) {
this.logger.info(`SignClient Initialization Failure`);
this.logger.error(error.message);
throw error;
}
}
}