From d1856309b17a78cf67608de6fa46735df14648f5 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 29 Nov 2018 20:40:40 -0400 Subject: [PATCH 1/3] Added chat request event --- src/index.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index c7773d0..eb8c00e 100644 --- a/src/index.js +++ b/src/index.js @@ -121,6 +121,10 @@ class StatusJS { } } + onChatRequest(cb){ + this.chatRequestCb = cb; + } + onChannelMessage(channelName, cb) { if (!this.channels[channelName]) { return cb("unknown channel: " + channelName); @@ -161,7 +165,20 @@ class StatusJS { this.contacts[data.sig].lastClockValue = payloadArray[1][3]; } - cb(null, {payload: hexToAscii(data.payload), data: data, username: this.contacts[data.sig].username}); + if(payloadArray[0] == '~#c4'){ + cb(null, {payload: hexToAscii(data.payload), data: data, username: this.contacts[data.sig].username}); + } else if(payloadArray[0] == '~#c2') { + this.contacts[data.sig].displayName = payloadArray[1][0]; + this.contacts[data.sig].profilePic = payloadArray[1][1]; + + if(this.chatRequestCb){ + this.chatRequestCb(null, { + 'username': this.contacts[data.sig].username, + 'displayName': this.contacts[data.sig].displayName, + 'profilePic': this.contacts[data.sig].profilePic, + }); + } + } }).on('error', (err) => { cb(err); }); From 808f2de60d70ae5cd707997a43227c1e32ef0dcf Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 29 Nov 2018 22:25:17 -0400 Subject: [PATCH 2/3] Update README.md --- README.md | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6c0ffce..01d27c2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,38 @@ -# status-js +status-js +=== -# Config: +

+Javascript client for sending / receiving messages in Status +

+

+WIP. DO NOT USE IN PRODUCTION. HIGH RISK ⚠ +

