diff --git a/packages/embark-whisper/package.json b/packages/embark-whisper/package.json index ff8a3b33c..b1a69c444 100644 --- a/packages/embark-whisper/package.json +++ b/packages/embark-whisper/package.json @@ -49,6 +49,7 @@ "@babel/runtime-corejs2": "7.3.1", "async": "2.6.1", "embark-utils": "^4.0.0", + "embarkjs-whisper": "^4.0.0", "rxjs": "6.4.0" }, "devDependencies": { diff --git a/packages/embark-whisper/src/index.js b/packages/embark-whisper/src/index.js index bd858eef0..65f076d05 100644 --- a/packages/embark-whisper/src/index.js +++ b/packages/embark-whisper/src/index.js @@ -1,14 +1,18 @@ /* global __ __dirname module require setTimeout */ -import {joinPath, canonicalHost, defaultHost} from 'embark-utils'; +import {canonicalHost, defaultHost} from 'embark-utils'; let Web3 = require('web3'); const {parallel} = require('async'); -const {sendMessage, listenTo} = require('./js/communicationFunctions'); const {fromEvent} = require('rxjs'); const {map, takeUntil} = require('rxjs/operators'); const EMBARK_RESOURCE_ORIGIN = "http://embark"; +import whisper from 'embarkjs-whisper'; + +const sendMessage = whisper.real_sendMessage; +const listenTo = whisper.real_listenTo; + class Whisper { constructor(embark, options) { this.logger = embark.logger; @@ -118,20 +122,12 @@ class Whisper { return; } - // TODO: possible race condition could be a concern - this.events.request("version:get:web3", function(web3Version) { - let code = ""; - if (web3Version[0] === "0") { - self.isOldWeb3 = true; - code += "\n" + self.fs.readFileSync(joinPath(__dirname, 'js', 'embarkjs_old_web3.js')).toString(); - 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); - }); + let code = ""; + + code += "\nconst __embarkWhisperNewWeb3 = require('embarkjs-whisper')"; + code += "\nEmbarkJS.Messages.registerProvider('whisper', __embarkWhisperNewWeb3.default || __embarkWhisperNewWeb3);"; + + self.embark.addCodeToEmbarkJS(code); } addSetProvider() { diff --git a/packages/embark-whisper/src/js/embarkjs_old_web3.js b/packages/embark-whisper/src/js/embarkjs_old_web3.js deleted file mode 100644 index 7aac43f4a..000000000 --- a/packages/embark-whisper/src/js/embarkjs_old_web3.js +++ /dev/null @@ -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); - } - }); -}; diff --git a/packages/embark/src/lib/modules/codeRunner/vm.ts b/packages/embark/src/lib/modules/codeRunner/vm.ts index 08e911f35..0aef94a88 100644 --- a/packages/embark/src/lib/modules/codeRunner/vm.ts +++ b/packages/embark/src/lib/modules/codeRunner/vm.ts @@ -41,6 +41,7 @@ class VM { "@babel/runtime-corejs2/core-js/object/assign", "eth-ens-namehash", "swarm-api", + "embarkjs-whisper", "rxjs", "rxjs/operators", ], diff --git a/packages/embarkjs-whisper/.babelrc.js b/packages/embarkjs-whisper/.babelrc.js new file mode 100644 index 000000000..787135151 --- /dev/null +++ b/packages/embarkjs-whisper/.babelrc.js @@ -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; + } +}; diff --git a/packages/embarkjs-whisper/.gitignore b/packages/embarkjs-whisper/.gitignore new file mode 100644 index 000000000..b6420126f --- /dev/null +++ b/packages/embarkjs-whisper/.gitignore @@ -0,0 +1 @@ +build-test diff --git a/packages/embarkjs-whisper/.npmrc b/packages/embarkjs-whisper/.npmrc new file mode 100644 index 000000000..e031d3432 --- /dev/null +++ b/packages/embarkjs-whisper/.npmrc @@ -0,0 +1,4 @@ +engine-strict = true +package-lock = false +save-exact = true +scripts-prepend-node-path = true diff --git a/packages/embarkjs-whisper/README.md b/packages/embarkjs-whisper/README.md new file mode 100644 index 000000000..07428460a --- /dev/null +++ b/packages/embarkjs-whisper/README.md @@ -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). diff --git a/packages/embarkjs-whisper/package.json b/packages/embarkjs-whisper/package.json new file mode 100644 index 000000000..c0ef5a0c8 --- /dev/null +++ b/packages/embarkjs-whisper/package.json @@ -0,0 +1,74 @@ +{ + "name": "embarkjs-whisper", + "version": "4.0.0", + "author": "Iuri Matias ", + "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" + } +} diff --git a/packages/embark-whisper/src/js/communicationFunctions.js b/packages/embarkjs-whisper/src/communicationFunctions.js similarity index 95% rename from packages/embark-whisper/src/js/communicationFunctions.js rename to packages/embarkjs-whisper/src/communicationFunctions.js index 9b3926bba..4470e3d70 100644 --- a/packages/embark-whisper/src/js/communicationFunctions.js +++ b/packages/embarkjs-whisper/src/communicationFunctions.js @@ -98,9 +98,7 @@ function listenTo(options) { return obsSub; } -if (typeof module !== 'undefined' && module.exports) { - module.exports = { - sendMessage, - listenTo - }; -} +export default { + sendMessage, + listenTo +}; diff --git a/packages/embarkjs-whisper/src/embarkjs.js b/packages/embarkjs-whisper/src/embarkjs.js new file mode 100644 index 000000000..eb6ba4c77 --- /dev/null +++ b/packages/embarkjs-whisper/src/embarkjs.js @@ -0,0 +1 @@ +export {default} from './index'; diff --git a/packages/embark-whisper/src/js/embarkjs.js b/packages/embarkjs-whisper/src/index.js similarity index 89% rename from packages/embark-whisper/src/js/embarkjs.js rename to packages/embarkjs-whisper/src/index.js index 8d65ac51d..0844dcfae 100644 --- a/packages/embark-whisper/src/js/embarkjs.js +++ b/packages/embarkjs-whisper/src/index.js @@ -1,8 +1,13 @@ /* global EmbarkJS Web3 listenTo sendMessage */ +let Web3 = require('web3'); +const {sendMessage, listenTo} = require('./communicationFunctions').default; // for the whisper v5 and web3.js 1.0 let __embarkWhisperNewWeb3 = {}; +__embarkWhisperNewWeb3.real_sendMessage = sendMessage; +__embarkWhisperNewWeb3.real_listenTo = listenTo; + __embarkWhisperNewWeb3.setProvider = function(options) { const self = this; let provider; @@ -49,7 +54,7 @@ __embarkWhisperNewWeb3.sendMessage = function(options) { data }); - sendMessage(options, (err) => { + this.real_sendMessage(options, (err) => { if (err) { throw new Error(err); } @@ -64,7 +69,7 @@ __embarkWhisperNewWeb3.listenTo = function (options) { subscribe: this.web3.shh.subscribe, symKeyID: options.symKeyID || this.symKeyID }); - return listenTo(options); + return this.real_listenTo(options); }; __embarkWhisperNewWeb3.getWhisperVersion = function(cb) { @@ -107,3 +112,5 @@ __embarkWhisperNewWeb3.isAvailable = function() { } }); }; + +export default __embarkWhisperNewWeb3; diff --git a/packages/embarkjs-whisper/src/node/index.js b/packages/embarkjs-whisper/src/node/index.js new file mode 100644 index 000000000..a61b612f1 --- /dev/null +++ b/packages/embarkjs-whisper/src/node/index.js @@ -0,0 +1 @@ +module.exports = require('../embarkjs').default;