@0xsequence/provider
Advanced tools
Comparing version 0.22.0 to 0.22.1
# @0xsequence/provider | ||
## 0.22.1 | ||
### Patch Changes | ||
- transport session cache | ||
- Updated dependencies [undefined] | ||
- @0xsequence/abi@0.22.1 | ||
- @0xsequence/auth@0.22.1 | ||
- @0xsequence/config@0.22.1 | ||
- @0xsequence/network@0.22.1 | ||
- @0xsequence/transactions@0.22.1 | ||
- @0xsequence/utils@0.22.1 | ||
- @0xsequence/wallet@0.22.1 | ||
## 0.22.0 | ||
@@ -4,0 +18,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { WalletTransport, ProviderMessage, ProviderMessageRequest, ProviderMessageResponse, ProviderRpcError, InitState, ConnectDetails, OpenWalletIntent, WalletSession } from '../types'; | ||
import { WalletTransport, ProviderMessage, ProviderMessageRequest, ProviderMessageResponse, ProviderRpcError, InitState, ConnectDetails, WalletSession, TransportSession } from '../types'; | ||
import { WalletRequestHandler } from './wallet-request-handler'; | ||
@@ -35,3 +35,5 @@ import { NetworkConfig, WalletContext, JsonRpcRequest, JsonRpcResponseCallback } from '@0xsequence/network'; | ||
private init; | ||
protected open: (intent?: OpenWalletIntent | undefined, networkId?: string | number | null | undefined) => Promise<boolean>; | ||
protected open: ({ sessionId, intent, networkId }: TransportSession) => Promise<boolean>; | ||
private saveTransportSession; | ||
protected getCachedTransportSession: () => TransportSession | null; | ||
} |
@@ -9,4 +9,5 @@ import { BaseWalletTransport } from '../base-wallet-transport'; | ||
register(): void; | ||
restoreSession(): void; | ||
unregister(): void; | ||
sendMessage(message: ProviderMessage<any>): void; | ||
} |
@@ -14,5 +14,3 @@ import { ProviderMessage } from '../../types'; | ||
private postMessage; | ||
private getWindowSession; | ||
private saveWindowSession; | ||
private restoreWindowSession; | ||
private getWindowTransportSession; | ||
} |
@@ -62,2 +62,7 @@ import { NetworkConfig, WalletContext, JsonRpcRequest, JsonRpcResponse, JsonRpcHandler } from '@0xsequence/network'; | ||
} | ||
export interface TransportSession { | ||
sessionId?: string | null; | ||
networkId?: string | number | null; | ||
intent?: OpenWalletIntent; | ||
} | ||
export declare enum EventType { | ||
@@ -64,0 +69,0 @@ OPEN = "open", |
{ | ||
"name": "@0xsequence/provider", | ||
"version": "0.22.0", | ||
"version": "0.22.1", | ||
"description": "provider sub-package for Sequence", | ||
@@ -16,9 +16,9 @@ "repository": "https://github.com/0xsequence/sequence.js/tree/master/packages/provider", | ||
"dependencies": { | ||
"@0xsequence/abi": "^0.22.0", | ||
"@0xsequence/auth": "^0.22.0", | ||
"@0xsequence/config": "^0.22.0", | ||
"@0xsequence/network": "^0.22.0", | ||
"@0xsequence/transactions": "^0.22.0", | ||
"@0xsequence/utils": "^0.22.0", | ||
"@0xsequence/wallet": "^0.22.0", | ||
"@0xsequence/abi": "^0.22.1", | ||
"@0xsequence/auth": "^0.22.1", | ||
"@0xsequence/config": "^0.22.1", | ||
"@0xsequence/network": "^0.22.1", | ||
"@0xsequence/transactions": "^0.22.1", | ||
"@0xsequence/utils": "^0.22.1", | ||
"@0xsequence/wallet": "^0.22.1", | ||
"@ethersproject/abstract-signer": "5.0.14", | ||
@@ -25,0 +25,0 @@ "@ethersproject/hash": "^5.0.12", |
@@ -5,3 +5,3 @@ import { ethers } from 'ethers' | ||
EventType, ProviderMessageResponse, ProviderMessageTransport, | ||
ProviderRpcError, InitState, ConnectDetails, OpenWalletIntent, WalletSession | ||
ProviderRpcError, InitState, ConnectDetails, OpenWalletIntent, WalletSession, TransportSession | ||
} from '../types' | ||
@@ -12,3 +12,3 @@ | ||
import { NetworkConfig, WalletContext, JsonRpcRequest, JsonRpcResponseCallback } from '@0xsequence/network' | ||
import { logger, sanitizeAlphanumeric, sanitizeHost } from '@0xsequence/utils' | ||
import { logger, sanitizeAlphanumeric, sanitizeHost, sanitizeNumberString } from '@0xsequence/utils' | ||
import { AuthorizationOptions } from '@0xsequence/auth' | ||
@@ -18,2 +18,4 @@ | ||
const TRANSPORT_SESSION_LS_KEY = '@sequence.transportSession' | ||
export abstract class BaseWalletTransport implements WalletTransport { | ||
@@ -105,3 +107,3 @@ | ||
if (this._initCallback) this._initCallback('invalid init') | ||
return | ||
return | ||
} | ||
@@ -119,4 +121,8 @@ } else { | ||
if (this._init !== InitState.OK) return | ||
const { intent, networkId } = request.data | ||
await this.open(intent, networkId) | ||
const session: TransportSession = { | ||
sessionId: request.data.sessionId, | ||
intent: request.data.intent, | ||
networkId: request.data.networkId | ||
} | ||
await this.open(session) | ||
return | ||
@@ -298,3 +304,8 @@ } | ||
protected open = async (intent?: OpenWalletIntent, networkId?: string | number | null): Promise<boolean> => { | ||
protected open = async ({ sessionId, intent, networkId }: TransportSession): Promise<boolean> => { | ||
if (sessionId) { | ||
this._sessionId = sanitizeNumberString(sessionId) | ||
// persist transport session in localstorage for restoring after redirect/reload | ||
this.saveTransportSession({ sessionId, intent, networkId }) | ||
} | ||
@@ -338,3 +349,2 @@ // init handshake for certain transports, before we can open the communication. | ||
} | ||
} else { | ||
@@ -407,9 +417,24 @@ this.walletRequestHandler.setConnectOptions(undefined) | ||
session: await this.walletRequestHandler.walletSession() | ||
}) | ||
}) | ||
} | ||
} | ||
return true | ||
} | ||
private saveTransportSession = (session: TransportSession) => { | ||
window.localStorage.setItem(TRANSPORT_SESSION_LS_KEY, JSON.stringify(session)) | ||
} | ||
protected getCachedTransportSession = (): TransportSession | null => { | ||
const session = window.localStorage.getItem(TRANSPORT_SESSION_LS_KEY) | ||
try { | ||
return session ? JSON.parse(session) as TransportSession : null | ||
} catch (err) { | ||
console.error(`unable to parse transport session: ${session}`) | ||
return null | ||
} | ||
} | ||
} |
@@ -23,2 +23,15 @@ import { BaseWalletTransport } from '../base-wallet-transport' | ||
// note: we can't decide whether to restore the session within register(), because session info is | ||
// received asyncronously via EventType.OPEN after register() is executed. | ||
// And in the case of a redirect/reload, EventType.OPEN is not sent at all, | ||
// because the wallet is already open. | ||
// | ||
// call this method from wallet redirect hander when a session restore is needed | ||
restoreSession() { | ||
const cachedSession = this.getCachedTransportSession() | ||
if (cachedSession) { | ||
this.open(cachedSession) | ||
} | ||
} | ||
unregister() { | ||
@@ -25,0 +38,0 @@ // @ts-ignore |
@@ -58,5 +58,7 @@ import { BaseProviderTransport } from '../base-provider-transport' | ||
this.state = OpenState.OPENING | ||
const sessionId = `${performance.now()}` | ||
this._sessionId = sessionId | ||
this.sendMessage({ | ||
idx: -1, type: EventType.OPEN, data: { | ||
path, intent, networkId | ||
path, intent, networkId, sessionId | ||
} | ||
@@ -63,0 +65,0 @@ }) |
@@ -8,3 +8,4 @@ import { | ||
OpenWalletIntent, | ||
ProviderRpcError | ||
ProviderRpcError, | ||
TransportSession | ||
} from '../../types' | ||
@@ -35,26 +36,18 @@ import { WalletRequestHandler } from '../wallet-request-handler' | ||
let params = this.getWindowSession(rawParams) | ||
let session: TransportSession | null = this.getWindowTransportSession(rawParams) | ||
// provider should always include sid when opening a new window | ||
const isNewWindowSession = params.get('sid') !== null | ||
const isNewWindowSession = !!session.sessionId | ||
if (isNewWindowSession) { | ||
// brand new popup opened, persist session params | ||
params = this.saveWindowSession(rawParams) | ||
} else { | ||
// no sid? might be popup window redirect or reload .. | ||
// attempt to restore previous session from local storage | ||
params = this.restoreWindowSession() | ||
// attempt to restore previous session in the case of a redirect or window reload | ||
if (!isNewWindowSession) { | ||
session = this.getCachedTransportSession() | ||
} | ||
this._sessionId = sanitizeNumberString(params.get('sid')) | ||
if (this._sessionId.length === 0) { | ||
logger.error('invalid sessionId') | ||
if (!session) { | ||
logger.error('window session is undefined') | ||
return | ||
} | ||
const intent = base64DecodeObject<OpenWalletIntent>(params.get('intent')) | ||
if (intent && isNewWindowSession) { | ||
if (session.intent && isNewWindowSession) { | ||
window.history.replaceState({ openWalletIntent: true }, document.title, pathname) | ||
@@ -70,9 +63,7 @@ } | ||
const networkId = params.get('net') | ||
// send open event to the app which opened us | ||
this.open(intent, networkId) | ||
this.open(session) | ||
.then(opened => { | ||
if (!opened) { | ||
const err = `failed to open to network ${networkId}` | ||
const err = `failed to open to network ${session?.networkId}` | ||
logger.error(err) | ||
@@ -84,3 +75,3 @@ this.notifyClose({ message: err } as ProviderRpcError) | ||
.catch(e => { | ||
const err = `failed to open to network ${networkId}, due to: ${e}` | ||
const err = `failed to open to network ${session?.networkId}, due to: ${e}` | ||
logger.error(err) | ||
@@ -171,15 +162,10 @@ this.notifyClose({ message: err } as ProviderRpcError) | ||
private getWindowSession = (params: string | undefined): WindowSessionParams => { | ||
return new WindowSessionParams(params) | ||
private getWindowTransportSession = (windowParams: string | undefined): TransportSession => { | ||
const params = new WindowSessionParams(windowParams) | ||
return { | ||
sessionId: params.get('sid'), | ||
networkId: params.get('net'), | ||
intent: base64DecodeObject<OpenWalletIntent>(params.get('intent')) | ||
} | ||
} | ||
private saveWindowSession = (params: string): WindowSessionParams => { | ||
window.localStorage.setItem('@sequence.windowSession', params) | ||
return this.getWindowSession(params) | ||
} | ||
private restoreWindowSession = (): WindowSessionParams => { | ||
const cachedWindowSessionParams = window.localStorage.getItem('@sequence.windowSession') | ||
return this.getWindowSession(cachedWindowSessionParams || undefined) | ||
} | ||
} |
@@ -25,3 +25,3 @@ import { NetworkConfig, WalletContext, JsonRpcRequest, JsonRpcResponse, JsonRpcHandler } from '@0xsequence/network' | ||
unregister(): void | ||
notifyOpen(openInfo: { chainId?: string, sessionId?: string, session?: WalletSession, error?: string }): void | ||
@@ -55,3 +55,3 @@ notifyClose(error?: ProviderRpcError): void | ||
code?: number | ||
data?: {[key: string]: any} | ||
data?: { [key: string]: any } | ||
} | ||
@@ -86,2 +86,8 @@ | ||
export interface TransportSession { | ||
sessionId?: string | null | ||
networkId?: string | number | null, | ||
intent?: OpenWalletIntent, | ||
} | ||
export enum EventType { | ||
@@ -107,6 +113,6 @@ OPEN = 'open', | ||
'close': (error?: ProviderRpcError) => void | ||
'connect': (connectDetails: ConnectDetails) => void | ||
'disconnect': (error?: ProviderRpcError) => void | ||
'accountsChanged': (accounts: string[]) => void | ||
@@ -217,3 +223,3 @@ 'chainChanged': (chainIdHex: string) => void | ||
// Caching provider responses for things such as account and chainId | ||
providerCache?: {[key: string]: any} | ||
providerCache?: { [key: string]: any } | ||
} | ||
@@ -220,0 +226,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
475390
11481
Updated@0xsequence/abi@^0.22.1
Updated@0xsequence/auth@^0.22.1
Updated@0xsequence/config@^0.22.1
Updated@0xsequence/network@^0.22.1
Updated@0xsequence/utils@^0.22.1
Updated@0xsequence/wallet@^0.22.1