bitski-provider
Advanced tools
Comparing version 3.3.2 to 3.4.0
# bitski-provider | ||
## 3.4.0 | ||
### Minor Changes | ||
- [#440](https://github.com/BitskiCo/bitski-js/pull/440) [`7a078e3980d44d0dc0949becaaefd1754827e1e9`](https://github.com/BitskiCo/bitski-js/commit/7a078e3980d44d0dc0949becaaefd1754827e1e9) Thanks [@chronicIntrovert](https://github.com/chronicIntrovert)! - Allow blockchain accounts to handle eth_accounts filtering of account type instead if only one account; otherwise, only prioritize vault accounts for users with more than one account. | ||
## 3.3.2 | ||
@@ -4,0 +10,0 @@ |
@@ -80,3 +80,3 @@ import SafeEventEmitter from '@metamask/safe-event-emitter'; | ||
} | ||
this.config = Object.assign(Object.assign({}, config), { fetch: (_b = config.fetch) !== null && _b !== void 0 ? _b : fetch, additionalHeaders: Object.assign({ 'X-API-KEY': appId, 'X-CLIENT-ID': appId, 'X-CLIENT-VERSION': "bitski-provider-v3.3.2" }, ((_c = config.additionalHeaders) !== null && _c !== void 0 ? _c : {})), apiBaseUrl, 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 }); | ||
this.config = Object.assign(Object.assign({}, config), { fetch: (_b = config.fetch) !== null && _b !== void 0 ? _b : fetch, additionalHeaders: Object.assign({ 'X-API-KEY': appId, 'X-CLIENT-ID': appId, 'X-CLIENT-VERSION': "bitski-provider-v3.4.0" }, ((_c = config.additionalHeaders) !== null && _c !== void 0 ? _c : {})), apiBaseUrl, 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 }); | ||
this.store = new BitskiProviderStateStore(this.config.store); | ||
@@ -83,0 +83,0 @@ // Setup the engine |
@@ -12,3 +12,3 @@ import { EthMethod } from 'eth-provider-types'; | ||
EthMethod.eth_signTypedData, | ||
EthMethod.eth_signTypedData_v3, | ||
EthMethod.eth_signTypedData_v3, // For metamask compatibility | ||
EthMethod.eth_signTypedData_v4, | ||
@@ -29,3 +29,3 @@ // Kept for legacy compat | ||
'ETIMEDOUT', | ||
'ENOTFOUND', | ||
'ENOTFOUND', // DNS error | ||
// ignore server sent html error pages | ||
@@ -40,4 +40,4 @@ // or truncated json responses | ||
export const UNAUTHORIZED_ERRORS = [ | ||
'Missing auth', | ||
'Invalid client id', | ||
'Missing auth', // No token sent | ||
'Invalid client id', // Wrong client id, or invalid access token | ||
'Not Authorized', | ||
@@ -44,0 +44,0 @@ ]; |
@@ -11,3 +11,2 @@ import { EthMethod } from 'eth-provider-types'; | ||
const fetchAccounts = async (config) => { | ||
var _a; | ||
const headers = Object.assign({}, config.additionalHeaders); | ||
@@ -21,7 +20,15 @@ if (config.getAccessToken) { | ||
})); | ||
const mainAccount = (_a = accounts.find((a) => a.kind === 'contract-wallet')) !== null && _a !== void 0 ? _a : accounts.find((a) => a.kind === 'bitski'); | ||
if (!mainAccount) { | ||
if (!accounts) { | ||
throw ethErrors.rpc.internal('Could not find blockchain accounts'); | ||
} | ||
return [mainAccount.address]; | ||
const moreThanOneAccount = accounts.length > 1; | ||
const mainAccount = accounts.find((a) => a.kind === 'bitski'); | ||
if (moreThanOneAccount && !mainAccount) { | ||
throw ethErrors.rpc.internal('Could not find blockchain accounts'); | ||
} | ||
if (moreThanOneAccount && mainAccount) { | ||
return [mainAccount.address]; | ||
} | ||
const accountAddresses = accounts.map((a) => a.address); | ||
return accountAddresses; | ||
}; | ||
@@ -28,0 +35,0 @@ export const createEthAccountsMiddleware = () => { |
@@ -13,3 +13,3 @@ // Default implementation of generic store interface. | ||
name: string, | ||
symbol: string, | ||
symbol: string, // 2-6 characters long | ||
decimals: number, | ||
@@ -16,0 +16,0 @@ })), |
@@ -12,3 +12,3 @@ import { ethErrors, EthereumRpcError, EthereumProviderError } from 'eth-rpc-errors'; | ||
'EHOSTUNREACH', | ||
'Timeout out while waiting for response', | ||
'Timeout out while waiting for response', // Actix timeout | ||
// ignore errors where http req failed to establish | ||
@@ -15,0 +15,0 @@ 'Failed to fetch', |
@@ -98,16 +98,16 @@ import { EthMethod } from 'eth-provider-types'; | ||
case EthMethod.eth_sendTransaction: | ||
return "ETH_SEND_TRANSACTION" /* SendTransaction */; | ||
return "ETH_SEND_TRANSACTION" /* TransactionKind.SendTransaction */; | ||
case EthMethod.eth_signTransaction: | ||
return "ETH_SIGN_TRANSACTION" /* SignTransaction */; | ||
return "ETH_SIGN_TRANSACTION" /* TransactionKind.SignTransaction */; | ||
case EthMethod.eth_sign: | ||
case 'personal_sign': | ||
return "ETH_SIGN" /* Sign */; | ||
return "ETH_SIGN" /* TransactionKind.Sign */; | ||
case EthMethod.eth_signTypedData: | ||
return "ETH_SIGN_TYPED_DATA" /* SignTypedData */; | ||
return "ETH_SIGN_TYPED_DATA" /* TransactionKind.SignTypedData */; | ||
case EthMethod.eth_signTypedData_v1: | ||
return "ETH_SIGN_TYPED_DATA_V1" /* SignTypedDataV1 */; | ||
return "ETH_SIGN_TYPED_DATA_V1" /* TransactionKind.SignTypedDataV1 */; | ||
case EthMethod.eth_signTypedData_v3: | ||
return "ETH_SIGN_TYPED_DATA_V3" /* SignTypedDataV3 */; | ||
return "ETH_SIGN_TYPED_DATA_V3" /* TransactionKind.SignTypedDataV3 */; | ||
case EthMethod.eth_signTypedData_v4: | ||
return "ETH_SIGN_TYPED_DATA_V4" /* SignTypedDataV4 */; | ||
return "ETH_SIGN_TYPED_DATA_V4" /* TransactionKind.SignTypedDataV4 */; | ||
default: | ||
@@ -114,0 +114,0 @@ throw ethErrors.rpc.internal('Unsupported method'); |
@@ -25,3 +25,2 @@ // jshint esversion: 9 | ||
}, | ||
name: 'bitski-provider', | ||
displayName: 'Bitski Provider', | ||
@@ -28,0 +27,0 @@ testEnvironment: 'jsdom', |
@@ -86,3 +86,3 @@ "use strict"; | ||
} | ||
this.config = Object.assign(Object.assign({}, config), { fetch: (_b = config.fetch) !== null && _b !== void 0 ? _b : fetch, additionalHeaders: Object.assign({ 'X-API-KEY': appId, 'X-CLIENT-ID': appId, 'X-CLIENT-VERSION': "bitski-provider-v3.3.2" }, ((_c = config.additionalHeaders) !== null && _c !== void 0 ? _c : {})), apiBaseUrl, 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 }); | ||
this.config = Object.assign(Object.assign({}, config), { fetch: (_b = config.fetch) !== null && _b !== void 0 ? _b : fetch, additionalHeaders: Object.assign({ 'X-API-KEY': appId, 'X-CLIENT-ID': appId, 'X-CLIENT-VERSION': "bitski-provider-v3.4.0" }, ((_c = config.additionalHeaders) !== null && _c !== void 0 ? _c : {})), apiBaseUrl, 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 }); | ||
this.store = new store_1.BitskiProviderStateStore(this.config.store); | ||
@@ -89,0 +89,0 @@ // Setup the engine |
@@ -15,3 +15,3 @@ "use strict"; | ||
eth_provider_types_1.EthMethod.eth_signTypedData, | ||
eth_provider_types_1.EthMethod.eth_signTypedData_v3, | ||
eth_provider_types_1.EthMethod.eth_signTypedData_v3, // For metamask compatibility | ||
eth_provider_types_1.EthMethod.eth_signTypedData_v4, | ||
@@ -32,3 +32,3 @@ // Kept for legacy compat | ||
'ETIMEDOUT', | ||
'ENOTFOUND', | ||
'ENOTFOUND', // DNS error | ||
// ignore server sent html error pages | ||
@@ -43,4 +43,4 @@ // or truncated json responses | ||
exports.UNAUTHORIZED_ERRORS = [ | ||
'Missing auth', | ||
'Invalid client id', | ||
'Missing auth', // No token sent | ||
'Invalid client id', // Wrong client id, or invalid access token | ||
'Not Authorized', | ||
@@ -47,0 +47,0 @@ ]; |
@@ -14,3 +14,2 @@ "use strict"; | ||
const fetchAccounts = async (config) => { | ||
var _a; | ||
const headers = Object.assign({}, config.additionalHeaders); | ||
@@ -24,7 +23,15 @@ if (config.getAccessToken) { | ||
})); | ||
const mainAccount = (_a = accounts.find((a) => a.kind === 'contract-wallet')) !== null && _a !== void 0 ? _a : accounts.find((a) => a.kind === 'bitski'); | ||
if (!mainAccount) { | ||
if (!accounts) { | ||
throw eth_rpc_errors_1.ethErrors.rpc.internal('Could not find blockchain accounts'); | ||
} | ||
return [mainAccount.address]; | ||
const moreThanOneAccount = accounts.length > 1; | ||
const mainAccount = accounts.find((a) => a.kind === 'bitski'); | ||
if (moreThanOneAccount && !mainAccount) { | ||
throw eth_rpc_errors_1.ethErrors.rpc.internal('Could not find blockchain accounts'); | ||
} | ||
if (moreThanOneAccount && mainAccount) { | ||
return [mainAccount.address]; | ||
} | ||
const accountAddresses = accounts.map((a) => a.address); | ||
return accountAddresses; | ||
}; | ||
@@ -31,0 +38,0 @@ const createEthAccountsMiddleware = () => { |
import { EthMethod, EthMethodResults } from 'eth-provider-types'; | ||
import { JsonRpcMiddleware } from 'json-rpc-engine'; | ||
export declare type Fixtures = Partial<{ | ||
export type Fixtures = Partial<{ | ||
[key in EthMethod]: EthMethodResults[key]; | ||
@@ -5,0 +5,0 @@ }>; |
import { RequestContext, SignFn } from '../types'; | ||
import { Transaction } from '../utils/transaction'; | ||
export declare type ShowSignerPopupFn = (transaction: Transaction, context: RequestContext<unknown>, submitTransaction: () => Promise<Transaction>) => Promise<string>; | ||
export type ShowSignerPopupFn = (transaction: Transaction, context: RequestContext<unknown>, submitTransaction: () => Promise<Transaction>) => Promise<string>; | ||
export interface BrowserSignerConfig { | ||
@@ -5,0 +5,0 @@ showPopup?: ShowSignerPopupFn; |
@@ -16,3 +16,3 @@ "use strict"; | ||
name: decoders_1.string, | ||
symbol: decoders_1.string, | ||
symbol: decoders_1.string, // 2-6 characters long | ||
decimals: decoders_1.number, | ||
@@ -19,0 +19,0 @@ })), |
@@ -44,4 +44,4 @@ import { EthChainDefinition, EthEvent, EthEventParams, EthMethod, EthMethodParams, EthRequest, EthResult } from 'eth-provider-types'; | ||
} | ||
declare type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>; | ||
export declare type BitskiProviderConfig<Extra = unknown> = Optional<InternalBitskiProviderConfig<Extra>, 'apiBaseUrl' | 'signerBaseUrl' | 'fetch' | 'additionalHeaders' | 'sign' | 'getUser' | 'store'> & { | ||
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>; | ||
export type BitskiProviderConfig<Extra = unknown> = Optional<InternalBitskiProviderConfig<Extra>, 'apiBaseUrl' | 'signerBaseUrl' | 'fetch' | 'additionalHeaders' | 'sign' | 'getUser' | 'store'> & { | ||
signerMethod?: 'popup' | 'iframe' | 'redirect'; | ||
@@ -70,7 +70,7 @@ }; | ||
} | ||
export declare type ProviderMiddleware<T = unknown[], U = unknown, V = unknown> = (req: JsonRpcRequest<T> & { | ||
export type ProviderMiddleware<T = unknown[], U = unknown, V = unknown> = (req: JsonRpcRequest<T> & { | ||
context: RequestContext<V>; | ||
}, res: PendingJsonRpcResponse<U>, next: () => Promise<void>) => Promise<void>; | ||
export declare type EthSignMethod = EthMethod.eth_sendTransaction | EthMethod.eth_signTransaction | EthMethod.eth_sign | EthMethod.eth_signTypedData | EthMethod.eth_signTypedData_v1 | EthMethod.eth_signTypedData_v3 | EthMethod.eth_signTypedData_v4 | 'personal_sign'; | ||
export declare type SignFn = <T extends EthSignMethod>(method: T, params: EthSignMethodParams[T], context: RequestContext) => Promise<string>; | ||
export type EthSignMethod = EthMethod.eth_sendTransaction | EthMethod.eth_signTransaction | EthMethod.eth_sign | EthMethod.eth_signTypedData | EthMethod.eth_signTypedData_v1 | EthMethod.eth_signTypedData_v3 | EthMethod.eth_signTypedData_v4 | 'personal_sign'; | ||
export type SignFn = <T extends EthSignMethod>(method: T, params: EthSignMethodParams[T], context: RequestContext) => Promise<string>; | ||
export {}; |
@@ -15,3 +15,3 @@ "use strict"; | ||
'EHOSTUNREACH', | ||
'Timeout out while waiting for response', | ||
'Timeout out while waiting for response', // Actix timeout | ||
// ignore errors where http req failed to establish | ||
@@ -18,0 +18,0 @@ 'Failed to fetch', |
@@ -43,2 +43,2 @@ import { EthTransactionSend, TypedData } from 'eth-provider-types'; | ||
*/ | ||
export declare const createBitskiTransaction: <T extends EthSignMethod>(method: T, params: EthSignMethodParams[T], chain: EthChainDefinitionWithRpcUrl, paymaster?: PaymasterDefinition | PaymasterDefinition[] | undefined, additionalContext?: Record<string, string> | undefined) => Transaction; | ||
export declare const createBitskiTransaction: <T extends EthSignMethod>(method: T, params: EthSignMethodParams[T], chain: EthChainDefinitionWithRpcUrl, paymaster?: PaymasterDefinition | PaymasterDefinition[], additionalContext?: Record<string, string>) => Transaction; |
@@ -102,16 +102,16 @@ "use strict"; | ||
case eth_provider_types_1.EthMethod.eth_sendTransaction: | ||
return "ETH_SEND_TRANSACTION" /* SendTransaction */; | ||
return "ETH_SEND_TRANSACTION" /* TransactionKind.SendTransaction */; | ||
case eth_provider_types_1.EthMethod.eth_signTransaction: | ||
return "ETH_SIGN_TRANSACTION" /* SignTransaction */; | ||
return "ETH_SIGN_TRANSACTION" /* TransactionKind.SignTransaction */; | ||
case eth_provider_types_1.EthMethod.eth_sign: | ||
case 'personal_sign': | ||
return "ETH_SIGN" /* Sign */; | ||
return "ETH_SIGN" /* TransactionKind.Sign */; | ||
case eth_provider_types_1.EthMethod.eth_signTypedData: | ||
return "ETH_SIGN_TYPED_DATA" /* SignTypedData */; | ||
return "ETH_SIGN_TYPED_DATA" /* TransactionKind.SignTypedData */; | ||
case eth_provider_types_1.EthMethod.eth_signTypedData_v1: | ||
return "ETH_SIGN_TYPED_DATA_V1" /* SignTypedDataV1 */; | ||
return "ETH_SIGN_TYPED_DATA_V1" /* TransactionKind.SignTypedDataV1 */; | ||
case eth_provider_types_1.EthMethod.eth_signTypedData_v3: | ||
return "ETH_SIGN_TYPED_DATA_V3" /* SignTypedDataV3 */; | ||
return "ETH_SIGN_TYPED_DATA_V3" /* TransactionKind.SignTypedDataV3 */; | ||
case eth_provider_types_1.EthMethod.eth_signTypedData_v4: | ||
return "ETH_SIGN_TYPED_DATA_V4" /* SignTypedDataV4 */; | ||
return "ETH_SIGN_TYPED_DATA_V4" /* TransactionKind.SignTypedDataV4 */; | ||
default: | ||
@@ -118,0 +118,0 @@ throw eth_rpc_errors_1.ethErrors.rpc.internal('Unsupported method'); |
@@ -12,3 +12,3 @@ { | ||
}, | ||
"version": "3.3.2", | ||
"version": "3.4.0", | ||
"scripts": { | ||
@@ -38,7 +38,9 @@ "test": "jest", | ||
"@testing-library/dom": "^8.19.0", | ||
"@types/jest": "^27.5.2", | ||
"@types/jest": "^29.5.11", | ||
"@types/node": "^20.11.5", | ||
"babelify": "^10.0.0", | ||
"jest": "27.2.0", | ||
"jest": "29.7.0", | ||
"jest-fetch-mock": "^3.0.3", | ||
"ts-jest": "^27.0.5" | ||
"jest-environment-jsdom": "^29.0.0", | ||
"ts-jest": "^29.1.1" | ||
}, | ||
@@ -45,0 +47,0 @@ "browserify": { |
import { EthMethod } from 'eth-provider-types'; | ||
import { createAsyncMiddleware, JsonRpcMiddleware, JsonRpcRequest } from 'json-rpc-engine'; | ||
import { createAsyncMiddleware, JsonRpcMiddleware } from 'json-rpc-engine'; | ||
import { getRequestContext } from '../utils/request-context'; | ||
@@ -41,10 +41,20 @@ import { InternalBitskiProviderConfig } from '../types'; | ||
const mainAccount = | ||
accounts.find((a) => a.kind === 'contract-wallet') ?? accounts.find((a) => a.kind === 'bitski'); | ||
if (!accounts) { | ||
throw ethErrors.rpc.internal('Could not find blockchain accounts'); | ||
} | ||
if (!mainAccount) { | ||
const moreThanOneAccount = accounts.length > 1; | ||
const mainAccount = accounts.find((a) => a.kind === 'bitski'); | ||
if (moreThanOneAccount && !mainAccount) { | ||
throw ethErrors.rpc.internal('Could not find blockchain accounts'); | ||
} | ||
return [mainAccount.address]; | ||
if (moreThanOneAccount && mainAccount) { | ||
return [mainAccount.address]; | ||
} | ||
const accountAddresses = accounts.map((a) => a.address); | ||
return accountAddresses; | ||
}; | ||
@@ -51,0 +61,0 @@ |
import { InternalBitskiProviderConfig, RequestContext, SignFn } from '../types'; | ||
import { fetchJsonWithRetry } from '../utils/fetch'; | ||
import { createBitskiTransaction, Transaction } from '../utils/transaction'; | ||
import { showIframe } from './iframe'; | ||
import { getSignerUrl } from './shared'; | ||
@@ -6,0 +5,0 @@ |
@@ -27,3 +27,3 @@ import { EthMethod } from 'eth-provider-types'; | ||
test('prioritizes contract wallets over vault wallets', async () => { | ||
test('returns vault account if multiple accounts', async () => { | ||
expect.assertions(3); | ||
@@ -51,5 +51,27 @@ const provider = createTestProvider(); | ||
const result = await provider.request({ method: EthMethod.eth_accounts }); | ||
expect(result).toEqual(['0x456']); | ||
expect(result).toEqual(['0x123']); | ||
}); | ||
test('returns accounts if only one account', async () => { | ||
expect.assertions(3); | ||
const provider = createTestProvider(); | ||
fetchMock.mockResponse(async (req) => { | ||
expect(req.url).toBe('https://api.bitski.com/v2/blockchain/accounts'); | ||
expect(req.method).toBe('GET'); | ||
return JSON.stringify({ | ||
accounts: [ | ||
{ | ||
kind: 'bitski', | ||
address: '0x123', | ||
}, | ||
], | ||
}); | ||
}); | ||
const result = await provider.request({ method: EthMethod.eth_accounts }); | ||
expect(result).toEqual(['0x123']); | ||
}); | ||
test('uses access token if available', async () => { | ||
@@ -56,0 +78,0 @@ expect.assertions(4); |
@@ -9,7 +9,5 @@ { | ||
"target": "ES2017", | ||
"esModuleInterop": true, | ||
"typeRoots": ["./node_modules/@types", "./types"], | ||
"types": ["node", "jest"] | ||
"esModuleInterop": true | ||
}, | ||
"include": ["src", "types", "**/*.test.ts"] | ||
"include": ["src", "**/*.test.ts"] | ||
} |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"include": ["src", "types"] | ||
"include": ["src"] | ||
} |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"include": ["src", "types"] | ||
"include": ["src"] | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
403330
8317
11