Merge pull request #18 from status-im/transit-js
Moved to transit-js usage
This commit is contained in:
commit
ef12d55dfd
|
@ -40,6 +40,7 @@
|
||||||
"chance": "^1.0.16",
|
"chance": "^1.0.16",
|
||||||
"ethereum-hdwallet": "0.0.19",
|
"ethereum-hdwallet": "0.0.19",
|
||||||
"ethereumjs-util": "^6.0.0",
|
"ethereumjs-util": "^6.0.0",
|
||||||
|
"transit-js": "0.8.861",
|
||||||
"web3": "1.0.0-beta.36"
|
"web3": "1.0.0-beta.36"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -4,12 +4,12 @@ enum Topics {
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
messageTags: {
|
messageTags: {
|
||||||
chatRequest: "~#c2",
|
contactRequest: "c2",
|
||||||
message: "~#c4",
|
message: "c4",
|
||||||
},
|
},
|
||||||
messageTypes: {
|
messageTypes: {
|
||||||
GROUP_MESSAGE: "~:public-group-user-message",
|
GROUP_MESSAGE: "public-group-user-message",
|
||||||
USER_MESSAGE: "~:user-message",
|
USER_MESSAGE: "user-message",
|
||||||
},
|
},
|
||||||
post: {
|
post: {
|
||||||
POW_TARGET: 0.002,
|
POW_TARGET: 0.002,
|
||||||
|
|
48
src/index.ts
48
src/index.ts
|
@ -3,6 +3,7 @@ import utils from "./utils.js";
|
||||||
import mailservers from "./mailservers";
|
import mailservers from "./mailservers";
|
||||||
import constants from "./constants";
|
import constants from "./constants";
|
||||||
import Bourne from "bourne";
|
import Bourne from "bourne";
|
||||||
|
import transit from "transit-js";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window { web3: any; }
|
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 Web3 = typeof window !== "undefined" && window.web3 ? new web3Lib(window.web3.currentProvider) : web3Lib;
|
||||||
const { utils: { stringToHex, hexToUtf8 } } = Web3;
|
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) {
|
function createStatusPayload(content: string, messageType: string, clockValue: number, isJson = false) {
|
||||||
const tag: string = constants.messageTags.message;
|
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 contentType = (isJson ? "content/json" : "text/plain");
|
||||||
const timestamp = new Date().getTime();
|
const timestamp = new Date().getTime();
|
||||||
|
return stringToHex(writer.write([tag, [content, contentType, messageType, clockValue, timestamp, content]]));
|
||||||
return stringToHex(
|
|
||||||
JSON.stringify([
|
|
||||||
tag,
|
|
||||||
[content, contentType, messageType, clockValue, timestamp, ["^ ", "~:text", content]],
|
|
||||||
]),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const sig = new WeakMap();
|
const sig = new WeakMap();
|
||||||
|
@ -42,7 +39,7 @@ class StatusJS {
|
||||||
private mailservers: any;
|
private mailservers: any;
|
||||||
private isHttpProvider: boolean;
|
private isHttpProvider: boolean;
|
||||||
private shh: any;
|
private shh: any;
|
||||||
private chatRequestCb: any;
|
private contactRequestCb: any;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.channels = {};
|
this.channels = {};
|
||||||
|
@ -171,8 +168,8 @@ class StatusJS {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public onChatRequest(cb: any) {
|
public onContactRequest(cb: any) {
|
||||||
this.chatRequestCb = cb;
|
this.contactRequestCb = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
public onChannelMessage(channelName: string, cb: any) {
|
public onChannelMessage(channelName: string, cb: any) {
|
||||||
|
@ -191,11 +188,12 @@ class StatusJS {
|
||||||
const messageHandler = (data: any) => {
|
const messageHandler = (data: any) => {
|
||||||
try {
|
try {
|
||||||
const username = utils.generateUsernameFromSeed(data.sig);
|
const username = utils.generateUsernameFromSeed(data.sig);
|
||||||
const payloadArray = Bourne.parse(hexToUtf8(data.payload));
|
const payload = reader.read(hexToUtf8(data.payload));
|
||||||
if (this.channels[channelName].lastClockValue < payloadArray[1][3]) {
|
const clockValue = payload[1][3];
|
||||||
this.channels[channelName].lastClockValue = payloadArray[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) {
|
} catch (err) {
|
||||||
cb("Discarding invalid message received");
|
cb("Discarding invalid message received");
|
||||||
}
|
}
|
||||||
|
@ -236,19 +234,21 @@ class StatusJS {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const payloadArray = Bourne.parse(hexToUtf8(data.payload));
|
const payload = reader.read(hexToUtf8(data.payload));
|
||||||
if (this.contacts[data.sig].lastClockValue < payloadArray[1][3]) {
|
const tag = payload[0];
|
||||||
this.contacts[data.sig].lastClockValue = payloadArray[1][3];
|
const clockValue = payload[1][3];
|
||||||
|
if (this.contacts[data.sig].lastClockValue < clockValue) {
|
||||||
|
this.contacts[data.sig].lastClockValue = clockValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (payloadArray[0] === constants.messageTags.message) {
|
if (tag === constants.messageTags.message) {
|
||||||
cb(null, {payload: hexToUtf8(data.payload), data, username: this.contacts[data.sig].username});
|
cb(null, {payload: payload, data, username: this.contacts[data.sig].username});
|
||||||
} else if (payloadArray[0] === constants.messageTags.chatRequest) {
|
} else if (tag === constants.messageTags.contactRequest) {
|
||||||
this.contacts[data.sig].displayName = payloadArray[1][0];
|
this.contacts[data.sig].displayName = payload[1][0];
|
||||||
this.contacts[data.sig].profilePic = payloadArray[1][1];
|
this.contacts[data.sig].profilePic = payload[1][1];
|
||||||
|
|
||||||
if (this.chatRequestCb) {
|
if (this.contactRequestCb) {
|
||||||
this.chatRequestCb(null, {
|
this.contactRequestCb(null, {
|
||||||
displayName: this.contacts[data.sig].displayName,
|
displayName: this.contacts[data.sig].displayName,
|
||||||
profilePic: this.contacts[data.sig].profilePic,
|
profilePic: this.contacts[data.sig].profilePic,
|
||||||
username: this.contacts[data.sig].username,
|
username: this.contacts[data.sig].username,
|
||||||
|
|
|
@ -5443,6 +5443,11 @@ tough-cookie@~2.4.3:
|
||||||
psl "^1.1.24"
|
psl "^1.1.24"
|
||||||
punycode "^1.4.1"
|
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:
|
trim-newlines@^2.0.0:
|
||||||
version "2.0.0"
|
version "2.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-2.0.0.tgz#b403d0b91be50c331dfc4b82eeceb22c3de16d20"
|
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-2.0.0.tgz#b403d0b91be50c331dfc4b82eeceb22c3de16d20"
|
||||||
|
|
Loading…
Reference in New Issue