Skip to content

Instantly share code, notes, and snippets.

@Dobrokhvalov
Last active September 4, 2019 16:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dobrokhvalov/3e5294cdbd2c28ed175477a3aacb9987 to your computer and use it in GitHub Desktop.
Save Dobrokhvalov/3e5294cdbd2c28ed175477a3aacb9987 to your computer and use it in GitHub Desktop.

Ens Login iFrame draft proposal

Abstract

Iframe benefits

  • Better security: wallet provider code is loaded in a sandbox environment, can't read dapp's localstorage/cookies and can't track dapp location
  • Formalized specs: a wallet provider needs to provide a js code that can respond to the eip-1474 messages

Example pseudo-code

Dapp

  1. Fetch wallet provider source from ENS
  2. Add the wallet provider iframe to the dapp's web page
  3. Subscribe to events from wallet provider iframe
  4. Exchange messages with wallet provider via iframe
// 1. Fetch wallet provider source from ENS 
walletProviderUrl = fetchIframeUrl(ensName)

// 2. Add the wallet provider iframe to the dapp's web page
walletIframe = document.body.append(‘iframe’)
walletIframe.src = walletProviderUrl

// 3. Subscribe to events from wallet provider iframe 
dappWindow.addEventListener("message",onMessageFromWallet, false)

// 4. Exchange messages with wallet provider via PostMessage interface
// e.g. when need to call some method like eth_sendTransaction
walletIframe.postMessage(msg, walletProviderUrl)
// Example of `msg`: 
// msg  = {
//    "id": 1337,
//    "jsonrpc": "2.0",
//    "method": "eth_blockNumber",
//    "params": []
// }`

Wallet provider

  1. Subscribe to events (messages) from dapp
  2. Handle request from the dapp
  3. Send response back to the dapp
// 1. Subscribe for events from dapp 
walletIframe.addEventListener("message", onMessageFromDapp, false)
// 
// 2. Handle request from the dapp
onMessageFromDapp(msg) { 
//   make approtiate action: send transaction, sign message or other
//   ... 
//   3. Send response back to the dapp window in the jsonrpc format - https://www.jsonrpc.org/specification#response_object
     dappWindow.postMessage(response, dappUrl)
}

Implementation

A library similar to web3js or ethers can be implemented to abstract away the messaging routine between a dapp and wallet. The lib can comply with eip-1193 and be built on top of Penpal lib

Mockups with the user flow

https://www.figma.com/file/kueZe26fxl440s9hUFoWSO/%5BENS-login%5D-iFrame-Demo?node-id=0%3A1

Useful links

@artignatyev
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment