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

Fix signing of transactions

This commit is contained in:
Frankie 2017-01-04 13:04:33 -08:00
parent fb002dc44e
commit e6da8e2762
3 changed files with 61 additions and 45 deletions

View File

@ -310,20 +310,22 @@ module.exports = class KeyringController extends EventEmitter {
//
// This method signs tx and returns a promise for
// TX Manager to update the state after signing
signTransaction (ethTx, selectedAddress, txId, cb) {
try {
const address = normalize(selectedAddress)
return this.getKeyringForAccount(address)
.then((keyring) => {
return keyring.signTransaction(address, ethTx)
}).then((tx) => {
this.emit(`${txId}:signed`, {tx, txId, cb})
})
} catch (e) {
cb(e)
}
}
signTransaction (ethTx, selectedAddress, txId) {
return new Promise((resolve, reject) => {
try {
const address = normalize(selectedAddress)
return this.getKeyringForAccount(address)
.then((keyring) => {
return keyring.signTransaction(address, ethTx)
}).then((tx) => {
resolve({tx, txId})
})
} catch (e) {
reject(e)
}
})
}
// Add Unconfirmed Message
// @object msgParams
// @function cb

View File

@ -176,10 +176,25 @@ module.exports = class MetamaskController {
},
// tx signing
approveTransaction: this.newUnsignedTransaction.bind(this),
signTransaction: (...args) => {
this.setupSigningListners(...args)
this.txManager.formatTxForSigining(...args)
this.sendUpdate()
signTransaction: (txParams, cb) => {
this.txManager.formatTxForSigining(txParams)
.then(({ethTx, address, txId}) => {
return this.keyringController.signTransaction(ethTx, address, txId)
})
.then(({tx, txId}) => {
return this.txManager.resolveSignedTransaction({tx, txId})
})
.then((rawTx) => {
cb(null, rawTx)
})
.catch((err) => {
console.error(err)
cb(err)
})
.then(() => {
this.sendUpdate()
this.txManager.emit(`${txParams.metamaskId}:signingComplete`)
})
},
// msg signing
@ -257,13 +272,6 @@ module.exports = class MetamaskController {
})
}
setupSigningListners (txParams) {
var txId = txParams.metamaskId
// apply event listeners for signing and formating events
this.txManager.once(`${txId}:formatted`, this.keyringController.signTransaction.bind(this.keyringController))
this.keyringController.once(`${txId}:signed`, this.txManager.resolveSignedTransaction.bind(this.txManager))
}
enforceTxValidations (txParams) {
if (('value' in txParams) && txParams.value.indexOf('-') === 0) {
const msg = `Invalid transaction value of ${txParams.value} not a positive number.`

View File

@ -17,7 +17,7 @@ module.exports = class TransactionManager extends EventEmitter {
this.provider = opts.provider
this.blockTracker = opts.blockTracker
this.txProviderUtils = new TxProviderUtil(this.provider)
this.blockTracker.on('block', this.checkForTxInBlock.bind(this))
// this.blockTracker.on('block', this.checkForTxInBlock.bind(this))
this.getGasMultiplier = opts.getGasMultiplier
this.getNetwork = opts.getNetwork
}
@ -128,7 +128,7 @@ module.exports = class TransactionManager extends EventEmitter {
approveTransaction (txId, cb = warn) {
this.setTxStatusSigned(txId)
cb()
this.once(`${txId}:signingComplete`, cb)
}
cancelTransaction (txId, cb = warn) {
@ -137,25 +137,30 @@ module.exports = class TransactionManager extends EventEmitter {
}
// formats txParams so the keyringController can sign it
formatTxForSigining (txParams, cb) {
var address = txParams.from
var metaTx = this.getTx(txParams.metamaskId)
var gasMultiplier = metaTx.gasMultiplier
var gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice), 16)
gasPrice = gasPrice.mul(new BN(gasMultiplier * 100, 10)).div(new BN(100, 10))
txParams.gasPrice = ethUtil.intToHex(gasPrice.toNumber())
formatTxForSigining (txParams) {
return new Promise((resolve, reject) => {
try {
var address = txParams.from
var metaTx = this.getTx(txParams.metamaskId)
var gasMultiplier = metaTx.gasMultiplier
var gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice), 16)
gasPrice = gasPrice.mul(new BN(gasMultiplier * 100, 10)).div(new BN(100, 10))
txParams.gasPrice = ethUtil.intToHex(gasPrice.toNumber())
// normalize values
txParams.to = normalize(txParams.to)
txParams.from = normalize(txParams.from)
txParams.value = normalize(txParams.value)
txParams.data = normalize(txParams.data)
txParams.gasLimit = normalize(txParams.gasLimit || txParams.gas)
txParams.nonce = normalize(txParams.nonce)
const ethTx = new Transaction(txParams)
// listener is assigned in metamaskController
this.emit(`${txParams.metamaskId}:formatted`, ethTx, address, txParams.metamaskId, cb)
// normalize values
txParams.to = normalize(txParams.to)
txParams.from = normalize(txParams.from)
txParams.value = normalize(txParams.value)
txParams.data = normalize(txParams.data)
txParams.gasLimit = normalize(txParams.gasLimit || txParams.gas)
txParams.nonce = normalize(txParams.nonce)
const ethTx = new Transaction(txParams)
var txId = txParams.metamaskId
resolve({ethTx, address, txId})
} catch (err) {
reject(err)
}
})
}
// receives a signed tx object and updates the tx hash
@ -167,7 +172,8 @@ module.exports = class TransactionManager extends EventEmitter {
metaTx.hash = txHash
this.updateTx(metaTx)
var rawTx = ethUtil.bufferToHex(tx.serialize())
cb(null, rawTx)
return Promise.resolve(rawTx)
}
/*