From c9603b4e5563a2c243c074954d4c0723d8a8962f Mon Sep 17 00:00:00 2001 From: Julien Eluard Date: Tue, 28 May 2019 11:49:49 +0200 Subject: [PATCH] Moved to transit-js usage --- package.json | 1 + src/constants.ts | 8 ++++---- src/index.ts | 48 ++++++++++++++++++++++++------------------------ yarn.lock | 5 +++++ 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index b0f8785..1757098 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "chance": "^1.0.16", "ethereum-hdwallet": "0.0.19", "ethereumjs-util": "^6.0.0", + "transit-js": "0.8.861", "web3": "1.0.0-beta.36" }, "devDependencies": { diff --git a/src/constants.ts b/src/constants.ts index d27fae5..364c4fd 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -4,12 +4,12 @@ enum Topics { export default { messageTags: { - chatRequest: "~#c2", - message: "~#c4", + contactRequest: "c2", + message: "c4", }, messageTypes: { - GROUP_MESSAGE: "~:public-group-user-message", - USER_MESSAGE: "~:user-message", + GROUP_MESSAGE: "public-group-user-message", + USER_MESSAGE: "user-message", }, post: { POW_TARGET: 0.002, diff --git a/src/index.ts b/src/index.ts index 2a1645a..95faf0c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import utils from "./utils.js"; import mailservers from "./mailservers"; import constants from "./constants"; import Bourne from "bourne"; +import transit from "transit-js"; declare global { interface Window { web3: any; } @@ -14,6 +15,8 @@ if (typeof window !== "undefined") { const Web3 = typeof window !== "undefined" && window.web3 ? new web3Lib(window.web3.currentProvider) : web3Lib; const { utils: { stringToHex, hexToUtf8 } } = Web3; +const reader = transit.reader("json"); +const writer = transit.writer("json"); function createStatusPayload(content: string, messageType: string, clockValue: number, isJson = false) { const tag: string = constants.messageTags.message; @@ -24,13 +27,7 @@ function createStatusPayload(content: string, messageType: string, clockValue: n const contentType = (isJson ? "content/json" : "text/plain"); const timestamp = new Date().getTime(); - - return stringToHex( - JSON.stringify([ - tag, - [content, contentType, messageType, clockValue, timestamp, ["^ ", "~:text", content]], - ]), - ); + return stringToHex(writer.write([tag, [content, contentType, messageType, clockValue, timestamp, content]])); } const sig = new WeakMap(); @@ -42,7 +39,7 @@ class StatusJS { private mailservers: any; private isHttpProvider: boolean; private shh: any; - private chatRequestCb: any; + private contactRequestCb: any; constructor() { this.channels = {}; @@ -171,8 +168,8 @@ class StatusJS { } } - public onChatRequest(cb: any) { - this.chatRequestCb = cb; + public onContactRequest(cb: any) { + this.contactRequestCb = cb; } public onChannelMessage(channelName: string, cb: any) { @@ -191,11 +188,12 @@ class StatusJS { const messageHandler = (data: any) => { try { const username = utils.generateUsernameFromSeed(data.sig); - const payloadArray = Bourne.parse(hexToUtf8(data.payload)); - if (this.channels[channelName].lastClockValue < payloadArray[1][3]) { - this.channels[channelName].lastClockValue = payloadArray[1][3]; + const payload = reader.read(hexToUtf8(data.payload)); + const clockValue = payload[1][3]; + if (this.channels[channelName].lastClockValue < clockValue) { + this.channels[channelName].lastClockValue = clockValue; } - cb(null, {payload: hexToUtf8(data.payload), data, username}); + cb(null, {payload: payload, data, username}); } catch (err) { cb("Discarding invalid message received"); } @@ -236,19 +234,21 @@ class StatusJS { } try { - const payloadArray = Bourne.parse(hexToUtf8(data.payload)); - if (this.contacts[data.sig].lastClockValue < payloadArray[1][3]) { - this.contacts[data.sig].lastClockValue = payloadArray[1][3]; + const payload = reader.read(hexToUtf8(data.payload)); + const tag = payload[0]; + const clockValue = payload[1][3]; + if (this.contacts[data.sig].lastClockValue < clockValue) { + this.contacts[data.sig].lastClockValue = clockValue; } - if (payloadArray[0] === constants.messageTags.message) { - cb(null, {payload: hexToUtf8(data.payload), data, username: this.contacts[data.sig].username}); - } else if (payloadArray[0] === constants.messageTags.chatRequest) { - this.contacts[data.sig].displayName = payloadArray[1][0]; - this.contacts[data.sig].profilePic = payloadArray[1][1]; + if (tag === constants.messageTags.message) { + cb(null, {payload: payload, data, username: this.contacts[data.sig].username}); + } else if (tag === constants.messageTags.contactRequest) { + this.contacts[data.sig].displayName = payload[1][0]; + this.contacts[data.sig].profilePic = payload[1][1]; - if (this.chatRequestCb) { - this.chatRequestCb(null, { + if (this.contactRequestCb) { + this.contactRequestCb(null, { displayName: this.contacts[data.sig].displayName, profilePic: this.contacts[data.sig].profilePic, username: this.contacts[data.sig].username, diff --git a/yarn.lock b/yarn.lock index 566b469..760dbe3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5443,6 +5443,11 @@ tough-cookie@~2.4.3: psl "^1.1.24" punycode "^1.4.1" +transit-js@^0.8.861: + version "0.8.861" + resolved "https://registry.yarnpkg.com/transit-js/-/transit-js-0.8.861.tgz#829e516b80349a41fff5d59f5e6993b5473f72c9" + integrity sha512-4O9OrYPZw6C0M5gMTvaeOp+xYz6EF79JsyxIvqXHlt+pisSrioJWFOE80N8aCPoJLcNaXF442RZrVtdmd4wkDQ== + trim-newlines@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-2.0.0.tgz#b403d0b91be50c331dfc4b82eeceb22c3de16d20"