Created
April 25, 2019 12:06
-
-
Save kdeme/17006627154cbb796df845a0b7a14fb2 to your computer and use it in GitHub Desktop.
whisper web3 tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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.2) | |
.then(console.log); | |
// choose a common channel name | |
let channel = 'nimbus-test-whisper' | |
// Topic = keccak256 of channel name, first 10 chars | |
let topic = web3.utils.sha3(channel).substring(0,10); | |
function readMessages(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 to topic and poll for messages | |
// SymKey generated from channel name | |
web3.shh.generateSymKeyFromPassword(channel, function (error, symKey) { | |
web3.shh.newMessageFilter({ | |
symKeyID: symKey, | |
topics: [topic] | |
}).then(function(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 so far.") | |
} | |
}); | |
// poll for messages every 2 seconds | |
setInterval(readMessages.bind(null,filter), 2000); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/nodejs | |
const Web3 = require('web3'); | |
const web3 = new Web3(Web3.givenProvider || new Web3.providers.HttpProvider('HTTP://127.0.0.1:8546'), 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.2) | |
.then(console.log); | |
// choose a common channel name | |
let channel = 'nimbus-test-whisper' | |
// Topic = keccak256 of channel name, first 10 chars | |
let topic = web3.utils.sha3(channel).substring(0,10); | |
// Message post to topic, with SymKey from channel name | |
// No signature added | |
web3.shh.generateSymKeyFromPassword(channel, function (error, symKey) { | |
web3.shh.post({ | |
symKeyID: symKey, | |
ttl: 30, | |
topic: topic, | |
powTarget: 0.2, | |
powTime: 1.5, | |
payload: web3.utils.asciiToHex("Hello nimbus!") | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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.2) | |
.then(console.log); | |
let topic = '0x12345678' | |
// Subscribe and post to topic with symmetric key | |
web3.shh.addSymKey('0x0000000000000000000000000000000000000000000000000000000000000001', function (error, result) { | |
web3.shh.newMessageFilter({ | |
symKeyID: result, | |
topics: [topic] | |
}).then(function(filter) { | |
web3.shh.post({ | |
symKeyID: result, | |
ttl: 30, | |
topic: topic, | |
powTarget: 0.2, | |
powTime: 1.5, | |
payload: web3.utils.asciiToHex("Hello nimbus!") | |
}).then( | |
web3.shh.getFilterMessages(filter) | |
.then(function(msgs) { | |
console.log(web3.utils.hexToUtf8(msgs[0].payload)); | |
}) | |
) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment