Trying to get Parity’s whisper to work with EmbarkJS

This commit is contained in:
emizzle 2019-11-27 18:57:57 +11:00
parent 3416b051d0
commit 4edb3192ab
No known key found for this signature in database
GPG Key ID: 1FD4BAB3C37EE9BA
23 changed files with 492 additions and 18 deletions

View File

@ -4,7 +4,7 @@ module.exports = {
// default applies to all environments
default: {
enabled: true,
client: "geth" // Can be geth or parity (default:geth)
client: "parity" // Can be geth or parity (default:geth)
},
development: {

View File

@ -11,7 +11,7 @@ module.exports = {
development: {
connection: {
host: "localhost", // Host of the blockchain node
port: 8557, // Port of the blockchain node
port: 8556, // Port of the blockchain node
type: "ws" // Type of connection (ws or rpc)
}
},
@ -36,11 +36,11 @@ module.exports = {
// "embark run custom_name"
//custom_name: {
//}
// Use this section when you need a specific symmetric or private keys in whisper
/*
,keys: {
symmetricKey: "your_symmetric_key",// Symmetric key for message decryption
privateKey: "your_private_key" // Private Key to be used as a signing key and for message decryption
}
*/
// Use this section when you need a specific symmetric or private keys in whisper
/*
,keys: {
symmetricKey: "your_symmetric_key",// Symmetric key for message decryption
privateKey: "your_private_key" // Private Key to be used as a signing key and for message decryption
}
*/
};

View File

@ -0,0 +1,64 @@
{
"name": "embarkjs-whisper-parity",
"version": "5.0.0-alpha.1",
"author": "Iuri Matias <iuri.matias@gmail.com>",
"contributors": [],
"description": "Whisper plugin for embarkjs, for use with Parity's version of Whisper",
"homepage": "https://github.com/embark-framework/embark/tree/master/packages/embarkjs/whisper-parity#readme",
"bugs": "https://github.com/embark-framework/embark/issues",
"keywords": [
"blockchain",
"dapps",
"ethereum",
"ipfs",
"serverless",
"solc",
"solidity"
],
"license": "MIT",
"repository": {
"directory": "packages/embarkjs/whisper-parity",
"type": "git",
"url": "https://github.com/embark-framework/embark.git"
},
"main": "dist/node/index.js",
"browser": "./dist/browser/index.js",
"browserslist": [
"last 1 version",
"not dead",
"> 0.2%"
],
"files": [
"dist"
],
"embark-collective": {
"build:browser": true,
"build:node": true
},
"scripts": {
"_build": "npm run solo -- build",
"ci": "npm run qa",
"clean": "npm run reset",
"qa": "npm-run-all _build",
"reset": "npx rimraf coverage dist embarkjs-*.tgz package",
"solo": "embark-solo"
},
"dependencies": {
"@babel/runtime-corejs3": "7.7.4",
"core-js": "3.4.3",
"rxjs": "6.4.0",
"web3": "1.2.1"
},
"devDependencies": {
"ajv": "6.10.2",
"embark-solo": "^5.0.0-alpha.0",
"lodash.clonedeep": "4.5.0",
"npm-run-all": "4.1.5",
"rimraf": "3.0.0"
},
"engines": {
"node": ">=10.17.0 <12.0.0",
"npm": ">=6.11.3",
"yarn": ">=1.19.1"
}
}

View File

@ -0,0 +1,114 @@
const { fromEvent, merge, throwError } = require('rxjs');
const { map, mergeMap } = require('rxjs/operators');
function sendMessage(options, callback) {
let topics = options.topic ? [options.topic] : [];
const data = options.data;
const ttl = options.ttl || 100;
const powTime = options.powTime || 3;
// const powTarget = options.powTarget || 0.5;
// const sig = options.sig;
// const fromAscii = options.fromAscii;
// const toHex = options.toHex;
// const symKeyID = options.symKeyID;
const asymKeyID = options.asymKeyID;
const post = options.post;
// if (topics) {
// // TODO: determine if we need to limit to 10 topics
// topics = toHex(topics).slice(0, 10);
// }
let message = {
// to: // Object The receiver of the message.Can be omitted for a broadcast message.Use one of the following two fields
// {
// public: Data - 64 bytes - The public key of the recipient
// identity: Data - 32 bytes - The identity of the recipient key on your local node.
// }
"from": null, //asymKeyID, // Data - 32 bytes - asymmetric identity to sign the message with, or null.
"topics": topics, // [Data] - Array of topics for the message.Should be non - empty.
"payload": JSON.stringify(data), // Data - Message data
// padding: // Data - Optional padding.Up to 2 ^ 24 - 1 bytes.
"priority": powTime, // Quantity - How many milliseconds to spend doing PoW.
"ttl": ttl // Quantity - Time to live(in seconds) of the message before expiry
};
// TODO: determine if we need to use keys other than the asymmetric key generated for us
// if (options.pubKey) {
// message.pubKey = options.pubKey; // encrypt using a given pubKey
// } else if (options.symKeyID) {
// message.symKeyID = options.symKeyID; // encrypts using given sym key ID
// } else {
// message.symKeyID = symKeyID; // encrypts using the sym key ID
// }
// TODO: determine if we need these checks
// if (topics === undefined && message.symKeyID && !message.pubKey) {
// callback("missing option: topic");
// } else {
post([message], callback);
// }
}
function listenTo(options) {
let topics = options.topic;
const toAscii = options.toAscii;
const toHex = options.toHex;
// const sig = options.sig;
const subscribe = options.subscribe;
const asymKeyID = options.asymKeyID;
// const symKeyID = options.symKeyID;
// let subOptions = {};
// TODO: determine if we need to limit to 10 topics
if (topics) {
if (typeof topics === 'string') {
topics = [toHex(topics).slice(0, 10)];
} else {
topics = topics.map((t) => toHex(t).slice(0, 10));
}
}
const filterRequest = {
"decryptWith": null, // Data - 32 bytes - Identity of key used for description. null if listening for broadcasts.
"from": null, // asymKeyID, // Data - 64 bytes - if present, only accept messages signed by this key.
"topics": topics // [Data] - Only accept messages matching these topics. Should be non-empty.
};
// TODO: enable using private key. what does this do?
// if (options.usePrivateKey === true) {
// filterRequest.decryptWith = options.privateKeyID || sig;
// } else {
// subOptions.symKeyID = symKeyID;
// }
const emitter = subscribe('messages', filterRequest, (error, message, subscription) => {
console.dir(error);
console.dir(message);
console.dir(subscription);
});
const obsData = fromEvent(emitter, 'data').pipe(map(result => ({
data: JSON.parse(toAscii(result.payload)),
payload: result.payload,
recipientPublicKey: result.recipientPublicKey,
result,
sig: result.sig,
time: result.timestamp,
topic: toAscii(result.topic)
})));
const obsErr = fromEvent(emitter, 'error').pipe(mergeMap(throwError));
const obsSub = merge(obsData, obsErr);
obsSub.shhSubscription = emitter;
return obsSub;
}
export default {
sendMessage,
listenTo
};

View File

@ -0,0 +1,125 @@
/* global EmbarkJS Web3 listenTo sendMessage */
let Web3 = require('web3');
let { Manager } = require('web3-core-requestmanager');
const { sendMessage, listenTo } = require('./communicationFunctions').default;
// for the whisper v5 and web3.js 1.x
let __embarkWhisperNewWeb3 = {};
__embarkWhisperNewWeb3.real_sendMessage = sendMessage;
__embarkWhisperNewWeb3.real_listenTo = listenTo;
__embarkWhisperNewWeb3.setProvider = function (options) {
const self = this;
let endpoint;
if (options === undefined) {
endpoint = "localhost:8546";
} else {
endpoint = options.server + ':' + options.port;
}
// TODO: take into account type
const provider = new Web3.providers.WebsocketProvider("ws://" + endpoint, options.providerOptions);
self.web3 = new Web3(provider);
self.requestManager = new Manager(provider);
self.web3.currentProvider.on('connect', () => {
self.getWhisperVersion(function (err, version) {
if (err) {
console.log("whisper not available");
} else if (version >= 5) {
self.web3.shh.newSymKey().then((id) => {
self.symKeyID = id; // symmetric encryption key id
});
self.web3.shh.newKeyPair().then((id) => {
self.asymKeyID = id; // asymmetric encryption key id
});
} else {
throw new Error("version of whisper not supported");
}
self.whisperVersion = self.web3.version.whisper;
});
});
self.web3.currentProvider.on('error', () => {
console.log("whisper not available");
});
};
__embarkWhisperNewWeb3.sendMessage = function (options) {
const data = options.data || options.payload;
if (!data) {
throw new Error("missing option: data");
}
Object.assign(options, {
asymKeyID: this.asymKeyID,
fromAscii: this.web3.utils.fromAscii,
toHex: this.web3.utils.toHex,
// symKeyID: options.symKeyID || this.symKeyID,
post: this.web3.shh.post,
data
});
return this.real_sendMessage(options, (err, result) => {
if (err) {
return console.log("====> ERR: " + err);
}
console.log("====> RESULT: " + result);
});
};
__embarkWhisperNewWeb3.listenTo = function (options) {
console.log("=====> PARITY");
Object.assign(options, {
toAscii: this.web3.utils.toAscii,
toHex: this.web3.utils.toHex,
// sig: this.sig,
asymKeyID: this.asymKeyID,
subscribe: this.web3.shh.subscribe
// symKeyID: options.symKeyID || this.symKeyID
});
return this.real_listenTo(options);
};
__embarkWhisperNewWeb3.getWhisperVersion = function (cb) {
// 1) Parity does not implement shh_version JSON-RPC method
// 2) web3 1.0 still does not implement web3_clientVersion
// so we must do all by our own
const self = this;
self.requestManager.send({ method: 'web3_clientVersion', params: [] }, (err, clientVersion) => {
if (err) return cb(err);
if (clientVersion.indexOf("Parity-Ethereum//v2") === 0) {
// This is Parity
self.web3.shh.getInfo(function (err) {
if (err) {
return cb(err, 0);
}
// TOFIX Assume Whisper v6 until there's a way to understand it via JSON-RPC
return cb(err, 6);
});
} else {
// Assume it is a Geth compliant client
self.web3.shh.getVersion(function (err, version) {
cb(err, version);
});
}
});
};
__embarkWhisperNewWeb3.isAvailable = function () {
return new Promise((resolve, reject) => {
if (!this.web3.shh) {
return resolve(false);
}
try {
this.getWhisperVersion((err) => {
resolve(Boolean(!err));
});
}
catch (err) {
reject(err);
}
});
};
export default __embarkWhisperNewWeb3;

View File

@ -0,0 +1,31 @@
/* global module require */
const cloneDeep = require('lodash.clonedeep');
module.exports = (api) => {
const env = api.env();
const base = {};
const browser = cloneDeep(base);
Object.assign(browser, {
ignore: [
'src/node'
]
});
const node = cloneDeep(base);
const test = cloneDeep(node);
switch (env) {
case 'browser':
return browser;
case 'node':
return node;
case 'test':
return test;
default:
return base;
}
};

View File

@ -0,0 +1 @@
build-test

View File

@ -0,0 +1,4 @@
engine-strict = true
package-lock = false
save-exact = true
scripts-prepend-node-path = true

View File

@ -0,0 +1,96 @@
# Change Log
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
# [5.0.0-alpha.1](https://github.com/embark-framework/embark/compare/v5.0.0-alpha.0...v5.0.0-alpha.1) (2019-11-05)
**Note:** Version bump only for package embarkjs-whisper
# [5.0.0-alpha.0](https://github.com/embark-framework/embark/compare/v4.1.1...v5.0.0-alpha.0) (2019-10-28)
### Build System
* bump all packages' engines settings ([#1985](https://github.com/embark-framework/embark/issues/1985)) ([ed02cc8](https://github.com/embark-framework/embark/commit/ed02cc8))
### BREAKING CHANGES
* node: >=10.17.0 <12.0.0
npm: >=6.11.3
yarn: >=1.19.1
node v10.17.0 is the latest in the 10.x series and is still in the Active LTS
lifecycle. Embark is still not compatible with node's 12.x and 13.x
series (because of some dependencies), otherwise it would probably make sense
to bump our minimum supported node version all the way to the most recent 12.x
release.
npm v6.11.3 is the version that's bundled with node v10.17.0.
yarn v1.19.1 is the most recent version as of the time node v10.17.0 was
released.
## [4.1.1](https://github.com/embark-framework/embark/compare/v4.1.0...v4.1.1) (2019-08-28)
**Note:** Version bump only for package embarkjs-whisper
# [4.1.0](https://github.com/embark-framework/embark/compare/v4.1.0-beta.6...v4.1.0) (2019-08-12)
**Note:** Version bump only for package embarkjs-whisper
# [4.1.0-beta.6](https://github.com/embark-framework/embark/compare/v4.1.0-beta.5...v4.1.0-beta.6) (2019-08-09)
**Note:** Version bump only for package embarkjs-whisper
# [4.1.0-beta.3](https://github.com/embark-framework/embark/compare/v4.1.0-beta.2...v4.1.0-beta.3) (2019-06-07)
### Bug Fixes
* **@embarkjs/whisper:** don't rely on global EmbarkJS in whisper APIs ([f2903e7](https://github.com/embark-framework/embark/commit/f2903e7)), closes [/github.com/embark-framework/embark/blob/ac76a40a6156603fa436f1fe173835cff5fb0c3d/packages/embarkjs-whisper/src/index.js#L43-L62](https://github.com//github.com/embark-framework/embark/blob/ac76a40a6156603fa436f1fe173835cff5fb0c3d/packages/embarkjs-whisper/src/index.js/issues/L43-L62) [/github.com/embark-framework/embark/blob/ac76a40a6156603fa436f1fe173835cff5fb0c3d/packages/embarkjs-whisper/src/index.js#L64-L73](https://github.com//github.com/embark-framework/embark/blob/ac76a40a6156603fa436f1fe173835cff5fb0c3d/packages/embarkjs-whisper/src/index.js/issues/L64-L73) [/github.com/embark-framework/embark/blob/ac76a40a6156603fa436f1fe173835cff5fb0c3d/packages/embark-code-runner/src/index.ts#L33](https://github.com//github.com/embark-framework/embark/blob/ac76a40a6156603fa436f1fe173835cff5fb0c3d/packages/embark-code-runner/src/index.ts/issues/L33)
# [4.1.0-beta.2](https://github.com/embark-framework/embark/compare/v4.1.0-beta.1...v4.1.0-beta.2) (2019-05-22)
**Note:** Version bump only for package embarkjs-whisper
# [4.1.0-beta.1](https://github.com/embark-framework/embark/compare/v4.1.0-beta.0...v4.1.0-beta.1) (2019-05-15)
**Note:** Version bump only for package embarkjs-whisper
# [4.1.0-beta.0](https://github.com/embark-framework/embark/compare/v4.0.0...v4.1.0-beta.0) (2019-04-17)
**Note:** Version bump only for package embarkjs-whisper

View File

@ -0,0 +1,6 @@
# `embarkjs-whisper`
> Whisper plugin for embarkjs
Visit [embark.status.im](https://embark.status.im/) to get started with
[Embark](https://github.com/embark-framework/embark).

View File

@ -1,10 +1,10 @@
{
"name": "embarkjs-whisper",
"name": "embarkjs-whisper-v6",
"version": "5.0.0-alpha.1",
"author": "Iuri Matias <iuri.matias@gmail.com>",
"contributors": [],
"description": "Whisper plugin for embarkjs",
"homepage": "https://github.com/embark-framework/embark/tree/master/packages/embarkjs/whisper#readme",
"description": "Whisper v6 plugin for embarkjs",
"homepage": "https://github.com/embark-framework/embark/tree/master/packages/embarkjs/whisper-v6#readme",
"bugs": "https://github.com/embark-framework/embark/issues",
"keywords": [
"blockchain",
@ -17,7 +17,7 @@
],
"license": "MIT",
"repository": {
"directory": "packages/embarkjs/whisper",
"directory": "packages/embarkjs/whisper-v6",
"type": "git",
"url": "https://github.com/embark-framework/embark.git"
},
@ -61,4 +61,4 @@
"npm": ">=6.11.3",
"yarn": ">=1.19.1"
}
}
}

View File

@ -0,0 +1 @@
module.exports = require('..').default;

View File

@ -25,8 +25,8 @@ class Whisper {
this.api = new API(embark);
this.whisperNodes = {};
this.events.request("embarkjs:plugin:register", "messages", "whisper", "embarkjs-whisper");
this.events.request("embarkjs:console:register", "messages", "whisper", "embarkjs-whisper");
this.events.request("embarkjs:plugin:register", "messages", "whisper", "embarkjs-whisper-v6");
this.events.request("embarkjs:console:register", "messages", "whisper", "embarkjs-whisper-v6");
this.events.request("communication:node:register", "whisper", (readyCb) => {
this.events.request("processes:register", "communication", {

View File

@ -23,8 +23,17 @@ class Whisper {
this.api = new API(embark);
this.whisperNodes = {};
this.events.request("embarkjs:plugin:register", "messages", "whisper", "embarkjs-whisper");
this.events.request("embarkjs:console:register", "messages", "whisper", "embarkjs-whisper");
this.events.request("embarkjs:plugin:register", "messages", "whisper", "embarkjs-whisper-parity");
this.events.request("embarkjs:console:register", "messages", "whisper", "embarkjs-whisper-parity");
this.embark.registerActionForEvent("blockchain:proxy:request", (params, callback) => {
if (params.request.method !== "shh_subscribe") {
return callback(null, params);
}
if (params.request.params[0] === "messages") {
params.request.params = params.request.params.slice(1);
}
callback(null, params);
});
this.events.request("communication:node:register", "whisper", (readyCb) => {
this.events.request("processes:register", "communication", {

View File

@ -1742,6 +1742,14 @@
core-js "^2.6.5"
regenerator-runtime "^0.13.2"
"@babel/runtime-corejs3@7.6.3":
version "7.6.3"
resolved "https://registry.yarnpkg.com/@babel/runtime-corejs3/-/runtime-corejs3-7.6.3.tgz#a2b2b7ceea6ecb37ff822d293a312f1f00c29b84"
integrity sha512-933SXHQr7apa95F+3IqkBne8mqOnu1kDh6dnSddC07aW/R51WsOVD7MSczJ6DRpq/L8KLll7TFDxmt30pft44w==
dependencies:
core-js-pure "^3.0.0"
regenerator-runtime "^0.13.2"
"@babel/runtime-corejs3@7.7.4":
version "7.7.4"
resolved "https://registry.yarnpkg.com/@babel/runtime-corejs3/-/runtime-corejs3-7.7.4.tgz#f861adc1cecb9903dfd66ea97917f02ff8d79888"
@ -7097,6 +7105,11 @@ core-js@3.2.1:
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.2.1.tgz#cd41f38534da6cc59f7db050fe67307de9868b09"
integrity sha512-Qa5XSVefSVPRxy2XfUC13WbvqkxhkwB3ve+pgCQveNgYzbM/UxZeu1dcOX/xr4UmfUd+muuvsaxilQzCyUurMw==
core-js@3.3.5:
version "3.3.5"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.3.5.tgz#58d20f48a95a07304b62ff752742b82b56431ed8"
integrity sha512-0J3K+Par/ZydhKg8pEiTcK/9d65/nqJOzY62uMkjeBmt05fDOt/khUVjDdh8TpeIuGQDy1yLDDCjiWN/8pFIuw==
core-js@3.4.3:
version "3.4.3"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.4.3.tgz#09ea102412a368d5f73d24f082e41ac90c633a49"
@ -8333,6 +8346,16 @@ embark-test-contract-1@0.0.1, embark-test-contract-1@^0.0.1:
resolved "https://registry.yarnpkg.com/embark-test-contract-1/-/embark-test-contract-1-0.0.1.tgz#802b84150e8038fef6681a3f23b6f4c0dc5b3e80"
integrity sha512-yFaXMOXOMfYRNFOKEspdXrZzyovceWedtegHtbfs8RZWRzRYY+v6ZDtGm13TRJt3Qt4VZhKky0l7G/SHZMGHCA==
embarkjs-whisper@^5.0.0-alpha.1:
version "5.0.0-alpha.1"
resolved "https://registry.yarnpkg.com/embarkjs-whisper/-/embarkjs-whisper-5.0.0-alpha.1.tgz#a8d1d2d3d6d9ddb733386fb8625f77aca052025c"
integrity sha512-hSyWlVM1TBLBaFh8gro5uOU67Ffe1Kf/mS1akjAHLDARsMPirleCoVG+iMfqXc37c7iv1bOTJSkXqX6Tr5uRMA==
dependencies:
"@babel/runtime-corejs3" "7.6.3"
core-js "3.3.5"
rxjs "6.4.0"
web3 "1.2.1"
emoji-regex@^7.0.1, emoji-regex@^7.0.2, emoji-regex@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-7.0.3.tgz#933a04052860c85e83c122479c4748a8e4c72156"