Socket
Socket
Sign inDemoInstall

@0xsequence/provider

Package Overview
Dependencies
Maintainers
2
Versions
498
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@0xsequence/provider - npm Package Compare versions

Comparing version 0.8.5 to 0.9.1

31

CHANGELOG.md
# @0xsequence/provider
## 0.9.1
### Patch Changes
- - patch bump
- Updated dependencies [undefined]
- @0xsequence/abi@0.9.1
- @0xsequence/auth@0.9.1
- @0xsequence/config@0.9.1
- @0xsequence/network@0.9.1
- @0xsequence/transactions@0.9.1
- @0xsequence/utils@0.9.1
- @0xsequence/wallet@0.9.1
## 0.9.0
### Minor Changes
- - provider transport hardening
### Patch Changes
- Updated dependencies [undefined]
- @0xsequence/abi@0.9.0
- @0xsequence/auth@0.9.0
- @0xsequence/config@0.9.0
- @0xsequence/network@0.9.0
- @0xsequence/transactions@0.9.0
- @0xsequence/utils@0.9.0
- @0xsequence/wallet@0.9.0
## 0.8.5

@@ -4,0 +35,0 @@

1

dist/declarations/src/transports/base-wallet-transport.d.ts

@@ -7,2 +7,3 @@ import { WalletTransport, ProviderMessage, ProviderMessageRequest, ProviderMessageResponse } from '../types';

protected _sessionId: string;
protected registered: boolean;
constructor(walletRequestHandler: WalletRequestHandler);

@@ -9,0 +10,0 @@ register(): void;

7

dist/declarations/src/transports/wallet-request-handler.d.ts

@@ -11,5 +11,6 @@ import { ProviderMessageRequest, ProviderMessageResponse, WalletMessageEvent, ProviderMessageRequestHandler, MessageToSign } from '../types';

private testnetNetworks;
private defaultNetworkId?;
private events;
constructor(signer: Signer | null, prompter: WalletUserPrompter | null, mainnetNetworks: Networks, testnetNetworks?: Networks);
setup(signer: Signer | null, mainnetNetworks?: Networks, testnetNetworks?: Networks): void;
setup(signer: Signer | null, mainnetNetworks?: Networks, testnetNetworks?: Networks): Promise<void>;
sendMessageRequest(message: ProviderMessageRequest): Promise<ProviderMessageResponse>;

@@ -21,7 +22,7 @@ sendAsync: (request: JsonRpcRequest, callback: JsonRpcResponseCallback, chainId?: number) => Promise<void>;

getChainId(): Promise<number>;
setDefaultChain(chainId: string | number): void;
setDefaultChain(chainId: string | number): Promise<boolean>;
getNetworks(jsonRpcResponse?: boolean): Promise<NetworkConfig[]>;
notifyNetworks(networks: NetworkConfig[]): void;
notifyLogin(accountAddress: string): void;
notifyLogout(): void;
notifyNetworks(networks?: NetworkConfig[]): Promise<void>;
getSigner(): Signer | null;

@@ -28,0 +29,0 @@ setSigner(signer: Signer | null): void;

@@ -68,3 +68,5 @@ import { NetworkConfig, WalletContext, ChainId } from '@0xsequence/network';

private useSession;
private useAccountAddress;
private useNetworks;
private clearSession;
}

