@0xsequence/provider 1.10.15 → 2.0.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.
- package/dist/0xsequence-provider.cjs.dev.js +656 -648
- package/dist/0xsequence-provider.cjs.prod.js +656 -648
- package/dist/0xsequence-provider.esm.js +660 -652
- package/dist/declarations/src/client.d.ts +7 -3
- package/dist/declarations/src/provider.d.ts +31 -22
- package/dist/declarations/src/signer.d.ts +15 -14
- package/dist/declarations/src/transports/base-provider-transport.d.ts +6 -2
- package/dist/declarations/src/transports/base-wallet-transport.d.ts +6 -2
- package/dist/declarations/src/transports/mux-transport/mux-message-provider.d.ts +5 -2
- package/dist/declarations/src/transports/wallet-request-handler.d.ts +7 -6
- package/dist/declarations/src/types.d.ts +6 -11
- package/dist/declarations/src/utils/index.d.ts +3 -3
- package/dist/declarations/src/utils.d.ts +7 -7
- package/package.json +12 -12
- package/src/client.ts +29 -51
- package/src/eip191exceptions.ts +2 -2
- package/src/extended.ts +5 -5
- package/src/init.ts +9 -10
- package/src/provider.ts +112 -66
- package/src/signer.ts +38 -43
- package/src/transactions.ts +4 -4
- package/src/transports/base-provider-transport.ts +19 -23
- package/src/transports/base-wallet-transport.ts +22 -13
- package/src/transports/mux-transport/mux-message-provider.ts +5 -6
- package/src/transports/proxy-transport/proxy-message-provider.ts +5 -2
- package/src/transports/wallet-request-handler.ts +91 -130
- package/src/transports/window-transport/window-message-handler.ts +15 -3
- package/src/transports/window-transport/window-message-provider.ts +1 -1
- package/src/types.ts +8 -11
- package/src/utils/index.ts +4 -4
- package/src/utils.ts +16 -18
|
@@ -6,14 +6,12 @@ import {
|
|
|
6
6
|
ChainIdLike,
|
|
7
7
|
findNetworkConfig,
|
|
8
8
|
findSupportedNetwork,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
JsonRpcResponse
|
|
12
|
-
JsonRpcResponseCallback,
|
|
13
|
-
NetworkConfig
|
|
9
|
+
NetworkConfig,
|
|
10
|
+
EIP1193Provider,
|
|
11
|
+
JsonRpcResponse
|
|
14
12
|
} from '@0xsequence/network'
|
|
15
|
-
import { logger, TypedData } from '@0xsequence/utils'
|
|
16
|
-
import {
|
|
13
|
+
import { logger, toHexString, TypedData } from '@0xsequence/utils'
|
|
14
|
+
import { ethers } from 'ethers'
|
|
17
15
|
import { EventEmitter2 as EventEmitter } from 'eventemitter2'
|
|
18
16
|
|
|
19
17
|
import { fromExtended } from '../extended'
|
|
@@ -36,8 +34,6 @@ import {
|
|
|
36
34
|
} from '../types'
|
|
37
35
|
import { prefixEIP191Message } from '../utils'
|
|
38
36
|
|
|
39
|
-
type ExternalProvider = providers.ExternalProvider
|
|
40
|
-
|
|
41
37
|
const SIGNER_READY_TIMEOUT = 10000
|
|
42
38
|
|
|
43
39
|
export interface WalletSignInOptions {
|
|
@@ -45,7 +41,7 @@ export interface WalletSignInOptions {
|
|
|
45
41
|
defaultNetworkId?: number
|
|
46
42
|
}
|
|
47
43
|
|
|
48
|
-
export class WalletRequestHandler implements
|
|
44
|
+
export class WalletRequestHandler implements EIP1193Provider, ProviderMessageRequestHandler {
|
|
49
45
|
// signer interface of the wallet. A null value means there is no signer (ie. user not signed in). An undefined
|
|
50
46
|
// value means the signer state is unknown, usually meaning the wallet app is booting up and initializing. Of course
|
|
51
47
|
// a Signer value is the actually interface to a signed-in account
|
|
@@ -154,7 +150,7 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
154
150
|
|
|
155
151
|
const connectDetails: ConnectDetails = {
|
|
156
152
|
connected: true,
|
|
157
|
-
chainId: ethers.
|
|
153
|
+
chainId: ethers.toQuantity(chainId)
|
|
158
154
|
}
|
|
159
155
|
|
|
160
156
|
if (options && options.authorize) {
|
|
@@ -215,35 +211,37 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
215
211
|
// sendMessageRequest will unwrap the ProviderMessageRequest and send it to the JsonRpcHandler
|
|
216
212
|
// (aka, the signer in this instance) and then responds with a wrapped response of
|
|
217
213
|
// ProviderMessageResponse to be sent over the transport
|
|
218
|
-
sendMessageRequest(message: ProviderMessageRequest): Promise<ProviderMessageResponse> {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
214
|
+
async sendMessageRequest(message: ProviderMessageRequest): Promise<ProviderMessageResponse> {
|
|
215
|
+
// Older versions of the client require the response to be jsonrpc wrapped
|
|
216
|
+
const majorVersion = Number(message.clientVersion?.split('.')[0] || '0')
|
|
217
|
+
const isJsonRpcResponse = majorVersion < 2
|
|
218
|
+
const jsonRpcResponse: JsonRpcResponse = {
|
|
219
|
+
id: message.data.id!,
|
|
220
|
+
jsonrpc: '2.0',
|
|
221
|
+
result: null,
|
|
222
|
+
error: undefined
|
|
223
|
+
}
|
|
229
224
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
message.chainId
|
|
235
|
-
)
|
|
236
|
-
})
|
|
237
|
-
}
|
|
225
|
+
try {
|
|
226
|
+
const result = await this.request({
|
|
227
|
+
method: message.data.method,
|
|
228
|
+
params: message.data.params,
|
|
229
|
+
chainId: message.chainId
|
|
230
|
+
})
|
|
238
231
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
232
|
+
return {
|
|
233
|
+
...message,
|
|
234
|
+
data: isJsonRpcResponse ? { ...jsonRpcResponse, result } : result
|
|
235
|
+
}
|
|
236
|
+
} catch (error) {
|
|
237
|
+
return {
|
|
238
|
+
...message,
|
|
239
|
+
data: isJsonRpcResponse ? { ...jsonRpcResponse, error } : error
|
|
240
|
+
}
|
|
245
241
|
}
|
|
242
|
+
}
|
|
246
243
|
|
|
244
|
+
async request(request: { method: string; params?: any[]; chainId?: number }): Promise<any> {
|
|
247
245
|
await this.getAccount()
|
|
248
246
|
|
|
249
247
|
try {
|
|
@@ -255,12 +253,16 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
255
253
|
|
|
256
254
|
// wallet account
|
|
257
255
|
const account = this.account
|
|
258
|
-
if (!account)
|
|
256
|
+
if (!account) {
|
|
257
|
+
throw new Error('WalletRequestHandler: wallet account is not configured')
|
|
258
|
+
}
|
|
259
259
|
|
|
260
260
|
// fetch the provider for the specific chain, or undefined will select defaultChain
|
|
261
|
-
const provider = this.account?.providerFor(chainId ?? this.defaultChainId())
|
|
262
|
-
if (!provider)
|
|
263
|
-
|
|
261
|
+
const provider = this.account?.providerFor(request.chainId ?? this.defaultChainId())
|
|
262
|
+
if (!provider) {
|
|
263
|
+
throw new Error(`WalletRequestHandler: wallet provider is not configured for chainId ${request.chainId}`)
|
|
264
|
+
}
|
|
265
|
+
const jsonRpcProvider = provider instanceof ethers.JsonRpcProvider ? provider : undefined
|
|
264
266
|
|
|
265
267
|
switch (request.method) {
|
|
266
268
|
case 'net_version': {
|
|
@@ -268,9 +270,7 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
268
270
|
throw new Error(`Account provider doesn't support send method`)
|
|
269
271
|
}
|
|
270
272
|
|
|
271
|
-
|
|
272
|
-
response.result = result
|
|
273
|
-
break
|
|
273
|
+
return await jsonRpcProvider.send('net_version', [])
|
|
274
274
|
}
|
|
275
275
|
|
|
276
276
|
case 'eth_chainId': {
|
|
@@ -278,22 +278,17 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
278
278
|
throw new Error(`Account provider doesn't support send method`)
|
|
279
279
|
}
|
|
280
280
|
|
|
281
|
-
|
|
282
|
-
response.result = result
|
|
283
|
-
break
|
|
281
|
+
return await jsonRpcProvider.send('eth_chainId', [])
|
|
284
282
|
}
|
|
285
283
|
|
|
286
284
|
case 'eth_accounts': {
|
|
287
|
-
|
|
288
|
-
response.result = [walletAddress]
|
|
289
|
-
break
|
|
285
|
+
return [account.address]
|
|
290
286
|
}
|
|
291
287
|
|
|
292
288
|
case 'eth_getBalance': {
|
|
293
289
|
const [accountAddress, blockTag] = request.params!
|
|
294
290
|
const walletBalance = await provider.getBalance(accountAddress, blockTag)
|
|
295
|
-
|
|
296
|
-
break
|
|
291
|
+
return toHexString(walletBalance)
|
|
297
292
|
}
|
|
298
293
|
|
|
299
294
|
case 'sequence_sign':
|
|
@@ -328,18 +323,19 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
328
323
|
// TODO:
|
|
329
324
|
// if (process.env.TEST_MODE === 'true' && this.prompter === null) {
|
|
330
325
|
const sequenceVerified = request.method === 'sequence_sign'
|
|
326
|
+
|
|
331
327
|
if (this.prompter === null) {
|
|
332
328
|
// prompter is null, so we'll sign from here
|
|
333
329
|
sig = await account.signMessage(
|
|
334
330
|
prefixedMessage,
|
|
335
|
-
chainId ?? this.defaultChainId(),
|
|
331
|
+
request.chainId ?? this.defaultChainId(),
|
|
336
332
|
sequenceVerified ? 'eip6492' : 'ignore'
|
|
337
333
|
)
|
|
338
334
|
} else {
|
|
339
335
|
sig = await this.prompter.promptSignMessage(
|
|
340
336
|
{
|
|
341
|
-
chainId: chainId,
|
|
342
|
-
message: prefixedMessage,
|
|
337
|
+
chainId: request.chainId,
|
|
338
|
+
message: ethers.getBytes(prefixedMessage),
|
|
343
339
|
eip6492: sequenceVerified
|
|
344
340
|
},
|
|
345
341
|
this.connectOptions
|
|
@@ -347,12 +343,11 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
347
343
|
}
|
|
348
344
|
|
|
349
345
|
if (sig && sig.length > 0) {
|
|
350
|
-
|
|
346
|
+
return sig
|
|
351
347
|
} else {
|
|
352
348
|
// The user has declined the request when value is null
|
|
353
349
|
throw new Error('declined by user')
|
|
354
350
|
}
|
|
355
|
-
break
|
|
356
351
|
}
|
|
357
352
|
|
|
358
353
|
case 'sequence_signTypedData_v4':
|
|
@@ -386,13 +381,13 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
386
381
|
typedData.domain,
|
|
387
382
|
typedData.types,
|
|
388
383
|
typedData.message,
|
|
389
|
-
chainId ?? this.defaultChainId(),
|
|
384
|
+
request.chainId ?? this.defaultChainId(),
|
|
390
385
|
sequenceVerified ? 'eip6492' : 'ignore'
|
|
391
386
|
)
|
|
392
387
|
} else {
|
|
393
388
|
sig = await this.prompter.promptSignMessage(
|
|
394
389
|
{
|
|
395
|
-
chainId: chainId,
|
|
390
|
+
chainId: request.chainId,
|
|
396
391
|
typedData: typedData,
|
|
397
392
|
eip6492: sequenceVerified
|
|
398
393
|
},
|
|
@@ -401,12 +396,11 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
401
396
|
}
|
|
402
397
|
|
|
403
398
|
if (sig && sig.length > 0) {
|
|
404
|
-
|
|
399
|
+
return sig
|
|
405
400
|
} else {
|
|
406
401
|
// The user has declined the request when value is null
|
|
407
402
|
throw new Error('declined by user')
|
|
408
403
|
}
|
|
409
|
-
break
|
|
410
404
|
}
|
|
411
405
|
|
|
412
406
|
case 'eth_sendTransaction': {
|
|
@@ -427,26 +421,25 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
427
421
|
let txnHash = ''
|
|
428
422
|
if (this.prompter === null) {
|
|
429
423
|
// prompter is null, so we'll send from here
|
|
430
|
-
const txnResponse = await account.sendTransaction(transactionParams, chainId ?? this.defaultChainId())
|
|
424
|
+
const txnResponse = await account.sendTransaction(transactionParams, request.chainId ?? this.defaultChainId())
|
|
431
425
|
txnHash = txnResponse?.hash ?? ''
|
|
432
426
|
} else {
|
|
433
427
|
// prompt user to provide the response
|
|
434
|
-
txnHash = await this.prompter.promptSendTransaction(transactionParams, chainId, this.connectOptions)
|
|
428
|
+
txnHash = await this.prompter.promptSendTransaction(transactionParams, request.chainId, this.connectOptions)
|
|
435
429
|
}
|
|
436
430
|
|
|
437
431
|
if (txnHash) {
|
|
438
|
-
|
|
432
|
+
return txnHash
|
|
439
433
|
} else {
|
|
440
434
|
// The user has declined the request when value is null
|
|
441
435
|
throw new Error('declined by user')
|
|
442
436
|
}
|
|
443
|
-
break
|
|
444
437
|
}
|
|
445
438
|
|
|
446
439
|
case 'eth_signTransaction': {
|
|
447
440
|
// https://eth.wiki/json-rpc/API#eth_signTransaction
|
|
448
441
|
const [transaction] = request.params!
|
|
449
|
-
const sender = ethers.
|
|
442
|
+
const sender = ethers.getAddress(transaction.from)
|
|
450
443
|
|
|
451
444
|
if (sender !== account.address) {
|
|
452
445
|
throw new Error('sender address does not match wallet')
|
|
@@ -460,12 +453,10 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
460
453
|
//
|
|
461
454
|
// TODO: verify serializing / transporting the SignedTransaction object works as expected, most likely however
|
|
462
455
|
// we will want to resolveProperties the big number values to hex strings
|
|
463
|
-
|
|
456
|
+
return await account.signTransactions(transaction, request.chainId ?? this.defaultChainId())
|
|
464
457
|
} else {
|
|
465
|
-
|
|
458
|
+
return await this.prompter.promptSignTransaction(transaction, request.chainId, this.connectOptions)
|
|
466
459
|
}
|
|
467
|
-
|
|
468
|
-
break
|
|
469
460
|
}
|
|
470
461
|
|
|
471
462
|
case 'eth_sendRawTransaction': {
|
|
@@ -474,71 +465,60 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
474
465
|
|
|
475
466
|
// https://eth.wiki/json-rpc/API#eth_sendRawTransaction
|
|
476
467
|
if (commons.transaction.isSignedTransactionBundle(request.params![0])) {
|
|
477
|
-
const txChainId =
|
|
468
|
+
const txChainId = Number(request.params![0].chainId)
|
|
478
469
|
const tx = await account.relayer(txChainId)!.relay(request.params![0])
|
|
479
|
-
|
|
470
|
+
return tx.hash
|
|
480
471
|
} else {
|
|
481
|
-
const tx = await provider.
|
|
482
|
-
|
|
472
|
+
const tx = await provider.broadcastTransaction(request.params![0])
|
|
473
|
+
return tx.hash
|
|
483
474
|
}
|
|
484
|
-
break
|
|
485
475
|
}
|
|
486
476
|
|
|
487
477
|
case 'eth_getTransactionCount': {
|
|
488
|
-
const address = ethers.
|
|
478
|
+
const address = ethers.getAddress(request.params![0] as string)
|
|
489
479
|
const tag = request.params![1]
|
|
490
480
|
|
|
491
481
|
// TODO: Maybe we should fetch this data from the relayer or from the reader
|
|
492
482
|
// but for now we keep it simple and just use the provider
|
|
493
483
|
|
|
494
484
|
const count = await provider.getTransactionCount(address, tag)
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
break
|
|
485
|
+
return toHexString(BigInt(count))
|
|
498
486
|
}
|
|
499
487
|
|
|
500
488
|
case 'eth_blockNumber': {
|
|
501
|
-
|
|
502
|
-
break
|
|
489
|
+
return await provider.getBlockNumber()
|
|
503
490
|
}
|
|
504
491
|
|
|
505
492
|
case 'eth_getBlockByNumber': {
|
|
506
|
-
|
|
507
|
-
break
|
|
493
|
+
return await provider.getBlock(request.params![0] /* , jsonRpcRequest.params[1] */)
|
|
508
494
|
}
|
|
509
495
|
|
|
510
496
|
case 'eth_getBlockByHash': {
|
|
511
|
-
|
|
512
|
-
break
|
|
497
|
+
return await provider.getBlock(request.params![0] /* , jsonRpcRequest.params[1] */)
|
|
513
498
|
}
|
|
514
499
|
|
|
515
500
|
case 'eth_getTransactionByHash': {
|
|
516
|
-
|
|
517
|
-
break
|
|
501
|
+
return await provider.getTransaction(request.params![0])
|
|
518
502
|
}
|
|
519
503
|
|
|
520
504
|
case 'eth_call': {
|
|
521
505
|
const [transactionObject, blockTag] = request.params!
|
|
522
|
-
|
|
523
|
-
break
|
|
506
|
+
return await provider.call({ ...transactionObject, blockTag })
|
|
524
507
|
}
|
|
525
508
|
|
|
526
509
|
case 'eth_getCode': {
|
|
527
510
|
const [contractAddress, blockTag] = request.params!
|
|
528
|
-
|
|
529
|
-
break
|
|
511
|
+
return await provider.getCode(contractAddress, blockTag)
|
|
530
512
|
}
|
|
531
513
|
|
|
532
514
|
case 'eth_estimateGas': {
|
|
533
515
|
const [transactionObject] = request.params!
|
|
534
|
-
|
|
535
|
-
break
|
|
516
|
+
return await provider.estimateGas(transactionObject)
|
|
536
517
|
}
|
|
537
518
|
|
|
538
519
|
case 'eth_gasPrice': {
|
|
539
|
-
const
|
|
540
|
-
|
|
541
|
-
break
|
|
520
|
+
const feeData = await provider.getFeeData()
|
|
521
|
+
return feeData.gasPrice === null ? null : toHexString(feeData.gasPrice)
|
|
542
522
|
}
|
|
543
523
|
|
|
544
524
|
case 'wallet_switchEthereumChain': {
|
|
@@ -547,34 +527,31 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
547
527
|
throw new Error('invalid chainId')
|
|
548
528
|
}
|
|
549
529
|
|
|
550
|
-
const chainId =
|
|
530
|
+
const chainId = BigInt(switchParams.chainId)
|
|
551
531
|
|
|
552
|
-
this.setDefaultChainId(chainId
|
|
532
|
+
this.setDefaultChainId(Number(chainId))
|
|
553
533
|
|
|
554
|
-
|
|
555
|
-
break
|
|
534
|
+
return null // success
|
|
556
535
|
}
|
|
557
536
|
|
|
558
537
|
// smart wallet method
|
|
559
538
|
case 'sequence_getWalletContext': {
|
|
560
|
-
|
|
561
|
-
break
|
|
539
|
+
return account.contexts
|
|
562
540
|
}
|
|
563
541
|
|
|
564
542
|
// smart wallet method
|
|
565
543
|
case 'sequence_getWalletConfig': {
|
|
566
544
|
const [chainId] = request.params!
|
|
567
545
|
if (chainId) {
|
|
568
|
-
|
|
546
|
+
return [(await account.status(chainId)).onChain.config]
|
|
569
547
|
} else {
|
|
570
|
-
|
|
548
|
+
return await Promise.all(
|
|
571
549
|
account.networks.map(async network => {
|
|
572
550
|
const status = await account.status(network.chainId)
|
|
573
551
|
return status.onChain.config
|
|
574
552
|
})
|
|
575
553
|
)
|
|
576
554
|
}
|
|
577
|
-
break
|
|
578
555
|
}
|
|
579
556
|
|
|
580
557
|
// smart wallet method
|
|
@@ -582,16 +559,15 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
582
559
|
const [chainId] = request.params!
|
|
583
560
|
// TODO: Add getWalletState to the Signer interface
|
|
584
561
|
if (chainId) {
|
|
585
|
-
|
|
562
|
+
return [getLegacyWalletState(chainId, await account.status(chainId))]
|
|
586
563
|
} else {
|
|
587
|
-
|
|
564
|
+
return await Promise.all(
|
|
588
565
|
account.networks.map(async network => {
|
|
589
566
|
const status = await account.status(network.chainId)
|
|
590
567
|
return getLegacyWalletState(network.chainId, status)
|
|
591
568
|
})
|
|
592
569
|
)
|
|
593
570
|
}
|
|
594
|
-
break
|
|
595
571
|
}
|
|
596
572
|
|
|
597
573
|
// smart wallet method
|
|
@@ -599,13 +575,11 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
599
575
|
// NOTE: must ensure that the response result below returns clean serialized data, which is to omit
|
|
600
576
|
// the provider and relayer objects and only return the urls so can be reinstantiated on dapp side.
|
|
601
577
|
// This is handled by this.getNetworks() but noted here for future readers.
|
|
602
|
-
|
|
603
|
-
break
|
|
578
|
+
return await this.getNetworks(true)
|
|
604
579
|
}
|
|
605
580
|
|
|
606
581
|
case 'sequence_isSequence': {
|
|
607
|
-
|
|
608
|
-
break
|
|
582
|
+
return true
|
|
609
583
|
}
|
|
610
584
|
|
|
611
585
|
// smart wallet method
|
|
@@ -616,14 +590,12 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
616
590
|
// for confirmation.
|
|
617
591
|
//
|
|
618
592
|
// const [newConfig] = request.params
|
|
619
|
-
//
|
|
620
|
-
break
|
|
593
|
+
// return await signer.updateConfig(newConfig)
|
|
621
594
|
}
|
|
622
595
|
|
|
623
596
|
// smart wallet method
|
|
624
597
|
case 'sequence_publishConfig': {
|
|
625
598
|
throw new Error('sequence_publishConfig method is not allowed from a dapp')
|
|
626
|
-
break
|
|
627
599
|
}
|
|
628
600
|
|
|
629
601
|
// relayer method
|
|
@@ -653,8 +625,7 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
653
625
|
}
|
|
654
626
|
|
|
655
627
|
this.setDefaultChainId(defaultChainId)
|
|
656
|
-
|
|
657
|
-
break
|
|
628
|
+
return await this.getNetworks(true)
|
|
658
629
|
}
|
|
659
630
|
|
|
660
631
|
default: {
|
|
@@ -663,22 +634,13 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
663
634
|
}
|
|
664
635
|
|
|
665
636
|
// NOTE: provider here will be chain-bound if chainId is provided
|
|
666
|
-
|
|
667
|
-
response.result = providerResponse
|
|
637
|
+
return await jsonRpcProvider.send(request.method, request.params!)
|
|
668
638
|
}
|
|
669
639
|
}
|
|
670
640
|
} catch (err) {
|
|
671
641
|
logger.error(err)
|
|
672
|
-
|
|
673
|
-
// See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1193.md#rpc-errors
|
|
674
|
-
response.result = null
|
|
675
|
-
response.error = {
|
|
676
|
-
...new Error(err),
|
|
677
|
-
code: 4001
|
|
678
|
-
}
|
|
642
|
+
throw { message: err?.message, code: 4001 } as ProviderRpcError
|
|
679
643
|
}
|
|
680
|
-
|
|
681
|
-
callback(undefined, response)
|
|
682
644
|
}
|
|
683
645
|
|
|
684
646
|
on<K extends keyof ProviderEventTypes>(event: K, fn: ProviderEventTypes[K]) {
|
|
@@ -707,7 +669,6 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
707
669
|
|
|
708
670
|
setConnectOptions(options: ConnectOptions | undefined) {
|
|
709
671
|
this._connectOptions = options
|
|
710
|
-
|
|
711
672
|
this.onConnectOptionsChange?.(options)
|
|
712
673
|
}
|
|
713
674
|
|
|
@@ -776,7 +737,7 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
776
737
|
}
|
|
777
738
|
|
|
778
739
|
notifyChainChanged(chainId: number, origin?: string) {
|
|
779
|
-
this.events.emit('chainChanged', ethers.
|
|
740
|
+
this.events.emit('chainChanged', ethers.toQuantity(chainId), origin)
|
|
780
741
|
}
|
|
781
742
|
|
|
782
743
|
async notifyNetworks(networks?: NetworkConfig[]) {
|
|
@@ -785,7 +746,7 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
|
|
|
785
746
|
if (n.length > 0) {
|
|
786
747
|
const defaultNetwork = n.find(network => network.chainId === this.defaultChainId())
|
|
787
748
|
if (defaultNetwork) {
|
|
788
|
-
this.events.emit('chainChanged', ethers.
|
|
749
|
+
this.events.emit('chainChanged', ethers.toQuantity(defaultNetwork.chainId))
|
|
789
750
|
}
|
|
790
751
|
} else {
|
|
791
752
|
this.events.emit('chainChanged', '0x0')
|
|
@@ -10,7 +10,8 @@ import {
|
|
|
10
10
|
} from '../../types'
|
|
11
11
|
import { WalletRequestHandler } from '../wallet-request-handler'
|
|
12
12
|
import { BaseWalletTransport } from '../base-wallet-transport'
|
|
13
|
-
import { logger,
|
|
13
|
+
import { logger, base64DecodeObject } from '@0xsequence/utils'
|
|
14
|
+
import { ethers } from 'ethers'
|
|
14
15
|
|
|
15
16
|
export class WindowMessageHandler extends BaseWalletTransport {
|
|
16
17
|
protected parentWindow: Window
|
|
@@ -30,7 +31,7 @@ export class WindowMessageHandler extends BaseWalletTransport {
|
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
// record open details (sessionId + default network) from the window url
|
|
33
|
-
const {
|
|
34
|
+
const { search: rawParams } = new URL(windowHref || window.location.href)
|
|
34
35
|
|
|
35
36
|
let session: TransportSession | null = this.getWindowTransportSession(rawParams)
|
|
36
37
|
|
|
@@ -92,7 +93,14 @@ export class WindowMessageHandler extends BaseWalletTransport {
|
|
|
92
93
|
// Wallet always expects json-rpc request messages from a dapp
|
|
93
94
|
let request: ProviderMessageRequest
|
|
94
95
|
try {
|
|
95
|
-
request = JSON.parse(event.data)
|
|
96
|
+
request = JSON.parse(event.data, (key, value) => {
|
|
97
|
+
// BigNumber compatibility with older versions of sequence.js
|
|
98
|
+
if (isBigNumberSerialized(value)) {
|
|
99
|
+
return BigInt(value.hex)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return value
|
|
103
|
+
})
|
|
96
104
|
} catch (err) {
|
|
97
105
|
// event is not a ProviderMessage JSON object, skip
|
|
98
106
|
return
|
|
@@ -161,3 +169,7 @@ export class WindowMessageHandler extends BaseWalletTransport {
|
|
|
161
169
|
}
|
|
162
170
|
}
|
|
163
171
|
}
|
|
172
|
+
|
|
173
|
+
const isBigNumberSerialized = (value: any): boolean => {
|
|
174
|
+
return typeof value === 'object' && value.type === 'BigNumber' && ethers.isHexString(value.hex)
|
|
175
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { OpenWalletIntent, ProviderMessage, InitState,
|
|
1
|
+
import { OpenWalletIntent, ProviderMessage, InitState, WindowSessionParams } from '../../types'
|
|
2
2
|
import { BaseProviderTransport } from '../base-provider-transport'
|
|
3
3
|
import { logger, base64EncodeObject } from '@0xsequence/utils'
|
|
4
4
|
import { isBrowserExtension, isUnityPlugin } from '../../utils'
|
package/src/types.ts
CHANGED
|
@@ -2,15 +2,15 @@ import { ETHAuthProof as AuthETHAuthProof } from '@0xsequence/auth'
|
|
|
2
2
|
import { commons } from '@0xsequence/core'
|
|
3
3
|
import {
|
|
4
4
|
ChainIdLike,
|
|
5
|
-
|
|
5
|
+
EIP1193Provider,
|
|
6
6
|
JsonRpcRequest,
|
|
7
7
|
JsonRpcResponse,
|
|
8
8
|
NetworkConfig,
|
|
9
|
-
|
|
9
|
+
JsonRpcErrorPayload
|
|
10
10
|
} from '@0xsequence/network'
|
|
11
11
|
import { TypedData } from '@0xsequence/utils'
|
|
12
12
|
|
|
13
|
-
export interface ProviderTransport extends
|
|
13
|
+
export interface ProviderTransport extends EIP1193Provider, ProviderMessageTransport, ProviderMessageRequestHandler {
|
|
14
14
|
register(): void
|
|
15
15
|
unregister(): void
|
|
16
16
|
|
|
@@ -42,7 +42,7 @@ export function isProviderTransport(transport: any): transport is ProviderTransp
|
|
|
42
42
|
)
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
export interface WalletTransport extends
|
|
45
|
+
export interface WalletTransport extends EIP1193Provider, ProviderMessageTransport, ProviderMessageRequestHandler {
|
|
46
46
|
register(): void
|
|
47
47
|
unregister(): void
|
|
48
48
|
|
|
@@ -61,18 +61,20 @@ export interface ProviderMessage<T> {
|
|
|
61
61
|
data: T // the ethereum json-rpc payload
|
|
62
62
|
chainId?: number // chain id which the message is intended
|
|
63
63
|
origin?: string // origin of the message
|
|
64
|
+
clientVersion: string // client version of the message
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
export type ProviderMessageRequest = ProviderMessage<JsonRpcRequest>
|
|
67
68
|
|
|
68
|
-
|
|
69
|
+
// Older versions of sequence.js will require a JsonRpcResponse result type, but newer versions use raw EIP1193 results
|
|
70
|
+
export type ProviderMessageResponse = ProviderMessage<JsonRpcResponse | any>
|
|
69
71
|
|
|
70
72
|
// ProviderMessageCallback is used to respond to ProviderMessage requests. The error
|
|
71
73
|
// argument is for exceptions during the execution, and response is the response payload
|
|
72
74
|
// which may contain the result or an error payload from the wallet.
|
|
73
75
|
export type ProviderMessageResponseCallback = (error?: ProviderRpcError, response?: ProviderMessageResponse) => void
|
|
74
76
|
|
|
75
|
-
export type ProviderRpcError =
|
|
77
|
+
export type ProviderRpcError = JsonRpcErrorPayload
|
|
76
78
|
|
|
77
79
|
export interface ProviderMessageRequestHandler {
|
|
78
80
|
// sendMessageRequest sends a ProviderMessageRequest over the wire to the wallet.
|
|
@@ -373,8 +375,3 @@ export type OptionalEIP6492 =
|
|
|
373
375
|
eip6492?: boolean
|
|
374
376
|
}
|
|
375
377
|
| undefined
|
|
376
|
-
|
|
377
|
-
// This is required by viem, it expects a provider to have an EIP-1193 compliant `request` attribute.
|
|
378
|
-
export interface EIP1193Provider {
|
|
379
|
-
request: (request: { method: string; params?: Array<any> }) => Promise<any>
|
|
380
|
-
}
|
package/src/utils/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ethers } from 'ethers'
|
|
2
2
|
import { ChainIdLike } from '@0xsequence/network'
|
|
3
3
|
import { encodeMessageDigest, TypedData, encodeTypedDataDigest } from '@0xsequence/utils'
|
|
4
4
|
import { isValidSignature, prefixEIP191Message } from '../utils'
|
|
@@ -17,14 +17,14 @@ export class WalletUtils {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
// Sign message on a specified chain, or DefaultChain by default
|
|
20
|
-
signMessage(message: BytesLike, chainId?: ChainIdLike, eip6492?: boolean): Promise<string> {
|
|
20
|
+
signMessage(message: ethers.BytesLike, chainId?: ChainIdLike, eip6492?: boolean): Promise<string> {
|
|
21
21
|
return this.signer.signMessage(message, { chainId, eip6492 })
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// Sign EIP-712 TypedData on a specified chain, or DefaultChain by default
|
|
25
25
|
signTypedData(
|
|
26
|
-
domain: TypedDataDomain,
|
|
27
|
-
types: Record<string, Array<TypedDataField>>,
|
|
26
|
+
domain: ethers.TypedDataDomain,
|
|
27
|
+
types: Record<string, Array<ethers.TypedDataField>>,
|
|
28
28
|
message: Record<string, any>,
|
|
29
29
|
chainId?: ChainIdLike,
|
|
30
30
|
eip6492?: boolean
|