1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

Use Infura v3 API (#9368)

* Use eth-json-rpc-infura@5.0.0
* Use Infura v3 API
* Add example .metamaskrc file
This commit is contained in:
Whymarrh Whitby 2020-09-10 13:46:00 -02:30 committed by GitHub
parent b80ab53396
commit e2dedaacdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 69 additions and 43 deletions

2
.gitattributes vendored
View File

@ -1,6 +1,8 @@
* text=auto * text=auto
CHANGELOG.md merge=union CHANGELOG.md merge=union
.metamaskrc.dist linguist-language=ini
# Reviewing the lockfile contents is an important step in verifying that # Reviewing the lockfile contents is an important step in verifying that
# we're using the dependencies we expect to be using # we're using the dependencies we expect to be using
package-lock.json linguist-generated=false package-lock.json linguist-generated=false

2
.metamaskrc.dist Normal file
View File

@ -0,0 +1,2 @@
; Extra environment variables
INFURA_PROJECT_ID=00000000000

View File

@ -16,6 +16,7 @@ To learn how to contribute to the MetaMask project itself, visit our [Internal D
- If you are using [nvm](https://github.com/creationix/nvm#installation) (recommended) running `nvm use` will automatically choose the right node version for you. - If you are using [nvm](https://github.com/creationix/nvm#installation) (recommended) running `nvm use` will automatically choose the right node version for you.
- Install [Yarn](https://yarnpkg.com/en/docs/install) - Install [Yarn](https://yarnpkg.com/en/docs/install)
- Install dependencies: `yarn` - Install dependencies: `yarn`
- Copy the `.metamaskrc.dist` file to `.metamaskrc`
- Build the project to the `./dist/` folder with `yarn dist`. - Build the project to the `./dist/` folder with `yarn dist`.
- Optionally, to start a development build (e.g. with logging and file watching) run `yarn start` instead. - Optionally, to start a development build (e.g. with logging and file watching) run `yarn start` instead.
- To start the [React DevTools](https://github.com/facebook/react-devtools) and [Redux DevTools Extension](http://extension.remotedev.io) - To start the [React DevTools](https://github.com/facebook/react-devtools) and [Redux DevTools Extension](http://extension.remotedev.io)

View File

@ -227,6 +227,7 @@ function setupController (initState, initLangCode) {
// //
const controller = new MetamaskController({ const controller = new MetamaskController({
infuraProjectId: process.env.INFURA_PROJECT_ID,
// User confirmation callbacks: // User confirmation callbacks:
showUnconfirmedMessage: triggerUi, showUnconfirmedMessage: triggerUi,
showUnapprovedTx: triggerUi, showUnapprovedTx: triggerUi,

View File

@ -10,8 +10,13 @@ import createInfuraMiddleware from 'eth-json-rpc-infura'
import BlockTracker from 'eth-block-tracker' import BlockTracker from 'eth-block-tracker'
import * as networkEnums from './enums' import * as networkEnums from './enums'
export default function createInfuraClient ({ network }) { export default function createInfuraClient ({ network, projectId }) {
const infuraMiddleware = createInfuraMiddleware({ network, maxAttempts: 5, source: 'metamask' }) const infuraMiddleware = createInfuraMiddleware({
network,
projectId,
maxAttempts: 5,
source: 'metamask',
})
const infuraProvider = providerFromMiddleware(infuraMiddleware) const infuraProvider = providerFromMiddleware(infuraMiddleware)
const blockTracker = new BlockTracker({ provider: infuraProvider }) const blockTracker = new BlockTracker({ provider: infuraProvider })

View File

@ -62,6 +62,21 @@ export default class NetworkController extends EventEmitter {
this._blockTrackerProxy = null this._blockTrackerProxy = null
} }
/**
* Sets the Infura project ID
*
* @param {string} projectId - The Infura project ID
* @throws {Error} if the project ID is not a valid string
* @return {void}
*/
setInfuraProjectId (projectId) {
if (!projectId || typeof projectId !== 'string') {
throw new Error('Invalid Infura project ID')
}
this._infuraProjectId = projectId
}
initializeProvider (providerParams) { initializeProvider (providerParams) {
this._baseProviderParams = providerParams this._baseProviderParams = providerParams
const { type, rpcTarget, chainId, ticker, nickname } = this.providerStore.getState() const { type, rpcTarget, chainId, ticker, nickname } = this.providerStore.getState()
@ -177,7 +192,7 @@ export default class NetworkController extends EventEmitter {
// infura type-based endpoints // infura type-based endpoints
const isInfura = INFURA_PROVIDER_TYPES.includes(type) const isInfura = INFURA_PROVIDER_TYPES.includes(type)
if (isInfura) { if (isInfura) {
this._configureInfuraProvider(opts) this._configureInfuraProvider(type, this._infuraProjectId)
// other type-based rpc endpoints // other type-based rpc endpoints
} else if (type === LOCALHOST) { } else if (type === LOCALHOST) {
this._configureLocalhostProvider() this._configureLocalhostProvider()
@ -189,10 +204,11 @@ export default class NetworkController extends EventEmitter {
} }
} }
_configureInfuraProvider ({ type }) { _configureInfuraProvider (type, projectId) {
log.info('NetworkController - configureInfuraProvider', type) log.info('NetworkController - configureInfuraProvider', type)
const networkClient = createInfuraClient({ const networkClient = createInfuraClient({
network: type, network: type,
projectId,
}) })
this._setNetworkClient(networkClient) this._setNetworkClient(networkClient)
// setup networkConfig // setup networkConfig

View File

@ -97,6 +97,7 @@ export default class MetamaskController extends EventEmitter {
// controller initialization order matters // controller initialization order matters
this.networkController = new NetworkController(initState.NetworkController) this.networkController = new NetworkController(initState.NetworkController)
this.networkController.setInfuraProjectId(opts.infuraProjectId)
this.preferencesController = new PreferencesController({ this.preferencesController = new PreferencesController({
initState: initState.PreferencesController, initState: initState.PreferencesController,

View File

@ -15,7 +15,9 @@ const pify = require('pify')
const endOfStream = pify(require('end-of-stream')) const endOfStream = pify(require('end-of-stream'))
const { makeStringTransform } = require('browserify-transform-tools') const { makeStringTransform } = require('browserify-transform-tools')
const conf = require('rc')('metamask', {}) const conf = require('rc')('metamask', {
INFURA_PROJECT_ID: process.env.INFURA_PROJECT_ID,
})
const packageJSON = require('../../package.json') const packageJSON = require('../../package.json')
const { createTask, composeParallel, composeSeries, runInChildProcess } = require('./task') const { createTask, composeParallel, composeSeries, runInChildProcess } = require('./task')
@ -326,6 +328,11 @@ function createScriptTasks ({ browserPlatforms, livereload }) {
ETH_GAS_STATION_API_KEY: process.env.ETH_GAS_STATION_API_KEY || '', ETH_GAS_STATION_API_KEY: process.env.ETH_GAS_STATION_API_KEY || '',
CONF: opts.devMode ? conf : ({}), CONF: opts.devMode ? conf : ({}),
SENTRY_DSN: process.env.SENTRY_DSN, SENTRY_DSN: process.env.SENTRY_DSN,
INFURA_PROJECT_ID: conf.INFURA_PROJECT_ID || (
opts.testing
? '00000000000000000000000000000000'
: undefined
),
}), { }), {
global: true, global: true,
}) })

View File

@ -101,7 +101,7 @@
"eth-ens-namehash": "^2.0.8", "eth-ens-namehash": "^2.0.8",
"eth-json-rpc-errors": "^2.0.2", "eth-json-rpc-errors": "^2.0.2",
"eth-json-rpc-filters": "^4.1.1", "eth-json-rpc-filters": "^4.1.1",
"eth-json-rpc-infura": "^4.0.2", "eth-json-rpc-infura": "^5.0.0",
"eth-json-rpc-middleware": "^5.0.2", "eth-json-rpc-middleware": "^5.0.2",
"eth-keyring-controller": "^6.1.0", "eth-keyring-controller": "^6.1.0",
"eth-method-registry": "^1.2.0", "eth-method-registry": "^1.2.0",

View File

@ -1,5 +1,4 @@
import assert from 'assert' import assert from 'assert'
import nock from 'nock'
import sinon from 'sinon' import sinon from 'sinon'
import ObservableStore from 'obs-store' import ObservableStore from 'obs-store'
import contracts from 'eth-contract-metadata' import contracts from 'eth-contract-metadata'
@ -21,13 +20,9 @@ describe('DetectTokensController', function () {
} }
beforeEach(async function () { beforeEach(async function () {
nock('https://api.infura.io')
.get(/.*/u)
.reply(200)
keyringMemStore = new ObservableStore({ isUnlocked: false }) keyringMemStore = new ObservableStore({ isUnlocked: false })
network = new NetworkController() network = new NetworkController()
network.setInfuraProjectId('foo')
preferences = new PreferencesController({ network }) preferences = new PreferencesController({ network })
preferences.setAddresses([ preferences.setAddresses([
'0x7e57e2', '0x7e57e2',
@ -39,7 +34,6 @@ describe('DetectTokensController', function () {
after(function () { after(function () {
sandbox.restore() sandbox.restore()
nock.cleanAll()
}) })
it('should poll on correct interval', async function () { it('should poll on correct interval', async function () {

View File

@ -82,19 +82,6 @@ describe('MetaMaskController', function () {
beforeEach(function () { beforeEach(function () {
nock('https://api.infura.io')
.get('/v1/ticker/ethusd')
.reply(200, '{"base": "ETH", "quote": "USD", "bid": 288.45, "ask": 288.46, "volume": 112888.17569277, "exchange": "bitfinex", "total_volume": 272175.00106721005, "num_exchanges": 8, "timestamp": 1506444677}')
nock('https://api.infura.io')
.get('/v1/ticker/ethjpy')
.reply(200, '{"base": "ETH", "quote": "JPY", "bid": 32300.0, "ask": 32400.0, "volume": 247.4616071, "exchange": "kraken", "total_volume": 247.4616071, "num_exchanges": 1, "timestamp": 1506444676}')
nock('https://api.infura.io')
.persist()
.get(/.*/u)
.reply(200)
nock('https://min-api.cryptocompare.com') nock('https://min-api.cryptocompare.com')
.persist() .persist()
.get(/.*/u) .get(/.*/u)
@ -114,6 +101,7 @@ describe('MetaMaskController', function () {
}, },
initState: cloneDeep(firstTimeState), initState: cloneDeep(firstTimeState),
platform: { showTransactionNotification: () => undefined, getVersion: () => 'foo' }, platform: { showTransactionNotification: () => undefined, getVersion: () => 'foo' },
infuraProjectId: 'foo',
}) })
// add sinon method spies // add sinon method spies

View File

@ -1,5 +1,4 @@
import assert from 'assert' import assert from 'assert'
import nock from 'nock'
import NetworkController from '../../../../../app/scripts/controllers/network' import NetworkController from '../../../../../app/scripts/controllers/network'
import { getNetworkDisplayName } from '../../../../../app/scripts/controllers/network/util' import { getNetworkDisplayName } from '../../../../../app/scripts/controllers/network/util'
@ -12,16 +11,8 @@ describe('NetworkController', function () {
} }
beforeEach(function () { beforeEach(function () {
nock('https://rinkeby.infura.io')
.persist()
.post('/metamask')
.reply(200)
networkController = new NetworkController() networkController = new NetworkController()
}) networkController.setInfuraProjectId('foo')
afterEach(function () {
nock.cleanAll()
}) })
describe('#provider', function () { describe('#provider', function () {

View File

@ -47,6 +47,7 @@ describe('Actions', function () {
}, },
}, },
initState: cloneDeep(firstTimeState), initState: cloneDeep(firstTimeState),
infuraProjectId: 'foo',
}) })
metamaskController.threeBoxController = { metamaskController.threeBoxController = {

View File

@ -3,7 +3,7 @@ const defaultNetworksData = [
labelKey: 'mainnet', labelKey: 'mainnet',
iconColor: '#29B6AF', iconColor: '#29B6AF',
providerType: 'mainnet', providerType: 'mainnet',
rpcUrl: 'https://api.infura.io/v1/jsonrpc/mainnet', rpcUrl: `https://mainnet.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
chainId: '1', chainId: '1',
ticker: 'ETH', ticker: 'ETH',
blockExplorerUrl: 'https://etherscan.io', blockExplorerUrl: 'https://etherscan.io',
@ -12,7 +12,7 @@ const defaultNetworksData = [
labelKey: 'ropsten', labelKey: 'ropsten',
iconColor: '#FF4A8D', iconColor: '#FF4A8D',
providerType: 'ropsten', providerType: 'ropsten',
rpcUrl: 'https://api.infura.io/v1/jsonrpc/ropsten', rpcUrl: `https://ropsten.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
chainId: '3', chainId: '3',
ticker: 'ETH', ticker: 'ETH',
blockExplorerUrl: 'https://ropsten.etherscan.io', blockExplorerUrl: 'https://ropsten.etherscan.io',
@ -21,7 +21,7 @@ const defaultNetworksData = [
labelKey: 'rinkeby', labelKey: 'rinkeby',
iconColor: '#F6C343', iconColor: '#F6C343',
providerType: 'rinkeby', providerType: 'rinkeby',
rpcUrl: 'https://api.infura.io/v1/jsonrpc/rinkeby', rpcUrl: `https://rinkeby.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
chainId: '4', chainId: '4',
ticker: 'ETH', ticker: 'ETH',
blockExplorerUrl: 'https://rinkeby.etherscan.io', blockExplorerUrl: 'https://rinkeby.etherscan.io',
@ -30,7 +30,7 @@ const defaultNetworksData = [
labelKey: 'goerli', labelKey: 'goerli',
iconColor: '#3099f2', iconColor: '#3099f2',
providerType: 'goerli', providerType: 'goerli',
rpcUrl: 'https://api.infura.io/v1/jsonrpc/goerli', rpcUrl: `https://goerli.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
chainId: '5', chainId: '5',
ticker: 'ETH', ticker: 'ETH',
blockExplorerUrl: 'https://goerli.etherscan.io', blockExplorerUrl: 'https://goerli.etherscan.io',
@ -39,7 +39,7 @@ const defaultNetworksData = [
labelKey: 'kovan', labelKey: 'kovan',
iconColor: '#9064FF', iconColor: '#9064FF',
providerType: 'kovan', providerType: 'kovan',
rpcUrl: 'https://api.infura.io/v1/jsonrpc/kovan', rpcUrl: `https://kovan.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,
chainId: '42', chainId: '42',
ticker: 'ETH', ticker: 'ETH',
blockExplorerUrl: 'https://etherscan.io', blockExplorerUrl: 'https://etherscan.io',

View File

@ -10371,7 +10371,7 @@ eth-json-rpc-infura@^3.1.0:
json-rpc-error "^2.0.0" json-rpc-error "^2.0.0"
tape "^4.8.0" tape "^4.8.0"
eth-json-rpc-infura@^4.0.1, eth-json-rpc-infura@^4.0.2: eth-json-rpc-infura@^4.0.1:
version "4.0.2" version "4.0.2"
resolved "https://registry.yarnpkg.com/eth-json-rpc-infura/-/eth-json-rpc-infura-4.0.2.tgz#8af1a1a2e9a0a82aaa302bbc96fb1a4c15d69b83" resolved "https://registry.yarnpkg.com/eth-json-rpc-infura/-/eth-json-rpc-infura-4.0.2.tgz#8af1a1a2e9a0a82aaa302bbc96fb1a4c15d69b83"
integrity sha512-dvgOrci9lZqpjpp0hoC3Zfedhg3aIpLFVDH0TdlKxRlkhR75hTrKTwxghDrQwE0bn3eKrC8RsN1m/JdnIWltpw== integrity sha512-dvgOrci9lZqpjpp0hoC3Zfedhg3aIpLFVDH0TdlKxRlkhR75hTrKTwxghDrQwE0bn3eKrC8RsN1m/JdnIWltpw==
@ -10381,6 +10381,16 @@ eth-json-rpc-infura@^4.0.1, eth-json-rpc-infura@^4.0.2:
eth-json-rpc-middleware "^4.1.4" eth-json-rpc-middleware "^4.1.4"
json-rpc-engine "^5.1.3" json-rpc-engine "^5.1.3"
eth-json-rpc-infura@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/eth-json-rpc-infura/-/eth-json-rpc-infura-5.0.0.tgz#a666efe860659ffa09e918abdf9aab9f2434e508"
integrity sha512-TvRgnDHsxNblUpFo1KTqsgfeZ0rMEg6Ga+v+Zbqjb467txODg6oPamKJKh/odOs+MXtYpt9e7d4yJCy+5azq5w==
dependencies:
eth-json-rpc-middleware "^4.4.0"
eth-rpc-errors "^3.0.0"
json-rpc-engine "^5.1.3"
node-fetch "^2.6.0"
eth-json-rpc-middleware@^1.5.0: eth-json-rpc-middleware@^1.5.0:
version "1.6.0" version "1.6.0"
resolved "https://registry.yarnpkg.com/eth-json-rpc-middleware/-/eth-json-rpc-middleware-1.6.0.tgz#5c9d4c28f745ccb01630f0300ba945f4bef9593f" resolved "https://registry.yarnpkg.com/eth-json-rpc-middleware/-/eth-json-rpc-middleware-1.6.0.tgz#5c9d4c28f745ccb01630f0300ba945f4bef9593f"
@ -10400,7 +10410,7 @@ eth-json-rpc-middleware@^1.5.0:
promise-to-callback "^1.0.0" promise-to-callback "^1.0.0"
tape "^4.6.3" tape "^4.6.3"
eth-json-rpc-middleware@^4.1.4, eth-json-rpc-middleware@^4.1.5: eth-json-rpc-middleware@^4.1.4, eth-json-rpc-middleware@^4.1.5, eth-json-rpc-middleware@^4.4.0:
version "4.4.1" version "4.4.1"
resolved "https://registry.yarnpkg.com/eth-json-rpc-middleware/-/eth-json-rpc-middleware-4.4.1.tgz#07d3dd0724c24a8d31e4a172ee96271da71b4228" resolved "https://registry.yarnpkg.com/eth-json-rpc-middleware/-/eth-json-rpc-middleware-4.4.1.tgz#07d3dd0724c24a8d31e4a172ee96271da71b4228"
integrity sha512-yoSuRgEYYGFdVeZg3poWOwAlRI+MoBIltmOB86MtpoZjvLbou9EB/qWMOWSmH2ryCWLW97VYY6NWsmWm3OAA7A== integrity sha512-yoSuRgEYYGFdVeZg3poWOwAlRI+MoBIltmOB86MtpoZjvLbou9EB/qWMOWSmH2ryCWLW97VYY6NWsmWm3OAA7A==
@ -10528,6 +10538,13 @@ eth-rpc-errors@^2.1.1:
dependencies: dependencies:
fast-safe-stringify "^2.0.6" fast-safe-stringify "^2.0.6"
eth-rpc-errors@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/eth-rpc-errors/-/eth-rpc-errors-3.0.0.tgz#d7b22653c70dbf9defd4ef490fd08fe70608ca10"
integrity sha512-iPPNHPrLwUlR9xCSYm7HHQjWBasor3+KZfRvwEWxMz3ca0yqnlBeJrnyphkGIXZ4J7AMAaOLmwy4AWhnxOiLxg==
dependencies:
fast-safe-stringify "^2.0.6"
eth-sig-util@2.3.0, eth-sig-util@^2.3.0: eth-sig-util@2.3.0, eth-sig-util@^2.3.0:
version "2.3.0" version "2.3.0"
resolved "https://registry.yarnpkg.com/eth-sig-util/-/eth-sig-util-2.3.0.tgz#c54a6ac8e8796f7e25f59cf436982a930e645231" resolved "https://registry.yarnpkg.com/eth-sig-util/-/eth-sig-util-2.3.0.tgz#c54a6ac8e8796f7e25f59cf436982a930e645231"