我一直在努力弄清楚如何使用web3与窃窃私语交互。我已经能够成功地使用geth console发送(并接收)一条消息,但无法使用web3.js进行同样的操作。
你会提供一个短的JavaScript片段,成功地发送和接收一条耳语消息吗?
发布于 2018-02-08 09:59:02
在运行Geth时,(我使用了geth --rinkeby --fast --cache=512 console --shh --rpc --rpccorsdomain '*' --ws --wsorigins '*')和浏览器中的web3.js v1,输入浏览器控制台:
> 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发布于 2017-12-04 16:46:44
此示例适用于Whisper v5:https://github.com/gballet/whisper-chat-example
https://ethereum.stackexchange.com/questions/32314
复制相似问题