@@ -71,0 +73,0 @@ export interface ProviderConfig {

{
"name": "@0xsequence/provider",
"version": "0.8.5",
"version": "0.9.1",
"description": "provider sub-package for Sequence",

@@ -16,14 +16,14 @@ "repository": "https://github.com/0xsequence/sequence.js/tree/master/packages/provider",

"dependencies": {
"@0xsequence/abi": "^0.8.5",
"@0xsequence/auth": "^0.8.5",
"@0xsequence/config": "^0.8.5",
"@0xsequence/network": "^0.8.5",
"@0xsequence/transactions": "^0.8.5",
"@0xsequence/utils": "^0.8.5",
"@0xsequence/wallet": "^0.8.5",
"@ethersproject/abstract-signer": "5.0.11",
"@ethersproject/hash": "^5.0.10",
"@ethersproject/providers": "^5.0.19",
"@ethersproject/web": "^5.0.12",
"ethers": "^5.0.26",
"@0xsequence/abi": "^0.9.1",
"@0xsequence/auth": "^0.9.1",
"@0xsequence/config": "^0.9.1",
"@0xsequence/network": "^0.9.1",
"@0xsequence/transactions": "^0.9.1",
"@0xsequence/utils": "^0.9.1",
"@0xsequence/wallet": "^0.9.1",
"@ethersproject/abstract-signer": "5.0.12",
"@ethersproject/hash": "^5.0.11",
"@ethersproject/providers": "^5.0.20",
"@ethersproject/web": "^5.0.13",
"ethers": "^5.0.27",
"eventemitter3": "^4.0.7",

@@ -30,0 +30,0 @@ "platform": "^1.3.6"

@@ -309,4 +309,2 @@ import { ethers } from 'ethers'

// sendUncheckedTransaction matches implementation from ethers JsonRpcSigner for compatibility, but with
// multi-chain support.
async sendUncheckedTransaction(transaction: Deferrable<TransactionRequest>, chainId?: ChainId): Promise<string> {

@@ -320,10 +318,12 @@ transaction = shallowCopy(transaction)

// The JSON-RPC for eth_sendTransaction uses 90000 gas; if the user
// wishes to use this, it is easy to specify explicitly, otherwise
// we look it up for them.
if (transaction.gasLimit == null) {
const estimate = shallowCopy(transaction)
estimate.from = fromAddress
transaction.gasLimit = this.provider.estimateGas(estimate)
}
// NOTE: we do not use provider estimation, and instead rely on our relayer to determine the gasLimit and gasPrice
//
// TODO: alternatively/one day, we could write a provider middleware to eth_estimateGas
// and send it to our relayer url instead for estimation..
//
// if (!transaction.gasLimit) {
// const estimate = shallowCopy(transaction)
// estimate.from = fromAddress
// transaction.gasLimit = this.provider.estimateGas(estimate)
// }

@@ -330,0 +330,0 @@ const provider = await this.getSender(maybeNetworkId(chainId) || this.defaultChainId)

@@ -111,5 +111,6 @@ import EventEmitter from 'eventemitter3'

if (message.data?.result?.error) {
console.error('connection to wallet failed')
const err = new Error(`connection to wallet failed: received ${message.data?.result?.error}`)
console.error(err)
this.disconnect()
throw new Error(message.data?.result?.error)
throw err
}

@@ -166,4 +167,6 @@

this.accountPayload = message.data[0].toLowerCase()
this.events.emit('accountsChanged', [this.accountPayload])
} else {
this.events.emit('accountsChanged', [])
}
this.events.emit('accountsChanged', message.data)
return

@@ -170,0 +173,0 @@ }

@@ -15,5 +15,24 @@ import { ethers } from 'ethers'

protected _sessionId: string
protected registered: boolean
constructor(walletRequestHandler: WalletRequestHandler) {
this.walletRequestHandler = walletRequestHandler
this.walletRequestHandler.on('accountsChanged', (accounts: string[]) => {
if (!this.registered) return
this.notifyAccountsChanged(accounts)
})
this.walletRequestHandler.on('networks', (networks: NetworkConfig[]) => {
if (!this.registered) return
this.notifyNetworks(networks)
if (!networks || networks.length === 0) {
this.notifyChainChanged('0x0')
} else {
this.notifyChainChanged(ethers.utils.hexlify(networks[0].chainId))
}
})
// TODO: add .on('chainChanged') event? or covered by networks?
}

@@ -40,17 +59,2 @@

// set defaultChain upon connecting if one is requested
const defaultNetworkId = message.data.defaultNetworkId
if (defaultNetworkId) {
try {
this.walletRequestHandler.setDefaultChain(defaultNetworkId)
} catch (err) {
console.error(err)
this.notifyConnect({
sessionId: this._sessionId,
error: `${err}`
})
return
}
}
// success, respond with 'connect' event to the dapp directly

@@ -65,10 +69,2 @@ this.notifyConnect({ sessionId: this._sessionId })

this.notifyAccountsChanged([accountAddress])
// notify networks once the user has authenticated to avoid non-authed access
const networks = await this.walletRequestHandler.getNetworks(true)
if (networks && networks.length > 0) {
this.notifyNetworks(networks)
this.notifyChainChanged(ethers.utils.hexlify(networks[0].chainId))
}
} else {

@@ -79,2 +75,8 @@ // not logged in, we do not emit chain details until logged in

// set defaultChain upon connecting if one is requested
const defaultNetworkId = message.data.defaultNetworkId
if (defaultNetworkId) {
await this.walletRequestHandler.setDefaultChain(defaultNetworkId)
}
return

@@ -81,0 +83,0 @@ }

@@ -19,2 +19,3 @@ import { BaseWalletTransport } from '../base-wallet-transport'

}
this.registered = true
}

@@ -24,2 +25,3 @@

this.port.handleMessage = undefined
this.registered = false
}

@@ -26,0 +28,0 @@

