@portal-hq/web 3.2.3 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1222 @@
1
+ "use strict";
2
+ /**
3
+ * @jest-environment jsdom
4
+ */
5
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
+ return new (P || (P = Promise))(function (resolve, reject) {
8
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
12
+ });
13
+ };
14
+ var __importDefault = (this && this.__importDefault) || function (mod) {
15
+ return (mod && mod.__esModule) ? mod : { "default": mod };
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ const portal_1 = __importDefault(require("../__mocks/portal/portal"));
19
+ const _1 = __importDefault(require("."));
20
+ const constants_1 = require("../__mocks/constants");
21
+ const errors_1 = require("./errors");
22
+ const __1 = require("../");
23
+ const mpc_1 = __importDefault(require("../__mocks/portal/mpc"));
24
+ describe('Provider', () => {
25
+ beforeEach(() => {
26
+ portal_1.default.autoApprove = true;
27
+ portal_1.default.mpc = mpc_1.default;
28
+ jest.clearAllMocks();
29
+ global.fetch = jest.fn().mockResolvedValue({
30
+ json: jest.fn().mockResolvedValue({ result: 'test', error: null }),
31
+ });
32
+ });
33
+ describe('request', () => {
34
+ let provider = new _1.default({ portal: portal_1.default });
35
+ describe('Signer methods with auto-approval', () => {
36
+ beforeAll(() => {
37
+ portal_1.default.autoApprove = true;
38
+ provider.emit = jest.fn();
39
+ });
40
+ describe('eth_chainId', () => {
41
+ it('should throw an error if no chainId is provided alongside eth_chainId', () => __awaiter(void 0, void 0, void 0, function* () {
42
+ expect(provider.request({
43
+ method: __1.RequestMethod.eth_chainId,
44
+ params: ['test'],
45
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
46
+ }));
47
+ it('should throw an error if malformed chainId is provided alongside eth_chainId', () => __awaiter(void 0, void 0, void 0, function* () {
48
+ const chainId = 'unsupported:chain';
49
+ expect(provider.request({
50
+ chainId,
51
+ method: __1.RequestMethod.eth_chainId,
52
+ params: ['test'],
53
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "eip155:" for the operation, got ${chainId}`));
54
+ }));
55
+ it('should successfully handle an eth_chainId request', () => __awaiter(void 0, void 0, void 0, function* () {
56
+ const result = yield provider.request({
57
+ chainId: 'eip155:1',
58
+ method: __1.RequestMethod.eth_chainId,
59
+ params: ['test'],
60
+ });
61
+ expect(result).toEqual('0x1');
62
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
63
+ chainId: 'eip155:1',
64
+ method: __1.RequestMethod.eth_chainId,
65
+ params: ['test'],
66
+ signature: result,
67
+ });
68
+ }));
69
+ });
70
+ describe('eth_accounts', () => {
71
+ it('should throw an error if no chainId is provided alongside eth_accounts', () => __awaiter(void 0, void 0, void 0, function* () {
72
+ expect(provider.request({
73
+ method: __1.RequestMethod.eth_accounts,
74
+ params: ['test'],
75
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
76
+ }));
77
+ it('should throw an error if malformed chainId is provided alongside eth_accounts', () => __awaiter(void 0, void 0, void 0, function* () {
78
+ const chainId = 'unsupported:chain';
79
+ expect(provider.request({
80
+ chainId,
81
+ method: __1.RequestMethod.eth_accounts,
82
+ params: ['test'],
83
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "eip155:" for the operation, got ${chainId}`));
84
+ }));
85
+ it('should successfully handle an eth_accounts request', () => __awaiter(void 0, void 0, void 0, function* () {
86
+ portal_1.default.address = 'test';
87
+ const result = yield provider.request({
88
+ chainId: 'eip155:1',
89
+ method: __1.RequestMethod.eth_accounts,
90
+ params: ['test'],
91
+ });
92
+ expect(result).toEqual([constants_1.mockSignedHash]);
93
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
94
+ chainId: 'eip155:1',
95
+ method: __1.RequestMethod.eth_accounts,
96
+ params: ['test'],
97
+ signature: result,
98
+ });
99
+ }));
100
+ });
101
+ describe('eth_requestAccounts', () => {
102
+ it('should throw an error if no chainId is provided alongside eth_requestAccounts', () => __awaiter(void 0, void 0, void 0, function* () {
103
+ expect(provider.request({
104
+ method: __1.RequestMethod.eth_requestAccounts,
105
+ params: ['test'],
106
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
107
+ }));
108
+ it('should throw an error if malformed chainId is provided alongside eth_requestAccounts', () => __awaiter(void 0, void 0, void 0, function* () {
109
+ const chainId = 'unsupported:chain';
110
+ expect(provider.request({
111
+ chainId,
112
+ method: __1.RequestMethod.eth_requestAccounts,
113
+ params: ['test'],
114
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "eip155:" for the operation, got ${chainId}`));
115
+ }));
116
+ it('should successfully handle an eth_requestAccounts request', () => __awaiter(void 0, void 0, void 0, function* () {
117
+ portal_1.default.address = 'test';
118
+ const result = yield provider.request({
119
+ chainId: 'eip155:1',
120
+ method: __1.RequestMethod.eth_requestAccounts,
121
+ params: ['test'],
122
+ });
123
+ expect(result).toEqual([constants_1.mockSignedHash]);
124
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
125
+ chainId: 'eip155:1',
126
+ method: __1.RequestMethod.eth_requestAccounts,
127
+ params: ['test'],
128
+ signature: result,
129
+ });
130
+ }));
131
+ });
132
+ describe('eth_sendTransaction', () => {
133
+ it('should throw an error if no chainId is provided alongside eth_sendTransaction', () => __awaiter(void 0, void 0, void 0, function* () {
134
+ expect(provider.request({
135
+ method: __1.RequestMethod.eth_sendTransaction,
136
+ params: ['test'],
137
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
138
+ }));
139
+ it('should throw an error if malformed chainId is provided alongside eth_sendTransaction', () => __awaiter(void 0, void 0, void 0, function* () {
140
+ const chainId = 'unsupported:chain';
141
+ expect(provider.request({
142
+ chainId,
143
+ method: __1.RequestMethod.eth_sendTransaction,
144
+ params: ['test'],
145
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "eip155:" for the operation, got ${chainId}`));
146
+ }));
147
+ it('should successfully handle an eth_sendTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
148
+ const result = yield provider.request({
149
+ chainId: 'eip155:1',
150
+ method: __1.RequestMethod.eth_sendTransaction,
151
+ params: ['test'],
152
+ });
153
+ expect(result).toEqual(constants_1.mockSignedHash);
154
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
155
+ chainId: 'eip155:1',
156
+ method: __1.RequestMethod.eth_sendTransaction,
157
+ params: 'test',
158
+ rpcUrl: constants_1.mockRpcUrl,
159
+ });
160
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
161
+ chainId: 'eip155:1',
162
+ method: __1.RequestMethod.eth_sendTransaction,
163
+ params: ['test'],
164
+ signature: result,
165
+ });
166
+ }));
167
+ });
168
+ describe('eth_signTransaction', () => {
169
+ it('should throw an error if no chainId is provided alongside eth_signTransaction', () => __awaiter(void 0, void 0, void 0, function* () {
170
+ expect(provider.request({
171
+ method: __1.RequestMethod.eth_signTransaction,
172
+ params: ['test'],
173
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
174
+ }));
175
+ it('should throw an error if malformed chainId is provided alongside eth_signTransaction', () => __awaiter(void 0, void 0, void 0, function* () {
176
+ const chainId = 'unsupported:chain';
177
+ expect(provider.request({
178
+ chainId,
179
+ method: __1.RequestMethod.eth_signTransaction,
180
+ params: ['test'],
181
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "eip155:" for the operation, got ${chainId}`));
182
+ }));
183
+ it('should successfully handle an eth_signTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
184
+ const result = yield provider.request({
185
+ chainId: 'eip155:1',
186
+ method: __1.RequestMethod.eth_signTransaction,
187
+ params: ['test'],
188
+ });
189
+ expect(result).toEqual(constants_1.mockSignedHash);
190
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
191
+ chainId: 'eip155:1',
192
+ method: __1.RequestMethod.eth_signTransaction,
193
+ params: 'test',
194
+ rpcUrl: constants_1.mockRpcUrl,
195
+ });
196
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
197
+ chainId: 'eip155:1',
198
+ method: __1.RequestMethod.eth_signTransaction,
199
+ params: ['test'],
200
+ signature: result,
201
+ });
202
+ }));
203
+ });
204
+ describe('eth_sign', () => {
205
+ it('should throw an error if no chainId is provided alongside eth_sign', () => __awaiter(void 0, void 0, void 0, function* () {
206
+ expect(provider.request({
207
+ method: __1.RequestMethod.eth_sign,
208
+ params: ['test'],
209
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
210
+ }));
211
+ it('should throw an error if malformed chainId is provided alongside eth_sign', () => __awaiter(void 0, void 0, void 0, function* () {
212
+ const chainId = 'unsupported:chain';
213
+ expect(provider.request({
214
+ chainId,
215
+ method: __1.RequestMethod.eth_sign,
216
+ params: ['test'],
217
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "eip155:" for the operation, got ${chainId}`));
218
+ }));
219
+ it('should successfully handle an eth_sign request', () => __awaiter(void 0, void 0, void 0, function* () {
220
+ const result = yield provider.request({
221
+ chainId: 'eip155:1',
222
+ method: __1.RequestMethod.eth_sign,
223
+ params: ['test'],
224
+ });
225
+ expect(result).toEqual(constants_1.mockSignedHash);
226
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
227
+ chainId: 'eip155:1',
228
+ method: __1.RequestMethod.eth_sign,
229
+ params: ['test'],
230
+ rpcUrl: constants_1.mockRpcUrl,
231
+ });
232
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
233
+ chainId: 'eip155:1',
234
+ method: __1.RequestMethod.eth_sign,
235
+ params: ['test'],
236
+ signature: result,
237
+ });
238
+ }));
239
+ });
240
+ describe('eth_signTypedData_v3', () => {
241
+ it('should throw an error if no chainId is provided alongside eth_signTypedData_v3', () => __awaiter(void 0, void 0, void 0, function* () {
242
+ expect(provider.request({
243
+ method: __1.RequestMethod.eth_signTypedData_v3,
244
+ params: ['test'],
245
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
246
+ }));
247
+ it('should throw an error if malformed chainId is provided alongside eth_signTypedData_v3', () => __awaiter(void 0, void 0, void 0, function* () {
248
+ const chainId = 'unsupported:chain';
249
+ expect(provider.request({
250
+ chainId,
251
+ method: __1.RequestMethod.eth_signTypedData_v3,
252
+ params: ['test'],
253
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "eip155:" for the operation, got ${chainId}`));
254
+ }));
255
+ it('should successfully handle an eth_signTypedData_v3 request', () => __awaiter(void 0, void 0, void 0, function* () {
256
+ const result = yield provider.request({
257
+ chainId: 'eip155:1',
258
+ method: __1.RequestMethod.eth_signTypedData_v3,
259
+ params: ['test'],
260
+ });
261
+ expect(result).toEqual(constants_1.mockSignedHash);
262
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
263
+ chainId: 'eip155:1',
264
+ method: __1.RequestMethod.eth_signTypedData_v3,
265
+ params: ['test'],
266
+ rpcUrl: constants_1.mockRpcUrl,
267
+ });
268
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
269
+ chainId: 'eip155:1',
270
+ method: __1.RequestMethod.eth_signTypedData_v3,
271
+ params: ['test'],
272
+ signature: result,
273
+ });
274
+ }));
275
+ });
276
+ describe('eth_signTypedData_v4', () => {
277
+ it('should throw an error if no chainId is provided alongside eth_signTypedData_v4', () => __awaiter(void 0, void 0, void 0, function* () {
278
+ expect(provider.request({
279
+ method: __1.RequestMethod.eth_signTypedData_v4,
280
+ params: ['test'],
281
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
282
+ }));
283
+ it('should throw an error if malformed chainId is provided alongside eth_signTypedData_v4', () => __awaiter(void 0, void 0, void 0, function* () {
284
+ const chainId = 'unsupported:chain';
285
+ expect(provider.request({
286
+ chainId,
287
+ method: __1.RequestMethod.eth_signTypedData_v4,
288
+ params: ['test'],
289
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "eip155:" for the operation, got ${chainId}`));
290
+ }));
291
+ it('should successfully handle an eth_signTypedData_v4 request', () => __awaiter(void 0, void 0, void 0, function* () {
292
+ const result = yield provider.request({
293
+ chainId: 'eip155:1',
294
+ method: __1.RequestMethod.eth_signTypedData_v4,
295
+ params: ['test'],
296
+ });
297
+ expect(result).toEqual(constants_1.mockSignedHash);
298
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
299
+ chainId: 'eip155:1',
300
+ method: __1.RequestMethod.eth_signTypedData_v4,
301
+ params: ['test'],
302
+ rpcUrl: constants_1.mockRpcUrl,
303
+ });
304
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
305
+ chainId: 'eip155:1',
306
+ method: __1.RequestMethod.eth_signTypedData_v4,
307
+ params: ['test'],
308
+ signature: result,
309
+ });
310
+ }));
311
+ });
312
+ describe('personal_sign', () => {
313
+ it('should throw an error if no chainId is provided alongside personal_sign', () => __awaiter(void 0, void 0, void 0, function* () {
314
+ expect(provider.request({
315
+ method: __1.RequestMethod.personal_sign,
316
+ params: ['test'],
317
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
318
+ }));
319
+ it('should throw an error if malformed chainId is provided alongside personal_sign', () => __awaiter(void 0, void 0, void 0, function* () {
320
+ const chainId = 'unsupported:chain';
321
+ expect(provider.request({
322
+ chainId,
323
+ method: __1.RequestMethod.personal_sign,
324
+ params: ['test'],
325
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "eip155:" for the operation, got ${chainId}`));
326
+ }));
327
+ it('should successfully handle a personal_sign request', () => __awaiter(void 0, void 0, void 0, function* () {
328
+ const result = yield provider.request({
329
+ chainId: 'eip155:1',
330
+ method: __1.RequestMethod.personal_sign,
331
+ params: ['test'],
332
+ });
333
+ expect(result).toEqual(constants_1.mockSignedHash);
334
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
335
+ chainId: 'eip155:1',
336
+ method: __1.RequestMethod.personal_sign,
337
+ params: ['test'],
338
+ rpcUrl: constants_1.mockRpcUrl,
339
+ });
340
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
341
+ chainId: 'eip155:1',
342
+ method: __1.RequestMethod.personal_sign,
343
+ params: ['test'],
344
+ signature: result,
345
+ });
346
+ }));
347
+ });
348
+ describe('sol_signAndConfirmTransaction', () => {
349
+ it('should throw an error if no chainId is provided alongside sol_signAndConfirmTransaction', () => __awaiter(void 0, void 0, void 0, function* () {
350
+ expect(provider.request({
351
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
352
+ params: ['test'],
353
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
354
+ }));
355
+ it('should throw an error if malformed chainId is provided alongside sol_signAndConfirmTransaction', () => __awaiter(void 0, void 0, void 0, function* () {
356
+ const chainId = 'unsupported:chain';
357
+ expect(provider.request({
358
+ chainId,
359
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
360
+ params: ['test'],
361
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "solana:" for the operation, got ${chainId}`));
362
+ }));
363
+ it('should successfully handle a sol_signAndConfirmTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
364
+ const result = yield provider.request({
365
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
366
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
367
+ params: ['test'],
368
+ });
369
+ expect(result).toEqual(constants_1.mockSignedHash);
370
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
371
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
372
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
373
+ params: ['test'],
374
+ rpcUrl: constants_1.mockRpcUrl,
375
+ });
376
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
377
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
378
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
379
+ params: ['test'],
380
+ signature: result,
381
+ });
382
+ }));
383
+ });
384
+ describe('sol_signAndSendTransaction', () => {
385
+ it('should throw an error if no chainId is provided alongside sol_signAndSendTransaction', () => __awaiter(void 0, void 0, void 0, function* () {
386
+ expect(provider.request({
387
+ method: __1.RequestMethod.sol_signAndSendTransaction,
388
+ params: ['test'],
389
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
390
+ }));
391
+ it('should throw an error if malformed chainId is provided alongside sol_signAndSendTransaction', () => __awaiter(void 0, void 0, void 0, function* () {
392
+ const chainId = 'unsupported:chain';
393
+ expect(provider.request({
394
+ chainId,
395
+ method: __1.RequestMethod.sol_signAndSendTransaction,
396
+ params: ['test'],
397
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "solana:" for the operation, got ${chainId}`));
398
+ }));
399
+ it('should successfully handle a sol_signAndSendTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
400
+ const result = yield provider.request({
401
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
402
+ method: __1.RequestMethod.sol_signAndSendTransaction,
403
+ params: ['test'],
404
+ });
405
+ expect(result).toEqual(constants_1.mockSignedHash);
406
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
407
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
408
+ method: __1.RequestMethod.sol_signAndSendTransaction,
409
+ params: ['test'],
410
+ rpcUrl: constants_1.mockRpcUrl,
411
+ });
412
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
413
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
414
+ method: __1.RequestMethod.sol_signAndSendTransaction,
415
+ params: ['test'],
416
+ signature: result,
417
+ });
418
+ }));
419
+ });
420
+ describe('sol_signMessage', () => {
421
+ it('should throw an error if no chainId is provided alongside sol_signMessage', () => __awaiter(void 0, void 0, void 0, function* () {
422
+ expect(provider.request({
423
+ method: __1.RequestMethod.sol_signMessage,
424
+ params: ['test'],
425
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
426
+ }));
427
+ it('should throw an error if malformed chainId is provided alongside sol_signMessage', () => __awaiter(void 0, void 0, void 0, function* () {
428
+ const chainId = 'unsupported:chain';
429
+ expect(provider.request({
430
+ chainId,
431
+ method: __1.RequestMethod.sol_signMessage,
432
+ params: ['test'],
433
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "solana:" for the operation, got ${chainId}`));
434
+ }));
435
+ it('should successfully handle a sol_signMessage request', () => __awaiter(void 0, void 0, void 0, function* () {
436
+ const result = yield provider.request({
437
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
438
+ method: __1.RequestMethod.sol_signMessage,
439
+ params: ['test'],
440
+ });
441
+ expect(result).toEqual(constants_1.mockSignedHash);
442
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
443
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
444
+ method: __1.RequestMethod.sol_signMessage,
445
+ params: ['test'],
446
+ rpcUrl: constants_1.mockRpcUrl,
447
+ });
448
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
449
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
450
+ method: __1.RequestMethod.sol_signMessage,
451
+ params: ['test'],
452
+ signature: result,
453
+ });
454
+ }));
455
+ });
456
+ describe('sol_signTransaction', () => {
457
+ it('should throw an error if no chainId is provided alongside sol_signTransaction', () => __awaiter(void 0, void 0, void 0, function* () {
458
+ expect(provider.request({
459
+ method: __1.RequestMethod.sol_signTransaction,
460
+ params: ['test'],
461
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID is required for the operation`));
462
+ }));
463
+ it('should throw an error if malformed chainId is provided alongside sol_signTransaction', () => __awaiter(void 0, void 0, void 0, function* () {
464
+ const chainId = 'unsupported:chain';
465
+ expect(provider.request({
466
+ chainId,
467
+ method: __1.RequestMethod.sol_signTransaction,
468
+ params: ['test'],
469
+ })).rejects.toThrow(new Error(`[PortalProvider] Chain ID must be prefixed with "solana:" for the operation, got ${chainId}`));
470
+ }));
471
+ it('should successfully handle a sol_signTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
472
+ const result = yield provider.request({
473
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
474
+ method: __1.RequestMethod.sol_signTransaction,
475
+ params: ['test'],
476
+ });
477
+ expect(result).toEqual(constants_1.mockSignedHash);
478
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
479
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
480
+ method: __1.RequestMethod.sol_signTransaction,
481
+ params: ['test'],
482
+ rpcUrl: constants_1.mockRpcUrl,
483
+ });
484
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
485
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
486
+ method: __1.RequestMethod.sol_signTransaction,
487
+ params: ['test'],
488
+ signature: result,
489
+ });
490
+ }));
491
+ });
492
+ });
493
+ describe('Signer methods without auto-approval', () => {
494
+ const mockSigningRequestedHandler = jest.fn();
495
+ const mockSignatureReceivedHandler = jest.fn();
496
+ const mockConsoleWarn = jest.spyOn(global.console, 'warn');
497
+ beforeEach(() => {
498
+ provider = new _1.default({ portal: portal_1.default });
499
+ portal_1.default.autoApprove = false;
500
+ provider.on('portal_signingRequested', mockSigningRequestedHandler);
501
+ provider.on('portal_signatureReceived', mockSignatureReceivedHandler);
502
+ });
503
+ describe('eth_chainId', () => {
504
+ it('should successfully handle an eth_chainId request', () => __awaiter(void 0, void 0, void 0, function* () {
505
+ const result = yield provider.request({
506
+ chainId: 'eip155:1',
507
+ method: __1.RequestMethod.eth_chainId,
508
+ params: ['test'],
509
+ });
510
+ expect(mockSigningRequestedHandler).toHaveBeenCalledTimes(0);
511
+ expect(result).toEqual('0x1');
512
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
513
+ chainId: 'eip155:1',
514
+ method: __1.RequestMethod.eth_chainId,
515
+ params: ['test'],
516
+ signature: result,
517
+ });
518
+ }));
519
+ });
520
+ describe('eth_accounts', () => {
521
+ it('should successfully handle an eth_accounts request', () => __awaiter(void 0, void 0, void 0, function* () {
522
+ portal_1.default.address = 'test';
523
+ const result = yield provider.request({
524
+ chainId: 'eip155:1',
525
+ method: __1.RequestMethod.eth_accounts,
526
+ params: ['test'],
527
+ });
528
+ expect(mockSigningRequestedHandler).toHaveBeenCalledTimes(0);
529
+ expect(result).toEqual([constants_1.mockSignedHash]);
530
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
531
+ chainId: 'eip155:1',
532
+ method: __1.RequestMethod.eth_accounts,
533
+ params: ['test'],
534
+ signature: result,
535
+ });
536
+ }));
537
+ });
538
+ describe('eth_requestAccounts', () => {
539
+ it('should successfully handle an approved eth_requestAccounts request', () => __awaiter(void 0, void 0, void 0, function* () {
540
+ portal_1.default.address = 'test';
541
+ const result = yield provider.request({
542
+ chainId: 'eip155:1',
543
+ method: __1.RequestMethod.eth_requestAccounts,
544
+ params: ['test'],
545
+ });
546
+ expect(mockSigningRequestedHandler).toHaveBeenCalledTimes(0);
547
+ expect(result).toEqual([constants_1.mockSignedHash]);
548
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
549
+ chainId: 'eip155:1',
550
+ method: __1.RequestMethod.eth_requestAccounts,
551
+ params: ['test'],
552
+ signature: result,
553
+ });
554
+ }));
555
+ });
556
+ describe('eth_sendTransaction', () => {
557
+ it('should successfully handle an approved eth_sendTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
558
+ provider.on('portal_signingRequested', () => {
559
+ provider.emit('portal_signingApproved', {
560
+ method: __1.RequestMethod.eth_sendTransaction,
561
+ params: ['test'],
562
+ });
563
+ });
564
+ const result = yield provider.request({
565
+ chainId: 'eip155:1',
566
+ method: __1.RequestMethod.eth_sendTransaction,
567
+ params: ['test'],
568
+ });
569
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
570
+ method: __1.RequestMethod.eth_sendTransaction,
571
+ params: ['test'],
572
+ });
573
+ expect(result).toEqual(constants_1.mockSignedHash);
574
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
575
+ chainId: 'eip155:1',
576
+ method: __1.RequestMethod.eth_sendTransaction,
577
+ params: 'test',
578
+ rpcUrl: constants_1.mockRpcUrl,
579
+ });
580
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
581
+ chainId: 'eip155:1',
582
+ method: __1.RequestMethod.eth_sendTransaction,
583
+ params: ['test'],
584
+ signature: result,
585
+ });
586
+ }));
587
+ it('should successfully handle a rejected eth_sendTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
588
+ provider.on('portal_signingRequested', () => {
589
+ provider.emit('portal_signingRejected', {
590
+ method: __1.RequestMethod.eth_sendTransaction,
591
+ params: ['test'],
592
+ });
593
+ });
594
+ const result = yield provider.request({
595
+ chainId: 'eip155:1',
596
+ method: __1.RequestMethod.eth_sendTransaction,
597
+ params: ['test'],
598
+ });
599
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
600
+ method: __1.RequestMethod.eth_sendTransaction,
601
+ params: ['test'],
602
+ });
603
+ expect(result).toEqual(undefined);
604
+ expect(mockConsoleWarn).toHaveBeenCalledWith("[PortalProvider] Request for signing method 'eth_sendTransaction' could not be completed because it was not approved by the user.");
605
+ }));
606
+ });
607
+ describe('eth_signTransaction', () => {
608
+ it('should successfully handle an approved eth_signTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
609
+ provider.on('portal_signingRequested', () => {
610
+ provider.emit('portal_signingApproved', {
611
+ method: __1.RequestMethod.eth_signTransaction,
612
+ params: ['test'],
613
+ });
614
+ });
615
+ const result = yield provider.request({
616
+ chainId: 'eip155:1',
617
+ method: __1.RequestMethod.eth_signTransaction,
618
+ params: ['test'],
619
+ });
620
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
621
+ method: __1.RequestMethod.eth_signTransaction,
622
+ params: ['test'],
623
+ });
624
+ expect(result).toEqual(constants_1.mockSignedHash);
625
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
626
+ chainId: 'eip155:1',
627
+ method: __1.RequestMethod.eth_signTransaction,
628
+ params: 'test',
629
+ rpcUrl: constants_1.mockRpcUrl,
630
+ });
631
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
632
+ chainId: 'eip155:1',
633
+ method: __1.RequestMethod.eth_signTransaction,
634
+ params: ['test'],
635
+ signature: result,
636
+ });
637
+ }));
638
+ it('should successfully handle a rejected eth_signTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
639
+ provider.on('portal_signingRequested', () => {
640
+ provider.emit('portal_signingRejected', {
641
+ method: __1.RequestMethod.eth_signTransaction,
642
+ params: ['test'],
643
+ });
644
+ });
645
+ const result = yield provider.request({
646
+ chainId: 'eip155:1',
647
+ method: __1.RequestMethod.eth_signTransaction,
648
+ params: ['test'],
649
+ });
650
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
651
+ method: __1.RequestMethod.eth_signTransaction,
652
+ params: ['test'],
653
+ });
654
+ expect(result).toEqual(undefined);
655
+ expect(mockConsoleWarn).toHaveBeenCalledWith("[PortalProvider] Request for signing method 'eth_signTransaction' could not be completed because it was not approved by the user.");
656
+ }));
657
+ });
658
+ describe('eth_sign', () => {
659
+ it('should successfully handle an approved eth_sign request', () => __awaiter(void 0, void 0, void 0, function* () {
660
+ provider.on('portal_signingRequested', () => {
661
+ provider.emit('portal_signingApproved', {
662
+ method: __1.RequestMethod.eth_sign,
663
+ params: ['test'],
664
+ });
665
+ });
666
+ const result = yield provider.request({
667
+ chainId: 'eip155:1',
668
+ method: __1.RequestMethod.eth_sign,
669
+ params: ['test'],
670
+ });
671
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
672
+ method: __1.RequestMethod.eth_sign,
673
+ params: ['test'],
674
+ });
675
+ expect(result).toEqual(constants_1.mockSignedHash);
676
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
677
+ chainId: 'eip155:1',
678
+ method: __1.RequestMethod.eth_sign,
679
+ params: ['test'],
680
+ rpcUrl: constants_1.mockRpcUrl,
681
+ });
682
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
683
+ chainId: 'eip155:1',
684
+ method: __1.RequestMethod.eth_sign,
685
+ params: ['test'],
686
+ signature: result,
687
+ });
688
+ }));
689
+ it('should successfully handle a rejected eth_sign request', () => __awaiter(void 0, void 0, void 0, function* () {
690
+ provider.on('portal_signingRequested', () => {
691
+ provider.emit('portal_signingRejected', {
692
+ method: __1.RequestMethod.eth_sign,
693
+ params: ['test'],
694
+ });
695
+ });
696
+ const result = yield provider.request({
697
+ chainId: 'eip155:1',
698
+ method: __1.RequestMethod.eth_sign,
699
+ params: ['test'],
700
+ });
701
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
702
+ method: __1.RequestMethod.eth_sign,
703
+ params: ['test'],
704
+ });
705
+ expect(result).toEqual(undefined);
706
+ expect(mockConsoleWarn).toHaveBeenCalledWith("[PortalProvider] Request for signing method 'eth_sign' could not be completed because it was not approved by the user.");
707
+ }));
708
+ });
709
+ describe('eth_signTypedData_v3', () => {
710
+ it('should successfully handle an approved eth_signTypedData_v3 request', () => __awaiter(void 0, void 0, void 0, function* () {
711
+ provider.on('portal_signingRequested', () => {
712
+ provider.emit('portal_signingApproved', {
713
+ method: __1.RequestMethod.eth_signTypedData_v3,
714
+ params: ['test'],
715
+ });
716
+ });
717
+ const result = yield provider.request({
718
+ chainId: 'eip155:1',
719
+ method: __1.RequestMethod.eth_signTypedData_v3,
720
+ params: ['test'],
721
+ });
722
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
723
+ method: __1.RequestMethod.eth_signTypedData_v3,
724
+ params: ['test'],
725
+ });
726
+ expect(result).toEqual(constants_1.mockSignedHash);
727
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
728
+ chainId: 'eip155:1',
729
+ method: __1.RequestMethod.eth_signTypedData_v3,
730
+ params: ['test'],
731
+ rpcUrl: constants_1.mockRpcUrl,
732
+ });
733
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
734
+ chainId: 'eip155:1',
735
+ method: __1.RequestMethod.eth_signTypedData_v3,
736
+ params: ['test'],
737
+ signature: result,
738
+ });
739
+ }));
740
+ it('should successfully handle a rejected eth_signTypedData_v3 request', () => __awaiter(void 0, void 0, void 0, function* () {
741
+ provider.on('portal_signingRequested', () => {
742
+ provider.emit('portal_signingRejected', {
743
+ method: __1.RequestMethod.eth_signTypedData_v3,
744
+ params: ['test'],
745
+ });
746
+ });
747
+ const result = yield provider.request({
748
+ chainId: 'eip155:1',
749
+ method: __1.RequestMethod.eth_signTypedData_v3,
750
+ params: ['test'],
751
+ });
752
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
753
+ method: __1.RequestMethod.eth_signTypedData_v3,
754
+ params: ['test'],
755
+ });
756
+ expect(result).toEqual(undefined);
757
+ expect(mockConsoleWarn).toHaveBeenCalledWith("[PortalProvider] Request for signing method 'eth_signTypedData_v3' could not be completed because it was not approved by the user.");
758
+ }));
759
+ });
760
+ describe('eth_signTypedData_v4', () => {
761
+ it('should successfully handle an approved eth_signTypedData_v4 request', () => __awaiter(void 0, void 0, void 0, function* () {
762
+ provider.on('portal_signingRequested', () => {
763
+ provider.emit('portal_signingApproved', {
764
+ method: __1.RequestMethod.eth_signTypedData_v4,
765
+ params: ['test'],
766
+ });
767
+ });
768
+ const result = yield provider.request({
769
+ chainId: 'eip155:1',
770
+ method: __1.RequestMethod.eth_signTypedData_v4,
771
+ params: ['test'],
772
+ });
773
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
774
+ method: __1.RequestMethod.eth_signTypedData_v4,
775
+ params: ['test'],
776
+ });
777
+ expect(result).toEqual(constants_1.mockSignedHash);
778
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
779
+ chainId: 'eip155:1',
780
+ method: __1.RequestMethod.eth_signTypedData_v4,
781
+ params: ['test'],
782
+ rpcUrl: constants_1.mockRpcUrl,
783
+ });
784
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
785
+ chainId: 'eip155:1',
786
+ method: __1.RequestMethod.eth_signTypedData_v4,
787
+ params: ['test'],
788
+ signature: result,
789
+ });
790
+ }));
791
+ it('should successfully handle a rejected eth_signTypedData_v4 request', () => __awaiter(void 0, void 0, void 0, function* () {
792
+ provider.on('portal_signingRequested', () => {
793
+ provider.emit('portal_signingRejected', {
794
+ method: __1.RequestMethod.eth_signTypedData_v4,
795
+ params: ['test'],
796
+ });
797
+ });
798
+ const result = yield provider.request({
799
+ chainId: 'eip155:1',
800
+ method: __1.RequestMethod.eth_signTypedData_v4,
801
+ params: ['test'],
802
+ });
803
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
804
+ method: __1.RequestMethod.eth_signTypedData_v4,
805
+ params: ['test'],
806
+ });
807
+ expect(result).toEqual(undefined);
808
+ expect(mockConsoleWarn).toHaveBeenCalledWith("[PortalProvider] Request for signing method 'eth_signTypedData_v4' could not be completed because it was not approved by the user.");
809
+ }));
810
+ });
811
+ describe('personal_sign', () => {
812
+ it('should successfully handle an approved personal_sign request', () => __awaiter(void 0, void 0, void 0, function* () {
813
+ provider.on('portal_signingRequested', () => {
814
+ provider.emit('portal_signingApproved', {
815
+ method: __1.RequestMethod.personal_sign,
816
+ params: ['test'],
817
+ });
818
+ });
819
+ const result = yield provider.request({
820
+ chainId: 'eip155:1',
821
+ method: __1.RequestMethod.personal_sign,
822
+ params: ['test'],
823
+ });
824
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
825
+ method: __1.RequestMethod.personal_sign,
826
+ params: ['test'],
827
+ });
828
+ expect(result).toEqual(constants_1.mockSignedHash);
829
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
830
+ chainId: 'eip155:1',
831
+ method: __1.RequestMethod.personal_sign,
832
+ params: ['test'],
833
+ rpcUrl: constants_1.mockRpcUrl,
834
+ });
835
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
836
+ chainId: 'eip155:1',
837
+ method: __1.RequestMethod.personal_sign,
838
+ params: ['test'],
839
+ signature: result,
840
+ });
841
+ }));
842
+ it('should successfully handle a rejected personal_sign request', () => __awaiter(void 0, void 0, void 0, function* () {
843
+ provider.on('portal_signingRequested', () => {
844
+ provider.emit('portal_signingRejected', {
845
+ method: __1.RequestMethod.personal_sign,
846
+ params: ['test'],
847
+ });
848
+ });
849
+ const result = yield provider.request({
850
+ chainId: 'eip155:1',
851
+ method: __1.RequestMethod.personal_sign,
852
+ params: ['test'],
853
+ });
854
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
855
+ method: __1.RequestMethod.personal_sign,
856
+ params: ['test'],
857
+ });
858
+ expect(result).toEqual(undefined);
859
+ expect(mockConsoleWarn).toHaveBeenCalledWith("[PortalProvider] Request for signing method 'personal_sign' could not be completed because it was not approved by the user.");
860
+ }));
861
+ });
862
+ describe('sol_signAndConfirmTransaction', () => {
863
+ it('should successfully handle an approved sol_signAndConfirmTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
864
+ provider.on('portal_signingRequested', () => {
865
+ provider.emit('portal_signingApproved', {
866
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
867
+ params: ['test'],
868
+ });
869
+ });
870
+ const result = yield provider.request({
871
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
872
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
873
+ params: ['test'],
874
+ });
875
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
876
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
877
+ params: ['test'],
878
+ });
879
+ expect(result).toEqual(constants_1.mockSignedHash);
880
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
881
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
882
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
883
+ params: ['test'],
884
+ rpcUrl: constants_1.mockRpcUrl,
885
+ });
886
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
887
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
888
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
889
+ params: ['test'],
890
+ signature: result,
891
+ });
892
+ }));
893
+ it('should successfully handle a rejected sol_signAndConfirmTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
894
+ provider.on('portal_signingRequested', () => {
895
+ provider.emit('portal_signingRejected', {
896
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
897
+ params: ['test'],
898
+ });
899
+ });
900
+ const result = yield provider.request({
901
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
902
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
903
+ params: ['test'],
904
+ });
905
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
906
+ method: __1.RequestMethod.sol_signAndConfirmTransaction,
907
+ params: ['test'],
908
+ });
909
+ expect(result).toEqual(undefined);
910
+ expect(mockConsoleWarn).toHaveBeenCalledWith("[PortalProvider] Request for signing method 'sol_signAndConfirmTransaction' could not be completed because it was not approved by the user.");
911
+ }));
912
+ });
913
+ describe('sol_signAndSendTransaction', () => {
914
+ it('should successfully handle an approved sol_signAndSendTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
915
+ provider.on('portal_signingRequested', () => {
916
+ provider.emit('portal_signingApproved', {
917
+ method: __1.RequestMethod.sol_signAndSendTransaction,
918
+ params: ['test'],
919
+ });
920
+ });
921
+ const result = yield provider.request({
922
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
923
+ method: __1.RequestMethod.sol_signAndSendTransaction,
924
+ params: ['test'],
925
+ });
926
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
927
+ method: __1.RequestMethod.sol_signAndSendTransaction,
928
+ params: ['test'],
929
+ });
930
+ expect(result).toEqual(constants_1.mockSignedHash);
931
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
932
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
933
+ method: __1.RequestMethod.sol_signAndSendTransaction,
934
+ params: ['test'],
935
+ rpcUrl: constants_1.mockRpcUrl,
936
+ });
937
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
938
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
939
+ method: __1.RequestMethod.sol_signAndSendTransaction,
940
+ params: ['test'],
941
+ signature: result,
942
+ });
943
+ }));
944
+ it('should successfully handle a rejected sol_signAndSendTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
945
+ provider.on('portal_signingRequested', () => {
946
+ provider.emit('portal_signingRejected', {
947
+ method: __1.RequestMethod.sol_signAndSendTransaction,
948
+ params: ['test'],
949
+ });
950
+ });
951
+ const result = yield provider.request({
952
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
953
+ method: __1.RequestMethod.sol_signAndSendTransaction,
954
+ params: ['test'],
955
+ });
956
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
957
+ method: __1.RequestMethod.sol_signAndSendTransaction,
958
+ params: ['test'],
959
+ });
960
+ expect(result).toEqual(undefined);
961
+ expect(mockConsoleWarn).toHaveBeenCalledWith("[PortalProvider] Request for signing method 'sol_signAndSendTransaction' could not be completed because it was not approved by the user.");
962
+ }));
963
+ });
964
+ describe('sol_signMessage', () => {
965
+ it('should successfully handle an approved sol_signMessage request', () => __awaiter(void 0, void 0, void 0, function* () {
966
+ provider.on('portal_signingRequested', () => {
967
+ provider.emit('portal_signingApproved', {
968
+ method: __1.RequestMethod.sol_signMessage,
969
+ params: ['test'],
970
+ });
971
+ });
972
+ const result = yield provider.request({
973
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
974
+ method: __1.RequestMethod.sol_signMessage,
975
+ params: ['test'],
976
+ });
977
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
978
+ method: __1.RequestMethod.sol_signMessage,
979
+ params: ['test'],
980
+ });
981
+ expect(result).toEqual(constants_1.mockSignedHash);
982
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
983
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
984
+ method: __1.RequestMethod.sol_signMessage,
985
+ params: ['test'],
986
+ rpcUrl: constants_1.mockRpcUrl,
987
+ });
988
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
989
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
990
+ method: __1.RequestMethod.sol_signMessage,
991
+ params: ['test'],
992
+ signature: result,
993
+ });
994
+ }));
995
+ it('should successfully handle a rejected sol_signMessage request', () => __awaiter(void 0, void 0, void 0, function* () {
996
+ provider.on('portal_signingRequested', () => {
997
+ provider.emit('portal_signingRejected', {
998
+ method: __1.RequestMethod.sol_signMessage,
999
+ params: ['test'],
1000
+ });
1001
+ });
1002
+ const result = yield provider.request({
1003
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
1004
+ method: __1.RequestMethod.sol_signMessage,
1005
+ params: ['test'],
1006
+ });
1007
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
1008
+ method: __1.RequestMethod.sol_signMessage,
1009
+ params: ['test'],
1010
+ });
1011
+ expect(result).toEqual(undefined);
1012
+ expect(mockConsoleWarn).toHaveBeenCalledWith("[PortalProvider] Request for signing method 'sol_signMessage' could not be completed because it was not approved by the user.");
1013
+ }));
1014
+ });
1015
+ describe('sol_signTransaction', () => {
1016
+ it('should successfully handle an approved sol_signTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
1017
+ provider.on('portal_signingRequested', () => {
1018
+ provider.emit('portal_signingApproved', {
1019
+ method: __1.RequestMethod.sol_signTransaction,
1020
+ params: ['test'],
1021
+ });
1022
+ });
1023
+ const result = yield provider.request({
1024
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
1025
+ method: __1.RequestMethod.sol_signTransaction,
1026
+ params: ['test'],
1027
+ });
1028
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
1029
+ method: __1.RequestMethod.sol_signTransaction,
1030
+ params: ['test'],
1031
+ });
1032
+ expect(result).toEqual(constants_1.mockSignedHash);
1033
+ expect(portal_1.default.mpc.sign).toHaveBeenCalledWith({
1034
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
1035
+ method: __1.RequestMethod.sol_signTransaction,
1036
+ params: ['test'],
1037
+ rpcUrl: constants_1.mockRpcUrl,
1038
+ });
1039
+ expect(mockSignatureReceivedHandler).toHaveBeenCalledWith({
1040
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
1041
+ method: __1.RequestMethod.sol_signTransaction,
1042
+ params: ['test'],
1043
+ signature: result,
1044
+ });
1045
+ }));
1046
+ it('should successfully handle a rejected sol_signTransaction request', () => __awaiter(void 0, void 0, void 0, function* () {
1047
+ provider.on('portal_signingRequested', () => {
1048
+ provider.emit('portal_signingRejected', {
1049
+ method: __1.RequestMethod.sol_signTransaction,
1050
+ params: ['test'],
1051
+ });
1052
+ });
1053
+ const result = yield provider.request({
1054
+ chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
1055
+ method: __1.RequestMethod.sol_signTransaction,
1056
+ params: ['test'],
1057
+ });
1058
+ expect(mockSigningRequestedHandler).toHaveBeenCalledWith({
1059
+ method: __1.RequestMethod.sol_signTransaction,
1060
+ params: ['test'],
1061
+ });
1062
+ expect(result).toEqual(undefined);
1063
+ expect(mockConsoleWarn).toHaveBeenCalledWith("[PortalProvider] Request for signing method 'sol_signTransaction' could not be completed because it was not approved by the user.");
1064
+ }));
1065
+ });
1066
+ });
1067
+ describe('Non-signer methods', () => {
1068
+ beforeAll(() => {
1069
+ portal_1.default.autoApprove = true;
1070
+ provider.emit = jest.fn();
1071
+ });
1072
+ it('should successfully handle non-signer method requests', () => __awaiter(void 0, void 0, void 0, function* () {
1073
+ const result = yield provider.request({
1074
+ chainId: 'eip155:1',
1075
+ method: __1.RequestMethod.eth_sendRawTransaction,
1076
+ params: ['test'],
1077
+ });
1078
+ expect(result).toEqual('test');
1079
+ expect(global.fetch).toHaveBeenCalledWith(constants_1.mockRpcUrl, {
1080
+ method: 'POST',
1081
+ headers: {
1082
+ 'Content-Type': 'application/json',
1083
+ },
1084
+ body: JSON.stringify({
1085
+ jsonrpc: '2.0',
1086
+ id: '0',
1087
+ method: __1.RequestMethod.eth_sendRawTransaction,
1088
+ params: ['test'],
1089
+ }),
1090
+ });
1091
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
1092
+ chainId: 'eip155:1',
1093
+ method: __1.RequestMethod.eth_sendRawTransaction,
1094
+ params: ['test'],
1095
+ signature: { result: 'test', error: null },
1096
+ });
1097
+ }));
1098
+ it('should throw an error if gateway request results in an error', () => __awaiter(void 0, void 0, void 0, function* () {
1099
+ const errRes = {
1100
+ result: null,
1101
+ error: { code: 4901, data: { key: 'value' } },
1102
+ };
1103
+ global.fetch = jest.fn().mockResolvedValue({
1104
+ json: jest.fn().mockResolvedValue(errRes),
1105
+ });
1106
+ yield expect(provider.request({
1107
+ chainId: 'eip155:1',
1108
+ method: __1.RequestMethod.eth_sendRawTransaction,
1109
+ params: ['test'],
1110
+ })).rejects.toThrow(new errors_1.ProviderRpcError(errRes.error));
1111
+ expect(provider.emit).toHaveBeenCalledWith('portal_signatureReceived', {
1112
+ chainId: 'eip155:1',
1113
+ method: __1.RequestMethod.eth_sendRawTransaction,
1114
+ params: ['test'],
1115
+ signature: errRes,
1116
+ });
1117
+ }));
1118
+ it('should throw an error for an unsupported method', () => __awaiter(void 0, void 0, void 0, function* () {
1119
+ yield expect(provider.request({
1120
+ chainId: 'eip155:1',
1121
+ method: 'wallet_switchEthereumChain',
1122
+ params: ['test'],
1123
+ })).rejects.toThrow(new errors_1.ProviderRpcError({
1124
+ code: errors_1.RpcErrorCodes.UnsupportedMethod,
1125
+ data: {
1126
+ method: 'wallet_switchEthereumChain',
1127
+ params: ['test'],
1128
+ },
1129
+ }));
1130
+ expect(provider.emit).toHaveBeenCalledTimes(0);
1131
+ }));
1132
+ });
1133
+ });
1134
+ describe('emit', () => {
1135
+ const provider = new _1.default({ portal: portal_1.default });
1136
+ const mockHandler1 = jest.fn();
1137
+ const mockHandler2 = jest.fn();
1138
+ beforeEach(() => {
1139
+ provider.on('test', mockHandler1);
1140
+ provider.on('test', mockHandler2);
1141
+ });
1142
+ it('should successfully execute all handlers of an event', () => {
1143
+ provider.emit('test', { data: 'test' });
1144
+ expect(mockHandler1).toHaveBeenCalledWith({ data: 'test' });
1145
+ expect(mockHandler1).toHaveBeenCalledTimes(1);
1146
+ expect(mockHandler2).toHaveBeenCalledWith({ data: 'test' });
1147
+ expect(mockHandler2).toHaveBeenCalledTimes(1);
1148
+ });
1149
+ it('should not execute handlers for a different event', () => {
1150
+ provider.emit('test1', { data: 'test' });
1151
+ expect(mockHandler1).toHaveBeenCalledTimes(0);
1152
+ expect(mockHandler2).toHaveBeenCalledTimes(0);
1153
+ });
1154
+ });
1155
+ describe('on', () => {
1156
+ let provider = new _1.default({ portal: portal_1.default });
1157
+ const mockHandler1 = jest.fn();
1158
+ const mockHandler2 = jest.fn();
1159
+ beforeEach(() => {
1160
+ provider = new _1.default({ portal: portal_1.default });
1161
+ });
1162
+ it('should successfully register handlers for an event', () => {
1163
+ const event = 'test';
1164
+ provider.on(event, mockHandler1);
1165
+ provider.on(event, mockHandler2);
1166
+ const eventHandlers = provider.events[event];
1167
+ expect(eventHandlers.length).toEqual(2);
1168
+ expect(eventHandlers).toEqual([
1169
+ { handler: mockHandler1, once: false },
1170
+ { handler: mockHandler2, once: false },
1171
+ ]);
1172
+ });
1173
+ it('should not register undefined handlers', () => {
1174
+ provider.on('test', undefined);
1175
+ const eventHandlers = provider.events['test'];
1176
+ expect(eventHandlers.length).toEqual(0);
1177
+ });
1178
+ });
1179
+ describe('removeEventListener', () => {
1180
+ let provider = new _1.default({ portal: portal_1.default });
1181
+ const mockHandler1 = jest.fn();
1182
+ const mockHandler2 = jest.fn();
1183
+ beforeEach(() => {
1184
+ provider = new _1.default({ portal: portal_1.default });
1185
+ });
1186
+ it('should successfully remove specified event listener of an event', () => {
1187
+ const event = 'test';
1188
+ provider.on(event, mockHandler1);
1189
+ provider.on(event, mockHandler2);
1190
+ provider.removeEventListener(event, mockHandler1);
1191
+ const eventHandlers = provider.events[event];
1192
+ expect(eventHandlers.length).toEqual(1);
1193
+ expect(eventHandlers).toEqual([{ handler: mockHandler2, once: false }]);
1194
+ });
1195
+ it('should successfully remove all event listeners of an event if no specific handler is specified to be removed', () => {
1196
+ const event = 'test';
1197
+ provider.on(event, mockHandler1);
1198
+ provider.on(event, mockHandler2);
1199
+ provider.removeEventListener(event);
1200
+ const eventHandlers = provider.events[event];
1201
+ expect(eventHandlers.length).toEqual(0);
1202
+ expect(eventHandlers).toEqual([]);
1203
+ });
1204
+ it('should skip handler removal for events with no handlers', () => {
1205
+ provider.removeEventListener('non-existent', mockHandler1);
1206
+ const eventHandlers = provider.events['non-existent'];
1207
+ expect(eventHandlers).toEqual(undefined);
1208
+ });
1209
+ it('should skip handler removal for a handler that does not exist for an event', () => {
1210
+ const event = 'test';
1211
+ provider.on(event, mockHandler1);
1212
+ provider.on(event, mockHandler2);
1213
+ provider.removeEventListener(event, jest.fn());
1214
+ const eventHandlers = provider.events[event];
1215
+ expect(eventHandlers.length).toEqual(2);
1216
+ expect(eventHandlers).toEqual([
1217
+ { handler: mockHandler1, once: false },
1218
+ { handler: mockHandler2, once: false },
1219
+ ]);
1220
+ });
1221
+ });
1222
+ });