from signal import pause
import iroh
import argparse
import asyncio
async def main():
iroh.iroh_ffi.uniffi_set_event_loop(asyncio.get_running_loop())
parser = argparse.ArgumentParser(description='Python Iroh Node Demo')
parser.add_argument('--ticket', type=str, help='ticket to join a document')
args = parser.parse_args()
options = iroh.NodeOptions()
options.enable_docs = True
node = await iroh.Iroh.memory_with_options(options)
node_id = await node.net().node_id()
print("Started Iroh node: {}".format(node_id))
if not args.ticket:
print("In example mode")
print("(To run the sync demo, please provide a ticket to join a document)")
print()
doc = await node.docs().create()
author = await node.authors().create()
doc_id = doc.id()
ticket = await doc.share(iroh.ShareMode.READ, iroh.AddrInfoOptions.RELAY_AND_ADDRESSES)
await doc.set_bytes(author, b"hello", b"world")
await doc.set_bytes(author, b"foo", b"bar")
await doc.set_bytes(author, b"baz", b"qux")
print("Created doc: {}".format(doc_id))
print("Keep this running and in another terminal run:\n\npython main.py --ticket {}".format(ticket))
else:
doc_ticket = iroh.DocTicket(args.ticket)
doc = await node.docs().join(doc_ticket)
doc_id = doc.id()
print("Joined doc: {}".format(doc_id))
print("Waiting 5 seconds to let stuff sync...")
await asyncio.sleep(5)
query = iroh.Query.all(None)
keys = await doc.get_many(query)
print("Data:")
for entry in keys:
key = entry.key()
hash = entry.content_hash()
content = await node.blobs().read_to_bytes(entry.content_hash())
print("{} : {} (hash: {})".format(key.decode("utf8"), content.decode("utf8"), hash))
input("Press Enter to exit...")
if __name__ == "__main__":
asyncio.run(main())