0

I'd like to see code that implements getAccounts and sendTransaction in a provider.

I tried digging into the Metamask source code and found the line where Metamask supplies the Web3 provider, but I couldn't figure out where this provider implements sendTransaction or getAccounts.

How/where are these methods implemented?

1

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 []
    }
  }
  • Thanks for the links, I'll carefully study them. – J. Hesters Sep 30 '19 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.