3

Good afternoon. I'm trying to implement the "Status" at the moment. I use the library Web3.shh. I sent the message and gives the answer "True", but I can not receive all the messages on the other address. In the documentation of the Ethereum and in the documentation "Status" I did not find any answers. Tell me how to correctly pull messages from the Geth at? Here's the code:

from web3 import shh
from web3 import HTTPProvider, Web3

status_host = 'https://host:port''
privatekey = '0x....7f2afa922ffdbeac65867f544a....'

connect = Web3(HTTPProvider(status_host))
print('connect status ===> ', connect.isConnected())

ms = shh.Shh(connect)
print('info ===>>>> ', ms.info)

id = ms.addPrivateKey(key=privatekey)
print('id ===>>>> ', id)

user_address = ms.getPublicKey(id)
print('user_address ===>>> ', user_address)

privkey = ms.getPrivateKey(id)
print("privkey => ", privkey)

topic = Web3.toHex(b'AS')
print("topic => ", topic)
text = 'test message'
address_to = '0x0487be55c072702a0e4da72158a7432281e8c26aca9501cd0bfeea726dc85f2611e96884e8fc4807c95c04c04af3387b83350a27cc18b96c37543e0f9a41ae47b5'

mes_send = ms.post(
    {
        'ttl': 20,
        'payload': Web3.toHex(text=text),
        'pubKey': address_to,
        'topic': topic,
        'powTarget': 2.5,
        'powTime': 2,
    }
)

if mes_send == True:
    print('Status message => Send')
else:
    print('Message not send')
  • I found a solution. In the official documentation 'WEB3.PY -> SHH' everything is described well, except for the indication that the filter should be started in a parallel process. Those. it is necessary to send and receive the file into two files + run the file in an infinite loop with a certain frequency of listening on the filter. The code itself will tell all for itself)) – Sergey Zaharov May 21 '18 at 7:06
1

I found a solution. In the official documentation 'WEB3.PY -> SHH' everything is described well, except for the indication that the filter should be started in a parallel process. Those. it is necessary to send and receive the file into two files + run the file in an infinite loop with a certain frequency of listening on the filter. The code itself will tell all for itself))

from web3 import shh
from web3 import HTTPProvider, Web3
status_host = 'http://****:9090'
privatekey = '0x5eea64de8906369612593480b7f2a...'
connect = Web3(HTTPProvider(status_host))
print('connect status ===> ', connect.isConnected())
sh_con = shh.Shh(connect)
print('info ===>>>> ', sh_con.info)
id = sh_con.addPrivateKey(key=privatekey)
print('id ===>>>> ', id)
user_pubkey = sh_con.getPublicKey(id)
print('user_pubkey ===>>> ', user_pubkey)
topic = Web3.toHex(b'ESSC')
print("topic => ", topic)
text = b'First test'
sumkey = sh_con.addSymKey(privatekey)
print('sumkey ====>>>> ', sumkey)
address_to = '0x043c03b3b0a0cd00...'
mes_send = sh_con.post(
    {
        'ttl': 40,
        'powTarget': 11,
        'powTime': 5,
        'payload': Web3.toHex(text),
        'topic': topic,
        'pubKey': address_to,
        'sig': user_pubkey,
    }
)
if mes_send:
    print('Message Send')
else:
    print('Message not send')

file filter -> :

class Daemon:
    def __init__(self):
        self.private_key = '0x36...'
       host = 'http://****:9090'
        self.new_id = '13b994193505744d...'
        self.connection = shh.Shh(Web3(HTTPProvider(host)))

    def ms_filter(self):
        message_filter = self.connection.newMessageFilter({'privateKeyID': self.new_id})
        return message_filter

    def in_daemon(self):
        filter_id = self._create_message_filter().filter_id
        while True:
            messages = self.connection.getMessages(filter_id)

            if messages != []:
                message = messages[0]
                mes_pars = message['payload']
                mes_from = message['recipientPublicKey']
                topic = message['topic']
                timestamp = message['timestamp']
                print('from user => {}'.format(Web3.toHex(mes_from)))
                print('message => {}'.format(Web3.toText(mes_pars)))
                print('topic => {}'.format(Web3.toText(topic)))
                print('timestamp => {}'.format(timestamp))

            time.sleep(0.3)

threads = []
daemon = Thread(target=Daemon().in_daemon())
threads.append(daemon)
daemon.start()
  • This is not a full solution, it's just a hint. Examples are in the official documentation WEB3.PY -> SHH – Sergey Zaharov May 21 '18 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.