mirror of
https://github.com/status-im/status-js-api.git
synced 2025-02-21 13:48:09 +00:00
Added a constant file and replaced hardcoded values for constants
This commit is contained in:
parent
b0dd0324a0
commit
95a56fd35e
21
src/constants.js
Normal file
21
src/constants.js
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
63
src/index.js
63
src/index.js
@ -1,22 +1,13 @@
|
|||||||
const Web3 = require('web3');
|
const Web3 = require('web3');
|
||||||
const utils = require('./utils.js');
|
const utils = require('./utils.js');
|
||||||
const mailservers = require('./mailservers.js');
|
const mailservers = require('./mailservers.js');
|
||||||
|
const constants = require('./constants');
|
||||||
|
|
||||||
const { utils: { asciiToHex, hexToAscii, sha3 } } = Web3;
|
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) {
|
function createStatusPayload(content, messageType, clockValue, isJson) {
|
||||||
const tag = '~#c4';
|
const tag = constants.messageTags.message;
|
||||||
const oneMonthInMs = 60 * 60 * 24 * 31 * 1000;
|
const oneMonthInMs = 60 * 60 * 24 * 31 * 1000;
|
||||||
if(clockValue < (new Date().getTime())){
|
if(clockValue < (new Date().getTime())){
|
||||||
clockValue = (new Date().getTime() + oneMonthInMs) * 100;
|
clockValue = (new Date().getTime() + oneMonthInMs) * 100;
|
||||||
@ -55,7 +46,7 @@ class StatusJS {
|
|||||||
this.shh = web3.shh;
|
this.shh = web3.shh;
|
||||||
this.mailservers = new mailservers(web3);
|
this.mailservers = new mailservers(web3);
|
||||||
|
|
||||||
await web3.shh.setMinPoW(POW_TARGET);
|
await web3.shh.setMinPoW(constants.post.POW_TARGET);
|
||||||
_sig.set(
|
_sig.set(
|
||||||
this,
|
this,
|
||||||
privateKey ? await this.generateWhisperKeyFromWallet(privateKey) : await web3.shh.newKeyPair()
|
privateKey ? await this.generateWhisperKeyFromWallet(privateKey) : await web3.shh.newKeyPair()
|
||||||
@ -151,9 +142,9 @@ class StatusJS {
|
|||||||
|
|
||||||
onUserMessage(cb) {
|
onUserMessage(cb) {
|
||||||
this.userMessagesSubscription = this.shh.subscribe("messages", {
|
this.userMessagesSubscription = this.shh.subscribe("messages", {
|
||||||
minPow: POW_TARGET,
|
minPow: constants.post.POW_TARGET,
|
||||||
privateKeyID: _sig.get(this),
|
privateKeyID: _sig.get(this),
|
||||||
topics: [CONTACT_DISCOVERY_TOPIC],
|
topics: [constants.topics.CONTACT_DISCOVERY_TOPIC],
|
||||||
allowP2P: true
|
allowP2P: true
|
||||||
}).on('data', (data) => {
|
}).on('data', (data) => {
|
||||||
if(!this.contacts[data.sig]){
|
if(!this.contacts[data.sig]){
|
||||||
@ -165,9 +156,9 @@ class StatusJS {
|
|||||||
this.contacts[data.sig].lastClockValue = payloadArray[1][3];
|
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});
|
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].displayName = payloadArray[1][0];
|
||||||
this.contacts[data.sig].profilePic = payloadArray[1][1];
|
this.contacts[data.sig].profilePic = payloadArray[1][1];
|
||||||
|
|
||||||
@ -194,11 +185,11 @@ class StatusJS {
|
|||||||
this.shh.post({
|
this.shh.post({
|
||||||
pubKey: contactCode,
|
pubKey: contactCode,
|
||||||
sig: _sig.get(this),
|
sig: _sig.get(this),
|
||||||
ttl: TTL,
|
ttl: constants.post.TTL,
|
||||||
topic: CONTACT_DISCOVERY_TOPIC,
|
topic: constants.topics.CONTACT_DISCOVERY_TOPIC,
|
||||||
payload: createStatusPayload(msg, USER_MESSAGE, this.contacts[contactCode].lastClockValue),
|
payload: createStatusPayload(msg, constants.messageTypes.USER_MESSAGE, this.contacts[contactCode].lastClockValue),
|
||||||
powTime: POW_TIME,
|
powTime: constants.post.POW_TIME,
|
||||||
powTarget: POW_TARGET
|
powTarget: constants.post.POW_TARGET
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
if (!cb) return;
|
if (!cb) return;
|
||||||
cb(null, true);
|
cb(null, true);
|
||||||
@ -219,11 +210,11 @@ class StatusJS {
|
|||||||
this.shh.post({
|
this.shh.post({
|
||||||
symKeyID: this.channels[channelName].channelKey,
|
symKeyID: this.channels[channelName].channelKey,
|
||||||
sig: _sig.get(this),
|
sig: _sig.get(this),
|
||||||
ttl: TTL,
|
ttl: constants.post.TTL,
|
||||||
topic: this.channels[channelName].channelCode,
|
topic: this.channels[channelName].channelCode,
|
||||||
payload: createStatusPayload(msg, GROUP_MESSAGE, this.channels[channelName].lastClockValue ),
|
payload: createStatusPayload(msg, constants.messageTypes.GROUP_MESSAGE, this.channels[channelName].lastClockValue ),
|
||||||
powTime: POW_TIME,
|
powTime: constants.post.POW_TIME,
|
||||||
powTarget: POW_TARGET
|
powTarget: constants.post.POW_TARGET
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
if (!cb) return;
|
if (!cb) return;
|
||||||
cb(null, true);
|
cb(null, true);
|
||||||
@ -234,7 +225,7 @@ class StatusJS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sendJsonMessage(destination, msg, cb) {
|
sendJsonMessage(destination, msg, cb) {
|
||||||
if (CONTACT_CODE_REGEXP.test(destination)) {
|
if (constants.regExp.CONTACT_CODE_REGEXP.test(destination)) {
|
||||||
if(!this.contacts[destination]){
|
if(!this.contacts[destination]){
|
||||||
this.addContact(destination);
|
this.addContact(destination);
|
||||||
}
|
}
|
||||||
@ -243,11 +234,11 @@ class StatusJS {
|
|||||||
this.shh.post({
|
this.shh.post({
|
||||||
pubKey: destination,
|
pubKey: destination,
|
||||||
sig: _sig.get(this),
|
sig: _sig.get(this),
|
||||||
ttl: TTL,
|
ttl: constants.post.TTL,
|
||||||
topic: CONTACT_DISCOVERY_TOPIC,
|
topic: constants.topics.CONTACT_DISCOVERY_TOPIC,
|
||||||
payload: createStatusPayload(msg, USER_MESSAGE, this.contacts[destination].lastClockValue, true),
|
payload: createStatusPayload(msg, constants.messageTypes.USER_MESSAGE, this.contacts[destination].lastClockValue, true),
|
||||||
powTime: POW_TIME,
|
powTime: constants.post.POW_TIME,
|
||||||
powTarget: POW_TARGET
|
powTarget: constants.post.POW_TARGET
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
if (!cb) return;
|
if (!cb) return;
|
||||||
cb(null, true);
|
cb(null, true);
|
||||||
@ -261,11 +252,11 @@ class StatusJS {
|
|||||||
this.shh.post({
|
this.shh.post({
|
||||||
symKeyID: this.channels[destination].channelKey,
|
symKeyID: this.channels[destination].channelKey,
|
||||||
sig: _sig.get(this),
|
sig: _sig.get(this),
|
||||||
ttl: TTL,
|
ttl: constants.post.TTL,
|
||||||
topic: this.channels[destination].channelCode,
|
topic: this.channels[destination].channelCode,
|
||||||
payload: createStatusPayload(JSON.stringify(msg), GROUP_MESSAGE, this.channels[destination].lastClockValue, true),
|
payload: createStatusPayload(JSON.stringify(msg), constants.messageTypes.GROUP_MESSAGE, this.channels[destination].lastClockValue, true),
|
||||||
powTime: POW_TIME,
|
powTime: constants.post.POW_TIME,
|
||||||
powTarget: POW_TARGET
|
powTarget: constants.post.POW_TARGET
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
if (!cb) return;
|
if (!cb) return;
|
||||||
cb(null, true);
|
cb(null, true);
|
||||||
@ -277,7 +268,7 @@ class StatusJS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(destination, msg, cb){
|
sendMessage(destination, msg, cb){
|
||||||
if (CONTACT_CODE_REGEXP.test(destination)) {
|
if (constants.regExp.CONTACT_CODE_REGEXP.test(destination)) {
|
||||||
this.sendUserMessage(destination, msg, cb);
|
this.sendUserMessage(destination, msg, cb);
|
||||||
} else {
|
} else {
|
||||||
this.sendGroupMessage(destination, msg, cb);
|
this.sendGroupMessage(destination, msg, cb);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user