1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

tx-signing works

This commit is contained in:
kumavis 2016-01-18 17:05:46 -08:00
parent ad5dd96a3b
commit 48d77c250f
3 changed files with 60 additions and 22 deletions

View File

@ -44,7 +44,6 @@ function handleInternalCommunication(remotePort){
signTransaction: wallet.signTransaction.bind(wallet), signTransaction: wallet.signTransaction.bind(wallet),
setLocked: wallet.setLocked.bind(wallet), setLocked: wallet.setLocked.bind(wallet),
getAccounts: wallet.getAccounts.bind(wallet), getAccounts: wallet.getAccounts.bind(wallet),
signTransaction: wallet.signTransaction.bind(wallet),
newBlock: wallet.newBlock.bind(wallet), newBlock: wallet.newBlock.bind(wallet),
setProvider: wallet.setProvider.bind(wallet), setProvider: wallet.setProvider.bind(wallet),
}) })
@ -82,17 +81,17 @@ function onRpcRequest(remotePort, payload){
} }
// setup badge text // setup badge text
// updateBadge() wallet.on('update', updateBadge)
// function updateBadge(){ function updateBadge(state){
// var label = '' var label = ''
// var count = Object.keys(unsignedTxs).length var count = Object.keys(state.unconfTxs).length
// if (count) { if (count) {
// label = String(count) label = String(count)
// } }
// chrome.browserAction.setBadgeText({text: label}) chrome.browserAction.setBadgeText({text: label})
// chrome.browserAction.setBadgeBackgroundColor({color: '#506F8B'}) chrome.browserAction.setBadgeBackgroundColor({color: '#506F8B'})
// } }
// function handleMessage(msg){ // function handleMessage(msg){
// console.log('got message!', msg.type) // console.log('got message!', msg.type)

View File

@ -3,6 +3,7 @@ const EventEmitter = require('events').EventEmitter
const async = require('async') const async = require('async')
const KeyStore = require('eth-lightwallet').keystore const KeyStore = require('eth-lightwallet').keystore
const createPayload = require('web3-provider-engine/util/create-payload') const createPayload = require('web3-provider-engine/util/create-payload')
const createId = require('web3-provider-engine/util/random-id')
const Transaction = require('ethereumjs-tx') const Transaction = require('ethereumjs-tx')
module.exports = IdentityManager module.exports = IdentityManager
@ -12,6 +13,9 @@ var selectedAddress = null
var identities = {} var identities = {}
var unconfTxs = {} var unconfTxs = {}
// not part of serilized metamask state - only keep in memory
var unconfTxCbs = {}
var provider = null var provider = null
var defaultPassword = 'test' var defaultPassword = 'test'
@ -59,7 +63,7 @@ function _getState(cb){
var result = { var result = {
isUnlocked: unlocked, isUnlocked: unlocked,
identities: unlocked ? getIdentities() : {}, identities: unlocked ? getIdentities() : {},
unconfTxs: unlocked ? unconfTxs() : {}, unconfTxs: unlocked ? unconfTxs : {},
selectedAddress: selectedAddress, selectedAddress: selectedAddress,
} }
return result return result
@ -125,6 +129,11 @@ IdentityManager.prototype.loadIdentities = function(){
} }
identities[address] = identity identities[address] = identity
}) })
self._didUpdate()
}
IdentityManager.prototype._didUpdate = function(){
const self = this
self.emit('update', _getState()) self.emit('update', _getState())
} }
@ -148,7 +157,7 @@ IdentityManager.prototype.updateIdentity = function(address, cb){
var identity = identities[address] var identity = identities[address]
identity.balance = result[0] identity.balance = result[0]
identity.txCount = result[1] identity.txCount = result[1]
self.emit('update', _getState()) self._didUpdate()
cb() cb()
}) })
} }
@ -198,20 +207,51 @@ function tryPassword(password, cb){
function addUnconfirmedTransaction(txParams, cb){ function addUnconfirmedTransaction(txParams, cb){
var time = (new Date()).getTime() var time = (new Date()).getTime()
var id = time var txId = createId()
unconfTxs[id] = { unconfTxs[txId] = {
id: txId,
txParams: txParams, txParams: txParams,
time: time, time: time,
status: 'unconfirmed',
} }
console.log('addUnconfirmedTransaction:', txParams) console.log('addUnconfirmedTransaction:', txParams)
// temp - just sign the tx // temp - just sign the tx
// otherwise we need to keep the cb around // otherwise we need to keep the cb around
signTransaction(id, cb) // signTransaction(txId, cb)
unconfTxCbs[txId] = cb
} }
function signTransaction(txParams, cb){ // called from
console.log('signTransaction:', txParams) 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 { try {
// console.log('signing tx:', txParams) // console.log('signing tx:', txParams)
var tx = new Transaction({ var tx = new Transaction({
@ -224,8 +264,7 @@ function signTransaction(txParams, cb){
}) })
var keyStore = getKeyStore() var keyStore = getKeyStore()
var serializedTx = keystore.signTx(tx.serialize(), defaultPassword, selectedAddress) var serializedTx = keyStore.signTx(tx.serialize(), defaultPassword, selectedAddress)
tx.sign(privateKey)
// // deserialize and dump values to confirm configuration // // deserialize and dump values to confirm configuration
// var verifyTx = new Transaction(tx.serialize()) // var verifyTx = new Transaction(tx.serialize())
@ -238,7 +277,7 @@ function signTransaction(txParams, cb){
// gasPrice: '0x'+verifyTx.gasPrice.toString('hex'), // gasPrice: '0x'+verifyTx.gasPrice.toString('hex'),
// gasLimit: '0x'+verifyTx.gasLimit.toString('hex'), // gasLimit: '0x'+verifyTx.gasLimit.toString('hex'),
// }) // })
cb(null, serializedTx) cb(null, serializedTx, tx.hash())
} catch (err) { } catch (err) {
cb(err) cb(err)
} }

View File

@ -33,7 +33,7 @@ function metamaskProvider(opts){
// id mgmt // id mgmt
engine.addProvider(new HookedWalletSubprovider({ engine.addProvider(new HookedWalletSubprovider({
getAccounts: opts.getAccounts, getAccounts: opts.getAccounts,
sendTransaction: opts.sendTransaction, signTransaction: opts.signTransaction,
})) }))
// data source // data source