mirror of https://github.com/embarklabs/embark.git
feature(@embark/embarkjs-whisper: make embarkjs whisper plugin a module on its own
feature(@embark/embarkjs-whisper: make embarkjs whisper plugin a module on its own feature(@embark/embarkjs-whisper: make embarkjs whisper plugin a module on its own
This commit is contained in:
parent
b792b3fc78
commit
07b2ecc448
|
@ -49,6 +49,7 @@
|
||||||
"@babel/runtime-corejs2": "7.3.1",
|
"@babel/runtime-corejs2": "7.3.1",
|
||||||
"async": "2.6.1",
|
"async": "2.6.1",
|
||||||
"embark-utils": "^4.0.0",
|
"embark-utils": "^4.0.0",
|
||||||
|
"embarkjs-whisper": "^4.0.0",
|
||||||
"rxjs": "6.4.0"
|
"rxjs": "6.4.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -1,14 +1,18 @@
|
||||||
/* global __ __dirname module require setTimeout */
|
/* global __ __dirname module require setTimeout */
|
||||||
|
|
||||||
import {joinPath, canonicalHost, defaultHost} from 'embark-utils';
|
import {canonicalHost, defaultHost} from 'embark-utils';
|
||||||
let Web3 = require('web3');
|
let Web3 = require('web3');
|
||||||
const {parallel} = require('async');
|
const {parallel} = require('async');
|
||||||
const {sendMessage, listenTo} = require('./js/communicationFunctions');
|
|
||||||
const {fromEvent} = require('rxjs');
|
const {fromEvent} = require('rxjs');
|
||||||
const {map, takeUntil} = require('rxjs/operators');
|
const {map, takeUntil} = require('rxjs/operators');
|
||||||
|
|
||||||
const EMBARK_RESOURCE_ORIGIN = "http://embark";
|
const EMBARK_RESOURCE_ORIGIN = "http://embark";
|
||||||
|
|
||||||
|
import whisper from 'embarkjs-whisper';
|
||||||
|
|
||||||
|
const sendMessage = whisper.real_sendMessage;
|
||||||
|
const listenTo = whisper.real_listenTo;
|
||||||
|
|
||||||
class Whisper {
|
class Whisper {
|
||||||
constructor(embark, options) {
|
constructor(embark, options) {
|
||||||
this.logger = embark.logger;
|
this.logger = embark.logger;
|
||||||
|
@ -118,20 +122,12 @@ class Whisper {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: possible race condition could be a concern
|
let code = "";
|
||||||
this.events.request("version:get:web3", function(web3Version) {
|
|
||||||
let code = "";
|
code += "\nconst __embarkWhisperNewWeb3 = require('embarkjs-whisper')";
|
||||||
if (web3Version[0] === "0") {
|
code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperNewWeb3.default || __embarkWhisperNewWeb3);";
|
||||||
self.isOldWeb3 = true;
|
|
||||||
code += "\n" + self.fs.readFileSync(joinPath(__dirname, 'js', 'embarkjs_old_web3.js')).toString();
|
self.embark.addCodeToEmbarkJS(code);
|
||||||
code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperOld);";
|
|
||||||
} else {
|
|
||||||
code += "\n" + self.fs.readFileSync(joinPath(__dirname, 'js', 'communicationFunctions.js')).toString();
|
|
||||||
code += "\n" + self.fs.readFileSync(joinPath(__dirname, 'js', 'embarkjs.js')).toString();
|
|
||||||
code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperNewWeb3);";
|
|
||||||
}
|
|
||||||
self.embark.addCodeToEmbarkJS(code);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addSetProvider() {
|
addSetProvider() {
|
||||||
|
|
|
@ -1,117 +0,0 @@
|
||||||
/* global EmbarkJS Web3 */
|
|
||||||
|
|
||||||
const {bindNodeCallback} = require('rxjs');
|
|
||||||
const {map} = require('rxjs/operators');
|
|
||||||
|
|
||||||
// for the old version of whisper and web3.js
|
|
||||||
let __embarkWhisperOld = {};
|
|
||||||
|
|
||||||
__embarkWhisperOld.setProvider = function (options) {
|
|
||||||
const self = this;
|
|
||||||
let provider;
|
|
||||||
if (options === undefined) {
|
|
||||||
provider = "localhost:8546";
|
|
||||||
} else {
|
|
||||||
provider = options.server + ':' + options.port;
|
|
||||||
}
|
|
||||||
self.web3 = new Web3(new Web3.providers.HttpProvider("http://" + provider));
|
|
||||||
self.getWhisperVersion(function (err, version) {
|
|
||||||
if (err) {
|
|
||||||
console.log("whisper not available");
|
|
||||||
} else if (version >= 5) {
|
|
||||||
throw new Error("whisper 5 not supported with this version of web3.js");
|
|
||||||
} else {
|
|
||||||
self.identity = self.web3.shh.newIdentity();
|
|
||||||
}
|
|
||||||
self.whisperVersion = self.web3.version.whisper;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
__embarkWhisperOld.sendMessage = function (options) {
|
|
||||||
var topics, data, ttl, priority, payload;
|
|
||||||
topics = options.topic || options.topics;
|
|
||||||
data = options.data || options.payload;
|
|
||||||
ttl = options.ttl || 100;
|
|
||||||
priority = options.priority || 1000;
|
|
||||||
var identity = options.identity || this.identity || this.web3.shh.newIdentity();
|
|
||||||
var _topics;
|
|
||||||
|
|
||||||
if (topics === undefined) {
|
|
||||||
throw new Error("missing option: topic");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data === undefined) {
|
|
||||||
throw new Error("missing option: data");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof topics === 'string') {
|
|
||||||
_topics = [EmbarkJS.Utils.fromAscii(topics)];
|
|
||||||
} else {
|
|
||||||
_topics = topics.map((t) => EmbarkJS.Utils.fromAscii(t));
|
|
||||||
}
|
|
||||||
topics = _topics;
|
|
||||||
|
|
||||||
payload = JSON.stringify(data);
|
|
||||||
|
|
||||||
var message;
|
|
||||||
message = {
|
|
||||||
from: identity,
|
|
||||||
topics: topics,
|
|
||||||
payload: EmbarkJS.Utils.fromAscii(payload),
|
|
||||||
ttl: ttl,
|
|
||||||
priority: priority
|
|
||||||
};
|
|
||||||
|
|
||||||
return this.web3.shh.post(message, function () {
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
__embarkWhisperOld.listenTo = function (options) {
|
|
||||||
var topics, _topics;
|
|
||||||
topics = options.topic || options.topics;
|
|
||||||
_topics = [];
|
|
||||||
|
|
||||||
if (typeof topics === 'string') {
|
|
||||||
_topics = [topics];
|
|
||||||
} else {
|
|
||||||
_topics = topics.map((t) => EmbarkJS.Utils.fromAscii(t));
|
|
||||||
}
|
|
||||||
topics = _topics;
|
|
||||||
|
|
||||||
var filterOptions = {
|
|
||||||
topics: topics
|
|
||||||
};
|
|
||||||
|
|
||||||
const obsFilter = bindNodeCallback(this.web3.shh.filter);
|
|
||||||
|
|
||||||
return obsFilter(filterOptions).pipe(map(result => ({
|
|
||||||
data: JSON.parse(EmbarkJS.Utils.toAscii(result.payload)),
|
|
||||||
from: result.from,
|
|
||||||
payload: result.payload,
|
|
||||||
result,
|
|
||||||
time: (new Date(result.sent * 1000)),
|
|
||||||
topic: topics
|
|
||||||
})));
|
|
||||||
};
|
|
||||||
|
|
||||||
__embarkWhisperOld.getWhisperVersion = function (cb) {
|
|
||||||
this.web3.version.getWhisper(function (err, _res) {
|
|
||||||
cb(err, self.web3.version.whisper);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
__embarkWhisperOld.isAvailable = function () {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
if (!this.web3) {
|
|
||||||
return resolve(false);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
this.getWhisperVersion((err) => {
|
|
||||||
resolve(Boolean(!err));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch (err) {
|
|
||||||
reject(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
|
@ -41,6 +41,7 @@ class VM {
|
||||||
"@babel/runtime-corejs2/core-js/object/assign",
|
"@babel/runtime-corejs2/core-js/object/assign",
|
||||||
"eth-ens-namehash",
|
"eth-ens-namehash",
|
||||||
"swarm-api",
|
"swarm-api",
|
||||||
|
"embarkjs-whisper",
|
||||||
"rxjs",
|
"rxjs",
|
||||||
"rxjs/operators",
|
"rxjs/operators",
|
||||||
],
|
],
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
/* 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/embarkjs.js',
|
||||||
|
'src/node/index.js'
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
const node = cloneDeep(base);
|
||||||
|
Object.assign(node, {
|
||||||
|
ignore: [
|
||||||
|
'src/browser.js'
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
const nodeAsync = cloneDeep(base);
|
||||||
|
Object.assign(nodeAsync, {
|
||||||
|
ignore: [
|
||||||
|
'src/node/index.js'
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
const nodeTest = cloneDeep(base);
|
||||||
|
|
||||||
|
switch (env) {
|
||||||
|
case 'browser':
|
||||||
|
return browser;
|
||||||
|
case 'node':
|
||||||
|
return node;
|
||||||
|
case 'node:async':
|
||||||
|
return nodeAsync;
|
||||||
|
case 'node:test':
|
||||||
|
return nodeTest;
|
||||||
|
default:
|
||||||
|
return base;
|
||||||
|
}
|
||||||
|
};
|
|
@ -0,0 +1 @@
|
||||||
|
build-test
|
|
@ -0,0 +1,4 @@
|
||||||
|
engine-strict = true
|
||||||
|
package-lock = false
|
||||||
|
save-exact = true
|
||||||
|
scripts-prepend-node-path = true
|
|
@ -0,0 +1,6 @@
|
||||||
|
# `embarkjs-whisper`
|
||||||
|
|
||||||
|
embarkjs plugin to interact with whisper
|
||||||
|
|
||||||
|
Visit [embark.status.im](https://embark.status.im/) to get started with
|
||||||
|
[Embark](https://github.com/embark-framework/embark).
|
|
@ -0,0 +1,74 @@
|
||||||
|
{
|
||||||
|
"name": "embarkjs-whisper",
|
||||||
|
"version": "4.0.0",
|
||||||
|
"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",
|
||||||
|
"bugs": "https://github.com/embark-framework/embark/issues",
|
||||||
|
"keywords": [
|
||||||
|
"blockchain",
|
||||||
|
"dapps",
|
||||||
|
"ethereum",
|
||||||
|
"ipfs",
|
||||||
|
"serverless",
|
||||||
|
"solc",
|
||||||
|
"solidity"
|
||||||
|
],
|
||||||
|
"license": "MIT",
|
||||||
|
"repository": {
|
||||||
|
"directory": "packages/embarkjs-whisper",
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/embark-framework/embark.git"
|
||||||
|
},
|
||||||
|
"main": "dist/node/index.js",
|
||||||
|
"browser": {
|
||||||
|
"./dist/node/index.js": "./dist/browser/index.js"
|
||||||
|
},
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 version",
|
||||||
|
"not dead",
|
||||||
|
"> 0.2%"
|
||||||
|
],
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"scripts": {
|
||||||
|
"build": "npm-run-all build:**",
|
||||||
|
"build:browser": "cross-env BABEL_ENV=browser babel src --out-dir dist/browser --root-mode upward",
|
||||||
|
"build:node": "cross-env BABEL_ENV=node babel src --out-dir dist --root-mode upward --source-maps",
|
||||||
|
"build:node:async": "cross-env BABEL_ENV=node:async babel src/node --out-dir dist --root-mode upward --source-maps",
|
||||||
|
"// build:node:test": "cross-env BABEL_ENV=node:test babel test --out-dir build-test --root-mode upward --source-maps",
|
||||||
|
"ci": "npm run qa",
|
||||||
|
"clean": "npm run reset",
|
||||||
|
"package": "npm pack",
|
||||||
|
"// qa": "npm-run-all build test package",
|
||||||
|
"qa": "npm-run-all build package",
|
||||||
|
"reset": "npx rimraf build-test dist embarkjs-*.tgz package",
|
||||||
|
"start": "npm run watch",
|
||||||
|
"test": "mocha \"build-test/**/*.js\" --exit --no-timeouts --require source-map-support/register",
|
||||||
|
"watch": "run-p \"build:** -- --verbose --watch\""
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/runtime-corejs2": "7.3.1",
|
||||||
|
"rxjs": "6.4.0",
|
||||||
|
"web3": "1.0.0-beta.37"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/cli": "7.2.3",
|
||||||
|
"@babel/core": "7.2.2",
|
||||||
|
"ajv": "6.5.2",
|
||||||
|
"chai": "4.2.0",
|
||||||
|
"cross-env": "5.2.0",
|
||||||
|
"lodash.clonedeep": "4.5.0",
|
||||||
|
"mocha": "5.2.0",
|
||||||
|
"npm-run-all": "4.1.5",
|
||||||
|
"rimraf": "2.6.3",
|
||||||
|
"source-map-support": "0.5.9"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=8.12.0",
|
||||||
|
"npm": ">=6.4.1",
|
||||||
|
"yarn": ">=1.12.3"
|
||||||
|
}
|
||||||
|
}
|
|
@ -98,9 +98,7 @@ function listenTo(options) {
|
||||||
return obsSub;
|
return obsSub;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof module !== 'undefined' && module.exports) {
|
export default {
|
||||||
module.exports = {
|
sendMessage,
|
||||||
sendMessage,
|
listenTo
|
||||||
listenTo
|
};
|
||||||
};
|
|
||||||
}
|
|
|
@ -0,0 +1 @@
|
||||||
|
export {default} from './index';
|
|
@ -1,8 +1,13 @@
|
||||||
/* global EmbarkJS Web3 listenTo sendMessage */
|
/* global EmbarkJS Web3 listenTo sendMessage */
|
||||||
|
let Web3 = require('web3');
|
||||||
|
const {sendMessage, listenTo} = require('./communicationFunctions').default;
|
||||||
|
|
||||||
// for the whisper v5 and web3.js 1.0
|
// for the whisper v5 and web3.js 1.0
|
||||||
let __embarkWhisperNewWeb3 = {};
|
let __embarkWhisperNewWeb3 = {};
|
||||||
|
|
||||||
|
__embarkWhisperNewWeb3.real_sendMessage = sendMessage;
|
||||||
|
__embarkWhisperNewWeb3.real_listenTo = listenTo;
|
||||||
|
|
||||||
__embarkWhisperNewWeb3.setProvider = function(options) {
|
__embarkWhisperNewWeb3.setProvider = function(options) {
|
||||||
const self = this;
|
const self = this;
|
||||||
let provider;
|
let provider;
|
||||||
|
@ -49,7 +54,7 @@ __embarkWhisperNewWeb3.sendMessage = function(options) {
|
||||||
data
|
data
|
||||||
});
|
});
|
||||||
|
|
||||||
sendMessage(options, (err) => {
|
this.real_sendMessage(options, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw new Error(err);
|
throw new Error(err);
|
||||||
}
|
}
|
||||||
|
@ -64,7 +69,7 @@ __embarkWhisperNewWeb3.listenTo = function (options) {
|
||||||
subscribe: this.web3.shh.subscribe,
|
subscribe: this.web3.shh.subscribe,
|
||||||
symKeyID: options.symKeyID || this.symKeyID
|
symKeyID: options.symKeyID || this.symKeyID
|
||||||
});
|
});
|
||||||
return listenTo(options);
|
return this.real_listenTo(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkWhisperNewWeb3.getWhisperVersion = function(cb) {
|
__embarkWhisperNewWeb3.getWhisperVersion = function(cb) {
|
||||||
|
@ -107,3 +112,5 @@ __embarkWhisperNewWeb3.isAvailable = function() {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export default __embarkWhisperNewWeb3;
|
|
@ -0,0 +1 @@
|
||||||
|
module.exports = require('../embarkjs').default;
|
Loading…
Reference in New Issue