From bc7b7846d3cf4e0c884ce398a0abf072d7046565 Mon Sep 17 00:00:00 2001 From: "Carlos C. Tapang" Date: Wed, 11 Nov 2020 00:18:04 -0800 Subject: [PATCH 1/7] Fixed call to signTransaction --- subproviders/hooked-wallet.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/subproviders/hooked-wallet.js b/subproviders/hooked-wallet.js index 67cc430f..a1903775 100644 --- a/subproviders/hooked-wallet.js +++ b/subproviders/hooked-wallet.js @@ -562,6 +562,15 @@ HookedWalletSubprovider.prototype.validateSender = function(senderAddress, cb){ // // tx helpers // +const callb = (error, result) => { + if (!!error) { + console.error(error) + return + } + if (!!result) { + console.log(result) + } +} HookedWalletSubprovider.prototype.finalizeAndSubmitTx = function(txParams, cb) { const self = this @@ -570,7 +579,7 @@ HookedWalletSubprovider.prototype.finalizeAndSubmitTx = function(txParams, cb) { self.nonceLock.take(function(){ waterfall([ self.fillInTxExtras.bind(self, txParams), - self.signTransaction.bind(self), + self.signTransaction.bind(self, txParams, callb), self.publishTransaction.bind(self), ], function(err, txHash){ self.nonceLock.leave() @@ -587,7 +596,7 @@ HookedWalletSubprovider.prototype.finalizeTx = function(txParams, cb) { self.nonceLock.take(function(){ waterfall([ self.fillInTxExtras.bind(self, txParams), - self.signTransaction.bind(self), + self.signTransaction.bind(self, txParams, callb), ], function(err, signedTx){ self.nonceLock.leave() if (err) return cb(err) From 7ca9ff7c03678875b633324960bbdfc00a0a56c5 Mon Sep 17 00:00:00 2001 From: "Carlos C. Tapang" Date: Thu, 12 Nov 2020 00:01:06 -0800 Subject: [PATCH 2/7] Changes for publishing to GitHub registry --- package.json | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 74e93aac..48d54bbb 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,11 @@ { - "name": "@trufflesuite/web3-provider-engine", - "version": "15.0.13-1", + "name": "@puremoney/web3-provider-engine", + "version": "15.0.13-x01", "description": "Fork of https://github.com/MetaMask/web3-provider-engine", - "repository": "https://github.com/trufflesuite/provider-engine", + "repository": { + "type": "git", + "url": "https://github.com/puremoney/provider-engine" + }, "main": "index.js", "scripts": { "test": "node test/index.js && yarn lint", @@ -55,6 +58,7 @@ "ws": false }, "publishConfig": { - "access": "public" + "access": "public", + "registry": "https://npm.pkg.github.com/" } } From 8cc7905d7cc2c343c92551564cac322392d3b2dc Mon Sep 17 00:00:00 2001 From: "Carlos C. Tapang" Date: Thu, 12 Nov 2020 00:05:13 -0800 Subject: [PATCH 3/7] Bumped version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 48d54bbb..2f04ced3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@puremoney/web3-provider-engine", - "version": "15.0.13-x01", + "version": "15.0.13-x02", "description": "Fork of https://github.com/MetaMask/web3-provider-engine", "repository": { "type": "git", From 33eeeb13fc9887e3b3451d57cdee812185aacf5b Mon Sep 17 00:00:00 2001 From: "Carlos C. Tapang" Date: Mon, 16 Nov 2020 21:55:32 -0800 Subject: [PATCH 4/7] Fixed hooked-wallet.js, use es6-promisify and bluebird --- package.json | 3 +- subproviders/hooked-wallet.js | 147 ++++++++++++++++++---------------- 2 files changed, 79 insertions(+), 71 deletions(-) diff --git a/package.json b/package.json index 2f04ced3..098a0ca8 100644 --- a/package.json +++ b/package.json @@ -25,8 +25,10 @@ "@trufflesuite/eth-sig-util": "^1.4.2", "async": "^2.5.0", "backoff": "^2.5.0", + "bluebird": "^3.7.2", "clone": "^2.0.0", "cross-fetch": "^2.1.0", + "es6-promisify": "^6.1.1", "eth-block-tracker": "^4.4.2", "eth-json-rpc-errors": "^2.0.2", "ethereumjs-block": "^1.2.2", @@ -34,7 +36,6 @@ "ethereumjs-util": "^5.1.5", "ethereumjs-vm": "^2.3.4", "json-stable-stringify": "^1.0.1", - "promise-to-callback": "^1.0.0", "readable-stream": "^2.2.9", "request": "^2.85.0", "semaphore": "^1.0.3", diff --git a/subproviders/hooked-wallet.js b/subproviders/hooked-wallet.js index a1903775..c571b879 100644 --- a/subproviders/hooked-wallet.js +++ b/subproviders/hooked-wallet.js @@ -5,9 +5,8 @@ * - getAccounts() -- array of addresses supported * - signTransaction(tx) -- sign a raw transaction object */ - +const {promisify} = require("es6-promisify") const waterfall = require('async/waterfall') -const parallel = require('async/parallel') const inherits = require('util').inherits const ethUtil = require('ethereumjs-util') const sigUtil = require('@trufflesuite/eth-sig-util') @@ -15,6 +14,8 @@ const extend = require('xtend') const Semaphore = require('semaphore') const Subprovider = require('./subprovider.js') const estimateGas = require('../util/estimate-gas.js') +const { map } = require("bluebird") +const Bluebird = require("bluebird") const hexRegex = /^[0-9A-Fa-f]+$/g module.exports = HookedWalletSubprovider @@ -50,7 +51,9 @@ module.exports = HookedWalletSubprovider // signTransaction (perform the signature) // publishTransaction (publish signed tx to network) // - + +// Now uses Bluebird +promisify.Promise = require("bluebird") inherits(HookedWalletSubprovider, Subprovider) @@ -116,6 +119,10 @@ HookedWalletSubprovider.prototype.handleRequest = function(payload, next, end){ end(null, accounts) }) return + + case 'eth_sendRawTransaction': + txParams = payload.params[0] + return case 'eth_sendTransaction': txParams = payload.params[0] @@ -277,8 +284,8 @@ HookedWalletSubprovider.prototype.handleRequest = function(payload, next, end){ data: message, }) waterfall([ - (cb) => self.validateTypedMessage(msgParams, cb), - (cb) => self.processTypedMessage(msgParams, cb), + (callb) => self.validateTypedMessage(msgParams, callb), + (callb) => self.processTypedMessage(msgParams, callb), ], end) })() @@ -330,9 +337,9 @@ HookedWalletSubprovider.prototype.getAccounts = function(cb) { HookedWalletSubprovider.prototype.processTransaction = function(txParams, cb) { const self = this waterfall([ - (cb) => self.approveTransaction(txParams, cb), - (didApprove, cb) => self.checkApproval('transaction', didApprove, cb), - (cb) => self.finalizeAndSubmitTx(txParams, cb), + (callb) => self.approveTransaction(txParams, callb), + (didApprove, callb) => self.checkApproval('transaction', didApprove, callb), + (callb) => self.finalizeAndSubmitTx(txParams, callb), ], cb) } @@ -340,54 +347,54 @@ HookedWalletSubprovider.prototype.processTransaction = function(txParams, cb) { HookedWalletSubprovider.prototype.processSignTransaction = function(txParams, cb) { const self = this waterfall([ - (cb) => self.approveTransaction(txParams, cb), - (didApprove, cb) => self.checkApproval('transaction', didApprove, cb), - (cb) => self.finalizeTx(txParams, cb), + (callb) => self.approveTransaction(txParams, callb), + (didApprove, callb) => self.checkApproval('transaction', didApprove, callb), + (callb) => self.finalizeTx(txParams, callb), ], cb) } HookedWalletSubprovider.prototype.processMessage = function(msgParams, cb) { const self = this waterfall([ - (cb) => self.approveMessage(msgParams, cb), - (didApprove, cb) => self.checkApproval('message', didApprove, cb), - (cb) => self.signMessage(msgParams, cb), + (callb) => self.approveMessage(msgParams, callb), + (didApprove, callb) => self.checkApproval('message', didApprove, callb), + (callb) => self.signMessage(msgParams, callb), ], cb) } HookedWalletSubprovider.prototype.processPersonalMessage = function(msgParams, cb) { const self = this waterfall([ - (cb) => self.approvePersonalMessage(msgParams, cb), - (didApprove, cb) => self.checkApproval('message', didApprove, cb), - (cb) => self.signPersonalMessage(msgParams, cb), + (callb) => self.approvePersonalMessage(msgParams, callb), + (didApprove, callb) => self.checkApproval('message', didApprove, callb), + (callb) => self.signPersonalMessage(msgParams, callb), ], cb) } HookedWalletSubprovider.prototype.processDecryptMessage = function(msgParams, cb) { const self = this waterfall([ - (cb) => self.approveDecryptMessage(msgParams, cb), - (didApprove, cb) => self.checkApproval('decryptMessage', didApprove, cb), - (cb) => self.decryptMessage(msgParams, cb), + (callb) => self.approveDecryptMessage(msgParams, callb), + (didApprove, callb) => self.checkApproval('decryptMessage', didApprove, callb), + (callb) => self.decryptMessage(msgParams, callb), ], cb) } HookedWalletSubprovider.prototype.processEncryptionPublicKey = function(msgParams, cb) { const self = this waterfall([ - (cb) => self.approveEncryptionPublicKey(msgParams, cb), - (didApprove, cb) => self.checkApproval('encryptionPublicKey', didApprove, cb), - (cb) => self.encryptionPublicKey(msgParams, cb), + (callb) => self.approveEncryptionPublicKey(msgParams, callb), + (didApprove, callb) => self.checkApproval('encryptionPublicKey', didApprove, callb), + (callb) => self.encryptionPublicKey(msgParams, callb), ], cb) } HookedWalletSubprovider.prototype.processTypedMessage = function(msgParams, cb) { const self = this waterfall([ - (cb) => self.approveTypedMessage(msgParams, cb), - (didApprove, cb) => self.checkApproval('message', didApprove, cb), - (cb) => self.signTypedMessage(msgParams, cb), + (callb) => self.approveTypedMessage(msgParams, callb), + (didApprove, callb) => self.checkApproval('message', didApprove, callb), + (callb) => self.signTypedMessage(msgParams, callb), ], cb) } @@ -400,7 +407,7 @@ HookedWalletSubprovider.prototype.autoApprove = function(txParams, cb) { } HookedWalletSubprovider.prototype.checkApproval = function(type, didApprove, cb) { - cb( didApprove ? null : new Error('User denied '+type+' signature.') ) + cb( didApprove ? null : new Error('User denied '+type+' signature.')) } // @@ -562,25 +569,17 @@ HookedWalletSubprovider.prototype.validateSender = function(senderAddress, cb){ // // tx helpers // -const callb = (error, result) => { - if (!!error) { - console.error(error) - return - } - if (!!result) { - console.log(result) - } -} HookedWalletSubprovider.prototype.finalizeAndSubmitTx = function(txParams, cb) { + console.log('finalizeAndSubmitTx') const self = this // can only allow one tx to pass through this flow at a time // so we can atomically consume a nonce self.nonceLock.take(function(){ waterfall([ - self.fillInTxExtras.bind(self, txParams), - self.signTransaction.bind(self, txParams, callb), - self.publishTransaction.bind(self), + (callb) => self.fillInTxExtras(txParams, callb), + (params, callb) => self.signTransaction(params, callb), + (params, callb) => self.publishTransaction(params, callb), ], function(err, txHash){ self.nonceLock.leave() if (err) return cb(err) @@ -590,13 +589,14 @@ HookedWalletSubprovider.prototype.finalizeAndSubmitTx = function(txParams, cb) { } HookedWalletSubprovider.prototype.finalizeTx = function(txParams, cb) { + console.log('finalizeTx') const self = this // can only allow one tx to pass through this flow at a time // so we can atomically consume a nonce self.nonceLock.take(function(){ waterfall([ - self.fillInTxExtras.bind(self, txParams), - self.signTransaction.bind(self, txParams, callb), + (callb) => self.fillInTxExtras(txParams, callb), + (params, callb) => self.signTransaction(params, callb), ], function(err, signedTx){ self.nonceLock.leave() if (err) return cb(err) @@ -606,6 +606,7 @@ HookedWalletSubprovider.prototype.finalizeTx = function(txParams, cb) { } HookedWalletSubprovider.prototype.publishTransaction = function(rawTx, cb) { + console.log('publishTransaction') const self = this self.emitPayload({ method: 'eth_sendRawTransaction', @@ -630,37 +631,43 @@ HookedWalletSubprovider.prototype.getGasPrice = function(cb) { } HookedWalletSubprovider.prototype.fillInTxExtras = function(txParams, cb){ + console.log('fillInTxExtras') const self = this - const address = txParams.from - // console.log('fillInTxExtras - address:', address) - - const tasks = {} - - if (txParams.gasPrice === undefined) { - // console.log("need to get gasprice") - tasks.gasPrice = self.getGasPrice.bind(self) - } - - if (txParams.nonce === undefined) { - // console.log("need to get nonce") - tasks.nonce = self.emitPayload.bind(self, { method: 'eth_getTransactionCount', params: [address, 'pending'] }) - } - - if (txParams.gas === undefined) { - // console.log("need to get gas") - tasks.gas = self.estimateGas.bind(self, cloneTxParams(txParams)) - } - - parallel(tasks, function(err, taskResults) { - if (err) return cb(err) - - const result = {} - if (taskResults.gasPrice) result.gasPrice = taskResults.gasPrice - if (taskResults.nonce) result.nonce = taskResults.nonce.result - if (taskResults.gas) result.gas = taskResults.gas + const {from, gasPrice, nonce, gas} = txParams + + let ta = new Array(3) + ta.push(!gasPrice ? + promisify((cb) => self.getGasPrice(cb)) : + Promise.resolve(gasPrice)) + ta.push(!nonce ? + promisify((cb) => self.emitPayload({ method: 'eth_getTransactionCount', + params: [from, 'pending'] }, cb)) : + Promise.resolve(nonce)) + ta.push(!gas ? + promisify((cb) => self.estimateGas(cloneTxParams(txParams), cb)) : + Promise.resolve(gas)) + + // do it all in parallel + Promise.all(ta).then( + (result) => { + result.map((p, i) => { + switch(i) { + case 0: txParams.gasPrice = p + break + case 1: txParams.nonce = p + break + case 2: txParams.gas = p + break + default: + } + }) + cb(null, txParams) + }, + (reason) => { + cb(reason) + } + ) - cb(null, extend(txParams, result)) - }) } // util From 06681bac9bf68350218cc10fdf74a49d1a9aa229 Mon Sep 17 00:00:00 2001 From: "Carlos C. Tapang" Date: Tue, 17 Nov 2020 19:57:39 -0800 Subject: [PATCH 5/7] Bumped version, modified 'bundle' script --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 098a0ca8..3b83f400 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@puremoney/web3-provider-engine", - "version": "15.0.13-x02", + "version": "15.0.13-x03", "description": "Fork of https://github.com/MetaMask/web3-provider-engine", "repository": { "type": "git", @@ -11,7 +11,7 @@ "test": "node test/index.js && yarn lint", "prepublishOnly": "yarn build && yarn bundle", "build": "babel zero.js index.js -d dist/es5 && babel subproviders -d dist/es5/subproviders && babel util -d dist/es5/util", - "bundle": "mkdir -p ./dist && yarn bundle-engine && yarn bundle-zero", + "bundle": "yarn bundle-engine && yarn bundle-zero", "bundle-zero": "browserify -s ZeroClientProvider -e zero.js -t [ babelify --presets [ @babel/preset-env ] ] > dist/ZeroClientProvider.js", "bundle-engine": "browserify -s ProviderEngine -e index.js -t [ babelify --presets [ @babel/preset-env ] ] > dist/ProviderEngine.js", "lint": "eslint --quiet --ignore-path .gitignore ." From 0aae4a60bbaa8bf2a29442b025c6b9df876edfdb Mon Sep 17 00:00:00 2001 From: "Carlos C. Tapang" Date: Wed, 18 Nov 2020 08:23:32 -0800 Subject: [PATCH 6/7] Fixed hooked-wallet.js --- subproviders/hooked-wallet.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/subproviders/hooked-wallet.js b/subproviders/hooked-wallet.js index c571b879..cd56f225 100644 --- a/subproviders/hooked-wallet.js +++ b/subproviders/hooked-wallet.js @@ -14,8 +14,6 @@ const extend = require('xtend') const Semaphore = require('semaphore') const Subprovider = require('./subprovider.js') const estimateGas = require('../util/estimate-gas.js') -const { map } = require("bluebird") -const Bluebird = require("bluebird") const hexRegex = /^[0-9A-Fa-f]+$/g module.exports = HookedWalletSubprovider @@ -119,10 +117,6 @@ HookedWalletSubprovider.prototype.handleRequest = function(payload, next, end){ end(null, accounts) }) return - - case 'eth_sendRawTransaction': - txParams = payload.params[0] - return case 'eth_sendTransaction': txParams = payload.params[0] @@ -571,7 +565,6 @@ HookedWalletSubprovider.prototype.validateSender = function(senderAddress, cb){ // HookedWalletSubprovider.prototype.finalizeAndSubmitTx = function(txParams, cb) { - console.log('finalizeAndSubmitTx') const self = this // can only allow one tx to pass through this flow at a time // so we can atomically consume a nonce @@ -589,7 +582,6 @@ HookedWalletSubprovider.prototype.finalizeAndSubmitTx = function(txParams, cb) { } HookedWalletSubprovider.prototype.finalizeTx = function(txParams, cb) { - console.log('finalizeTx') const self = this // can only allow one tx to pass through this flow at a time // so we can atomically consume a nonce @@ -606,7 +598,6 @@ HookedWalletSubprovider.prototype.finalizeTx = function(txParams, cb) { } HookedWalletSubprovider.prototype.publishTransaction = function(rawTx, cb) { - console.log('publishTransaction') const self = this self.emitPayload({ method: 'eth_sendRawTransaction', @@ -631,10 +622,14 @@ HookedWalletSubprovider.prototype.getGasPrice = function(cb) { } HookedWalletSubprovider.prototype.fillInTxExtras = function(txParams, cb){ - console.log('fillInTxExtras') const self = this const {from, gasPrice, nonce, gas} = txParams + if (gasPrice && nonce && gas) { + cb(null, txParams) + return + } + let ta = new Array(3) ta.push(!gasPrice ? promisify((cb) => self.getGasPrice(cb)) : @@ -652,11 +647,11 @@ HookedWalletSubprovider.prototype.fillInTxExtras = function(txParams, cb){ (result) => { result.map((p, i) => { switch(i) { - case 0: txParams.gasPrice = p + case 0: txParams.gasPrice = !txParams.gasPrice ? p : txParams.gasPrice break - case 1: txParams.nonce = p + case 1: txParams.nonce = !txParams.nonce ? p : txParams.nonce break - case 2: txParams.gas = p + case 2: txParams.gas = !txParams.gas ? p : txParams.gas break default: } From 30564ecc68f28996f49d2d5dd8f15ba90e2c01c6 Mon Sep 17 00:00:00 2001 From: "Carlos C. Tapang" Date: Wed, 18 Nov 2020 08:31:36 -0800 Subject: [PATCH 7/7] Bumped version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3b83f400..f7a33bb1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@puremoney/web3-provider-engine", - "version": "15.0.13-x03", + "version": "15.0.13-x04", "description": "Fork of https://github.com/MetaMask/web3-provider-engine", "repository": { "type": "git",