first stab at attempting to send/receive messages

This commit is contained in:
Iuri Matias 2018-11-06 07:17:07 -05:00
parent ede6ea2845
commit 80484d0d8c
4 changed files with 2562 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

2472
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

22
package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "status-js",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/status-im/status-js.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/status-im/status-js/issues"
},
"homepage": "https://github.com/status-im/status-js#readme",
"dependencies": {
"web3": "^1.0.0-beta.36"
}
}

67
src/index.js Normal file
View File

@ -0,0 +1,67 @@
const Web3 = require('web3');
const { utils: { asciiToHex, hexToAscii, sha3 } } = Web3;
const POW_TIME = 1;
const TTL = 10;
const POW_TARGET = 0.002;
const CHANNEL = Web3.utils.sha3("status").slice(0, 10);
function createStatusPayload(
{
tag = '~#c4',
content = 'Hello everyone',
messageType = '~:public-group-user-message',
clockValue = (new Date().getTime()) * 100,
contentType = 'text/plain',
timestamp = new Date().getTime(),
} = {},
) {
return asciiToHex(
JSON.stringify([
tag,
[
content,
contentType,
messageType,
clockValue,
timestamp,
],
]),
);
}
(async () => {
let web3 = new Web3();
web3.setProvider(new Web3.providers.WebsocketProvider('ws://localhost:8546', {headers: {Origin: "http://localhost:8080"}}));
let keys = {};
keys.symKeyID = await web3.shh.newSymKey();
keys.sig = await web3.shh.newKeyPair();
console.dir("keys generated");
console.dir(keys);
subscription = web3.shh.subscribe("messages", {
symKeyID: keys.symKeyID,
topics: [CHANNEL]
}).on('data', (data) => {
console.dir("message received!");
console.dir(data);
});
web3.shh.post({
symKeyID: keys.symKeyID, // encrypts using the sym key ID
sig: keys.sig, // signs the message using the keyPair ID
ttl: TTL,
topic: CHANNEL,
payload: createStatusPayload("hello there"),
powTime: POW_TIME,
powTarget: POW_TARGET
}).then(() => {
console.dir('message sent!');
})
})()