bitski-provider
Advanced tools
Comparing version 2.0.0-beta.5 to 2.0.0-beta.6
# bitski-provider | ||
## 2.0.0-beta.6 | ||
### Patch Changes | ||
- [#315](https://github.com/BitskiCo/bitski-js/pull/315) [`7c938da`](https://github.com/BitskiCo/bitski-js/commit/7c938dab487b90e36d68fb6ccedb8ae990a14e3d) Thanks [@pzuraq](https://github.com/pzuraq)! - Move chain-management logic to middleware | ||
## 2.0.0-beta.5 | ||
@@ -4,0 +10,0 @@ |
@@ -16,6 +16,6 @@ import { createAsyncMiddleware, JsonRpcEngine, } from 'json-rpc-engine'; | ||
import { BITSKI_API_BASE_URL, BITSKI_SIGNER_BASE_URL, UNAUTHORIZED_ERRORS } from './constants'; | ||
import { ethErrors } from 'eth-rpc-errors'; | ||
import createBrowserSigner from './signers/browser'; | ||
import { BitskiProviderStateStore, LocalStorageStore } from './store'; | ||
import { assert, expect } from './utils/type-utils'; | ||
import { createEthChainMiddleware } from './middleware/chain-management'; | ||
// Some eth methods result in a subscription being created, and return the id of that subscription. | ||
@@ -44,3 +44,3 @@ // We need to keep track of the subscription id and the chain id it was created on, so we can | ||
this.activeSubs = new Set(); | ||
this.config = Object.assign(Object.assign({}, config), { fetch: (_a = config.fetch) !== null && _a !== void 0 ? _a : fetch, additionalHeaders: Object.assign({ 'X-API-KEY': config.clientId, 'X-CLIENT-ID': config.clientId, 'X-CLIENT-VERSION': "bitski-provider-v2.0.0-beta.5" }, ((_b = config.additionalHeaders) !== null && _b !== void 0 ? _b : {})), apiBaseUrl: (_c = config.apiBaseUrl) !== null && _c !== void 0 ? _c : BITSKI_API_BASE_URL, signerBaseUrl: (_d = config.signerBaseUrl) !== null && _d !== void 0 ? _d : BITSKI_SIGNER_BASE_URL, store: (_e = config.store) !== null && _e !== void 0 ? _e : new LocalStorageStore(), sign: (_f = config.sign) !== null && _f !== void 0 ? _f : createBrowserSigner() }); | ||
this.config = Object.assign(Object.assign({}, config), { fetch: (_a = config.fetch) !== null && _a !== void 0 ? _a : fetch, additionalHeaders: Object.assign({ 'X-API-KEY': config.clientId, 'X-CLIENT-ID': config.clientId, 'X-CLIENT-VERSION': "bitski-provider-v2.0.0-beta.6" }, ((_b = config.additionalHeaders) !== null && _b !== void 0 ? _b : {})), apiBaseUrl: (_c = config.apiBaseUrl) !== null && _c !== void 0 ? _c : BITSKI_API_BASE_URL, signerBaseUrl: (_d = config.signerBaseUrl) !== null && _d !== void 0 ? _d : BITSKI_SIGNER_BASE_URL, store: (_e = config.store) !== null && _e !== void 0 ? _e : new LocalStorageStore(), sign: (_f = config.sign) !== null && _f !== void 0 ? _f : createBrowserSigner() }); | ||
this.store = new BitskiProviderStateStore(this.config.store); | ||
@@ -55,2 +55,3 @@ // Setup the engine | ||
engine.push(createFixtureMiddleware()); | ||
engine.push(createEthChainMiddleware()); | ||
if (!config.disableValidation) { | ||
@@ -92,27 +93,18 @@ // Ensures that transactions are well formed (nonce, gas, gasPrice, from) before they are sent to Bitski | ||
} | ||
switch (method) { | ||
case EthMethod.wallet_addEthereumChain: | ||
return this.addChain(params[0]); | ||
case EthMethod.wallet_switchEthereumChain: | ||
return this.switchChain(params[0]); | ||
case EthMethod.eth_chainId: | ||
return chainId; | ||
default: | ||
try { | ||
let result = await this.requestWithChain(chainId, request, { extra }); | ||
if (SUB_METHODS.has(method)) { | ||
// Ensure the subscription id is unique across chains | ||
// by creating unique compound id | ||
result = `${chainId}:${result}`; | ||
this.activeSubs.add(result); | ||
} | ||
return result; | ||
} | ||
catch (err) { | ||
if (UNAUTHORIZED_ERRORS.some((phrase) => err.message.includes(phrase))) { | ||
await ((_b = (_a = this.config).clearAccessToken) === null || _b === void 0 ? void 0 : _b.call(_a)); | ||
} | ||
throw err; | ||
} | ||
try { | ||
let result = await this.requestWithChain(chainId, request, { extra }); | ||
if (SUB_METHODS.has(method)) { | ||
// Ensure the subscription id is unique across chains | ||
// by creating unique compound id | ||
result = `${chainId}:${result}`; | ||
this.activeSubs.add(result); | ||
} | ||
return result; | ||
} | ||
catch (err) { | ||
if (UNAUTHORIZED_ERRORS.some((phrase) => err.message.includes(phrase))) { | ||
await ((_b = (_a = this.config).clearAccessToken) === null || _b === void 0 ? void 0 : _b.call(_a)); | ||
} | ||
throw err; | ||
} | ||
} | ||
@@ -155,2 +147,3 @@ async request(request) { | ||
config: this.config, | ||
store: this.store, | ||
emit: this.events.emit.bind(this.events), | ||
@@ -178,15 +171,2 @@ request: (req, opts) => this.requestWithChain(chainId, req, opts), | ||
} | ||
async addChain(definition) { | ||
this.store.addChain(definition); | ||
return null; | ||
} | ||
async switchChain(chainDetails) { | ||
const chain = this.store.findChain(chainDetails.chainId); | ||
if (!chain) { | ||
throw ethErrors.provider.userRejectedRequest({ message: 'Chain does not exist' }); | ||
} | ||
await this.store.setCurrentChainId(chainDetails.chainId); | ||
this.events.emit('chainChanged', chainDetails.chainId); | ||
return null; | ||
} | ||
} | ||
@@ -193,0 +173,0 @@ export const createBitskiProvider = (config) => { |
@@ -21,5 +21,3 @@ import { EthProvider, EthMethod, EthRequest, EthResult, EthEvent, EthEventListener } from 'eth-provider-types'; | ||
private requestWithChain; | ||
private addChain; | ||
private switchChain; | ||
} | ||
export declare const createBitskiProvider: <Extra = unknown>(config: BitskiProviderConfig<Extra>) => BitskiProvider<Extra>; |
@@ -22,6 +22,6 @@ "use strict"; | ||
const constants_1 = require("./constants"); | ||
const eth_rpc_errors_1 = require("eth-rpc-errors"); | ||
const browser_1 = __importDefault(require("./signers/browser")); | ||
const store_1 = require("./store"); | ||
const type_utils_1 = require("./utils/type-utils"); | ||
const chain_management_1 = require("./middleware/chain-management"); | ||
// Some eth methods result in a subscription being created, and return the id of that subscription. | ||
@@ -50,3 +50,3 @@ // We need to keep track of the subscription id and the chain id it was created on, so we can | ||
this.activeSubs = new Set(); | ||
this.config = Object.assign(Object.assign({}, config), { fetch: (_a = config.fetch) !== null && _a !== void 0 ? _a : fetch, additionalHeaders: Object.assign({ 'X-API-KEY': config.clientId, 'X-CLIENT-ID': config.clientId, 'X-CLIENT-VERSION': "bitski-provider-v2.0.0-beta.5" }, ((_b = config.additionalHeaders) !== null && _b !== void 0 ? _b : {})), apiBaseUrl: (_c = config.apiBaseUrl) !== null && _c !== void 0 ? _c : constants_1.BITSKI_API_BASE_URL, signerBaseUrl: (_d = config.signerBaseUrl) !== null && _d !== void 0 ? _d : constants_1.BITSKI_SIGNER_BASE_URL, store: (_e = config.store) !== null && _e !== void 0 ? _e : new store_1.LocalStorageStore(), sign: (_f = config.sign) !== null && _f !== void 0 ? _f : (0, browser_1.default)() }); | ||
this.config = Object.assign(Object.assign({}, config), { fetch: (_a = config.fetch) !== null && _a !== void 0 ? _a : fetch, additionalHeaders: Object.assign({ 'X-API-KEY': config.clientId, 'X-CLIENT-ID': config.clientId, 'X-CLIENT-VERSION': "bitski-provider-v2.0.0-beta.6" }, ((_b = config.additionalHeaders) !== null && _b !== void 0 ? _b : {})), apiBaseUrl: (_c = config.apiBaseUrl) !== null && _c !== void 0 ? _c : constants_1.BITSKI_API_BASE_URL, signerBaseUrl: (_d = config.signerBaseUrl) !== null && _d !== void 0 ? _d : constants_1.BITSKI_SIGNER_BASE_URL, store: (_e = config.store) !== null && _e !== void 0 ? _e : new store_1.LocalStorageStore(), sign: (_f = config.sign) !== null && _f !== void 0 ? _f : (0, browser_1.default)() }); | ||
this.store = new store_1.BitskiProviderStateStore(this.config.store); | ||
@@ -61,2 +61,3 @@ // Setup the engine | ||
engine.push((0, fixture_1.createFixtureMiddleware)()); | ||
engine.push((0, chain_management_1.createEthChainMiddleware)()); | ||
if (!config.disableValidation) { | ||
@@ -98,27 +99,18 @@ // Ensures that transactions are well formed (nonce, gas, gasPrice, from) before they are sent to Bitski | ||
} | ||
switch (method) { | ||
case eth_provider_types_1.EthMethod.wallet_addEthereumChain: | ||
return this.addChain(params[0]); | ||
case eth_provider_types_1.EthMethod.wallet_switchEthereumChain: | ||
return this.switchChain(params[0]); | ||
case eth_provider_types_1.EthMethod.eth_chainId: | ||
return chainId; | ||
default: | ||
try { | ||
let result = await this.requestWithChain(chainId, request, { extra }); | ||
if (SUB_METHODS.has(method)) { | ||
// Ensure the subscription id is unique across chains | ||
// by creating unique compound id | ||
result = `${chainId}:${result}`; | ||
this.activeSubs.add(result); | ||
} | ||
return result; | ||
} | ||
catch (err) { | ||
if (constants_1.UNAUTHORIZED_ERRORS.some((phrase) => err.message.includes(phrase))) { | ||
await ((_b = (_a = this.config).clearAccessToken) === null || _b === void 0 ? void 0 : _b.call(_a)); | ||
} | ||
throw err; | ||
} | ||
try { | ||
let result = await this.requestWithChain(chainId, request, { extra }); | ||
if (SUB_METHODS.has(method)) { | ||
// Ensure the subscription id is unique across chains | ||
// by creating unique compound id | ||
result = `${chainId}:${result}`; | ||
this.activeSubs.add(result); | ||
} | ||
return result; | ||
} | ||
catch (err) { | ||
if (constants_1.UNAUTHORIZED_ERRORS.some((phrase) => err.message.includes(phrase))) { | ||
await ((_b = (_a = this.config).clearAccessToken) === null || _b === void 0 ? void 0 : _b.call(_a)); | ||
} | ||
throw err; | ||
} | ||
} | ||
@@ -161,2 +153,3 @@ async request(request) { | ||
config: this.config, | ||
store: this.store, | ||
emit: this.events.emit.bind(this.events), | ||
@@ -184,15 +177,2 @@ request: (req, opts) => this.requestWithChain(chainId, req, opts), | ||
} | ||
async addChain(definition) { | ||
this.store.addChain(definition); | ||
return null; | ||
} | ||
async switchChain(chainDetails) { | ||
const chain = this.store.findChain(chainDetails.chainId); | ||
if (!chain) { | ||
throw eth_rpc_errors_1.ethErrors.provider.userRejectedRequest({ message: 'Chain does not exist' }); | ||
} | ||
await this.store.setCurrentChainId(chainDetails.chainId); | ||
this.events.emit('chainChanged', chainDetails.chainId); | ||
return null; | ||
} | ||
} | ||
@@ -199,0 +179,0 @@ exports.BitskiProvider = BitskiProvider; |
import { EthChainDefinition, EthEvent, EthEventParams, EthMethod, EthMethodParams, EthRequest, EthResult } from 'eth-provider-types'; | ||
import { JsonRpcRequest, PendingJsonRpcResponse } from 'json-rpc-engine'; | ||
import type { BitskiProviderStateStore } from './store'; | ||
export interface User { | ||
@@ -48,2 +49,3 @@ id: string; | ||
config: InternalBitskiProviderConfig<Extra>; | ||
store: BitskiProviderStateStore; | ||
extra?: Extra; | ||
@@ -50,0 +52,0 @@ emit: <T extends EthEvent>(eventName: T, ...params: EthEventParams[T]) => void; |
@@ -12,3 +12,3 @@ { | ||
}, | ||
"version": "2.0.0-beta.5", | ||
"version": "2.0.0-beta.6", | ||
"scripts": { | ||
@@ -15,0 +15,0 @@ "test": "jest", |
@@ -15,4 +15,2 @@ import { | ||
EthEventListener, | ||
SwitchEthereumChainParameter, | ||
EthChainDefinition, | ||
} from 'eth-provider-types'; | ||
@@ -34,6 +32,6 @@ | ||
import { BITSKI_API_BASE_URL, BITSKI_SIGNER_BASE_URL, UNAUTHORIZED_ERRORS } from './constants'; | ||
import { ethErrors } from 'eth-rpc-errors'; | ||
import createBrowserSigner from './signers/browser'; | ||
import { BitskiProviderStateStore, LocalStorageStore } from './store'; | ||
import { assert, expect } from './utils/type-utils'; | ||
import { createEthChainMiddleware } from './middleware/chain-management'; | ||
@@ -103,2 +101,3 @@ // global value provided by scripts/insert-package-version.mjs | ||
engine.push(createFixtureMiddleware()); | ||
engine.push(createEthChainMiddleware()); | ||
@@ -151,31 +150,19 @@ if (!config.disableValidation) { | ||
switch (method) { | ||
case EthMethod.wallet_addEthereumChain: | ||
return this.addChain(params[0]); | ||
try { | ||
let result = await this.requestWithChain(chainId, request, { extra }); | ||
case EthMethod.wallet_switchEthereumChain: | ||
return this.switchChain(params[0]); | ||
if (SUB_METHODS.has(method)) { | ||
// Ensure the subscription id is unique across chains | ||
// by creating unique compound id | ||
result = `${chainId}:${result as string}`; | ||
this.activeSubs.add(result); | ||
} | ||
case EthMethod.eth_chainId: | ||
return chainId; | ||
return result; | ||
} catch (err) { | ||
if (UNAUTHORIZED_ERRORS.some((phrase) => (err as Error).message.includes(phrase))) { | ||
await this.config.clearAccessToken?.(); | ||
} | ||
default: | ||
try { | ||
let result = await this.requestWithChain(chainId, request, { extra }); | ||
if (SUB_METHODS.has(method)) { | ||
// Ensure the subscription id is unique across chains | ||
// by creating unique compound id | ||
result = `${chainId}:${result as string}`; | ||
this.activeSubs.add(result); | ||
} | ||
return result; | ||
} catch (err) { | ||
if (UNAUTHORIZED_ERRORS.some((phrase) => (err as Error).message.includes(phrase))) { | ||
await this.config.clearAccessToken?.(); | ||
} | ||
throw err; | ||
} | ||
throw err; | ||
} | ||
@@ -231,2 +218,3 @@ } | ||
config: this.config, | ||
store: this.store, | ||
emit: this.events.emit.bind(this.events), | ||
@@ -257,20 +245,2 @@ request: (req, opts) => this.requestWithChain(chainId, req, opts), | ||
} | ||
private async addChain(definition: EthChainDefinition): Promise<null> { | ||
this.store.addChain(definition); | ||
return null; | ||
} | ||
private async switchChain(chainDetails: SwitchEthereumChainParameter): Promise<null> { | ||
const chain = this.store.findChain(chainDetails.chainId); | ||
if (!chain) { | ||
throw ethErrors.provider.userRejectedRequest({ message: 'Chain does not exist' }); | ||
} | ||
await this.store.setCurrentChainId(chainDetails.chainId); | ||
this.events.emit('chainChanged', chainDetails.chainId); | ||
return null; | ||
} | ||
} | ||
@@ -277,0 +247,0 @@ |
@@ -11,2 +11,3 @@ import { | ||
import { JsonRpcRequest, PendingJsonRpcResponse } from 'json-rpc-engine'; | ||
import type { BitskiProviderStateStore } from './store'; | ||
@@ -86,2 +87,5 @@ export interface User { | ||
store: BitskiProviderStateStore; | ||
// Extra information that can be added by a user of the provider | ||
extra?: Extra; | ||
@@ -88,0 +92,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
352909
158
7610