2021-02-04 19:15:23 +01:00
|
|
|
import { MESSAGE_TYPE } from '../../../../../shared/constants/app';
|
2020-12-08 20:48:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This RPC method gets background state relevant to the provider.
|
|
|
|
* The background sends RPC notifications on state changes, but the provider
|
|
|
|
* first requests state on initialization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const getProviderState = {
|
|
|
|
methodNames: [MESSAGE_TYPE.GET_PROVIDER_STATE],
|
|
|
|
implementation: getProviderStateHandler,
|
2021-11-16 00:11:51 +01:00
|
|
|
hookNames: {
|
|
|
|
getProviderState: true,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
export default getProviderState;
|
2020-12-08 20:48:47 +01:00
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} ProviderStateHandlerResult
|
2020-12-08 20:48:47 +01:00
|
|
|
* @property {string} chainId - The current chain ID.
|
|
|
|
* @property {boolean} isUnlocked - Whether the extension is unlocked or not.
|
|
|
|
* @property {string} networkVersion - The current network ID.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} ProviderStateHandlerOptions
|
2020-12-08 20:48:47 +01:00
|
|
|
* @property {() => ProviderStateHandlerResult} getProviderState - A function that
|
|
|
|
* gets the current provider state.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import('json-rpc-engine').JsonRpcRequest<[]>} req - The JSON-RPC request object.
|
|
|
|
* @param {import('json-rpc-engine').JsonRpcResponse<ProviderStateHandlerResult>} res - The JSON-RPC response object.
|
|
|
|
* @param {Function} _next - The json-rpc-engine 'next' callback.
|
|
|
|
* @param {Function} end - The json-rpc-engine 'end' callback.
|
|
|
|
* @param {ProviderStateHandlerOptions} options
|
|
|
|
*/
|
|
|
|
async function getProviderStateHandler(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
_next,
|
|
|
|
end,
|
|
|
|
{ getProviderState: _getProviderState },
|
|
|
|
) {
|
|
|
|
res.result = {
|
|
|
|
...(await _getProviderState(req.origin)),
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
return end();
|
2020-12-08 20:48:47 +01:00
|
|
|
}
|