- What's new with the Metamask plugin
- Permission System :- users are forced to consent to some truly excessive permissions
- Plugin System :- important innovations are happening all around us, from scaling strategies, to new smart contract protocols, and many of these require interacting with a user’s accounts, and running a persistent script
- How this improves the extension
- Some bakar regarding this
- Break down of the new Plugin APIs of Metamask. (Writeup)
- A MetaMask plugin is a script loaded over a verified and permissionless protocol like ENS, IPFS, or Swarm. These scripts have zero privilege by default, but will be able to request a variety of powerful wallet APIs from MetaMask, via a new API we call the
wallet
API.
The wallet
API is an extension of the classic ethereum
or web3.currentProvider
API.
APIs Currently Provided -
.registerRpcMessageHandler(rpcMessageHandler)
- Used to extend the MetaMask API exposed to dapps. Developers can create their own APIs making this very extendible and powerful..registerApiRequestHandler(handler)
- Used to create Responsive, Event Driven APIs, that can be provided to the dapp..onMetaMaskEvent(eventName, callback)
- Just for beta purposes, exposes every event internal to the MetaMask controllers for Transactions, Networks, and Block tracking. Some are :-tx:status-update
: 'Be notified when the status of your transactions changes',latest
: 'Be notified when the new blocks are added to the blockchain',networkDidChange
: 'Be notified when your selected network changes',newUnapprovedTx
: 'Be notified with details of your new transactions',
Permission for above can be asked in following format
"initialPermissions": {
"metamask_newUnapprovedTx": {}
}
.getAppKey()
- Every Snap can request a unique secret seed based onhash(script_origin + user_private_key)
. It is available on the Snap global aswallet.getAppKey()
. This method returns a promise, which resolves to a 32 byte (64 character) hex-encoded string which will be re-generated if the user were to have their computer wiped but restored MetaMask from the same seed phrase..updatePluginState(yourStateToPersist)
- Used to persist state to our store..getPluginState()
- Returns whatever the most recent value you passed to.updatePluginState()
. Useful when first starting up your Snap to restore its state.
Above APIs can be changed or removed in future. A list of all the methods are given here. These can be asked for in permissions and then called :- Link
- Explain Approach, and suggest concepts.
- POC Demo (Walkthrough) 5.1. Abstract level code description.
wallet.registerRpcMessageHandler(async (originString, requestObject) => {
switch (requestObject.method) {
case 'hello':
return wallet.send({
method: 'alert',
params: [`Hello, ${originString}!`]
})
default:
throw new Error('Method not found.')
}
})
The code registers a RPC Handler i.e., creates a developer defined API by the name of hello
which can be called via the frontend given below.
requestObject
contains the method to be executedAlert
is a inbuilt method which allows us to create a alert on the webpage.- An alert is created when the hello method is called using the metamask api.
async function connect () {
await ethereum.send({
method: 'wallet_enable',
params: [{
wallet_plugin: { [snapId]: {} },
}]
})
}
// here we call the plugin's "hello" method
async function send () {
try {
const response = await ethereum.send({
method: 'wallet_invokePlugin',
params: [snapId, {
method: 'hello'
}]
})
} catch (err) {
console.error(err)
alert('Problem happened: ' + err.message || err)
}
}
Connect
function is called to connect metamask with the plugin and download it if does not exist.wallet_enable
- when this method is send, the metamask asks user for the permissions of the plugin.wallet_invokePlugin
is another method used to call an RPC method we declared abovehello
in our case. The hello case in our switch statement is called leading to an alert.
5.2. Plugin APIs metioned above can be written here too 5.3. Creating a Simple Plugin (Code and Description with Images) What to write here ? ALready given examples or something of my own ?
- Future Developments and Current status
- Wiki
- Video References