I'd suggest to check the Guide to Porting MetaMask to a New Environment. Even if you are not porting MetaMask it is useful because it explains the project organization.
If you search for getAccounts
in MetaMask you will find it in createMetamaskMiddleware.js. It passes it among other functions to createWalletSubprovider
createWalletSubprovider({
getAccounts,
processTransaction,
processEthSignMessage,
processTypedMessage,
processTypedMessageV3,
processTypedMessageV4,
processPersonalMessage,
}),
Which is provided in a separate repo. This middleware will map JSON-RPC requests to functions in MetaMask extension.
- eth_accounts => getAccounts
- eth_sendTransaction => processTransaction
You can determine the implementations of the functions examing the parameters of createMetamaskMiddleware
when it is being called.
If you follow through some hops you will find it is done at MetamaskController.initializeProvider
For example you have for getAccounts
:
getAccounts: async ({ origin }) => {
// Expose no accounts if this origin has not been approved, preventing
// account-requring RPC methods from completing successfully
const exposeAccounts = this.providerApprovalController.shouldExposeAccounts(origin)
if (origin !== 'MetaMask' && !exposeAccounts) { return [] }
const isUnlocked = this.keyringController.memStore.getState().isUnlocked
const selectedAddress = this.preferencesController.getSelectedAddress()
// only show address if account is unlocked
if (isUnlocked && selectedAddress) {
return [selectedAddress]
} else {
return []
}
}