From 80c2834b326dd39821d575ea6d303c3802aa6d55 Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Fri, 7 Aug 2020 08:55:00 -0700 Subject: [PATCH 1/2] Improve RequestManager send method (#3649) * improve send method --- CHANGELOG.md | 1 + .../web3-core-requestmanager/src/index.js | 64 ++++++++++++------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0938f9ca488..fa85c9c22dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -274,6 +274,7 @@ Released with 1.0.0-beta.37 code base. ### Changed +- Improve RequestManager send method (#3649) - `npm run build` now uses TSC to compile (.js allowed) and the build folder is now located under `lib` (#3652) - Modernized web3-core to use newer es syntax (#3652) - Bump lodash from 4.17.15 to 4.17.19 diff --git a/packages/web3-core-requestmanager/src/index.js b/packages/web3-core-requestmanager/src/index.js index 9b3c591d077..74f3a79092c 100644 --- a/packages/web3-core-requestmanager/src/index.js +++ b/packages/web3-core-requestmanager/src/index.js @@ -157,34 +157,19 @@ RequestManager.prototype.send = function (data, callback) { return callback(errors.InvalidProvider()); } - const payload = Jsonrpc.toPayload(data.method, data.params); + const { method, params } = data - const onJsonrpcResult = function (err, result) { - if(result && result.id && payload.id !== result.id) { - return callback(new Error(`Wrong response id ${result.id} (expected: ${payload.id}) in ${JSON.stringify(payload)}`)); - } - - if (err) { - return callback(err); - } - - if (result && result.error) { - return callback(errors.ErrorResponse(result)); - } - - if (!Jsonrpc.isValidResponse(result)) { - return callback(errors.InvalidResponse(result)); - } - - callback(null, result.result); - }; + const jsonrpcPayload = Jsonrpc.toPayload(method, params); + const jsonrpcResultCallback = this._jsonrpcResultCallback(callback, jsonrpcPayload) if (this.provider.request) { - callbackify(this.provider.request.bind(this.provider))(payload, callback); + const callbackRequest = callbackify(this.provider.request) + const requestArgs = { method, params } + callbackRequest(requestArgs, callback); } else if (this.provider.sendAsync) { - this.provider.sendAsync(payload, onJsonrpcResult); + this.provider.sendAsync(jsonrpcPayload, jsonrpcResultCallback); } else if (this.provider.send) { - this.provider.send(payload, onJsonrpcResult); + this.provider.send(jsonrpcPayload, jsonrpcResultCallback); } else { throw new Error('Provider does not have a request or send method to use.'); } @@ -315,6 +300,39 @@ RequestManager.prototype._isIpcCloseError = function (event) { return typeof event === 'boolean' && event; }; +/** + * The jsonrpc result callback for RequestManager.send + * + * @method _jsonrpcResultCallback + * + * @param {Function} callback the callback to use + * @param {Object} payload the jsonrpc payload + * + * @returns {Function} return callback of form (err, result) + * + */ +RequestManager.prototype._jsonrpcResultCallback = function (callback, payload) { + return function(err, result) { + if(result && result.id && payload.id !== result.id) { + return callback(new Error(`Wrong response id ${result.id} (expected: ${payload.id}) in ${JSON.stringify(payload)}`)); + } + + if (err) { + return callback(err); + } + + if (result && result.error) { + return callback(errors.ErrorResponse(result)); + } + + if (!Jsonrpc.isValidResponse(result)) { + return callback(errors.InvalidResponse(result)); + } + + callback(null, result.result); + } +}; + module.exports = { Manager: RequestManager, BatchManager: BatchManager From e31a9db6176cac0709b0a58a1e9939c112c50e9b Mon Sep 17 00:00:00 2001 From: daoauth <57783762+daoauth@users.noreply.github.com> Date: Sun, 9 Aug 2020 04:41:25 +0900 Subject: [PATCH 2/2] web3.shh.post example update (#3668) * web3.shh.post example update var identities = []; -> var identities = {}; * Update web3-shh.rst Co-authored-by: Gregory Markou <16929357+GregTheGreek@users.noreply.github.com> --- docs/web3-shh.rst | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/web3-shh.rst b/docs/web3-shh.rst index ddacc1026e2..bb74e34e511 100644 --- a/docs/web3-shh.rst +++ b/docs/web3-shh.rst @@ -759,25 +759,25 @@ Example .. code-block:: javascript - var identities = []; + var identities = {}; var subscription = null; Promise.all([ - web3.shh.newSymKey().then((id) => {identities.push(id);}), - web3.shh.newKeyPair().then((id) => {identities.push(id);}) + web3.shh.newSymKey().then((id) => {identities.symKey = id;}), + web3.shh.newKeyPair().then((id) => {identities.keyPair = id;}) ]).then(() => { // will receive also its own message send, below - subscription = shh.subscribe("messages", { - symKeyID: identities[0], + subscription = web3.shh.subscribe("messages", { + symKeyID: identities.symKey, topics: ['0xffaadd11'] }).on('data', console.log); }).then(() => { web3.shh.post({ - symKeyID: identities[0], // encrypts using the sym key ID - sig: identities[1], // signs the message using the keyPair ID + symKeyID: identities.symKey, // encrypts using the sym key ID + sig: identities.keyPair, // signs the message using the keyPair ID ttl: 10, topic: '0xffaadd11', payload: '0xffffffdddddd1122',