1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 10:30:04 +01:00
metamask-extension/app/scripts/createStandardProvider.js
kumavis 2845398c3d Refactor ProviderApprovalController to use rpc and publicConfigStore (#6410)
* Ensure home screen does not render if there are unapproved txs (#6501)

* Ensure that the confirm screen renders before the home screen if there are unapproved txs.

* Only render confirm screen before home screen on mount.

* inpage - revert _metamask api to isEnabled isApproved isUnlocked
2019-05-03 13:32:05 -04:00

74 lines
2.0 KiB
JavaScript

class StandardProvider {
_isConnected
_provider
constructor (provider) {
this._provider = provider
this._subscribe()
// indicate that we've connected, mostly just for standard compliance
setTimeout(() => {
this._onConnect()
})
}
_onClose () {
if (this._isConnected === undefined || this._isConnected) {
this._provider.emit('close', {
code: 1011,
reason: 'Network connection error',
})
}
this._isConnected = false
}
_onConnect () {
!this._isConnected && this._provider.emit('connect')
this._isConnected = true
}
_subscribe () {
this._provider.on('data', (error, { method, params }) => {
if (!error && method === 'eth_subscription') {
this._provider.emit('notification', params.result)
}
})
}
/**
* Initiate an RPC method call
*
* @param {string} method - RPC method name to call
* @param {string[]} params - Array of RPC method parameters
* @returns {Promise<*>} Promise resolving to the result if successful
*/
send (method, params = []) {
return new Promise((resolve, reject) => {
try {
this._provider.sendAsync({ id: 1, jsonrpc: '2.0', method, params }, (error, response) => {
error = error || response.error
error ? reject(error) : resolve(response)
})
} catch (error) {
reject(error)
}
})
}
}
/**
* Converts a legacy provider into an EIP-1193-compliant standard provider
* @param {Object} provider - Legacy provider to convert
* @returns {Object} Standard provider
*/
export default function createStandardProvider (provider) {
const standardProvider = new StandardProvider(provider)
const sendLegacy = provider.send
provider.send = (methodOrPayload, callbackOrArgs) => {
if (typeof methodOrPayload === 'string' && !callbackOrArgs || Array.isArray(callbackOrArgs)) {
return standardProvider.send(methodOrPayload, callbackOrArgs)
}
return sendLegacy.call(provider, methodOrPayload, callbackOrArgs)
}
return provider
}