-
-
Save joseluis/b7faa2b38a5070bc75e970e2f42d398d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import asyncio | |
import websockets | |
import binascii | |
import ujson as json | |
method_map = { | |
'6d01': 'Set Name', | |
'6d02': 'Post Memo', | |
'6d03': 'Reply', | |
'6d04': 'Like/Tip', | |
'6d05': 'Set Profile', | |
'6d06': 'Follow', | |
'6d07': 'Unfollow', | |
'6d0c': 'Topic Message' | |
} | |
async def client_loop(): | |
async with websockets.connect('wss://ws.blockchain.info/bch/inv') as websocket: | |
await websocket.send('{"op":"unconfirmed_sub"}') | |
while True: | |
message = await websocket.recv() | |
payload = json.loads(message) | |
data = payload['x'] | |
outputs = data['out'] | |
for output in outputs: | |
if output['script'].startswith('6a02'): | |
print("----------") | |
print('TX: {}'.format(data['hash'])) | |
print('Script: {}'.format(output['script'])) | |
method = output['script'][4:8] | |
if method not in method_map: | |
print('Incompatible Operation') | |
continue | |
if method == '6d02': | |
message = output['script'][10:] | |
print('New Memo: {}'.format(binascii.unhexlify(message))) | |
elif method == '6d03': | |
rts = output['script'][10:74] | |
reply_to = "".join(reversed([rts[i:i+2] for i in range(0, len(rts), 2)])) | |
reply_len = output['script'][74:76] | |
reply_msg = output['script'][76:] | |
print('To: {}'.format(reply_to)) | |
print('Length: {}'.format(reply_len)) | |
print('Message: {}'.format(binascii.unhexlify(reply_msg))) | |
else: | |
print('MEMO {} > {}'.format(method_map[method], output['script'][10:])) | |
asyncio.get_event_loop().run_until_complete(client_loop()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment