Пример #1
0
def main():
    w3 = Web3(HTTPProvider('http://localhost:8545'))
    print(w3.admin.peers)
    print('Peers:')
    for peer in w3.admin.peers:
        print(' -> ' + peer['network']['remoteAddress'] + ' ' + peer['name'] +
              ' ' + str(peer['caps']))

    shh = Shh(w3)
    print('Whisper version: ' + shh.version)
    print('Whisper info: [memory: ' + str(shh.info.memory) + ', messages: ' +
          str(shh.info.messages) + ', max_message_size: ' +
          str(shh.info.maxMessageSize) + ', min_pow: ' + str(shh.info.minPow))

    symKeyID = shh.generateSymKeyFromPassword(
        'shh!! this is a secret password')
    print('Identity: ' + symKeyID)
    print('Symmetric key: ' + shh.getSymKey(symKeyID))

    result = shh.post({
        'payload': Web3.toHex(text='Hello world!'),
        'symKeyID': symKeyID,
        'topic': '0x12340000',
        'powTarget': 2.5,
        'powTime': 2
    })
    print('Message sent: ' + str(result))
Пример #2
0
class Bot(threading.Thread):
    def __init__(self, name):
        super(Bot, self).__init__()
        self.name = name
        self.shh = Shh(Web3(HTTPProvider('http://localhost:8545')))
        print(self.name + ': Whisper version: ' + self.shh.version)
        print(self.name + ': Whisper info: [memory: ' +
              str(self.shh.info.memory) + ', messages: ' +
              str(self.shh.info.messages) + ', max_message_size: ' +
              str(self.shh.info.maxMessageSize) + ', min_pow: ' +
              str(self.shh.info.minPow))
        self.symKeyID = self.shh.generateSymKeyFromPassword(
            'shh!! this is a secret password')
        print(self.name + ': Identity: ' + self.symKeyID)
        print(self.name + ': Symmetric key: ' +
              self.shh.getSymKey(self.symKeyID))

    def filter(self):
        message_filter = self.shh.newMessageFilter({
            'topic': '0x12340000',
            'symKeyID': self.symKeyID
        })
        return message_filter

    def run(self):
        filter_id = self.filter().filter_id
        print(self.name + ': Waiting for messages...')
        while True:
            messages = self.shh.getMessages(filter_id)
            for message in messages:
                print(self.name + ': Received message: ' +
                      Web3.toText(message['payload']))
                print(self.name + ': Envelope: ' + str(message))
            time.sleep(0.3)