@0xsequence/provider 0.0.0-20230320165616 → 0.0.0-20230323151415

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.
@@ -6,8 +6,8 @@ var eventemitter2 = require('eventemitter2');
6
6
  var utils = require('@0xsequence/utils');
7
7
  var ethers = require('ethers');
8
8
  var core = require('@0xsequence/core');
9
- var auth = require('@0xsequence/auth');
10
9
  var network = require('@0xsequence/network');
10
+ var auth = require('@0xsequence/auth');
11
11
  var wallet = require('@0xsequence/wallet');
12
12
 
13
13
  function _extends() {
@@ -1774,19 +1774,23 @@ class WalletRequestHandler {
1774
1774
  // fetch the provider for the specific chain, or undefined will select defaultChain
1775
1775
  const provider = (_this$account = _this.account) == null ? void 0 : _this$account.provider(chainId != null ? chainId : _this.defaultNetworkId);
1776
1776
  if (!provider) throw new Error(`WalletRequestHandler: wallet provider is not configured for chainId ${chainId}`);
1777
- if (provider.send === undefined) {
1778
- throw new Error(`Account provider doesn't support send method`);
1779
- }
1777
+ const jsonRpcProvider = provider instanceof ethers.ethers.providers.JsonRpcProvider ? provider : undefined;
1780
1778
  switch (request.method) {
1781
1779
  case 'net_version':
1782
1780
  {
1783
- const result = await provider.send('net_version', []);
1781
+ if (!jsonRpcProvider) {
1782
+ throw new Error(`Account provider doesn't support send method`);
1783
+ }
1784
+ const result = await jsonRpcProvider.send('net_version', []);
1784
1785
  response.result = result;
1785
1786
  break;
1786
1787
  }
1787
1788
  case 'eth_chainId':
1788
1789
  {
1789
- const result = await provider.send('eth_chainId', []);
1790
+ if (!jsonRpcProvider) {
1791
+ throw new Error(`Account provider doesn't support send method`);
1792
+ }
1793
+ const result = await jsonRpcProvider.send('eth_chainId', []);
1790
1794
  response.result = result;
1791
1795
  break;
1792
1796
  }
@@ -2106,8 +2110,12 @@ class WalletRequestHandler {
2106
2110
  }
2107
2111
  default:
2108
2112
  {
2113
+ if (!jsonRpcProvider) {
2114
+ throw new Error(`Account provider doesn't support send method`);
2115
+ }
2116
+
2109
2117
  // NOTE: provider here will be chain-bound if chainId is provided
2110
- const providerResponse = await provider.send(request.method, request.params);
2118
+ const providerResponse = await jsonRpcProvider.send(request.method, request.params);
2111
2119
  response.result = providerResponse;
2112
2120
  }
2113
2121
  }
@@ -2216,9 +2224,21 @@ class WalletRequestHandler {
2216
2224
  error: 'unable to connect without signed in account'
2217
2225
  };
2218
2226
  }
2227
+ let chainId;
2228
+ switch (typeof (options == null ? void 0 : options.networkId)) {
2229
+ case 'string':
2230
+ chainId = network.getChainId(options.networkId);
2231
+ break;
2232
+ case 'number':
2233
+ chainId = options.networkId;
2234
+ break;
2235
+ default:
2236
+ chainId = this.defaultNetworkId;
2237
+ break;
2238
+ }
2219
2239
  const connectDetails = {
2220
2240
  connected: true,
2221
- chainId: ethers.ethers.BigNumber.from(this.defaultNetworkId).toHexString()
2241
+ chainId: ethers.ethers.BigNumber.from(chainId).toHexString()
2222
2242
  };
2223
2243
  if (options && options.askForEmail) {
2224
2244
  var _this$auxDataProvider;
@@ -2241,7 +2261,7 @@ class WalletRequestHandler {
2241
2261
 
2242
2262
  try {
2243
2263
  // TODO: Either implement account as a signer, or change signAuthorization to accept an account
2244
- connectDetails.proof = await auth.signAuthorization(this.account, this.defaultNetworkId, authOptions);
2264
+ connectDetails.proof = await auth.signAuthorization(this.account, chainId, authOptions);
2245
2265
  } catch (err) {
2246
2266
  utils.logger.warn(`connect, signAuthorization failed for options: ${JSON.stringify(options)}, due to: ${err.message}`);
2247
2267
  return {
@@ -6,8 +6,8 @@ var eventemitter2 = require('eventemitter2');
6
6
  var utils = require('@0xsequence/utils');
7
7
  var ethers = require('ethers');
8
8
  var core = require('@0xsequence/core');
9
- var auth = require('@0xsequence/auth');
10
9
  var network = require('@0xsequence/network');
10
+ var auth = require('@0xsequence/auth');
11
11
  var wallet = require('@0xsequence/wallet');
12
12
 
13
13
  function _extends() {
@@ -1774,19 +1774,23 @@ class WalletRequestHandler {
1774
1774
  // fetch the provider for the specific chain, or undefined will select defaultChain
1775
1775
  const provider = (_this$account = _this.account) == null ? void 0 : _this$account.provider(chainId != null ? chainId : _this.defaultNetworkId);
1776
1776
  if (!provider) throw new Error(`WalletRequestHandler: wallet provider is not configured for chainId ${chainId}`);
1777
- if (provider.send === undefined) {
1778
- throw new Error(`Account provider doesn't support send method`);
1779
- }
1777
+ const jsonRpcProvider = provider instanceof ethers.ethers.providers.JsonRpcProvider ? provider : undefined;
1780
1778
  switch (request.method) {
1781
1779
  case 'net_version':
1782
1780
  {
1783
- const result = await provider.send('net_version', []);
1781
+ if (!jsonRpcProvider) {
1782
+ throw new Error(`Account provider doesn't support send method`);
1783
+ }
1784
+ const result = await jsonRpcProvider.send('net_version', []);
1784
1785
  response.result = result;
1785
1786
  break;
1786
1787
  }
1787
1788
  case 'eth_chainId':
1788
1789
  {
1789
- const result = await provider.send('eth_chainId', []);
1790
+ if (!jsonRpcProvider) {
1791
+ throw new Error(`Account provider doesn't support send method`);
1792
+ }
1793
+ const result = await jsonRpcProvider.send('eth_chainId', []);
1790
1794
  response.result = result;
1791
1795
  break;
1792
1796
  }
@@ -2106,8 +2110,12 @@ class WalletRequestHandler {
2106
2110
  }
2107
2111
  default:
2108
2112
  {
2113
+ if (!jsonRpcProvider) {
2114
+ throw new Error(`Account provider doesn't support send method`);
2115
+ }
2116
+
2109
2117
  // NOTE: provider here will be chain-bound if chainId is provided
2110
- const providerResponse = await provider.send(request.method, request.params);
2118
+ const providerResponse = await jsonRpcProvider.send(request.method, request.params);
2111
2119
  response.result = providerResponse;
2112
2120
  }
2113
2121
  }
@@ -2216,9 +2224,21 @@ class WalletRequestHandler {
2216
2224
  error: 'unable to connect without signed in account'
2217
2225
  };
2218
2226
  }
2227
+ let chainId;
2228
+ switch (typeof (options == null ? void 0 : options.networkId)) {
2229
+ case 'string':
2230
+ chainId = network.getChainId(options.networkId);
2231
+ break;
2232
+ case 'number':
2233
+ chainId = options.networkId;
2234
+ break;
2235
+ default:
2236
+ chainId = this.defaultNetworkId;
2237
+ break;
2238
+ }
2219
2239
  const connectDetails = {
2220
2240
  connected: true,
2221
- chainId: ethers.ethers.BigNumber.from(this.defaultNetworkId).toHexString()
2241
+ chainId: ethers.ethers.BigNumber.from(chainId).toHexString()
2222
2242
  };
2223
2243
  if (options && options.askForEmail) {
2224
2244
  var _this$auxDataProvider;
@@ -2241,7 +2261,7 @@ class WalletRequestHandler {
2241
2261
 
2242
2262
  try {
2243
2263
  // TODO: Either implement account as a signer, or change signAuthorization to accept an account
2244
- connectDetails.proof = await auth.signAuthorization(this.account, this.defaultNetworkId, authOptions);
2264
+ connectDetails.proof = await auth.signAuthorization(this.account, chainId, authOptions);
2245
2265
  } catch (err) {
2246
2266
  utils.logger.warn(`connect, signAuthorization failed for options: ${JSON.stringify(options)}, due to: ${err.message}`);
2247
2267
  return {
@@ -2,8 +2,8 @@ import { EventEmitter2 } from 'eventemitter2';
2
2
  import { logger, sanitizeNumberString, sanitizeHost, sanitizeAlphanumeric, base64EncodeObject, base64DecodeObject, shallowCopy, resolveProperties, encodeMessageDigest, encodeTypedDataDigest } from '@0xsequence/utils';
3
3
  import { ethers, BigNumber, providers } from 'ethers';
4
4
  import { commons, universal } from '@0xsequence/core';
5
+ import { getChainId, JsonRpcSender, maybeChainId, allowProviderMiddleware, networkProviderMiddleware, findNetworkConfig, CachedProvider, JsonRpcRouter, loggingProviderMiddleware, exceptionProviderMiddleware, findSupportedNetwork, sortNetworks, EagerProvider, SigningProvider, updateNetworkConfig, ensureValidNetworks } from '@0xsequence/network';
5
6
  import { signAuthorization } from '@0xsequence/auth';
6
- import { JsonRpcSender, maybeChainId, allowProviderMiddleware, networkProviderMiddleware, findNetworkConfig, CachedProvider, JsonRpcRouter, loggingProviderMiddleware, exceptionProviderMiddleware, findSupportedNetwork, sortNetworks, EagerProvider, SigningProvider, updateNetworkConfig, ensureValidNetworks } from '@0xsequence/network';
7
7
  import { Signer, resolveArrayProperties } from '@0xsequence/wallet';
8
8
 
9
9
  function _extends() {
@@ -1770,19 +1770,23 @@ class WalletRequestHandler {
1770
1770
  // fetch the provider for the specific chain, or undefined will select defaultChain
1771
1771
  const provider = (_this$account = _this.account) == null ? void 0 : _this$account.provider(chainId != null ? chainId : _this.defaultNetworkId);
1772
1772
  if (!provider) throw new Error(`WalletRequestHandler: wallet provider is not configured for chainId ${chainId}`);
1773
- if (provider.send === undefined) {
1774
- throw new Error(`Account provider doesn't support send method`);
1775
- }
1773
+ const jsonRpcProvider = provider instanceof ethers.providers.JsonRpcProvider ? provider : undefined;
1776
1774
  switch (request.method) {
1777
1775
  case 'net_version':
1778
1776
  {
1779
- const result = await provider.send('net_version', []);
1777
+ if (!jsonRpcProvider) {
1778
+ throw new Error(`Account provider doesn't support send method`);
1779
+ }
1780
+ const result = await jsonRpcProvider.send('net_version', []);
1780
1781
  response.result = result;
1781
1782
  break;
1782
1783
  }
1783
1784
  case 'eth_chainId':
1784
1785
  {
1785
- const result = await provider.send('eth_chainId', []);
1786
+ if (!jsonRpcProvider) {
1787
+ throw new Error(`Account provider doesn't support send method`);
1788
+ }
1789
+ const result = await jsonRpcProvider.send('eth_chainId', []);
1786
1790
  response.result = result;
1787
1791
  break;
1788
1792
  }
@@ -2102,8 +2106,12 @@ class WalletRequestHandler {
2102
2106
  }
2103
2107
  default:
2104
2108
  {
2109
+ if (!jsonRpcProvider) {
2110
+ throw new Error(`Account provider doesn't support send method`);
2111
+ }
2112
+
2105
2113
  // NOTE: provider here will be chain-bound if chainId is provided
2106
- const providerResponse = await provider.send(request.method, request.params);
2114
+ const providerResponse = await jsonRpcProvider.send(request.method, request.params);
2107
2115
  response.result = providerResponse;
2108
2116
  }
2109
2117
  }
@@ -2212,9 +2220,21 @@ class WalletRequestHandler {
2212
2220
  error: 'unable to connect without signed in account'
2213
2221
  };
2214
2222
  }
2223
+ let chainId;
2224
+ switch (typeof (options == null ? void 0 : options.networkId)) {
2225
+ case 'string':
2226
+ chainId = getChainId(options.networkId);
2227
+ break;
2228
+ case 'number':
2229
+ chainId = options.networkId;
2230
+ break;
2231
+ default:
2232
+ chainId = this.defaultNetworkId;
2233
+ break;
2234
+ }
2215
2235
  const connectDetails = {
2216
2236
  connected: true,
2217
- chainId: ethers.BigNumber.from(this.defaultNetworkId).toHexString()
2237
+ chainId: ethers.BigNumber.from(chainId).toHexString()
2218
2238
  };
2219
2239
  if (options && options.askForEmail) {
2220
2240
  var _this$auxDataProvider;
@@ -2237,7 +2257,7 @@ class WalletRequestHandler {
2237
2257
 
2238
2258
  try {
2239
2259
  // TODO: Either implement account as a signer, or change signAuthorization to accept an account
2240
- connectDetails.proof = await signAuthorization(this.account, this.defaultNetworkId, authOptions);
2260
+ connectDetails.proof = await signAuthorization(this.account, chainId, authOptions);
2241
2261
  } catch (err) {
2242
2262
  logger.warn(`connect, signAuthorization failed for options: ${JSON.stringify(options)}, due to: ${err.message}`);
2243
2263
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xsequence/provider",
3
- "version": "0.0.0-20230320165616",
3
+ "version": "0.0.0-20230323151415",
4
4
  "description": "provider sub-package for Sequence",
5
5
  "repository": "https://github.com/0xsequence/sequence.js/tree/master/packages/provider",
6
6
  "source": "src/index.ts",
@@ -9,15 +9,15 @@
9
9
  "author": "Horizon Blockchain Games",
10
10
  "license": "Apache-2.0",
11
11
  "dependencies": {
12
- "@0xsequence/abi": "0.0.0-20230320165616",
13
- "@0xsequence/account": "0.0.0-20230320165616",
14
- "@0xsequence/auth": "0.0.0-20230320165616",
15
- "@0xsequence/core": "0.0.0-20230320165616",
16
- "@0xsequence/migration": "0.0.0-20230320165616",
17
- "@0xsequence/network": "0.0.0-20230320165616",
18
- "@0xsequence/relayer": "0.0.0-20230320165616",
19
- "@0xsequence/utils": "0.0.0-20230320165616",
20
- "@0xsequence/wallet": "0.0.0-20230320165616",
12
+ "@0xsequence/abi": "0.0.0-20230323151415",
13
+ "@0xsequence/account": "0.0.0-20230323151415",
14
+ "@0xsequence/auth": "0.0.0-20230323151415",
15
+ "@0xsequence/core": "0.0.0-20230323151415",
16
+ "@0xsequence/migration": "0.0.0-20230323151415",
17
+ "@0xsequence/network": "0.0.0-20230323151415",
18
+ "@0xsequence/relayer": "0.0.0-20230323151415",
19
+ "@0xsequence/utils": "0.0.0-20230323151415",
20
+ "@0xsequence/wallet": "0.0.0-20230323151415",
21
21
  "eventemitter2": "^6.4.5",
22
22
  "webextension-polyfill": "^0.10.0"
23
23
  },
@@ -18,7 +18,7 @@ import {
18
18
 
19
19
  import { BigNumber, ethers, providers } from 'ethers'
20
20
 
21
- import { NetworkConfig, JsonRpcHandler, JsonRpcRequest, JsonRpcResponseCallback, JsonRpcResponse } from '@0xsequence/network'
21
+ import { NetworkConfig, JsonRpcHandler, JsonRpcRequest, JsonRpcResponseCallback, JsonRpcResponse, getChainId } from '@0xsequence/network'
22
22
  import { signAuthorization, AuthorizationOptions } from '@0xsequence/auth'
23
23
  import { logger, TypedData } from '@0xsequence/utils'
24
24
  import { commons } from '@0xsequence/core'
@@ -155,9 +155,22 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
155
155
  }
156
156
  }
157
157
 
158
+ let chainId: number
159
+ switch (typeof options?.networkId) {
160
+ case 'string':
161
+ chainId = getChainId(options.networkId)
162
+ break
163
+ case 'number':
164
+ chainId = options.networkId
165
+ break
166
+ default:
167
+ chainId = this.defaultNetworkId
168
+ break
169
+ }
170
+
158
171
  const connectDetails: ConnectDetails = {
159
172
  connected: true,
160
- chainId: ethers.BigNumber.from(this.defaultNetworkId).toHexString()
173
+ chainId: ethers.BigNumber.from(chainId).toHexString()
161
174
  }
162
175
 
163
176
  if (options && options.askForEmail) {
@@ -181,7 +194,7 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
181
194
 
182
195
  try {
183
196
  // TODO: Either implement account as a signer, or change signAuthorization to accept an account
184
- connectDetails.proof = await signAuthorization(this.account, this.defaultNetworkId, authOptions)
197
+ connectDetails.proof = await signAuthorization(this.account, chainId, authOptions)
185
198
  } catch (err) {
186
199
  logger.warn(`connect, signAuthorization failed for options: ${JSON.stringify(options)}, due to: ${err.message}`)
187
200
  return {
@@ -267,22 +280,27 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
267
280
  if (!account) throw new Error('WalletRequestHandler: wallet account is not configured')
268
281
 
269
282
  // fetch the provider for the specific chain, or undefined will select defaultChain
270
- const provider = this.account?.provider(chainId ?? this.defaultNetworkId) as ethers.providers.JsonRpcProvider
283
+ const provider = this.account?.provider(chainId ?? this.defaultNetworkId)
271
284
  if (!provider) throw new Error(`WalletRequestHandler: wallet provider is not configured for chainId ${chainId}`)
272
-
273
- if (provider.send === undefined) {
274
- throw new Error(`Account provider doesn't support send method`)
275
- }
285
+ const jsonRpcProvider = provider instanceof ethers.providers.JsonRpcProvider ? provider : undefined
276
286
 
277
287
  switch (request.method) {
278
288
  case 'net_version': {
279
- const result = await provider.send('net_version', [])
289
+ if (!jsonRpcProvider) {
290
+ throw new Error(`Account provider doesn't support send method`)
291
+ }
292
+
293
+ const result = await jsonRpcProvider.send('net_version', [])
280
294
  response.result = result
281
295
  break
282
296
  }
283
297
 
284
298
  case 'eth_chainId': {
285
- const result = await provider.send('eth_chainId', [])
299
+ if (!jsonRpcProvider) {
300
+ throw new Error(`Account provider doesn't support send method`)
301
+ }
302
+
303
+ const result = await jsonRpcProvider.send('eth_chainId', [])
286
304
  response.result = result
287
305
  break
288
306
  }
@@ -606,8 +624,12 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
606
624
  }
607
625
 
608
626
  default: {
627
+ if (!jsonRpcProvider) {
628
+ throw new Error(`Account provider doesn't support send method`)
629
+ }
630
+
609
631
  // NOTE: provider here will be chain-bound if chainId is provided
610
- const providerResponse = await provider.send(request.method, request.params!)
632
+ const providerResponse = await jsonRpcProvider.send(request.method, request.params!)
611
633
  response.result = providerResponse
612
634
  }
613
635
  }