Skip to content

Commit 189ba2a

Browse files
committed
wallet provider
1 parent db7e39c commit 189ba2a

3 files changed

Lines changed: 25 additions & 32 deletions

File tree

packages/provider/src/wallet-request-handler.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,14 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
6767

6868
// TODO: if signer account is empty, aka user is not logged in to the wallet,
6969
// then prevent the use of many of the methods below.
70+
const loggedIn = true // ..
7071

7172
try {
7273

74+
if (!loggedIn && !publicJsonRpcMethods.includes(request.method)) {
75+
throw new Error(`not logged in. ${request.method} is unavailable`)
76+
}
77+
7378
// wallet signer
7479
const signer = this.signer
7580
if (!signer) throw new Error('WalletRequestHandler: wallet signer is not configured')
@@ -441,3 +446,11 @@ export interface WalletUserPrompter {
441446
promptSignTransaction(txs: TransactionRequest, chaindId?: number): Promise<string>
442447
promptSendTransaction(txs: TransactionRequest, chaindId?: number): Promise<string>
443448
}
449+
450+
const publicJsonRpcMethods = [
451+
'net_version', 'eth_chainId', 'eth_getBalance', 'eth_getTransactionCount',
452+
'eth_blockNumber', 'eth_getBlockByNumber', 'eth_getBlockByHash', 'eth_getTransactionByHash',
453+
'eth_getCode', 'eth_estimateGas', 'eth_gasPrice',
454+
455+
'sequence_getWalletContext', 'sequence_getNetworks', 'sequence_setDefaultChain'
456+
]

packages/provider/src/wallet.ts

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export class Wallet implements WalletProvider {
5151
router?: JsonRpcRouter
5252
allowProvider?: JsonRpcMiddleware
5353
cachedProvider?: CachedProvider
54-
publicProvider?: PublicProvider
5554

5655
windowMessageProvider?: WindowMessageProvider
5756
proxyMessageProvider?: ProxyMessageProvider // TODO ..
@@ -114,11 +113,6 @@ export class Wallet implements WalletProvider {
114113
// Provider proxy to support middleware stack of logging, caching and read-only rpc calls
115114
this.transport.cachedProvider = new CachedProvider()
116115

117-
// ..
118-
// TODO: might not need this since getProvider() could do this for us..?
119-
// but need to check chainId, etc. etc.. check later
120-
// this.publicProvider = new PublicProvider()
121-
122116
// ..
123117
this.transport.windowMessageProvider = new WindowMessageProvider(this.config.walletAppURL)
124118
this.transport.windowMessageProvider.register()
@@ -129,7 +123,6 @@ export class Wallet implements WalletProvider {
129123
this.transport.allowProvider,
130124
exceptionProviderMiddleware,
131125
this.transport.cachedProvider,
132-
// this.publicProvider
133126
], this.transport.windowMessageProvider)
134127

135128
this.transport.provider = new Web3Provider(this.transport.router)
@@ -250,12 +243,6 @@ export class Wallet implements WalletProvider {
250243
}
251244

252245
getNetworks = async (chainId?: ChainId): Promise<NetworkConfig[]> => {
253-
// TODO: we should store the networks list in the session, but allow it to be refreshed here
254-
// as well, consider an interface where we update the session whenever we call up the wallet window (for any reason)
255-
// after some time (ie 1 hour)
256-
257-
// return this.getSigner().getNetworks()
258-
259246
if (!this.isLoggedIn()) {
260247
throw new Error('login first')
261248
}
@@ -292,8 +279,8 @@ export class Wallet implements WalletProvider {
292279

293280
await this.transport.windowMessageProvider.waitUntilConnected()
294281

295-
// setDefaultNetworkId
296-
// it's important to send this right away upon connection
282+
// setDefaultChain - it's important to send this right away upon connection. This will also
283+
// update the network list in the session each time the wallet is opened & connected.
297284
const networks = await this.transport.provider.send('sequence_setDefaultChain', [this.config.defaultNetworkId])
298285
this.useNetworks(networks)
299286

@@ -310,28 +297,21 @@ export class Wallet implements WalletProvider {
310297

311298
getProvider(chainId?: ChainId): Web3Provider | undefined {
312299
// return the top-level provider message transport when chainId is unspecified
300+
// and user has not logged in
301+
if (chainId && !this.isLoggedIn()) {
302+
throw new Error(`session is empty. login and try again.`)
303+
}
313304
if (!chainId) {
314-
315-
316-
// TODO: for the "defaultNetwork", we'll also have an entry here..
317-
// maybe we should even return that instead of this.provider, as it will have built-in
318-
// public-provider and other stuff..?
319-
// if (this.networks && this.networks.length > 0 && this.providers[this.networks[0].chainId]) {
320-
// return this.providers[this.networks[0].chainId]
321-
// }
322-
// or... we set chainId = this.networks[0].chainId ..
323-
324305
return this.transport.provider
325306
}
326-
327-
if (!this.isLoggedIn()) {
328-
throw new Error(`session is empty. login and try again.`)
329-
}
330307
if (this.session.networks.length === 0) {
331308
throw new Error('networks list is empty. upon logging in, networks should be populated')
332309
}
333310

334-
const network = findNetworkConfig(this.session.networks, chainId)
311+
let network = this.session.networks[0]
312+
if (chainId) {
313+
network = findNetworkConfig(this.session.networks, chainId)
314+
}
335315

336316
// return memoized network provider
337317
if (this.providers[network.chainId]) {

packages/wallet/src/account.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ export class Account extends Signer {
109109

110110
getProvider(chainId?: number): Promise<JsonRpcProvider | undefined> {
111111
if (!chainId) return this.mainWallet().getProvider()
112-
return this._wallets.find((w) => w.network.chainId === chainId)?.wallet.getProvider()
112+
return this._wallets.find(w => w.network.chainId === chainId)?.wallet.getProvider()
113113
}
114114

115115
getRelayer(chainId?: number): Promise<Relayer | undefined> {
116116
if (!chainId) return this.mainWallet().getRelayer()
117-
return this._wallets.find((w) => w.network.chainId === chainId)?.wallet.getRelayer()
117+
return this._wallets.find(w => w.network.chainId === chainId)?.wallet.getRelayer()
118118
}
119119

120120
async getNetworks(): Promise<NetworkConfig[]> {

0 commit comments

Comments
 (0)