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 // This method signs tx and returns a promise for
// TX Manager to update the state after signing // 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 // Add Unconfirmed Message
// @object msgParams // @object msgParams
// @function cb // @function cb

View File

@ -176,10 +176,25 @@ module.exports = class MetamaskController {
}, },
// tx signing // tx signing
approveTransaction: this.newUnsignedTransaction.bind(this), approveTransaction: this.newUnsignedTransaction.bind(this),
signTransaction: (...args) => { signTransaction: (txParams, cb) => {
this.setupSigningListners(...args) this.txManager.formatTxForSigining(txParams)
this.txManager.formatTxForSigining(...args) .then(({ethTx, address, txId}) => {
this.sendUpdate() 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 // 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) { enforceTxValidations (txParams) {
if (('value' in txParams) && txParams.value.indexOf('-') === 0) { if (('value' in txParams) && txParams.value.indexOf('-') === 0) {
const msg = `Invalid transaction value of ${txParams.value} not a positive number.` 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.provider = opts.provider
this.blockTracker = opts.blockTracker this.blockTracker = opts.blockTracker
this.txProviderUtils = new TxProviderUtil(this.provider) 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.getGasMultiplier = opts.getGasMultiplier
this.getNetwork = opts.getNetwork this.getNetwork = opts.getNetwork
} }
@ -128,7 +128,7 @@ module.exports = class TransactionManager extends EventEmitter {
approveTransaction (txId, cb = warn) { approveTransaction (txId, cb = warn) {
this.setTxStatusSigned(txId) this.setTxStatusSigned(txId)
cb() this.once(`${txId}:signingComplete`, cb)
} }
cancelTransaction (txId, cb = warn) { cancelTransaction (txId, cb = warn) {
@ -137,25 +137,30 @@ module.exports = class TransactionManager extends EventEmitter {
} }
// formats txParams so the keyringController can sign it // formats txParams so the keyringController can sign it
formatTxForSigining (txParams, cb) { formatTxForSigining (txParams) {
var address = txParams.from return new Promise((resolve, reject) => {
var metaTx = this.getTx(txParams.metamaskId) try {
var gasMultiplier = metaTx.gasMultiplier var address = txParams.from
var gasPrice = new BN(ethUtil.stripHexPrefix(txParams.gasPrice), 16) var metaTx = this.getTx(txParams.metamaskId)
gasPrice = gasPrice.mul(new BN(gasMultiplier * 100, 10)).div(new BN(100, 10)) var gasMultiplier = metaTx.gasMultiplier
txParams.gasPrice = ethUtil.intToHex(gasPrice.toNumber()) 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 // normalize values
txParams.to = normalize(txParams.to) txParams.to = normalize(txParams.to)
txParams.from = normalize(txParams.from) txParams.from = normalize(txParams.from)
txParams.value = normalize(txParams.value) txParams.value = normalize(txParams.value)
txParams.data = normalize(txParams.data) txParams.data = normalize(txParams.data)
txParams.gasLimit = normalize(txParams.gasLimit || txParams.gas) txParams.gasLimit = normalize(txParams.gasLimit || txParams.gas)
txParams.nonce = normalize(txParams.nonce) txParams.nonce = normalize(txParams.nonce)
const ethTx = new Transaction(txParams) const ethTx = new Transaction(txParams)
var txId = txParams.metamaskId
// listener is assigned in metamaskController resolve({ethTx, address, txId})
this.emit(`${txParams.metamaskId}:formatted`, ethTx, address, txParams.metamaskId, cb) } catch (err) {
reject(err)
}
})
} }
// receives a signed tx object and updates the tx hash // receives a signed tx object and updates the tx hash
@ -167,7 +172,8 @@ module.exports = class TransactionManager extends EventEmitter {
metaTx.hash = txHash metaTx.hash = txHash
this.updateTx(metaTx) this.updateTx(metaTx)
var rawTx = ethUtil.bufferToHex(tx.serialize()) var rawTx = ethUtil.bufferToHex(tx.serialize())
cb(null, rawTx) return Promise.resolve(rawTx)
} }
/* /*