Skip to content

Instantly share code, notes, and snippets.

@Man-Jain
Created December 14, 2019 19:09
Show Gist options
  • Save Man-Jain/e9af6c91fd852c15319cc9c026a1e208 to your computer and use it in GitHub Desktop.
Save Man-Jain/e9af6c91fd852c15319cc9c026a1e208 to your computer and use it in GitHub Desktop.

Introduction

  1. 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
  1. How this improves the extension
  • Some bakar regarding this
  1. 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 on hash(script_origin + user_private_key). It is available on the Snap global as wallet.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

  1. Explain Approach, and suggest concepts.

POC

  1. POC Demo (Walkthrough) 5.1. Abstract level code description.

Sample Code

Snap Code

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 executed
  • Alert 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.

Dapp Code :-

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 above hello 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 ?

  1. Future Developments and Current status
  • Wiki
  • Video References
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment