Cleanup Whisper a bit

This commit is contained in:
Oskar Thoren 2019-02-04 14:53:05 -05:00
parent bc925240f1
commit 17c29435fd
3 changed files with 136 additions and 102 deletions

View File

@ -1,60 +1,90 @@
from web3 import shh
from web3 import HTTPProvider, Web3
from web3.personal import Personal
from web3 import Web3, HTTPProvider
import time
# Run geth repo with <OPTIONS>
# Ensure there's a private key ~
# Pubkey
# 0x040cb9373bf8cd9dcbca4b75ccbfad52bbc66d1aaca8095adb2a7dcf8504146f0abde4ca22adf77b5062f113585befbf37e06dcadd0ce1093695e53d00d2109528
# Constant
#---------------------------------------------------------------------
# XXX: Message not sent, due to threads?
host = 'http://localhost:8545'
a_keyPair = "0x57083392b29bdf24512c93cfdf45d38c87d9d882da3918c59f4406445ea976a4"
b_keyPair= "0x7b5c5af9736d9f1773f2020dd0fef0bc3c8aeaf147d2bf41961e766588e086e7"
status_host = 'http://127.0.0.1:8545'
# Derived, used for addressing
a_pubKey = "0x04d94a1a01872b598c7cdc5aca2358d35eb91cd8a91eaea8da277451bb71d45c0d1eb87a31ea04e32f537e90165c870b3e115a12438c754d507ac75bddd6ecacd5"
b_pubkey = "0x04ff921ddf78b5ed4537402f59a150caf9d96a83f2a345a1ddf9df12e99e7778f314c9ca72e8285eb213af84f5a7b01aabb62c67e46657976ded6658e1b9e83c73"
# XXX: Hardcoded
privatekey = "0x633f01b2b607d4e777db626d876d04613decb5145ec7faeae3e57bf8f008c994"
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)
# XXX
#topic = Web3.toHex(b'AAAA')
topic = '0x00000000'
print("topic => ", topic)
text = 'test message'
# XXX
#address_to = '0x0487be55c072702a0e4da72158a7432281e8c26aca9501cd0bfeea726dc85f2611e96884e8fc4807c95c04c04af3387b83350a27cc18b96c37543e0f9a41ae47b5'
# Connection
#---------------------------------------------------------------------
# Copy pasted
address_to = '0x04bbfb9fbe8239c2fb1895511f306d731c283ba3070d8642d4fbb4da1e4923454b8de6d82b671a4787c8e24d2cf2cf947d6da5ff6d12bf7147d1c0c5d2acbaf8ba'
web3 = Web3(HTTPProvider(host))
from web3.shh import Shh
Shh.attach(web3, "shh")
# nice works
#kId = web3.shh.addPrivateKey(keyPair)
#pubKey = web3.shh.getPublicKey(kId)
# address too is who?
mes_send = ms.post(
{
'ttl': 20,
'payload': Web3.toHex(text=text),
# API
#---------------------------------------------------------------------
# XXX: Hacky
def newKeyPair():
raw_keyPair = web3.shh.newKeyPair()
keyPair = "0x" + raw_keyPair
return keyPair
def pollFilter(topic, keyPair):
kId = web3.shh.addPrivateKey(keyPair)
myFilter = web3.shh.newMessageFilter({'topic': topic,
'privateKeyID': kId})
myFilter.poll_interval = 600;
return myFilter
def sendMessage(address_to, topic, msg):
web3.shh.post({
'pubKey': address_to,
'topic': topic,
'powTarget': 2.5,
'powTarget': 2.01,
'powTime': 2,
}
)
'ttl': 10,
'payload': web3.toHex(text=msg)
});
# How check status?
print(mes_send)
def getMessages(myFilter):
filterID = myFilter.filter_id
retreived_messages = web3.shh.getMessages(filterID)
for i in range(0, len(retreived_messages)):
print(retreived_messages[i]['payload'].decode("utf-8"))
print(retreived_messages[i])
# Run
#---------------------------------------------------------------------
# A sending messages to B and B then checking
# keyPair used for decryption
myFilter = pollFilter(topic, b_keyPair)
# XXX: Part of A's state
sendMessage(b_pubkey, topic, "hello")
sendMessage(b_pubkey, topic, "hi")
#sendMessage(b_pubkey, topic, "hello b")
time.sleep(1)
getMessages(myFilter)
# TODO
# Usage:
# ./whisper a
# => type to send message
# => see incoming messages (poll regularly)
# Vice versa for b
# Then:
# - check discovery topic listen
# - try to get end to end e.g. mobile phone
# - hook up to sync
# - consider subscribe instead?

