我希望看到在提供程序中实现getAccounts和sendTransaction的代码。
我试着深入研究Metamask源代码,并找到Metamask提供Web3 3提供者的行,但我不知道这个提供者在哪里实现了sendTransaction或getAccounts。
这些方法是如何/在哪里实现的?
发布于 2019-09-15 15:34:46
我建议检查一下将元掩模移植到新环境指南。即使您没有移植MetaMask,它也是有用的,因为它解释了项目组织。
如果您在getAccounts中搜索MetaMask,您将在createMetamaskMiddleware.js中找到它。它将它与其他函数一起传递给createWalletSubprovider。
createWalletSubprovider({
getAccounts,
processTransaction,
processEthSignMessage,
processTypedMessage,
processTypedMessageV3,
processTypedMessageV4,
processPersonalMessage,
}),它在一个单独的存储库中提供。这个中间件将把JSON请求映射到MetaMask扩展中的函数。
您可以确定函数的实现,在调用createMetamaskMiddleware参数时检查它的参数。
如果你跟着一些跳,你会发现它是在MetamaskController.initializeProvider完成的。
例如,对于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 []
}
}https://ethereum.stackexchange.com/questions/74824
复制相似问题