diff options
author | Frankie <frankie.diamond@gmail.com> | 2017-01-05 05:04:33 +0800 |
---|---|---|
committer | Frankie <frankie.diamond@gmail.com> | 2017-01-05 05:04:33 +0800 |
commit | e6da8e2762cd54975c334314357f1cd27cc980c8 (patch) | |
tree | 995589dbb09b07b0b19e9d956aff49e0c9854bd4 /app | |
parent | fb002dc44eae82e0471b8b52acecc61ba5f39332 (diff) | |
download | tangerine-wallet-browser-e6da8e2762cd54975c334314357f1cd27cc980c8.tar tangerine-wallet-browser-e6da8e2762cd54975c334314357f1cd27cc980c8.tar.gz tangerine-wallet-browser-e6da8e2762cd54975c334314357f1cd27cc980c8.tar.bz2 tangerine-wallet-browser-e6da8e2762cd54975c334314357f1cd27cc980c8.tar.lz tangerine-wallet-browser-e6da8e2762cd54975c334314357f1cd27cc980c8.tar.xz tangerine-wallet-browser-e6da8e2762cd54975c334314357f1cd27cc980c8.tar.zst tangerine-wallet-browser-e6da8e2762cd54975c334314357f1cd27cc980c8.zip |
Fix signing of transactions
Diffstat (limited to 'app')
-rw-r--r-- | app/scripts/keyring-controller.js | 28 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 30 | ||||
-rw-r--r-- | app/scripts/transaction-manager.js | 50 |
3 files changed, 62 insertions, 46 deletions
diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js index 016740d88..acec8feb1 100644 --- a/app/scripts/keyring-controller.js +++ b/app/scripts/keyring-controller.js @@ -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 diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 5df10672a..32e9faab2 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -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.` diff --git a/app/scripts/transaction-manager.js b/app/scripts/transaction-manager.js index fd136a51b..40078d644 100644 --- a/app/scripts/transaction-manager.js +++ b/app/scripts/transaction-manager.js @@ -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()) - - // 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) + 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) + 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) + } /* |