Implement exchange of open transactions
This commit is contained in:
44
list-open-transactions.py
Executable file
44
list-open-transactions.py
Executable file
@@ -0,0 +1,44 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
import base64, socket, sys
|
||||
|
||||
def get_transaction(s, i):
|
||||
request = b"\0\0\0\0\x05" + i.to_bytes(2, "big")
|
||||
for _ in range(10):
|
||||
s.sendto(request, ("::1", 62039))
|
||||
try:
|
||||
while True:
|
||||
msg, sender = s.recvfrom(4096)
|
||||
if sender[0:2] == ("::1", 62039) and len(msg) == 155 and msg[0:5] == b"\0\0\0\0\x06" and msg[5:7] == i.to_bytes(2, "big"):
|
||||
return msg[7:155]
|
||||
except TimeoutError:
|
||||
pass
|
||||
return None
|
||||
|
||||
def format_addr(raw_addr):
|
||||
return base64.b64encode(raw_addr).decode()
|
||||
|
||||
def format_amount(raw_amount):
|
||||
int_amount = int.from_bytes(raw_amount, "big")
|
||||
coins = int_amount // 100
|
||||
cents = int_amount % 100
|
||||
return f"{coins},{cents:02} cc"
|
||||
|
||||
def main():
|
||||
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
||||
s.settimeout(1)
|
||||
for i in range(1024):
|
||||
transaction = get_transaction(s, i)
|
||||
if transaction is None:
|
||||
print("- no response from local node -", file=sys.stderr)
|
||||
exit(1)
|
||||
if transaction == 148 * b"\0":
|
||||
return
|
||||
sender = format_addr(transaction[4:36])
|
||||
receiver = format_addr(transaction[36:68])
|
||||
amount = format_amount(transaction[68:76])
|
||||
fee = format_amount(transaction[76:84])
|
||||
print(f"{sender} {receiver} {amount:>13} {fee:>10}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user