@@ -25,2 +25,4 @@ import EventEmitter from 'eventemitter3'

private defaultNetworkId?: string | number
private events: EventEmitter<WalletMessageEvent, any> = new EventEmitter()

@@ -39,3 +41,3 @@

setup(signer: Signer | null, mainnetNetworks: Networks = [], testnetNetworks: Networks = []) {
async setup(signer: Signer | null, mainnetNetworks: Networks = [], testnetNetworks: Networks = []) {
this.signer = signer

@@ -48,2 +50,7 @@ if (mainnetNetworks && mainnetNetworks.length > 0) {

}
if (this.defaultNetworkId) {
if (!(await this.setDefaultChain(this.defaultNetworkId))) {
throw new Error(`WalletRequestHandler setup unable to set defaulNetworkId ${this.defaultNetworkId}`)
}
}
}

@@ -127,2 +134,4 @@

let sig = ''
// TODO:
// if (process.env.DEBUG_MODE === 'true' && this.prompter === null) {
if (this.prompter === null) {

@@ -376,5 +385,11 @@ // prompter is null, so we'll sign from here

const [defaultNetworkId] = request.params
if (defaultNetworkId) {
this.setDefaultChain(defaultNetworkId)
if (!defaultNetworkId) {
throw new Error('invalid request, method argument defaultNetworkId cannot be empty')
}
const ok = await this.setDefaultChain(defaultNetworkId)
if (!ok) {
throw new Error(`unable to set default network ${defaultNetworkId}`)
}
response.result = await this.getNetworks(true)

@@ -414,18 +429,27 @@ break

getAddress(): Promise<string> {
// TODO return '' if not logged in, or undefined.. first need
// something on login state
return this.signer.getAddress()
async getAddress(): Promise<string> {
if (!this.signer) {
return ''
} else {
return this.signer.getAddress()
}
}
getChainId(): Promise<number> {
return this.signer.getChainId()
async getChainId(): Promise<number> {
if (!this.signer) {
return 0
} else {
return this.signer.getChainId()
}
}
setDefaultChain(chainId: string | number) {
async setDefaultChain(chainId: string | number): Promise<boolean> {
if (!chainId) return
if ((<any>this.signer).setNetworks) {
this.defaultNetworkId = chainId
if (this.signer && (<any>this.signer).setNetworks) {
(<any>this.signer).setNetworks(this.mainnetNetworks, this.testnetNetworks, chainId)
await this.notifyNetworks()
return true
} else {
throw new Error('signer does not have setNetworks method available')
return false
}

@@ -435,2 +459,7 @@ }

async getNetworks(jsonRpcResponse?: boolean): Promise<NetworkConfig[]> {
if (!this.signer) {
console.warn('getNetworks returns empty list as signer is not set on WalletRequestHandler')
return []
}
const networks = await this.signer.getNetworks()

@@ -451,9 +480,9 @@

notifyNetworks(networks: NetworkConfig[]) {
this.events.emit('networks', networks)
this.events.emit('chainChanged', ethers.utils.hexlify(networks[0].chainId))
}
notifyLogin(accountAddress: string) {
this.events.emit('accountsChanged', [accountAddress])
if (!accountAddress || accountAddress.length === 0) {
this.events.emit('accountsChanged', [])
} else {
this.events.emit('accountsChanged', [accountAddress])
}
this.notifyNetworks()
}

@@ -463,4 +492,15 @@

this.events.emit('accountsChanged', [])
this.events.emit('networks', [])
}
async notifyNetworks(networks?: NetworkConfig[]) {
const n = networks || await this.getNetworks(true)
this.events.emit('networks', n)
if (n && n.length > 0) {
this.events.emit('chainChanged', ethers.utils.hexlify(n[0].chainId))
} else {
this.events.emit('chainChanged', '0x0')
}
}
getSigner(): Signer | null {

@@ -467,0 +507,0 @@ return this.signer

@@ -34,2 +34,3 @@ import { ProviderMessageRequest, ProviderMessage, ProviderMessageType, ProviderMessageResponse } from '../../types'

window.addEventListener('message', this.onWindowEvent, false)
this.registered = true
}

@@ -39,2 +40,3 @@

window.removeEventListener('message', this.onWindowEvent)
this.registered = false
}

@@ -41,0 +43,0 @@

@@ -152,4 +152,7 @@ import { ProviderMessage } from '../../types'

const postedMessage = typeof message !== 'string' ? JSON.stringify(message) : message
// TODO: connecting so fast, we're sending a message so quickly, that in certain instances
// we receive a target origin failure as the window's origin is not set yet but we're sending anyway
// The error is annoying, but the system should work correctly.
this.walletWindow.postMessage(postedMessage, this.walletURL.origin)
}
}

@@ -111,11 +111,2 @@ import { Networks, NetworkConfig, WalletContext, sequenceContext, ChainId, getNetworkId, JsonRpcSender,

// ...
this.transport.networkProvider = networkProviderMiddleware((request: JsonRpcRequest): number => {
// return stub chainId of 0 when not connected to any
if (!this.networks || this.networks.length === 0) return 0
// return the default chainId as we're connected
return this.networks[0].chainId
})
// .....

@@ -132,4 +123,18 @@ this.transport.allowProvider = allowProviderMiddleware((request: JsonRpcRequest): boolean => {

// ...
this.transport.networkProvider = networkProviderMiddleware((request: JsonRpcRequest): number => {
// return stub chainId of 0 when not connected to any
if (!this.networks || this.networks.length === 0) return 0
// return the default chainId as we're connected
return this.networks[0].chainId
})
// Provider proxy to support middleware stack of logging, caching and read-only rpc calls
this.transport.cachedProvider = new CachedProvider()
this.transport.cachedProvider.onUpdate(() => {
if (!this.session) this.session = { providerCache: {} }
this.session.providerCache = this.transport.cachedProvider.getCache()
this.saveSession(this.session)
})

@@ -148,18 +153,16 @@ // ..

// below will update the networks automatically when the wallet networks change, however
// this is currently disabled as it may confuse the dapp. Instead the dapp can
// check active networks list from the session and switch the default network
// with useNetwork() explicitly
//
// this.windowTransportProvider.on('networks', networks => {
// this.useNetworks(networks)
// this.saveSession(this.session)
// })
this.transport.messageProvider.on('accountsChanged', (accounts) => {
if (accounts && accounts.length === 0) {
this.logout()
// below will update the account upon wallet login/logout
this.transport.messageProvider.on('accountsChanged', (accounts: string[]) => {
if (!accounts || accounts.length === 0 || accounts[0] === '') {
this.clearSession()
} else {
this.useSession({ accountAddress: accounts[0] }, true)
}
})
// below will update the networks automatically when the wallet networks change
this.transport.messageProvider.on('networks', (networks: NetworkConfig[]) => {
this.useSession({ networks: networks }, true)
})
// Load existing session from localStorage

@@ -191,8 +194,3 @@ const session = this.loadSession()

}
window.localStorage.removeItem('@sequence.session')
this.session = undefined
this.networks = undefined
this.providers = {}
this.transport.cachedProvider?.clearCache()
this.transport.cachedProvider?.onUpdate(undefined)
this.clearSession()
}

@@ -409,40 +407,28 @@

private useSession = async (session: WalletSession, autoSave: boolean = true) => {
if (!session.accountAddress || session.accountAddress === '') {
throw new Error('session error, accountAddress is empty')
if (!this.session) this.session = {}
// setup account
if (session.accountAddress) {
this.useAccountAddress(session.accountAddress)
}
// set active session
this.session = session
// setup networks
if (session.networks) {
this.useNetworks(session.networks)
}
// setup provider cache
if (!session.providerCache) {
session.providerCache = {}
if (session.providerCache) {
this.transport.cachedProvider.setCache(session.providerCache)
}
this.transport.cachedProvider.setCache(session.providerCache)
this.transport.cachedProvider.onUpdate(() => {
this.session.providerCache = this.transport.cachedProvider.getCache()
this.saveSession(this.session)
})
// set networks from the session
if (session.networks) {
// reset session.networks in case it doesn't match defaultNetwork, assuming the dapp has changed it.
if (this.config.defaultNetworkId && !checkNetworkConfig(session.networks[0], this.config.defaultNetworkId)) {
console.warn('session.networks defaultNetworkId has changed, so clearing the network list')
session.networks = undefined
} else {
this.useNetworks(session.networks)
}
}
// save session
// persist
if (autoSave) {
this.saveSession(this.session)
}
}
// confirm the session address matches the one with the signer
const accountAddress = await this.getSigner().getAddress()
if (session.accountAddress.toLowerCase() !== accountAddress.toLowerCase()) {
throw new Error('wallet account address does not match the session')
}
private useAccountAddress(accountAddress: string) {
if (!this.session) this.session = {}
this.session.accountAddress = accountAddress.toLowerCase()
}

@@ -486,2 +472,10 @@

}
private clearSession(): void {
window.localStorage.removeItem('@sequence.session')
this.session = undefined
this.networks = undefined
this.providers = {}
this.transport.cachedProvider?.clearCache()
}
}

@@ -488,0 +482,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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc