@0xsequence/provider 0.35.2 → 0.35.6

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.
@@ -10,6 +10,7 @@ import {
10
10
  ConnectDetails,
11
11
  PromptConnectDetails,
12
12
  WalletSession,
13
+ OpenWalletIntent,
13
14
  ErrSignedInRequired,
14
15
  ProviderEventTypes,
15
16
  TypedEventEmitter
@@ -41,11 +42,17 @@ export interface WalletSignInOptions {
41
42
  }
42
43
 
43
44
  export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, ProviderMessageRequestHandler {
44
- private signer: Signer | null
45
+ // signer interface of the wallet. A null value means there is no signer (ie. user not signed in). An undefined
46
+ // value means the signer state is unknown, usually meaning the wallet app is booting up and initializing. Of course
47
+ // a Signer value is the actually interface to a signed-in account
48
+ private signer: Signer | null | undefined
49
+ private signerReadyCallbacks: Array<() => void> = []
50
+
45
51
  private prompter: WalletUserPrompter | null
46
52
  private mainnetNetworks: NetworkConfig[]
47
53
  private testnetNetworks: NetworkConfig[]
48
54
 
55
+ private _openIntent?: OpenWalletIntent
49
56
  private _connectOptions?: ConnectOptions
50
57
  private _defaultNetworkId?: string | number
51
58
  private _chainId?: number
@@ -53,7 +60,7 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
53
60
  private events: TypedEventEmitter<ProviderEventTypes> = new EventEmitter() as TypedEventEmitter<ProviderEventTypes>
54
61
 
55
62
  constructor(
56
- signer: Signer | null,
63
+ signer: Signer | null | undefined,
57
64
  prompter: WalletUserPrompter | null,
58
65
  mainnetNetworks: Networks,
59
66
  testnetNetworks: Networks = []
@@ -65,7 +72,8 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
65
72
  }
66
73
 
67
74
  async signIn(signer: Signer | null, options: WalletSignInOptions = {}) {
68
- this.signer = signer
75
+ this.setSigner(signer)
76
+
69
77
 
70
78
  const { connect, mainnetNetworks, testnetNetworks, defaultNetworkId } = options
71
79
 
@@ -109,6 +117,29 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
109
117
  }
110
118
  }
111
119
 
120
+ signOut() {
121
+ // signed out state
122
+ this.signer = null
123
+ }
124
+
125
+ signerReset() {
126
+ // resetting signer puts the wallet in an uninitialized state, which requires the app to
127
+ // re-initiatize and set the signer either as "null" (ie. no signer) or "Signer" (ie. signed in).
128
+ this.signer = undefined
129
+ }
130
+
131
+ signerReady() {
132
+ return new Promise((resolve, reject) => {
133
+ if (this.signer !== undefined) {
134
+ resolve(undefined)
135
+ } else {
136
+ this.signerReadyCallbacks.push(() => {
137
+ resolve(undefined)
138
+ })
139
+ }
140
+ })
141
+ }
142
+
112
143
  async connect(options?: ConnectOptions): Promise<ConnectDetails> {
113
144
  if (!this.signer) {
114
145
  return {
@@ -165,6 +196,7 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
165
196
  }
166
197
 
167
198
  const promptConnectDetails = await this.prompter.promptConnect(options || this._connectOptions).catch(_ => {
199
+
168
200
  return { connected: false } as ConnectDetails
169
201
  })
170
202
 
@@ -208,6 +240,10 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
208
240
  result: null
209
241
  }
210
242
 
243
+ if (this.signer === undefined) {
244
+ await this.signerReady()
245
+ }
246
+
211
247
  try {
212
248
  // only allow public json rpc method to the provider when user is not logged in, aka signer is not set
213
249
  if ((!this.signer || this.signer === null) && !permittedJsonRpcMethods.includes(request.method)) {
@@ -583,6 +619,14 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
583
619
  }
584
620
  }
585
621
 
622
+ get openIntent(): OpenWalletIntent | undefined {
623
+ return this._openIntent
624
+ }
625
+
626
+ setOpenIntent(intent: OpenWalletIntent | undefined) {
627
+ this._openIntent = intent
628
+ }
629
+
586
630
  get connectOptions(): ConnectOptions | undefined {
587
631
  return this._connectOptions
588
632
  }
@@ -682,12 +726,19 @@ export class WalletRequestHandler implements ExternalProvider, JsonRpcHandler, P
682
726
  return !!this.signer
683
727
  }
684
728
 
685
- getSigner(): Signer | null {
729
+ getSigner(): Signer | null | undefined {
686
730
  return this.signer
687
731
  }
688
732
 
689
- setSigner(signer: Signer | null) {
733
+ setSigner(signer: Signer | null | undefined) {
690
734
  this.signer = signer
735
+
736
+ if (signer !== undefined) {
737
+ for (let i=0; i < this.signerReadyCallbacks.length; i++) {
738
+ this.signerReadyCallbacks[i]()
739
+ }
740
+ this.signerReadyCallbacks = []
741
+ }
691
742
  }
692
743
 
693
744
  private async handleConfirmWalletDeployPrompt(
@@ -47,10 +47,6 @@ export class WindowMessageHandler extends BaseWalletTransport {
47
47
  return
48
48
  }
49
49
 
50
- if (session.intent && isNewWindowSession) {
51
- window.history.replaceState({ openWalletIntent: true }, document.title, pathname)
52
- }
53
-
54
50
  // record parent window instance for communication
55
51
  this.parentWindow = parent.window.opener
56
52
 
@@ -101,11 +101,22 @@ export class WindowMessageProvider extends BaseProviderTransport {
101
101
  }
102
102
 
103
103
  // Open popup window on center of the app window
104
- const windowSize = [450, 750]
105
- const windowPos = [
106
- Math.abs(window.screenX + window.innerWidth / 2 - windowSize[0] / 2),
107
- Math.abs(window.screenY + window.innerHeight / 2 - windowSize[1] / 2)
108
- ]
104
+ let windowSize: number[]
105
+ let windowPos: number[]
106
+
107
+ if (isBrowserExtension()) {
108
+ windowSize = [450, 750]
109
+ windowPos = [
110
+ Math.abs(window.screen.width / 2 - windowSize[0] / 2),
111
+ Math.abs(window.screen.height / 2 - windowSize[1] / 2)
112
+ ]
113
+ } else {
114
+ windowSize = [450, 750]
115
+ windowPos = [
116
+ Math.abs(window.screenX + window.innerWidth / 2 - windowSize[0] / 2),
117
+ Math.abs(window.screenY + window.innerHeight / 2 - windowSize[1] / 2)
118
+ ]
119
+ }
109
120
 
110
121
  const windowFeatures =
111
122
  `toolbar=0,location=0,menubar=0,scrollbars=yes,status=yes` +