首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Whisper上发送1-N信号或广播消息?

如何在Whisper上发送1-N信号或广播消息?
EN

Ethereum用户
提问于 2018-06-27 17:12:14
回答 2查看 726关注 0票数 4

来自语音维基

低延迟,1-1或1-N信令消息.

Shh.post(自我,消息:)shh.post /低语用法

创建一个窃窃私语消息并将其注入网络以供分发。

示例使用web3.shh.post,有关详细信息,请参阅回答:

代码语言:javascript
复制
web3.shh.post({
  pubKey: 'PUBLIC_KEY_OF_THE_RECEIVER',
  ttl: 3600,
  topic: '0x07678231',
  powTarget: 2.01,
  powTime: 2,
  payload: web3.fromAscii("Hello there!")
  });

当我将pubKey作为参数从web3.shh.post中移除时,它会说:Error: specify either a symmetric or an asymmetric key

shh.post({ "topic": t, "payload": p });没有签名,没有加密:匿名广播;有点像一个匿名主题过滤的twitter提要。

问:,因为web3.shh.post()要求我们提供接收者的单个公钥;是否可以使用whisper protocol发送1-N或广播消息?如果是,怎么做?

EN

回答 2

Ethereum用户

回答已采纳

发布于 2018-07-08 16:28:05

我建议使用以下文档:https://github.com/ethereum/go-ethereum/wiki/Whisper

低语用法

您发布的链接已经过时,而且还适用于Whisperv2.0

答:如果您为所有收件人提供相同的symKey,则可能有1:N消息。

我发布的链接是为Whisper5.0提供的,当前版本的Whisper是6.0,但是v6的API与v5几乎完全相同。

节点上的

-1‘S-客户端:

代码语言:javascript
复制
generatedSymKey=shh.generateSymKeyFromPassword("hello");    
symKey=shh.getSymKey(generatedSymKey)    
symKeyID=shh.addSymKey(symKey) //ex: "d5212e736703afbb21246e8acd192e4345ea910398544d765ed6b49f0ec524b5"
filter = web3.shh.newMessageFilter(
        {symKeyID:symKeyID, topic: '0x07678231'}, 
        function(err, res) {console.log(web3.toUtf8(res.payload))});

On node-2's geth-client:

代码语言:javascript
复制
generatedSymKey=shh.generateSymKeyFromPassword("hello")    
symKey=shh.getSymKey(generatedSymKey)    
symKeyID=shh.addSymKey(symKey) //ex: "c4c4cecf6ad9499c2386b8ce6416f44684e042e491726b38315646c0b4afadc6"
filter = web3.shh.newMessageFilter(
        {symKeyID:symKeyID, topic: '0x07678231'}, 
        function(err, res) {console.log(web3.toUtf8(res.payload))});

在另一个节点上发送消息,这两个消息都显示在节点-1和节点-2上,即使symKeyIDshh.post()函数上的节点-1‘S或节点-2’S。

node-1's geth-client上运行以下代码:

节点-1‘S symKeyID给出:

代码语言:javascript
复制
web3.shh.post({
  symKeyID: 'd5212e736703afbb21246e8acd192e4345ea910398544d765ed6b49f0ec524b5', //symKeyID of the node-1
  ttl: 10,
  topic: '0x07678231',
  powTarget: 2.01,
  powTime: 2,
  payload: web3.fromAscii("Hello there!")
  });

node-2's geth-client上运行以下代码:

节点-2‘S symKeyID给出:

代码语言:javascript
复制
web3.shh.post({
  symKeyID: 'c4c4cecf6ad9499c2386b8ce6416f44684e042e491726b38315646c0b4afadc6', //symKeyID of the node-2
  ttl: 10,
  topic: '0x07678231',
  powTarget: 2.01,
  powTime: 2,
  payload: web3.fromAscii("Hello there!")
  });
票数 3
EN

Ethereum用户

发布于 2018-09-07 18:18:03

使用

解决方案的Web3.py:

我跟踪了ssh模式码

运行在节点-1上的receiver.py

代码语言:javascript
复制
from os.path import expanduser
from web3 import HTTPProvider, Web3
import asyncio
import time


def handle_event(event):
    print(f"recipientPublicKey={event['recipientPublicKey'].hex()}")
    print(event["payload"].decode("utf-8"))


async def log_loop(filter_id, poll_interval):
    while True:
        for event in w3.geth.shh.getMessages(filter_id):  # event_filter.get_new_entries():
            handle_event(event)  # TODO: add try catch
        await asyncio.sleep(poll_interval)
        time.sleep(0.1)

if __name__ == "__main__":
    w3 = Web3(HTTPProvider("http://localhost:8545"))
    home = expanduser("~")
    topic = "0x07678231"

    print("Initializing...")
    key_id = w3.geth.shh.newKeyPair()  # generates a new public and private key pair for message decryption and encryption.
    public_key = w3.geth.shh.getPublicKey(key_id)
    filter_id = w3.geth.shh.new_message_filter({"topic": topic, "privateKeyID": key_id, "recipientPublicKey": public_key})
    print(f"my_public_key={public_key}")

    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(asyncio.gather(log_loop(filter_id, 2)))
    finally:
        loop.close()

输出:

代码语言:javascript
复制
my_public_key=0x0429824f14966d802e8a12b09c276361cdf13eda4dd5f4f776d0a2a4db61ba4e24683be84df2b521ff8c6290e3851bf9b59a8e9a7e13345fee603de9be862f5ef3

Node_1's消息发送之后,我在下面的代码中将receiverPublicKey复制到receiver_pub变量中。我在Node 2上运行以下代码。Node 2向网络发送一条消息。稍后,我在Node_1上按enter键,Node_1打印消息

代码语言:javascript
复制
from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://localhost:8545'))

# obtained from node_1 and assigned here.
receiver_pub =
"0x0429824f14966d802e8a12b09c276361cdf13eda4dd5f4f776d0a2a4db61ba4e24683be84df2b521ff8c6290e3851bf9b59a8e9a7e13345fee603de9be862f5ef3"

topic = '0x07678231'
payloads = [web3.toHex(text="test message :)"), web3.toHex(text="2nd test message")]

web3.geth.shh.post(
    {"powTarget": 2.5, "powTime": 2, "ttl": 60, "payload": payloads[0], "topic": topic, "pubKey": receiver_pub}
)
票数 3
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/52218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档