forked from 0xsequence/sequence.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDappTransport.ts
More file actions
587 lines (524 loc) · 19.8 KB
/
Copy pathDappTransport.ts
File metadata and controls
587 lines (524 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/* eslint-disable @typescript-eslint/no-explicit-any */
import { jsonReplacers, jsonRevivers } from './utils/index.js'
import {
MessageType,
PendingRequest,
PopupModeOptions,
SendRequestOptions,
SequenceSessionStorage,
TransportMessage,
TransportMode,
WalletSize,
} from './types/index.js'
const isBrowserEnvironment = typeof window !== 'undefined' && typeof document !== 'undefined'
const bytesToBinaryString = (bytes: Uint8Array) => {
let binary = ''
const chunkSize = 0x8000
for (let i = 0; i < bytes.length; i += chunkSize) {
binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize))
}
return binary
}
const binaryStringToBytes = (value: string) => {
const bytes = new Uint8Array(value.length)
for (let i = 0; i < value.length; i += 1) {
bytes[i] = value.charCodeAt(i)
}
return bytes
}
const base64Encode = (value: string) => {
if (typeof btoa !== 'undefined' && typeof TextEncoder !== 'undefined') {
return btoa(bytesToBinaryString(new TextEncoder().encode(value)))
}
if (typeof Buffer !== 'undefined') {
return Buffer.from(value, 'utf-8').toString('base64')
}
throw new Error('Base64 encoding is not supported in this environment.')
}
const base64Decode = (value: string) => {
if (typeof atob !== 'undefined' && typeof TextDecoder !== 'undefined') {
const decoded = atob(value)
try {
return new TextDecoder('utf-8', { fatal: true }).decode(binaryStringToBytes(decoded))
} catch {
return decoded
}
}
if (typeof Buffer !== 'undefined') {
return Buffer.from(value, 'base64').toString('utf-8')
}
throw new Error('Base64 decoding is not supported in this environment.')
}
enum ConnectionState {
DISCONNECTED = 'DISCONNECTED',
CONNECTING = 'CONNECTING',
CONNECTED = 'CONNECTED',
}
const REDIRECT_REQUEST_KEY = 'dapp-redirect-request'
export class DappTransport {
private walletWindow: Window | undefined = undefined
private connectionState: ConnectionState = ConnectionState.DISCONNECTED
private readyPromise: Promise<void> | undefined = undefined
private readyPromiseResolve: (() => void) | undefined = undefined
private readyPromiseReject: ((reason?: any) => void) | undefined = undefined
private initId: string | undefined = undefined
private handshakeTimeoutId: number | undefined = undefined
private closeCheckIntervalId: number | undefined = undefined
private sessionId: string | undefined = undefined
private pendingRequests = new Map<string, PendingRequest>()
private messageQueue: TransportMessage[] = []
private readonly requestTimeoutMs: number
private readonly handshakeTimeoutMs: number
private readonly sequenceSessionStorage: SequenceSessionStorage
private readonly redirectActionHandler?: (url: string) => void
private readonly isBrowser: boolean
public readonly walletOrigin: string
constructor(
public readonly walletUrl: string,
readonly mode: TransportMode = TransportMode.POPUP,
popupModeOptions: PopupModeOptions = {},
sequenceSessionStorage?: SequenceSessionStorage,
redirectActionHandler?: (url: string) => void,
) {
this.isBrowser = isBrowserEnvironment
try {
this.walletOrigin = new URL(walletUrl).origin
} catch (e) {
console.error('[DApp] Invalid walletUrl provided:', walletUrl, e)
throw new Error(`Invalid walletUrl: ${walletUrl}`)
}
if (!this.walletOrigin || this.walletOrigin === 'null' || this.walletOrigin === '*') {
console.error('[DApp] Could not determine a valid wallet origin from the URL:', walletUrl)
throw new Error('Invalid wallet origin derived from walletUrl.')
}
this.sequenceSessionStorage =
sequenceSessionStorage ||
({
getItem: (key: string) => (this.isBrowser && window.sessionStorage ? window.sessionStorage.getItem(key) : null),
setItem: (key: string, value: string) => {
if (this.isBrowser && window.sessionStorage) {
window.sessionStorage.setItem(key, value)
}
},
removeItem: (key: string) => {
if (this.isBrowser && window.sessionStorage) {
window.sessionStorage.removeItem(key)
}
},
} satisfies SequenceSessionStorage)
this.requestTimeoutMs = popupModeOptions.requestTimeoutMs ?? 300000
this.handshakeTimeoutMs = popupModeOptions.handshakeTimeoutMs ?? 15000
if (this.mode === TransportMode.POPUP && this.isBrowser) {
window.addEventListener('message', this.handleMessage)
}
this.redirectActionHandler = redirectActionHandler
}
get isWalletOpen(): boolean {
if (this.mode === TransportMode.REDIRECT) return false
return !!this.walletWindow && !this.walletWindow.closed
}
get isReady(): boolean {
if (this.mode === TransportMode.REDIRECT) return false
return this.connectionState === ConnectionState.CONNECTED
}
async sendRequest<TResponse = any, TRequest = any>(
action: string,
redirectUrl: string,
payload?: TRequest,
options: SendRequestOptions = {},
): Promise<TResponse> {
if (!this.isBrowser && this.mode === TransportMode.POPUP) {
throw new Error(
'Popup transport requires a browser environment. Use redirect mode or provide a redirect handler.',
)
}
if (this.mode === TransportMode.REDIRECT) {
const url = await this.getRequestRedirectUrl(action, payload, redirectUrl, options.path)
if (this.redirectActionHandler) {
this.redirectActionHandler(url)
} else if (this.isBrowser) {
console.info('[DappTransport] No redirectActionHandler provided. Using window.location.href to navigate.')
window.location.href = url
} else {
throw new Error(
'Redirect navigation is not possible outside the browser without a redirectActionHandler. Provide a handler to perform navigation.',
)
}
return new Promise<TResponse>(() => {})
}
if (this.connectionState !== ConnectionState.CONNECTED) {
await this.openWallet(options.path)
}
if (!this.isWalletOpen || this.connectionState !== ConnectionState.CONNECTED) {
throw new Error('Wallet connection is not available or failed to establish.')
}
const id = this.generateId()
const message: TransportMessage<TRequest> = {
id,
type: MessageType.REQUEST,
action,
payload,
}
return new Promise((resolve, reject) => {
const timeout = options.timeout ?? this.requestTimeoutMs
const timer = window.setTimeout(() => {
if (this.pendingRequests.has(id)) {
this.pendingRequests.delete(id)
reject(new Error(`Request '${action}' (ID: ${id}) timed out after ${timeout}ms.`))
}
}, timeout)
this.pendingRequests.set(id, { resolve, reject, timer, action })
this.postMessageToWallet(message)
})
}
public async getRequestRedirectUrl(
action: string,
payload: any,
redirectUrl: string,
path?: string,
): Promise<string> {
const id = this.generateId()
const request = { id, action, timestamp: Date.now() }
try {
await this.sequenceSessionStorage.setItem(REDIRECT_REQUEST_KEY, JSON.stringify(request, jsonReplacers))
} catch (e) {
console.error('Failed to set redirect request in storage', e)
throw new Error('Could not save redirect state to storage. Redirect flow is unavailable.')
}
const serializedPayload = base64Encode(JSON.stringify(payload || {}, jsonReplacers))
const fullWalletUrl = path ? `${this.walletUrl}${path}` : this.walletUrl
const url = new URL(fullWalletUrl)
url.searchParams.set('action', action)
url.searchParams.set('payload', serializedPayload)
url.searchParams.set('id', id)
url.searchParams.set('redirectUrl', redirectUrl)
url.searchParams.set('mode', 'redirect')
return url.toString()
}
public async getRedirectResponse<TResponse = any>(
cleanState: boolean = true,
url?: string,
): Promise<{ payload: TResponse; action: string } | { error: any; action: string } | null> {
if (!url && !this.isBrowser) {
throw new Error('A URL must be provided when handling redirect responses outside of a browser environment.')
}
const search = url ? new URL(url).search : this.isBrowser ? window.location.search : ''
const params = new URLSearchParams(search)
const responseId = params.get('id')
if (!responseId) return null
let originalRequest: { id: string; action: string; timestamp: number }
try {
const storedRequest = await this.sequenceSessionStorage.getItem(REDIRECT_REQUEST_KEY)
if (!storedRequest) {
return null
}
originalRequest = JSON.parse(storedRequest, jsonRevivers)
} catch (e) {
console.error('Failed to parse redirect request from storage', e)
return null
}
if (originalRequest.id !== responseId) {
console.error(`Mismatched ID in redirect response. Expected ${originalRequest.id}, got ${responseId}.`)
if (cleanState) {
await this.sequenceSessionStorage.removeItem(REDIRECT_REQUEST_KEY)
}
return null
}
const responsePayloadB64 = params.get('payload')
const responseErrorB64 = params.get('error')
if (cleanState) {
await this.sequenceSessionStorage.removeItem(REDIRECT_REQUEST_KEY)
if (this.isBrowser && !url && window.history) {
const cleanUrl = new URL(window.location.href)
;['id', 'payload', 'error', 'mode'].forEach((p) => cleanUrl.searchParams.delete(p))
history.replaceState({}, document.title, cleanUrl.toString())
}
}
if (responseErrorB64) {
try {
return {
error: JSON.parse(base64Decode(responseErrorB64), jsonRevivers),
action: originalRequest.action,
}
} catch (e) {
console.error('Failed to parse error from redirect response', e)
return {
error: 'Failed to parse error from redirect',
action: originalRequest.action,
}
}
}
if (responsePayloadB64) {
try {
return {
payload: JSON.parse(base64Decode(responsePayloadB64), jsonRevivers),
action: originalRequest.action,
}
} catch (e) {
console.error('Failed to parse payload from redirect response', e)
return {
error: 'Failed to parse payload from redirect',
action: originalRequest.action,
}
}
}
return {
error: "Redirect response missing 'payload' or 'error'",
action: originalRequest.action,
}
}
public openWallet(path?: string): Promise<void> {
if (this.mode === TransportMode.REDIRECT) {
throw new Error("`openWallet` is not available in 'redirect' mode.")
}
if (!this.isBrowser) {
throw new Error('Popup transport requires a browser environment.')
}
if (this.connectionState !== ConnectionState.DISCONNECTED) {
if (this.isWalletOpen) this.walletWindow?.focus()
return this.readyPromise || Promise.resolve()
}
this.connectionState = ConnectionState.CONNECTING
this.clearPendingRequests(new Error('Wallet connection reset during open.'))
this.messageQueue = []
this.clearTimeouts()
this.readyPromise = new Promise<void>((resolve, reject) => {
this.readyPromiseResolve = resolve
this.readyPromiseReject = reject
})
this.readyPromise.catch(() => {})
this.initId = this.generateId()
const fullWalletUrl = path ? `${this.walletUrl}${path}` : this.walletUrl
this.sessionId = this.generateId()
const urlWithParams = new URL(fullWalletUrl)
urlWithParams.searchParams.set('dappOrigin', window.location.origin)
urlWithParams.searchParams.set('sessionId', this.sessionId)
try {
const openedWindow = window.open(
urlWithParams.toString(),
'Wallet',
`width=${WalletSize.width},height=${WalletSize.height},scrollbars=yes,resizable=yes`,
)
this.walletWindow = openedWindow || undefined
} catch (error) {
const openError = new Error(
`Failed to open wallet window: ${error instanceof Error ? error.message : String(error)}`,
)
this._handlePreConnectionFailure(openError)
return Promise.reject(openError)
}
if (!this.walletWindow) {
const error = new Error('Failed to open wallet window. Please check your pop-up blocker settings.')
this._handlePreConnectionFailure(error)
return Promise.reject(error)
}
this.handshakeTimeoutId = window.setTimeout(() => {
if (this.connectionState === ConnectionState.CONNECTING) {
const timeoutError = new Error(`Wallet handshake timed out after ${this.handshakeTimeoutMs}ms.`)
this._handlePreConnectionFailure(timeoutError)
}
}, this.handshakeTimeoutMs)
this.closeCheckIntervalId = window.setInterval(() => {
if (!this.isWalletOpen) {
if (this.connectionState === ConnectionState.CONNECTING)
this._handlePreConnectionFailure(new Error('Wallet window was closed before becoming ready.'))
else if (this.connectionState === ConnectionState.CONNECTED) this._handleDetectedClosure()
}
}, 500)
return this.readyPromise
}
public closeWallet(): void {
if (this.mode === TransportMode.REDIRECT) {
console.warn(
"[DApp] `closeWallet` is not available in 'redirect' mode. Use window.location.href to navigate away.",
)
return
}
if (this.connectionState === ConnectionState.DISCONNECTED) return
if (this.isWalletOpen) this.walletWindow?.close()
this.connectionState = ConnectionState.DISCONNECTED
this.readyPromise = undefined
this.readyPromiseResolve = undefined
this.readyPromiseReject = undefined
this._resetConnection(new Error('Wallet closed intentionally by DApp.'), 'Wallet closed intentionally by DApp.')
}
destroy(): void {
if (this.mode === TransportMode.POPUP && this.isBrowser) {
window.removeEventListener('message', this.handleMessage)
if (this.isWalletOpen) {
this.walletWindow?.close()
}
this._resetConnection(new Error('Transport destroyed.'), 'Destroying transport...')
} else {
this._resetConnection(new Error('Transport destroyed.'), 'Destroying transport...')
}
}
private handleMessage = (event: MessageEvent): void => {
if (event.origin !== this.walletOrigin) {
return
}
if (!this.walletWindow || event.source !== this.walletWindow) {
return
}
const message = event.data as TransportMessage
if (
!message ||
typeof message !== 'object' ||
!message.id ||
!message.type ||
(message.type === MessageType.WALLET_OPENED && !message.sessionId)
) {
return
}
try {
switch (message.type) {
case MessageType.WALLET_OPENED:
this.handleWalletReadyMessage(message)
break
case MessageType.RESPONSE:
this.handleResponseMessage(message)
break
case MessageType.REQUEST:
case MessageType.INIT:
default:
break
}
} catch (error) {
console.error(`[DApp] Error processing received message (Type: ${message.type}, ID: ${message.id}):`, error)
}
}
private handleWalletReadyMessage(message: TransportMessage): void {
if (this.connectionState !== ConnectionState.CONNECTING) {
return
}
if (message.sessionId !== this.sessionId) {
return
}
if (this.handshakeTimeoutId !== undefined) {
window.clearTimeout(this.handshakeTimeoutId)
this.handshakeTimeoutId = undefined
}
const initMessage: TransportMessage = {
id: this.initId!,
type: MessageType.INIT,
sessionId: this.sessionId,
}
this.postMessageToWallet(initMessage)
this.connectionState = ConnectionState.CONNECTED
if (this.readyPromiseResolve) {
this.readyPromiseResolve()
}
this.messageQueue.forEach((queuedMsg) => {
this.postMessageToWallet(queuedMsg)
})
this.messageQueue = []
}
private handleResponseMessage(message: TransportMessage): void {
const pending = this.pendingRequests.get(message.id)
if (pending) {
window.clearTimeout(pending.timer)
this.pendingRequests.delete(message.id)
if (message.error) {
const error = new Error(`Wallet responded with error: ${JSON.stringify(message.error)}`)
pending.reject(error)
} else {
pending.resolve(message.payload)
}
}
}
private postMessageToWallet(message: TransportMessage): void {
if (!this.isWalletOpen) {
if (
message.type === MessageType.INIT &&
this.connectionState === ConnectionState.CONNECTING &&
message.id === this.initId
) {
this._handlePreConnectionFailure(new Error('Failed to send INIT: Wallet window closed unexpectedly.'))
} else if (message.type === MessageType.REQUEST) {
const pendingReq = this.pendingRequests.get(message.id)
if (pendingReq) {
window.clearTimeout(pendingReq.timer)
this.pendingRequests.delete(message.id)
pendingReq.reject(new Error(`Failed to send request '${pendingReq.action}': Wallet window closed.`))
}
}
return
}
if (this.connectionState !== ConnectionState.CONNECTED && message.type !== MessageType.INIT) {
this.messageQueue.push(message)
return
}
try {
this.walletWindow?.postMessage(message, this.walletOrigin)
} catch (error) {
const rejectionError =
error instanceof Error ? error : new Error('Failed to send message to wallet due to unknown error')
if (
message.type === MessageType.INIT &&
this.connectionState === ConnectionState.CONNECTING &&
message.id === this.initId
) {
this._handlePreConnectionFailure(rejectionError)
} else if (message.type === MessageType.REQUEST) {
const pendingReq = this.pendingRequests.get(message.id)
if (pendingReq) {
window.clearTimeout(pendingReq.timer)
this.pendingRequests.delete(message.id)
pendingReq.reject(rejectionError)
}
this._handleDetectedClosure()
} else {
this._handleDetectedClosure()
}
}
}
private _resetConnection(reason: Error, logMessage: string): void {
console.log(`[DApp] ${logMessage}`)
if (this.readyPromiseReject) {
this.readyPromiseReject(reason)
}
this.clearTimeouts()
this.clearPendingRequests(reason)
this.connectionState = ConnectionState.DISCONNECTED
this.walletWindow = undefined
this.readyPromise = undefined
this.readyPromiseResolve = undefined
this.readyPromiseReject = undefined
this.initId = undefined
this.sessionId = undefined
this.messageQueue = []
}
private _handlePreConnectionFailure(error: Error): void {
this._resetConnection(error, `Connection failure: ${error.message}`)
}
private _handleDetectedClosure(): void {
if (this.connectionState === ConnectionState.CONNECTED) {
const reason = new Error('Wallet connection terminated unexpectedly.')
this._resetConnection(reason, 'Wallet connection terminated unexpectedly after ready.')
}
}
private clearPendingRequests(reason: Error): void {
if (this.pendingRequests.size > 0) {
const requestsToClear = new Map(this.pendingRequests)
this.pendingRequests.clear()
requestsToClear.forEach((pending) => {
clearTimeout(pending.timer)
const errorToSend = reason instanceof Error ? reason : new Error(`Operation failed: ${reason}`)
pending.reject(errorToSend)
})
}
}
private clearTimeouts(): void {
if (this.handshakeTimeoutId !== undefined) {
clearTimeout(this.handshakeTimeoutId)
this.handshakeTimeoutId = undefined
}
if (this.closeCheckIntervalId !== undefined) {
clearInterval(this.closeCheckIntervalId)
this.closeCheckIntervalId = undefined
}
}
private generateId(): string {
return `${Date.now().toString(36)}-${Math.random().toString(36).substring(2, 9)}`
}
}