+
-`geth --testnet --syncmode=light --ws --wsport=8546 --wsaddr=localhost --wsorigins=statusjs --maxpeers=25 --shh --shh.pow=0.002 --wsapi=eth,web3,net,shh,debug,admin` +## Install +clone the repo via git: +``` +$ git clone https://github.com/status-im/status-js.git +``` +And then install the dependencies with `npm`. +``` +$ cd status-js +$ npm install +``` -web3.ssh options: `powTime (1), ttl (10) and powTarget (0.002) +`status-js` requires `geth` or `murmur` to be able to connect to Whisper. If using `geth`, you may start it with the following flags. + +`geth --testnet --syncmode=light --ws --wsport=8546 --wsaddr=localhost --wsorigins=statusjs --rpc --maxpeers=25 --shh --shh.pow=0.002 --wsapi=eth,web3,net,shh,debug,admin` + +Also, due to the lack of nodes with Whisper enabled, you need to create a [static-nodes.json](https://github.com/status-im/murmur/blob/master/src/data/static-nodes.json) file, that must be placed in a specific path (if using testnet and Linux, `~/.ethereum/testnet/geth/static-nodes.json` + +## API +*TODO* +See test files for use in the meantime + + +## Contribution + +Thank you for considering to help out with the source code! We welcome contributions from anyone on the internet, and are grateful for even the smallest of fixes! + +If you'd like to contribute to `status-js`, please fork, fix, commit and send a pull request for the maintainers to review and merge into the main code base. If you wish to submit more complex changes though, please check up with the core devs first on [#status-js channel](https://get.status.im/chat/public/status-js) to ensure those changes are in line with the general philosophy of the project and/or get some early feedback which can make both your efforts much lighter as well as our review and merge procedures quick and simple. From 95a56fd35ec03edb482947a176b5c9c99a3cbede Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 30 Nov 2018 09:46:25 -0400 Subject: [PATCH 3/3] Added a constant file and replaced hardcoded values for constants --- src/constants.js | 21 ++++++++++++++++ src/index.js | 63 +++++++++++++++++++++--------------------------- 2 files changed, 48 insertions(+), 36 deletions(-) create mode 100644 src/constants.js diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 0000000..80a4ccc --- /dev/null +++ b/src/constants.js @@ -0,0 +1,21 @@ +module.exports = { + post: { + POW_TIME: 1, + TTL: 10, + POW_TARGET: 0.002 + }, + messageTypes: { + GROUP_MESSAGE: "~:public-group-user-message", + USER_MESSAGE: "~:user-message" + }, + messageTags: { + message: "~#c4", + chatRequest: "~#c2" + }, + topics: { + CONTACT_DISCOVERY_TOPIC: '0xf8946aac' + }, + regExp: { + CONTACT_CODE_REGEXP: /^(0x)?[0-9a-f]{130}$/i + } +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index eb8c00e..3745f11 100644 --- a/src/index.js +++ b/src/index.js @@ -1,22 +1,13 @@ const Web3 = require('web3'); const utils = require('./utils.js'); const mailservers = require('./mailservers.js'); +const constants = require('./constants'); const { utils: { asciiToHex, hexToAscii, sha3 } } = Web3; -const POW_TIME = 1; -const TTL = 10; -const POW_TARGET = 0.002; - -const GROUP_MESSAGE = "~:public-group-user-message"; -const USER_MESSAGE = "~:user-message"; - -const CONTACT_DISCOVERY_TOPIC = '0xf8946aac'; - -const CONTACT_CODE_REGEXP = /^(0x)?[0-9a-f]{130}$/i; function createStatusPayload(content, messageType, clockValue, isJson) { - const tag = '~#c4'; + const tag = constants.messageTags.message; const oneMonthInMs = 60 * 60 * 24 * 31 * 1000; if(clockValue < (new Date().getTime())){ clockValue = (new Date().getTime() + oneMonthInMs) * 100; @@ -55,7 +46,7 @@ class StatusJS { this.shh = web3.shh; this.mailservers = new mailservers(web3); - await web3.shh.setMinPoW(POW_TARGET); + await web3.shh.setMinPoW(constants.post.POW_TARGET); _sig.set( this, privateKey ? await this.generateWhisperKeyFromWallet(privateKey) : await web3.shh.newKeyPair() @@ -151,9 +142,9 @@ class StatusJS { onUserMessage(cb) { this.userMessagesSubscription = this.shh.subscribe("messages", { - minPow: POW_TARGET, + minPow: constants.post.POW_TARGET, privateKeyID: _sig.get(this), - topics: [CONTACT_DISCOVERY_TOPIC], + topics: [constants.topics.CONTACT_DISCOVERY_TOPIC], allowP2P: true }).on('data', (data) => { if(!this.contacts[data.sig]){ @@ -165,9 +156,9 @@ class StatusJS { this.contacts[data.sig].lastClockValue = payloadArray[1][3]; } - if(payloadArray[0] == '~#c4'){ + if(payloadArray[0] == constants.messageTags.message){ cb(null, {payload: hexToAscii(data.payload), data: data, username: this.contacts[data.sig].username}); - } else if(payloadArray[0] == '~#c2') { + } else if(payloadArray[0] == constants.messageTags.chatRequest) { this.contacts[data.sig].displayName = payloadArray[1][0]; this.contacts[data.sig].profilePic = payloadArray[1][1]; @@ -194,11 +185,11 @@ class StatusJS { this.shh.post({ pubKey: contactCode, sig: _sig.get(this), - ttl: TTL, - topic: CONTACT_DISCOVERY_TOPIC, - payload: createStatusPayload(msg, USER_MESSAGE, this.contacts[contactCode].lastClockValue), - powTime: POW_TIME, - powTarget: POW_TARGET + ttl: constants.post.TTL, + topic: constants.topics.CONTACT_DISCOVERY_TOPIC, + payload: createStatusPayload(msg, constants.messageTypes.USER_MESSAGE, this.contacts[contactCode].lastClockValue), + powTime: constants.post.POW_TIME, + powTarget: constants.post.POW_TARGET }).then(() => { if (!cb) return; cb(null, true); @@ -219,11 +210,11 @@ class StatusJS { this.shh.post({ symKeyID: this.channels[channelName].channelKey, sig: _sig.get(this), - ttl: TTL, + ttl: constants.post.TTL, topic: this.channels[channelName].channelCode, - payload: createStatusPayload(msg, GROUP_MESSAGE, this.channels[channelName].lastClockValue ), - powTime: POW_TIME, - powTarget: POW_TARGET + payload: createStatusPayload(msg, constants.messageTypes.GROUP_MESSAGE, this.channels[channelName].lastClockValue ), + powTime: constants.post.POW_TIME, + powTarget: constants.post.POW_TARGET }).then(() => { if (!cb) return; cb(null, true); @@ -234,7 +225,7 @@ class StatusJS { } sendJsonMessage(destination, msg, cb) { - if (CONTACT_CODE_REGEXP.test(destination)) { + if (constants.regExp.CONTACT_CODE_REGEXP.test(destination)) { if(!this.contacts[destination]){ this.addContact(destination); } @@ -243,11 +234,11 @@ class StatusJS { this.shh.post({ pubKey: destination, sig: _sig.get(this), - ttl: TTL, - topic: CONTACT_DISCOVERY_TOPIC, - payload: createStatusPayload(msg, USER_MESSAGE, this.contacts[destination].lastClockValue, true), - powTime: POW_TIME, - powTarget: POW_TARGET + ttl: constants.post.TTL, + topic: constants.topics.CONTACT_DISCOVERY_TOPIC, + payload: createStatusPayload(msg, constants.messageTypes.USER_MESSAGE, this.contacts[destination].lastClockValue, true), + powTime: constants.post.POW_TIME, + powTarget: constants.post.POW_TARGET }).then(() => { if (!cb) return; cb(null, true); @@ -261,11 +252,11 @@ class StatusJS { this.shh.post({ symKeyID: this.channels[destination].channelKey, sig: _sig.get(this), - ttl: TTL, + ttl: constants.post.TTL, topic: this.channels[destination].channelCode, - payload: createStatusPayload(JSON.stringify(msg), GROUP_MESSAGE, this.channels[destination].lastClockValue, true), - powTime: POW_TIME, - powTarget: POW_TARGET + payload: createStatusPayload(JSON.stringify(msg), constants.messageTypes.GROUP_MESSAGE, this.channels[destination].lastClockValue, true), + powTime: constants.post.POW_TIME, + powTarget: constants.post.POW_TARGET }).then(() => { if (!cb) return; cb(null, true); @@ -277,7 +268,7 @@ class StatusJS { } sendMessage(destination, msg, cb){ - if (CONTACT_CODE_REGEXP.test(destination)) { + if (constants.regExp.CONTACT_CODE_REGEXP.test(destination)) { this.sendUserMessage(destination, msg, cb); } else { this.sendGroupMessage(destination, msg, cb);