Skip to content

Instantly share code, notes, and snippets.

@kdeme
Created April 23, 2019 18:43
Show Gist options
  • Save kdeme/ca9c9bf0ab6f832d2ab5b69b7a8dc6d9 to your computer and use it in GitHub Desktop.
Save kdeme/ca9c9bf0ab6f832d2ab5b69b7a8dc6d9 to your computer and use it in GitHub Desktop.
whisper-test
#!/usr/bin/nodejs
const Web3 = require('web3');
const web3 = new Web3(Web3.givenProvider || new Web3.providers.HttpProvider('HTTP://127.0.0.1:8545'), null, {});
web3.shh.getVersion()
.then(function(result) {console.log("Get Whisper version:", result)});
web3.shh.getInfo()
.then(function(result) {console.log("Get Whisper info:", result)});
web3.shh.setMaxMessageSize(1000000)
.then(console.log);
// web3.shh.setMinPoW(0.001)
// .then(console.log);
web3.shh.setMinPoW(0.0000000001)
.then(console.log);
// web3.shh.markTrustedPeer("",
// function(error, result) {
// console.log("marking trusted peer:", result);
// // console.log("marking trusted peer:", error);
// });
// SymKey subscribe and post
// web3.shh.addSymKey('0x0000000000000000000000000000000000000000000000000000000000000001', function (error, result) {
// web3.shh.newMessageFilter(
// {
// symKeyID: result,
// topics: ['0x12345678']
// }
// ).then(function(filter) {
// web3.shh.post({
// symKeyID: result,
// ttl: 30,
// topic: '0x12345678',
// powTarget: 0.2,
// powTime: 1.1,
// payload: web3.utils.asciiToHex("Hello nimbus!")
// }).then(
// web3.shh.getFilterMessages(filter)
// .then(function(msgs) {
// console.log(web3.utils.hexToUtf8(msgs[0].payload));
// })
// )
// });
// });
// Status message subscribe
let channel = 'nimbus-test-whisper'
let topic = web3.utils.sha3(channel).substring(0,10);
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function waitAndGetMessages(duration, filterId) {
console.log("Reading message queue in", duration, "ms, go post something!")
await sleep(duration)
console.log(filterId)
web3.shh.getFilterMessages(filterId)
.then(function(msgs) {
// console.log(msgs)
if(msgs.length > 0) {
console.log(web3.utils.hexToUtf8(msgs[0].payload));
}
else {
console.log("Nothing arrived")
}
})
}
// subscribe without signature
web3.shh.generateSymKeyFromPassword(channel, function (error, symKey) {
// console.debug(symKey);
web3.shh.newMessageFilter(
{
symKeyID: symKey,
topics: [topic]
}
).then(function(filter) {
console.log(filter)
web3.shh.getFilterMessages(filter)
.then(function(msgs) {
if(msgs.length > 0) {
console.log(web3.utils.hexToUtf8(msg[0].payload));
}
else {
console.log("Nothing arrived yet")
}
})
waitAndGetMessages(10000, filter)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment