aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/setupLedgerIframe.js
blob: 2831d072e63d02d4e052cd68525e552f40a05a88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const extension = require('extensionizer')
module.exports = setupLedgerIframe
/**
 * Injects an iframe into the current document to 
 * enable the interaction with ledger devices
 */
function setupLedgerIframe () {
    const ORIGIN  = 'http://localhost:9000'
    const ledgerIframe = document.createElement('iframe')
    ledgerIframe.src = ORIGIN
    console.log('Injecting ledger iframe')
    document.head.appendChild(ledgerIframe)
  
      console.log('[LEDGER]: LEDGER BG LISTENER READY')
      extension.runtime.onMessage.addListener(({action, params}) => {
          console.log('[LEDGER]: GOT MSG FROM THE KEYRING', action, params)
          if (action.search('ledger-') !== -1) {
              //Forward messages from the keyring to the iframe
              sendMessage({action, params})
          }
      })
  
      function sendMessage(msg) {
        ledgerIframe.contentWindow.postMessage({...msg, target: 'LEDGER-IFRAME'}, '*')
      }
    
     /*
    Passing messages from iframe to background script
    */
    console.log('[LEDGER]: LEDGER FROM-IFRAME LISTENER READY')
    window.addEventListener('message', event => {
        if(event.origin !== ORIGIN) return false
        if (event.data && event.data.action && event.data.action.search('ledger-') !== -1) {
          // Forward messages from the iframe to the keyring
          console.log('[LEDGER] : forwarding msg', event.data)
          extension.runtime.sendMessage(event.data)
        }
    })
  
  }