60
data_sync/whisper.py.old Normal file
View File

@ -0,0 +1,60 @@
from web3 import shh
from web3 import HTTPProvider, Web3
from web3.personal import Personal
import time
# Run geth repo with <OPTIONS>
# Ensure there's a private key ~
# Pubkey
# 0x040cb9373bf8cd9dcbca4b75ccbfad52bbc66d1aaca8095adb2a7dcf8504146f0abde4ca22adf77b5062f113585befbf37e06dcadd0ce1093695e53d00d2109528
# XXX: Message not sent, due to threads?
status_host = 'http://127.0.0.1:8545'
# XXX: Hardcoded
privatekey = "0x633f01b2b607d4e777db626d876d04613decb5145ec7faeae3e57bf8f008c994"
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)
# XXX
#topic = Web3.toHex(b'AAAA')
topic = '0x00000000'
print("topic => ", topic)
text = 'test message'
# XXX
#address_to = '0x0487be55c072702a0e4da72158a7432281e8c26aca9501cd0bfeea726dc85f2611e96884e8fc4807c95c04c04af3387b83350a27cc18b96c37543e0f9a41ae47b5'
# Copy pasted
address_to = '0x04bbfb9fbe8239c2fb1895511f306d731c283ba3070d8642d4fbb4da1e4923454b8de6d82b671a4787c8e24d2cf2cf947d6da5ff6d12bf7147d1c0c5d2acbaf8ba'
# nice works
# address too is who?
mes_send = ms.post(
{
'ttl': 20,
'payload': Web3.toHex(text=text),
'pubKey': address_to,
'topic': topic,
'powTarget': 2.5,
'powTime': 2,
}
)
# How check status?
print(mes_send)

View File

@ -1,56 +0,0 @@
from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://localhost:8545'))
from web3.shh import Shh
Shh.attach(web3, "shh")
import time
# XXX: Seems broken
raw_keyPair = web3.shh.newKeyPair()
keyPair = "0x" + raw_keyPair
kId = web3.shh.addPrivateKey(keyPair)
topic = '0x00000000'
# TODO: Discovery topic or other one, or all
myFilter = web3.shh.newMessageFilter({'topic': topic,
'privateKeyID': kId})
myFilter.poll_interval = 600;
print('keyPair: ' + keyPair)
print('Filter ID: ' + myFilter.filter_id)
print(web3.shh.hasKeyPair(kId))
print('PubKey: ' + web3.shh.getPublicKey(kId))
# Test messages
web3.shh.post({
'pubKey': web3.shh.getPublicKey(kId),
'topic': topic,
'powTarget': 2.01,
'powTime': 2,
'ttl': 10,
'payload': web3.toHex(text="test message :)")
});
web3.shh.post({
'pubKey': web3.shh.getPublicKey(kId),
'topic': topic,
'powTarget': 2.01,
'powTime': 2,
'ttl': 10,
'payload': web3.toHex(text="test hello! :)")
});
# get and send message
# should be same
def getMessages():
filterID = myFilter.filter_id
retreived_messages = web3.shh.getMessages(filterID)
for i in range(0, len(retreived_messages)):
print(retreived_messages[i]['payload'].decode("utf-8"))
print(retreived_messages[i])
# getMessages()