@0xsequence/provider 0.0.0-20230726105834 → 0.0.0-20230726160744

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.
@@ -24,6 +24,10 @@ function _extends() {
24
24
  return _extends.apply(this, arguments);
25
25
  }
26
26
 
27
+ function isProviderTransport(transport) {
28
+ return transport && typeof transport === 'object' && typeof transport.register === 'function' && typeof transport.unregister === 'function' && typeof transport.openWallet === 'function' && typeof transport.closeWallet === 'function' && typeof transport.isOpened === 'function' && typeof transport.isConnected === 'function' && typeof transport.on === 'function';
29
+ }
30
+
27
31
  // ProviderMessageCallback is used to respond to ProviderMessage requests. The error
28
32
  // argument is for exceptions during the execution, and response is the response payload
29
33
  // which may contain the result or an error payload from the wallet.
@@ -3524,8 +3528,10 @@ class SequenceClient {
3524
3528
  this.callbacks = {};
3525
3529
  if (isMuxTransportTemplate(transport)) {
3526
3530
  this.transport = MuxMessageProvider.new(transport);
3527
- } else {
3531
+ } else if (isProviderTransport(transport)) {
3528
3532
  this.transport = transport;
3533
+ } else {
3534
+ throw new Error('Invalid transport');
3529
3535
  }
3530
3536
  this.session = new SequenceClientSession(store);
3531
3537
  this.defaultChainId = new DefaultChainIdTracker(store, defaultChainId);
@@ -3602,7 +3608,7 @@ class SequenceClient {
3602
3608
  // Proxy transport methods
3603
3609
 
3604
3610
  async openWallet(path, intent) {
3605
- this.transport.openWallet(path, intent, 1);
3611
+ this.transport.openWallet(path, intent, this.getChainId());
3606
3612
  await this.transport.waitUntilOpened();
3607
3613
  return this.isOpened();
3608
3614
  }
@@ -3644,7 +3650,9 @@ class SequenceClient {
3644
3650
  }
3645
3651
  await this.openWallet(undefined, {
3646
3652
  type: 'connect',
3647
- options
3653
+ options: _extends({}, options, {
3654
+ networkId: this.getChainId()
3655
+ })
3648
3656
  });
3649
3657
  const connectDetails = await this.transport.waitUntilConnected().catch(error => {
3650
3658
  if (error instanceof Error) {
@@ -3659,6 +3667,12 @@ class SequenceClient {
3659
3667
  };
3660
3668
  }
3661
3669
  });
3670
+
3671
+ // Normalize chainId into a decimal string
3672
+ // TODO: Remove this once wallet-webapp returns chainId as a string
3673
+ if (connectDetails.chainId) {
3674
+ connectDetails.chainId = ethers.ethers.BigNumber.from(connectDetails.chainId).toString();
3675
+ }
3662
3676
  if (connectDetails.connected) {
3663
3677
  if (!connectDetails.session) {
3664
3678
  throw new Error('impossible state, connect response is missing session');
@@ -3780,7 +3794,9 @@ const initWallet = partialConfig => {
3780
3794
  }
3781
3795
 
3782
3796
  // Combine both the provided config and the default config
3783
- const config = _extends({}, DefaultProviderConfig, partialConfig);
3797
+ const config = _extends({}, DefaultProviderConfig, partialConfig, {
3798
+ transports: _extends({}, DefaultProviderConfig.transports, partialConfig == null ? void 0 : partialConfig.transports)
3799
+ });
3784
3800
  const rpcProviders = {};
3785
3801
 
3786
3802
  // We can't allow the dapp to define networks that aren't already defined in sequence.js
@@ -3881,6 +3897,7 @@ exports.getWallet = getWallet;
3881
3897
  exports.initWallet = initWallet;
3882
3898
  exports.isBrowserExtension = isBrowserExtension;
3883
3899
  exports.isMuxTransportTemplate = isMuxTransportTemplate;
3900
+ exports.isProviderTransport = isProviderTransport;
3884
3901
  exports.isUnityPlugin = isUnityPlugin;
3885
3902
  exports.isValidMessageSignature = isValidMessageSignature;
3886
3903
  exports.isValidSignature = isValidSignature;
@@ -24,6 +24,10 @@ function _extends() {
24
24
  return _extends.apply(this, arguments);
25
25
  }
26
26
 
27
+ function isProviderTransport(transport) {
28
+ return transport && typeof transport === 'object' && typeof transport.register === 'function' && typeof transport.unregister === 'function' && typeof transport.openWallet === 'function' && typeof transport.closeWallet === 'function' && typeof transport.isOpened === 'function' && typeof transport.isConnected === 'function' && typeof transport.on === 'function';
29
+ }
30
+
27
31
  // ProviderMessageCallback is used to respond to ProviderMessage requests. The error
28
32
  // argument is for exceptions during the execution, and response is the response payload
29
33
  // which may contain the result or an error payload from the wallet.
@@ -3524,8 +3528,10 @@ class SequenceClient {
3524
3528
  this.callbacks = {};
3525
3529
  if (isMuxTransportTemplate(transport)) {
3526
3530
  this.transport = MuxMessageProvider.new(transport);
3527
- } else {
3531
+ } else if (isProviderTransport(transport)) {
3528
3532
  this.transport = transport;
3533
+ } else {
3534
+ throw new Error('Invalid transport');
3529
3535
  }
3530
3536
  this.session = new SequenceClientSession(store);
3531
3537
  this.defaultChainId = new DefaultChainIdTracker(store, defaultChainId);
@@ -3602,7 +3608,7 @@ class SequenceClient {
3602
3608
  // Proxy transport methods
3603
3609
 
3604
3610
  async openWallet(path, intent) {
3605
- this.transport.openWallet(path, intent, 1);
3611
+ this.transport.openWallet(path, intent, this.getChainId());
3606
3612
  await this.transport.waitUntilOpened();
3607
3613
  return this.isOpened();
3608
3614
  }
@@ -3644,7 +3650,9 @@ class SequenceClient {
3644
3650
  }
3645
3651
  await this.openWallet(undefined, {
3646
3652
  type: 'connect',
3647
- options
3653
+ options: _extends({}, options, {
3654
+ networkId: this.getChainId()
3655
+ })
3648
3656
  });
3649
3657
  const connectDetails = await this.transport.waitUntilConnected().catch(error => {
3650
3658
  if (error instanceof Error) {
@@ -3659,6 +3667,12 @@ class SequenceClient {
3659
3667
  };
3660
3668
  }
3661
3669
  });
3670
+
3671
+ // Normalize chainId into a decimal string
3672
+ // TODO: Remove this once wallet-webapp returns chainId as a string
3673
+ if (connectDetails.chainId) {
3674
+ connectDetails.chainId = ethers.ethers.BigNumber.from(connectDetails.chainId).toString();
3675
+ }
3662
3676
  if (connectDetails.connected) {
3663
3677
  if (!connectDetails.session) {
3664
3678
  throw new Error('impossible state, connect response is missing session');
@@ -3780,7 +3794,9 @@ const initWallet = partialConfig => {
3780
3794
  }
3781
3795
 
3782
3796
  // Combine both the provided config and the default config
3783
- const config = _extends({}, DefaultProviderConfig, partialConfig);
3797
+ const config = _extends({}, DefaultProviderConfig, partialConfig, {
3798
+ transports: _extends({}, DefaultProviderConfig.transports, partialConfig == null ? void 0 : partialConfig.transports)
3799
+ });
3784
3800
  const rpcProviders = {};
3785
3801
 
3786
3802
  // We can't allow the dapp to define networks that aren't already defined in sequence.js
@@ -3881,6 +3897,7 @@ exports.getWallet = getWallet;
3881
3897
  exports.initWallet = initWallet;
3882
3898
  exports.isBrowserExtension = isBrowserExtension;
3883
3899
  exports.isMuxTransportTemplate = isMuxTransportTemplate;
3900
+ exports.isProviderTransport = isProviderTransport;
3884
3901
  exports.isUnityPlugin = isUnityPlugin;
3885
3902
  exports.isValidMessageSignature = isValidMessageSignature;
3886
3903
  exports.isValidSignature = isValidSignature;
@@ -20,6 +20,10 @@ function _extends() {
20
20
  return _extends.apply(this, arguments);
21
21
  }
22
22
 
23
+ function isProviderTransport(transport) {
24
+ return transport && typeof transport === 'object' && typeof transport.register === 'function' && typeof transport.unregister === 'function' && typeof transport.openWallet === 'function' && typeof transport.closeWallet === 'function' && typeof transport.isOpened === 'function' && typeof transport.isConnected === 'function' && typeof transport.on === 'function';
25
+ }
26
+
23
27
  // ProviderMessageCallback is used to respond to ProviderMessage requests. The error
24
28
  // argument is for exceptions during the execution, and response is the response payload
25
29
  // which may contain the result or an error payload from the wallet.
@@ -3520,8 +3524,10 @@ class SequenceClient {
3520
3524
  this.callbacks = {};
3521
3525
  if (isMuxTransportTemplate(transport)) {
3522
3526
  this.transport = MuxMessageProvider.new(transport);
3523
- } else {
3527
+ } else if (isProviderTransport(transport)) {
3524
3528
  this.transport = transport;
3529
+ } else {
3530
+ throw new Error('Invalid transport');
3525
3531
  }
3526
3532
  this.session = new SequenceClientSession(store);
3527
3533
  this.defaultChainId = new DefaultChainIdTracker(store, defaultChainId);
@@ -3598,7 +3604,7 @@ class SequenceClient {
3598
3604
  // Proxy transport methods
3599
3605
 
3600
3606
  async openWallet(path, intent) {
3601
- this.transport.openWallet(path, intent, 1);
3607
+ this.transport.openWallet(path, intent, this.getChainId());
3602
3608
  await this.transport.waitUntilOpened();
3603
3609
  return this.isOpened();
3604
3610
  }
@@ -3640,7 +3646,9 @@ class SequenceClient {
3640
3646
  }
3641
3647
  await this.openWallet(undefined, {
3642
3648
  type: 'connect',
3643
- options
3649
+ options: _extends({}, options, {
3650
+ networkId: this.getChainId()
3651
+ })
3644
3652
  });
3645
3653
  const connectDetails = await this.transport.waitUntilConnected().catch(error => {
3646
3654
  if (error instanceof Error) {
@@ -3655,6 +3663,12 @@ class SequenceClient {
3655
3663
  };
3656
3664
  }
3657
3665
  });
3666
+
3667
+ // Normalize chainId into a decimal string
3668
+ // TODO: Remove this once wallet-webapp returns chainId as a string
3669
+ if (connectDetails.chainId) {
3670
+ connectDetails.chainId = ethers.BigNumber.from(connectDetails.chainId).toString();
3671
+ }
3658
3672
  if (connectDetails.connected) {
3659
3673
  if (!connectDetails.session) {
3660
3674
  throw new Error('impossible state, connect response is missing session');
@@ -3776,7 +3790,9 @@ const initWallet = partialConfig => {
3776
3790
  }
3777
3791
 
3778
3792
  // Combine both the provided config and the default config
3779
- const config = _extends({}, DefaultProviderConfig, partialConfig);
3793
+ const config = _extends({}, DefaultProviderConfig, partialConfig, {
3794
+ transports: _extends({}, DefaultProviderConfig.transports, partialConfig == null ? void 0 : partialConfig.transports)
3795
+ });
3780
3796
  const rpcProviders = {};
3781
3797
 
3782
3798
  // We can't allow the dapp to define networks that aren't already defined in sequence.js
@@ -3840,4 +3856,4 @@ const getWallet = () => {
3840
3856
  return sequenceWalletProvider;
3841
3857
  };
3842
3858
 
3843
- export { BaseInjectedTransport, BaseProviderTransport, BaseWalletTransport, CHANNEL_ID, DefaultChainIdTracker, DefaultProviderConfig, ErrSignedInRequired, EventType, ExtensionMessageHandler, ExtensionMessageProvider, InitState, LocalStorage, MemoryItemStore, MuxMessageProvider, OpenState, PROVIDER_OPEN_TIMEOUT, ProviderError, ProxyMessageChannel, ProxyMessageChannelPort, ProxyMessageHandler, ProxyMessageProvider, SequenceClient, SequenceClientSession, SequenceProvider, SequenceSigner, SingleNetworkSequenceProvider, SingleNetworkSequenceSigner, UnrealMessageHandler, UnrealMessageProvider, WalletRequestHandler, WindowMessageHandler, WindowMessageProvider, WindowSessionParams, getWallet, initWallet, isBrowserExtension, isMuxTransportTemplate, isUnityPlugin, isValidMessageSignature, isValidSignature, isValidTypedDataSignature, isWalletUpToDate, messageToBytes, nextMessageIdx, prefixEIP191Message, resolveArrayProperties, trimEIP191Prefix, unregisterWallet, useBestStore };
3859
+ export { BaseInjectedTransport, BaseProviderTransport, BaseWalletTransport, CHANNEL_ID, DefaultChainIdTracker, DefaultProviderConfig, ErrSignedInRequired, EventType, ExtensionMessageHandler, ExtensionMessageProvider, InitState, LocalStorage, MemoryItemStore, MuxMessageProvider, OpenState, PROVIDER_OPEN_TIMEOUT, ProviderError, ProxyMessageChannel, ProxyMessageChannelPort, ProxyMessageHandler, ProxyMessageProvider, SequenceClient, SequenceClientSession, SequenceProvider, SequenceSigner, SingleNetworkSequenceProvider, SingleNetworkSequenceSigner, UnrealMessageHandler, UnrealMessageProvider, WalletRequestHandler, WindowMessageHandler, WindowMessageProvider, WindowSessionParams, getWallet, initWallet, isBrowserExtension, isMuxTransportTemplate, isProviderTransport, isUnityPlugin, isValidMessageSignature, isValidSignature, isValidTypedDataSignature, isWalletUpToDate, messageToBytes, nextMessageIdx, prefixEIP191Message, resolveArrayProperties, trimEIP191Prefix, unregisterWallet, useBestStore };
@@ -15,6 +15,7 @@ export interface ProviderTransport extends JsonRpcHandler, ProviderMessageTransp
15
15
  waitUntilOpened(): Promise<WalletSession | undefined>;
16
16
  waitUntilConnected(): Promise<ConnectDetails>;
17
17
  }
18
+ export declare function isProviderTransport(transport: any): transport is ProviderTransport;
18
19
  export interface WalletTransport extends JsonRpcHandler, ProviderMessageTransport, ProviderMessageRequestHandler {
19
20
  register(): void;
20
21
  unregister(): void;
@@ -126,6 +127,12 @@ export interface ConnectOptions {
126
127
  /** Options to further customize the wallet experience. */
127
128
  settings?: Settings;
128
129
  }
130
+ export interface NetworkedConnectOptions extends ConnectOptions {
131
+ /** chainId is the chainId to connect to. If not specified, the default chainId
132
+ * will be used. This does not define a default chain id, it is only used for the connect
133
+ * authorization signature. */
134
+ networkId?: string | number;
135
+ }
129
136
  /** Options to further customize the wallet experience. */
130
137
  export interface Settings {
131
138
  /** Specify a wallet theme. `light` and `dark` are the main themes, to use other available
@@ -189,7 +196,7 @@ export interface ConnectDetails {
189
196
  export type PromptConnectDetails = Pick<ConnectDetails, 'chainId' | 'error' | 'connected' | 'proof' | 'email'>;
190
197
  export type OpenWalletIntent = {
191
198
  type: 'connect';
192
- options?: ConnectOptions;
199
+ options?: NetworkedConnectOptions;
193
200
  } | {
194
201
  type: 'openWithOptions';
195
202
  options?: ConnectOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xsequence/provider",
3
- "version": "0.0.0-20230726105834",
3
+ "version": "0.0.0-20230726160744",
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",
@@ -11,15 +11,15 @@
11
11
  "dependencies": {
12
12
  "eventemitter2": "^6.4.5",
13
13
  "webextension-polyfill": "^0.10.0",
14
- "@0xsequence/abi": "0.0.0-20230726105834",
15
- "@0xsequence/auth": "0.0.0-20230726105834",
16
- "@0xsequence/core": "0.0.0-20230726105834",
17
- "@0xsequence/migration": "0.0.0-20230726105834",
18
- "@0xsequence/account": "0.0.0-20230726105834",
19
- "@0xsequence/relayer": "0.0.0-20230726105834",
20
- "@0xsequence/network": "0.0.0-20230726105834",
21
- "@0xsequence/utils": "0.0.0-20230726105834",
22
- "@0xsequence/wallet": "0.0.0-20230726105834"
14
+ "@0xsequence/abi": "0.0.0-20230726160744",
15
+ "@0xsequence/auth": "0.0.0-20230726160744",
16
+ "@0xsequence/core": "0.0.0-20230726160744",
17
+ "@0xsequence/migration": "0.0.0-20230726160744",
18
+ "@0xsequence/account": "0.0.0-20230726160744",
19
+ "@0xsequence/network": "0.0.0-20230726160744",
20
+ "@0xsequence/utils": "0.0.0-20230726160744",
21
+ "@0xsequence/relayer": "0.0.0-20230726160744",
22
+ "@0xsequence/wallet": "0.0.0-20230726160744"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "ethers": ">=5.5 < 6"
package/src/client.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { JsonRpcRequest, JsonRpcResponse, NetworkConfig } from "@0xsequence/network"
2
- import { ConnectDetails, ConnectOptions, ItemStore, MuxMessageProvider, MuxTransportTemplate, OpenWalletIntent, OptionalChainId, OptionalEIP6492, ProviderTransport, WalletSession, isMuxTransportTemplate } from "."
2
+ import { ConnectDetails, ConnectOptions, ItemStore, MuxMessageProvider, MuxTransportTemplate, OpenWalletIntent, OptionalChainId, OptionalEIP6492, ProviderTransport, WalletSession, isMuxTransportTemplate, isProviderTransport } from "."
3
3
  import { commons } from "@0xsequence/core"
4
4
  import { TypedData } from "@0xsequence/utils"
5
5
  import { toExtended } from "./extended"
@@ -134,8 +134,10 @@ export class SequenceClient {
134
134
  ) {
135
135
  if (isMuxTransportTemplate(transport)) {
136
136
  this.transport = MuxMessageProvider.new(transport)
137
- } else {
137
+ } else if (isProviderTransport(transport)) {
138
138
  this.transport = transport
139
+ } else {
140
+ throw new Error('Invalid transport')
139
141
  }
140
142
 
141
143
  this.session = new SequenceClientSession(store)
@@ -222,7 +224,7 @@ export class SequenceClient {
222
224
  // Proxy transport methods
223
225
 
224
226
  async openWallet(path?: string, intent?: OpenWalletIntent) {
225
- this.transport.openWallet(path, intent, 1)
227
+ this.transport.openWallet(path, intent, this.getChainId())
226
228
  await this.transport.waitUntilOpened()
227
229
  return this.isOpened()
228
230
  }
@@ -271,7 +273,7 @@ export class SequenceClient {
271
273
  }
272
274
  }
273
275
 
274
- await this.openWallet(undefined, { type: 'connect', options })
276
+ await this.openWallet(undefined, { type: 'connect', options: { ...options, networkId: this.getChainId() } })
275
277
 
276
278
  const connectDetails = await this.transport.waitUntilConnected().catch((error): ConnectDetails => {
277
279
  if (error instanceof Error) {
@@ -281,6 +283,12 @@ export class SequenceClient {
281
283
  }
282
284
  })
283
285
 
286
+ // Normalize chainId into a decimal string
287
+ // TODO: Remove this once wallet-webapp returns chainId as a string
288
+ if (connectDetails.chainId) {
289
+ connectDetails.chainId = ethers.BigNumber.from(connectDetails.chainId).toString()
290
+ }
291
+
284
292
  if (connectDetails.connected) {
285
293
  if (!connectDetails.session) {
286
294
  throw new Error('impossible state, connect response is missing session')
package/src/init.ts CHANGED
@@ -50,7 +50,15 @@ export const initWallet = (
50
50
  }
51
51
 
52
52
  // Combine both the provided config and the default config
53
- const config = { ...DefaultProviderConfig, ...partialConfig }
53
+ const config = {
54
+ ...DefaultProviderConfig,
55
+ ...partialConfig,
56
+ transports: {
57
+ ...DefaultProviderConfig.transports,
58
+ ...partialConfig?.transports,
59
+ }
60
+ }
61
+
54
62
  const rpcProviders: Record<number, ethers.providers.JsonRpcProvider> = {}
55
63
 
56
64
  // We can't allow the dapp to define networks that aren't already defined in sequence.js
package/src/types.ts CHANGED
@@ -21,6 +21,20 @@ export interface ProviderTransport extends JsonRpcHandler, ProviderMessageTransp
21
21
  waitUntilConnected(): Promise<ConnectDetails>
22
22
  }
23
23
 
24
+ export function isProviderTransport(transport: any): transport is ProviderTransport {
25
+ return (
26
+ transport &&
27
+ typeof transport === 'object' &&
28
+ typeof transport.register === 'function' &&
29
+ typeof transport.unregister === 'function' &&
30
+ typeof transport.openWallet === 'function' &&
31
+ typeof transport.closeWallet === 'function' &&
32
+ typeof transport.isOpened === 'function' &&
33
+ typeof transport.isConnected === 'function' &&
34
+ typeof transport.on === 'function'
35
+ )
36
+ }
37
+
24
38
  export interface WalletTransport extends JsonRpcHandler, ProviderMessageTransport, ProviderMessageRequestHandler {
25
39
  register(): void
26
40
  unregister(): void
@@ -167,6 +181,13 @@ export interface ConnectOptions {
167
181
  settings?: Settings
168
182
  }
169
183
 
184
+ export interface NetworkedConnectOptions extends ConnectOptions {
185
+ /** chainId is the chainId to connect to. If not specified, the default chainId
186
+ * will be used. This does not define a default chain id, it is only used for the connect
187
+ * authorization signature. */
188
+ networkId?: string | number;
189
+ }
190
+
170
191
  /** Options to further customize the wallet experience. */
171
192
  export interface Settings {
172
193
  /** Specify a wallet theme. `light` and `dark` are the main themes, to use other available
@@ -252,7 +273,7 @@ export interface ConnectDetails {
252
273
  export type PromptConnectDetails = Pick<ConnectDetails, 'chainId' | 'error' | 'connected' | 'proof' | 'email'>
253
274
 
254
275
  export type OpenWalletIntent =
255
- | { type: 'connect'; options?: ConnectOptions }
276
+ | { type: 'connect'; options?: NetworkedConnectOptions }
256
277
  | { type: 'openWithOptions'; options?: ConnectOptions }
257
278
  | { type: 'jsonRpcRequest'; method: string }
258
279