@0xsequence/provider
Advanced tools
Comparing version 0.21.2 to 0.21.3
# @0xsequence/provider | ||
## 0.21.3 | ||
### Patch Changes | ||
- add window session cache | ||
- Updated dependencies [undefined] | ||
- @0xsequence/abi@0.21.3 | ||
- @0xsequence/auth@0.21.3 | ||
- @0xsequence/config@0.21.3 | ||
- @0xsequence/network@0.21.3 | ||
- @0xsequence/transactions@0.21.3 | ||
- @0xsequence/utils@0.21.3 | ||
- @0xsequence/wallet@0.21.3 | ||
## 0.21.2 | ||
@@ -4,0 +18,0 @@ |
@@ -35,3 +35,3 @@ import { WalletTransport, ProviderMessage, ProviderMessageRequest, ProviderMessageResponse, ProviderRpcError, InitState, ConnectDetails, OpenWalletIntent, WalletSession } from '../types'; | ||
private init; | ||
protected open: (intent?: OpenWalletIntent | undefined, networkId?: string | number | undefined) => Promise<boolean>; | ||
protected open: (intent?: OpenWalletIntent | undefined, networkId?: string | number | null | undefined) => Promise<boolean>; | ||
} |
@@ -14,2 +14,5 @@ import { ProviderMessage } from '../../types'; | ||
private postMessage; | ||
private getWindowSession; | ||
private saveWindowSession; | ||
private restoreWindowSession; | ||
} |
@@ -54,2 +54,10 @@ import { NetworkConfig, WalletContext, JsonRpcRequest, JsonRpcResponse, JsonRpcHandler } from '@0xsequence/network'; | ||
} | ||
export declare type WindowSessionParam = 'sid' | 'net' | 'intent'; | ||
export interface WindowSessionParams extends URLSearchParams { | ||
get(name: WindowSessionParam): string | null; | ||
set(name: WindowSessionParam, value: string): void; | ||
} | ||
export declare class WindowSessionParams extends URLSearchParams { | ||
static new(init?: Record<WindowSessionParam, string> | string): WindowSessionParams; | ||
} | ||
export declare enum EventType { | ||
@@ -56,0 +64,0 @@ OPEN = "open", |
{ | ||
"name": "@0xsequence/provider", | ||
"version": "0.21.2", | ||
"version": "0.21.3", | ||
"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.21.2", | ||
"@0xsequence/auth": "^0.21.2", | ||
"@0xsequence/config": "^0.21.2", | ||
"@0xsequence/network": "^0.21.2", | ||
"@0xsequence/transactions": "^0.21.2", | ||
"@0xsequence/utils": "^0.21.2", | ||
"@0xsequence/wallet": "^0.21.2", | ||
"@0xsequence/abi": "^0.21.3", | ||
"@0xsequence/auth": "^0.21.3", | ||
"@0xsequence/config": "^0.21.3", | ||
"@0xsequence/network": "^0.21.3", | ||
"@0xsequence/transactions": "^0.21.3", | ||
"@0xsequence/utils": "^0.21.3", | ||
"@0xsequence/wallet": "^0.21.3", | ||
"@ethersproject/abstract-signer": "5.0.14", | ||
@@ -25,0 +25,0 @@ "@ethersproject/hash": "^5.0.12", |
@@ -126,22 +126,24 @@ import EventEmitter from 'eventemitter3' | ||
// init incoming for initial handshake with transport. | ||
// always respond to INIT messages, e.g. on popup window reload | ||
if (message.type === EventType.INIT) { | ||
logger.debug('MessageProvider, received INIT message', message) | ||
const { nonce } = message.data as { nonce: string } | ||
if (!nonce || nonce.length == 0) { | ||
logger.error('invalid init nonce') | ||
return | ||
} | ||
this._init = InitState.OK | ||
this.sendMessage({ | ||
idx: -1, | ||
type: EventType.INIT, | ||
data: { | ||
sessionId: this._sessionId, | ||
nonce: nonce | ||
} | ||
}) | ||
} | ||
if (this._init !== InitState.OK) { | ||
// if provider is not init'd, then we drop any received messages. the only | ||
// message we will process is of event type 'init', as our acknowledgement | ||
if (message.type === EventType.INIT) { | ||
logger.debug('MessageProvider, received INIT message', message) | ||
const { nonce } = message.data as { nonce: string } | ||
if (!nonce || nonce.length == 0) { | ||
logger.error('invalid init nonce') | ||
return | ||
} | ||
this._init = InitState.OK | ||
this.sendMessage({ | ||
idx: -1, | ||
type: EventType.INIT, | ||
data: { | ||
sessionId: this._sessionId, | ||
nonce: nonce | ||
} | ||
}) | ||
} | ||
return | ||
@@ -148,0 +150,0 @@ } |
@@ -293,3 +293,3 @@ import { ethers } from 'ethers' | ||
protected open = async (intent?: OpenWalletIntent, networkId?: string | number): Promise<boolean> => { | ||
protected open = async (intent?: OpenWalletIntent, networkId?: string | number | null): Promise<boolean> => { | ||
@@ -296,0 +296,0 @@ // init handshake for certain transports, before we can open the communication. |
@@ -5,5 +5,4 @@ import { | ||
EventType, | ||
ProviderMessageResponse, | ||
InitState, | ||
ConnectDetails, | ||
WindowSessionParams, | ||
OpenWalletIntent, | ||
@@ -34,6 +33,20 @@ ProviderRpcError | ||
// record open details (sessionId + default network) from the window url | ||
const location = new URL(window.location.href) | ||
const params = new URLSearchParams(location.search) | ||
const { pathname, search: rawParams } = new URL(window.location.href) | ||
this._sessionId = sanitizeNumberString(params.get('sid')!) | ||
let params = this.getWindowSession(rawParams) | ||
// provider should always include sid when opening a new window | ||
const isNewWindowSession = params.get('sid') !== null | ||
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() | ||
} | ||
this._sessionId = sanitizeNumberString(params.get('sid')) | ||
if (this._sessionId.length === 0) { | ||
@@ -44,7 +57,6 @@ logger.error('invalid sessionId') | ||
const intent = base64DecodeObject<OpenWalletIntent>(params.get('intent')!) | ||
const networkId = params.get('net')! | ||
const intent = base64DecodeObject<OpenWalletIntent>(params.get('intent')) | ||
if (intent) { | ||
window.history.replaceState({ openWalletIntent: true }, document.title, location.pathname) | ||
if (intent && isNewWindowSession) { | ||
window.history.replaceState({ openWalletIntent: true }, document.title, pathname) | ||
} | ||
@@ -59,2 +71,4 @@ | ||
const networkId = params.get('net') | ||
// send open event to the app which opened us | ||
@@ -156,2 +170,16 @@ this.open(intent, networkId) | ||
} | ||
private getWindowSession = (params: string | undefined): WindowSessionParams => { | ||
return new WindowSessionParams(params) | ||
} | ||
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) | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import { OpenWalletIntent, ProviderMessage, InitState, EventType } from '../../types' | ||
import { OpenWalletIntent, ProviderMessage, InitState, EventType, WindowSessionParams } from '../../types' | ||
import { BaseProviderTransport } from '../base-provider-transport' | ||
@@ -70,4 +70,6 @@ import { logger, base64EncodeObject } from '@0xsequence/utils' | ||
// Instantiate new walletURL for this call | ||
// Instantiate new walletURL for this call | ||
const walletURL = new URL(this.walletURL.href) | ||
const windowSessionParams = new WindowSessionParams() | ||
if (path && path !== '') { | ||
@@ -80,3 +82,3 @@ walletURL.pathname = path.toLowerCase() | ||
this._sessionId = `${performance.now()}` | ||
walletURL.searchParams.set('sid', this._sessionId) | ||
windowSessionParams.set('sid', this._sessionId) | ||
if (intent) { | ||
@@ -87,10 +89,10 @@ // for the window-transport, we eagerly/optimistically set the origin host | ||
if (intent.type === 'connect') { | ||
if (!intent.options) intent.options = {} | ||
if (!intent.options) intent.options = {} | ||
intent.options.origin = window.location.origin | ||
} | ||
// encode intent as base6 url-encoded param | ||
walletURL.searchParams.set('intent', base64EncodeObject(intent)) | ||
windowSessionParams.set('intent', base64EncodeObject(intent)) | ||
} | ||
if (networkId) { | ||
walletURL.searchParams.set('net', `${networkId}`) | ||
windowSessionParams.set('net', `${networkId}`) | ||
} | ||
@@ -101,11 +103,14 @@ | ||
const windowPos = [ | ||
Math.abs(window.screenX+(window.innerWidth/2)-(windowSize[0]/2)), | ||
Math.abs(window.screenY+(window.innerHeight/2)-(windowSize[1]/2)) | ||
Math.abs(window.screenX + window.innerWidth / 2 - windowSize[0] / 2), | ||
Math.abs(window.screenY + window.innerHeight / 2 - windowSize[1] / 2) | ||
] | ||
const windowFeatures = | ||
`toolbar=0,location=0,menubar=0,scrollbars=yes,status=yes`+ | ||
`,width=${windowSize[0]},height=${windowSize[1]}`+ | ||
`toolbar=0,location=0,menubar=0,scrollbars=yes,status=yes` + | ||
`,width=${windowSize[0]},height=${windowSize[1]}` + | ||
`,left=${windowPos[0]},top=${windowPos[1]}` | ||
// serialize params | ||
walletURL.search = windowSessionParams.toString() | ||
this.walletWindow = window.open(walletURL.href, 'sequence.app', windowFeatures) | ||
@@ -151,4 +156,4 @@ | ||
} | ||
let message: ProviderMessage<any> | ||
let message: ProviderMessage<any> | ||
try { | ||
@@ -155,0 +160,0 @@ message = JSON.parse(event.data) |
@@ -71,2 +71,15 @@ import { NetworkConfig, WalletContext, JsonRpcRequest, JsonRpcResponse, JsonRpcHandler } from '@0xsequence/network' | ||
export type WindowSessionParam = 'sid' | 'net' | 'intent' | ||
export interface WindowSessionParams extends URLSearchParams { | ||
get(name: WindowSessionParam): string | null | ||
set(name: WindowSessionParam, value: string): void | ||
} | ||
export class WindowSessionParams extends URLSearchParams { | ||
static new(init?: Record<WindowSessionParam, string> | string) { | ||
return new URLSearchParams(init) as WindowSessionParams | ||
} | ||
} | ||
export enum EventType { | ||
@@ -73,0 +86,0 @@ OPEN = 'open', |
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
469209
11353
Updated@0xsequence/abi@^0.21.3
Updated@0xsequence/auth@^0.21.3
Updated@0xsequence/config@^0.21.3
Updated@0xsequence/network@^0.21.3
Updated@0xsequence/utils@^0.21.3
Updated@0xsequence/wallet@^0.21.3