Skip to content

Commit

Permalink
Merge branch '1.x' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
GregTheGreek committed Aug 8, 2020
2 parents 8be9cee + e31a9db commit bc3e6c3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions docs/web3-shh.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
64 changes: 41 additions & 23 deletions packages/web3-core-requestmanager/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bc3e6c3

Please sign in to comment.