diff --git a/packages/portis-web3/src/index.ts b/packages/portis-web3/src/index.ts index 1dc0557..172581f 100644 --- a/packages/portis-web3/src/index.ts +++ b/packages/portis-web3/src/index.ts @@ -1,5 +1,6 @@ import ProviderEngine from '@portis/web3-provider-engine'; import CacheSubprovider from '@portis/web3-provider-engine/subproviders/cache.js'; +import In3Subprovider from '@portis/web3-provider-engine/subproviders/in3.js'; import FixtureSubprovider from '@portis/web3-provider-engine/subproviders/fixture.js'; import FilterSubprovider from '@portis/web3-provider-engine/subproviders/filters.js'; import HookedWalletSubprovider from '@portis/web3-provider-engine/subproviders/hooked-wallet.js'; @@ -52,7 +53,7 @@ export default class Portis { this._valiadateParams(dappId, network, options); this.config = { dappId, - network: networkAdapter(network, options.gasRelay), + network: networkAdapter(network, options.gasRelay, options.useIn3), version: VERSION, scope: options.scope, registerPageByDefault: options.registerPageByDefault, @@ -61,10 +62,25 @@ export default class Portis { this.provider = this._initProvider(options); } - changeNetwork(network: string | INetwork, gasRelay?: boolean) { - const newNetwork = networkAdapter(network, gasRelay); + changeNetwork(network: string | INetwork, gasRelay?: boolean, useIn3?: boolean) { + const newNetwork = networkAdapter(network, gasRelay, useIn3); this.clearSubprovider(NonceSubprovider); this.clearSubprovider(CacheSubprovider); + + const subprovider = this.provider._providers.find(subprovider => subprovider instanceof In3Subprovider); + + //if a in3 provider was added then remove it. + if (subprovider) this.provider.removeProvider(subprovider); + + //if user plans to useIn3 with the change then add the in3 Provider. + if (useIn3) { + const in3Config = Object.assign({}, newNetwork); + delete in3Config.nodeUrl; + + //add the in3 subprovider to the engine + this.provider.addProvider(new In3Subprovider(in3Config)); + } + this.config.network = newNetwork; } @@ -348,7 +364,13 @@ export default class Portis { }), ); - if (!options.pocketDevId) { + if (options.useIn3) { + const in3Config = Object.assign({}, this.config.network); + delete in3Config.nodeUrl; + + //add the in3 subprovider to the engine + engine.addProvider(new In3Subprovider(in3Config)); + } else if (!options.pocketDevId && !options.useIn3) { engine.addProvider({ setEngine: _ => _, handleRequest: async (payload, next, end) => { diff --git a/packages/portis-web3/src/interfaces.ts b/packages/portis-web3/src/interfaces.ts index 46d9906..d8048d3 100644 --- a/packages/portis-web3/src/interfaces.ts +++ b/packages/portis-web3/src/interfaces.ts @@ -50,6 +50,7 @@ export interface IOptions { gasRelay?: boolean; registerPageByDefault?: boolean; pocketDevId?: string; + useIn3?: boolean; } export interface ITransactionRequest { diff --git a/packages/portis-web3/src/networks.ts b/packages/portis-web3/src/networks.ts index d00dfa1..df08308 100644 --- a/packages/portis-web3/src/networks.ts +++ b/packages/portis-web3/src/networks.ts @@ -1,6 +1,8 @@ import { INetwork } from './interfaces'; -export function networkAdapter(network: string | INetwork, gasRelay?: boolean) { +const IN3_SUPPORTED_CHAINS = ['mainnet', 'kovan', 'goerli']; + +export function networkAdapter(network: string | INetwork, gasRelay?: boolean, useIn3?: boolean) { const networkObj = typeof network === 'string' ? Object.assign({}, networks[network]) : network; if (typeof networkObj !== 'object') { @@ -9,6 +11,20 @@ export function networkAdapter(network: string | INetwork, gasRelay?: boolean) { ); } + if (useIn3) { + if (!(networkObj.chainId && networkObj['requestCount'] && networkObj['minDeposit'])) { + throw new Error( + '[Portis-In3] a in3 config with chainId, requestCount and minDeposit is required. Some or all are missing', + ); + } else if (!IN3_SUPPORTED_CHAINS.includes(networkObj.chainId)) { + throw new Error( + '[Portis-In3] Only mainnet, kovan and goerli are supported with In3. Specify chainId as ["kovan", "mainnet", "goerli"]', + ); + } else { + networkObj.nodeUrl = networks[networkObj.chainId].nodeUrl; + } + } + if (!networkObj.nodeUrl) { throw new Error( "[Portis] 'nodeUrl' is required. Read more about it here: https://docs.portis.io/#/configuration?id=network", diff --git a/packages/portis-web3/webpack.config.babel.js b/packages/portis-web3/webpack.config.babel.js index f4eb3e7..2f98302 100644 --- a/packages/portis-web3/webpack.config.babel.js +++ b/packages/portis-web3/webpack.config.babel.js @@ -1,4 +1,5 @@ const path = require('path'); +const webpack = require('webpack'); export default () => [ { @@ -15,13 +16,14 @@ export default () => [ library: 'Portis', libraryExport: 'default', }, + plugins: [new webpack.SourceMapDevToolPlugin({})], module: { rules: [ { test: /\.(js)$/, include: [ path.resolve(__dirname, 'src'), - path.resolve(__dirname, 'node_modules/@portis/web3-provider-engine') + path.resolve(__dirname, 'node_modules/@portis/web3-provider-engine'), ], use: 'babel-loader', },