aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/background.js
blob: 3de152d0aab0ae4f0d33c9b118542e351b622d9d (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const Dnode = require('dnode')
const PortStream = require('./lib/port-stream.js')
const MetaMaskProvider = require('./lib/metamask-provider')
const IdentityManager = require('./lib/idmgmt')
const eos = require('end-of-stream')

console.log('ready to roll')

var wallet = new IdentityManager()

// setup provider
var zeroClient = MetaMaskProvider({
  rpcUrl: 'https://rawtestrpc.metamask.io/',
  getAccounts: wallet.getAccounts.bind(wallet),
  sendTransaction: wallet.confirmTransaction.bind(wallet),
})

wallet.setProvider(zeroClient)
zeroClient.on('block', function(block){
  wallet.newBlock(block)
})


// setup messaging
chrome.runtime.onConnect.addListener(connectRemote)
function connectRemote(remotePort){
  var isMetaMaskInternalProcess = (remotePort.name === 'popup')
  if (isMetaMaskInternalProcess) {
    // communication with popup
    handleInternalCommunication(remotePort)
  } else {
    // communication with page
    handleExternalCommunication(remotePort)
  }
}

function handleInternalCommunication(remotePort){
  var duplex = new PortStream(remotePort)
  var connection = Dnode({
    // this is annoying, have to decompose wallet
    getState:           wallet.getState.bind(wallet),
    submitPassword:     wallet.submitPassword.bind(wallet),
    setSelectedAddress: wallet.setSelectedAddress.bind(wallet),
    signTransaction:    wallet.signTransaction.bind(wallet),
    setLocked:          wallet.setLocked.bind(wallet),
    getAccounts:        wallet.getAccounts.bind(wallet),
    confirmTransaction: wallet.confirmTransaction.bind(wallet),
    newBlock:           wallet.newBlock.bind(wallet),
    setProvider:        wallet.setProvider.bind(wallet),
  })
  duplex.pipe(connection).pipe(duplex)
  connection.on('remote', function(remote){
    
    // push updates to popup
    wallet.on('update', sendUpdate)
    eos(duplex, function unsubscribe(){
      wallet.removeListener('update', sendUpdate)
    })
    function sendUpdate(state){
      remote.sendUpdate(state)
    }

  })

  
    
  // sub to metamask store
}

function handleExternalCommunication(remotePort){
  remotePort.onMessage.addListener(onRpcRequest.bind(null, remotePort))
}

// handle rpc requests
function onRpcRequest(remotePort, payload){
  // console.log('MetaMaskPlugin - incoming payload:', payload)
  zeroClient.sendAsync(payload, function onPayloadHandled(err, response){
    if (err) throw err
    console.log('MetaMaskPlugin - RPC complete:', payload, '->', response)
    remotePort.postMessage(response)
  })
}

// setup badge text
// updateBadge()

// function updateBadge(){
//   var label = ''
//   var count = Object.keys(unsignedTxs).length
//   if (count) {
//     label = String(count)
//   }
//   chrome.browserAction.setBadgeText({text: label})
//   chrome.browserAction.setBadgeBackgroundColor({color: '#506F8B'})
// }

// function handleMessage(msg){
//   console.log('got message!', msg.type)
//   switch(msg.type){
    
//     case 'addUnsignedTx':
//       addTransaction(msg.payload)
//       return

//     case 'removeUnsignedTx':
//       removeTransaction(msg.payload)
//       return

//   }
// }