From 48d77c250fe9a0fd2282da4a8dd0489b02773b7f Mon Sep 17 00:00:00 2001 From: kumavis Date: Mon, 18 Jan 2016 17:05:46 -0800 Subject: [PATCH] tx-signing works --- app/scripts/background.js | 21 +++++----- app/scripts/lib/idmgmt.js | 59 +++++++++++++++++++++++----- app/scripts/lib/metamask-provider.js | 2 +- 3 files changed, 60 insertions(+), 22 deletions(-) diff --git a/app/scripts/background.js b/app/scripts/background.js index 112791e02..1dfee605c 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -44,7 +44,6 @@ function handleInternalCommunication(remotePort){ signTransaction: wallet.signTransaction.bind(wallet), setLocked: wallet.setLocked.bind(wallet), getAccounts: wallet.getAccounts.bind(wallet), - signTransaction: wallet.signTransaction.bind(wallet), newBlock: wallet.newBlock.bind(wallet), setProvider: wallet.setProvider.bind(wallet), }) @@ -82,17 +81,17 @@ function onRpcRequest(remotePort, payload){ } // setup badge text -// updateBadge() +wallet.on('update', 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 updateBadge(state){ + var label = '' + var count = Object.keys(state.unconfTxs).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) diff --git a/app/scripts/lib/idmgmt.js b/app/scripts/lib/idmgmt.js index 73a6a4bf6..35c07721a 100644 --- a/app/scripts/lib/idmgmt.js +++ b/app/scripts/lib/idmgmt.js @@ -3,6 +3,7 @@ const EventEmitter = require('events').EventEmitter const async = require('async') const KeyStore = require('eth-lightwallet').keystore const createPayload = require('web3-provider-engine/util/create-payload') +const createId = require('web3-provider-engine/util/random-id') const Transaction = require('ethereumjs-tx') module.exports = IdentityManager @@ -12,6 +13,9 @@ var selectedAddress = null var identities = {} var unconfTxs = {} +// not part of serilized metamask state - only keep in memory +var unconfTxCbs = {} + var provider = null var defaultPassword = 'test' @@ -59,7 +63,7 @@ function _getState(cb){ var result = { isUnlocked: unlocked, identities: unlocked ? getIdentities() : {}, - unconfTxs: unlocked ? unconfTxs() : {}, + unconfTxs: unlocked ? unconfTxs : {}, selectedAddress: selectedAddress, } return result @@ -125,6 +129,11 @@ IdentityManager.prototype.loadIdentities = function(){ } identities[address] = identity }) + self._didUpdate() +} + +IdentityManager.prototype._didUpdate = function(){ + const self = this self.emit('update', _getState()) } @@ -148,7 +157,7 @@ IdentityManager.prototype.updateIdentity = function(address, cb){ var identity = identities[address] identity.balance = result[0] identity.txCount = result[1] - self.emit('update', _getState()) + self._didUpdate() cb() }) } @@ -198,20 +207,51 @@ function tryPassword(password, cb){ function addUnconfirmedTransaction(txParams, cb){ var time = (new Date()).getTime() - var id = time - unconfTxs[id] = { + var txId = createId() + unconfTxs[txId] = { + id: txId, txParams: txParams, time: time, + status: 'unconfirmed', } console.log('addUnconfirmedTransaction:', txParams) // temp - just sign the tx // otherwise we need to keep the cb around - signTransaction(id, cb) + // signTransaction(txId, cb) + unconfTxCbs[txId] = cb } -function signTransaction(txParams, cb){ - console.log('signTransaction:', txParams) +// called from +function signTransaction(password, txId, cb){ + const self = this + + var txData = unconfTxs[txId] + var txParams = txData.txParams + console.log('signTransaction:', txData) + + self._signTransaction(txParams, function(err, rawTx, txHash){ + if (err) { + txData.status = 'error' + txData.error = err + self._didUpdate() + return + } + txData.hash = txHash + txData.status = 'pending' + // for now just kill it + delete unconfTxs[txData.id] + + var txSigCb = unconfTxCbs[txId] || function(){} + txSigCb(null, rawTx) + + cb(null, _getState()) + self._didUpdate() + }) +} + +// internal - actually signs the tx +IdentityManager.prototype._signTransaction = function(txParams, cb){ try { // console.log('signing tx:', txParams) var tx = new Transaction({ @@ -224,8 +264,7 @@ function signTransaction(txParams, cb){ }) var keyStore = getKeyStore() - var serializedTx = keystore.signTx(tx.serialize(), defaultPassword, selectedAddress) - tx.sign(privateKey) + var serializedTx = keyStore.signTx(tx.serialize(), defaultPassword, selectedAddress) // // deserialize and dump values to confirm configuration // var verifyTx = new Transaction(tx.serialize()) @@ -238,7 +277,7 @@ function signTransaction(txParams, cb){ // gasPrice: '0x'+verifyTx.gasPrice.toString('hex'), // gasLimit: '0x'+verifyTx.gasLimit.toString('hex'), // }) - cb(null, serializedTx) + cb(null, serializedTx, tx.hash()) } catch (err) { cb(err) } diff --git a/app/scripts/lib/metamask-provider.js b/app/scripts/lib/metamask-provider.js index 09326e3c1..36d88e52e 100644 --- a/app/scripts/lib/metamask-provider.js +++ b/app/scripts/lib/metamask-provider.js @@ -33,7 +33,7 @@ function metamaskProvider(opts){ // id mgmt engine.addProvider(new HookedWalletSubprovider({ getAccounts: opts.getAccounts, - sendTransaction: opts.sendTransaction, + signTransaction: opts.signTransaction, })) // data source