He estado tratando de descubrir cómo interactuar con susurro usando web3. Pude enviar (y recibir) correctamente un mensaje usando geth console
, pero no pude hacer lo mismo usando web3.js.
¿Proporcionaría un fragmento breve de JavaScript que envíe y reciba correctamente un mensaje susurrado?
Con Geth ejecutándose (usé geth --rinkeby --fast --cache=512 console --shh --rpc --rpccorsdomain '*' --ws --wsorigins '*'
) y web3.js v1 en el navegador, escriba en la consola del navegador:
> web3.shh.setProvider("ws://localhost:8546") // must use websockets (read this on https://ethereum.stackexchange.com/a/34840/6014)
> web3.shh.newKeyPair().then(console.log) // logs key pair
> web3.shh.subscribe("messages", {privateKeyID: keyPair}, (err, msg) => console.log("msg received!", msg)) // returns subscription that will log the message when it's received
> web3.shh.getPublicKey(keyPair).then(console.log) // logs public key
> web3.shh.post({pubKey: publicKey, ttl: 10, payload: '0xffffffdddddd1122', powTime: 3, powTarget: 0.5}) // sends the message
Este ejemplo funciona para Whisper v5: https://github.com/gballet/whisper-chat-example
Miao Zhi Cheng