diff --git a/src/constants.js b/src/constants.ts similarity index 80% rename from src/constants.js rename to src/constants.ts index 0d19f86..0bedd28 100644 --- a/src/constants.js +++ b/src/constants.ts @@ -1,3 +1,7 @@ +enum Topics { + CONTACT_DISCOVERY_TOPIC = '0xf8946aac' +} + module.exports = { post: { POW_TIME: 1, @@ -12,10 +16,10 @@ module.exports = { message: "~#c4", chatRequest: "~#c2" }, - topics: { - CONTACT_DISCOVERY_TOPIC: '0xf8946aac' - }, + topics: Topics, regExp: { CONTACT_CODE_REGEXP: /^(0x)?[0-9a-f]{130}$/i } }; + + diff --git a/src/index.ts b/src/index.ts index 3dcf687..48a3eaf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ const Web3 = require("web3"); import utils from "./utils.js"; import mailservers from "./mailservers.js"; -const constants = require("./constants"); +import constants from "./constants.js"; const { utils: { asciiToHex, hexToAscii } } = Web3; diff --git a/src/mailservers.ts b/src/mailservers.ts index 3a74b0c..4833a64 100644 --- a/src/mailservers.ts +++ b/src/mailservers.ts @@ -1,4 +1,7 @@ import mailserverList from "./data/mailservers.json"; +import constants from "./constants.js"; + +const Topics = constants.topics; class MailServers { private web3: any; @@ -60,7 +63,7 @@ class MailServers { }); } - public async requestMessages(topic: string, options: any, cb?: any) { + public async requestMessages(topic: string|Topics, options: any, cb?: any) { if (this.mailserver === "") { if (!cb) { return; @@ -68,7 +71,7 @@ class MailServers { return cb("Mailserver is not set", false); } - const topics = [this.web3.utils.sha3(topic).slice(0, 10)]; + const topics = [(topic in Topics) ? Topics[topic] : this.web3.utils.sha3(topic).slice(0, 10)]; const mailserverPeer = this.mailserver; const timeout = options.timeout || 30; // seconds diff --git a/test-mailserver.js b/test-mailserver.js index bd55ded..e8259a3 100644 --- a/test-mailserver.js +++ b/test-mailserver.js @@ -1,4 +1,4 @@ -var StatusJS = require('./src/index.js'); +var StatusJS = require('./dist/index.js'); (async () => { var status = new StatusJS(); @@ -19,4 +19,4 @@ var status = new StatusJS(); }); setInterval(() => { }, 3000); -})() +})();