#!/usr/bin/env python3 """Sync a chain in the browser wallet through the live rendezvous relay. The page is pointed at a bare `wss://` relay, so the channel discovers the gateway from the relay's listings instead of being handed a gateway key. The sync is bounded on purpose: a mainnet chain without a checkpoint in its profile starts at genesis, and this run records how far it gets through the relay in a fixed window, then pauses. Every verified height stays in the store, so a later run resumes. usage: RELAY=wss://swaprelay.smesh.lol CHAIN=btc SECONDS=180 run.py """ import asyncio import os import sys import time from playwright.async_api import async_playwright RELAY = os.environ.get("RELAY", "wss://swaprelay.smesh.lol") CHAIN = os.environ.get("CHAIN", "btc") SECONDS = int(os.environ.get("SECONDS", "180")) NETWORK = os.environ.get("NETWORK", "btc") PAGE = os.environ.get("PAGE", "http://127.0.0.1:8080/") PASSPHRASE = "relay-run-passphrase" HERE = os.path.dirname(os.path.abspath(__file__)) PROFILE = os.path.join(HERE, "chromium-profile-" + CHAIN) LOG_PATH = os.path.join(HERE, "run-" + CHAIN + ".log") def say(line): print(line, flush=True) with open(LOG_PATH, "a") as handle: handle.write(str(line) + "\n") def card(page): return page.locator("#cards .card").first async def wait_text(page, needle, seconds): deadline = time.time() + seconds text = "" while time.time() < deadline: text = (await card(page).locator(".status").last.inner_text()).strip() if needle in text: return text await asyncio.sleep(1) return text async def main(): url = PAGE + "?chains=" + CHAIN + "&network=" + NETWORK say("page " + url) say("relay " + RELAY + " (bare: the channel discovers the gateway)") async with async_playwright() as p: context = await p.chromium.launch_persistent_context( PROFILE, viewport={"width": 1280, "height": 1400}) page = context.pages[0] if context.pages else await context.new_page() # The worker scripts are fetched by URL, and Chromium caches them across runs; a stale # channel wasm would make every fix look untested. cdp = await context.new_cdp_session(page) await cdp.send("Network.clearBrowserCache") page.on("console", lambda message: say("console " + message.type + ": " + message.text)) page.on("pageerror", lambda error: say("pageerror: " + str(error))) await page.goto(url, wait_until="load", timeout=60000) await page.wait_for_timeout(500) inputs = card(page).locator("input") await inputs.nth(0).fill(RELAY) await inputs.nth(1).fill("") await page.screenshot(path=os.path.join(HERE, "01-relay-set.png"), full_page=True) say("gateway field: " + await inputs.nth(0).input_value()) say("profile field: " + repr(await inputs.nth(1).input_value()) + " (built-in chain profile)") say("network profile: " + await card(page).locator(".status").first.inner_text()) started = time.time() await page.get_by_role("button", name="Connect").click() await page.wait_for_function("document.getElementById('connect-status').textContent.includes('connected')", timeout=120000) say("connect (%.1fs): %s" % (time.time() - started, (await page.inner_text('#connect-status')).strip())) await page.screenshot(path=os.path.join(HERE, "02-connected.png"), full_page=True) state_text = (await card(page).locator(".status").first.inner_text()).strip() if "no wallet yet" in state_text: await card(page).locator("input[type=password]").fill(PASSPHRASE) await card(page).get_by_role("button", name="Create").click() words = card(page).locator(".words") await words.wait_for(timeout=60000) mnemonic = (await words.inner_text()).strip() await card(page).locator("textarea[placeholder^='type the words']").fill(mnemonic) await card(page).get_by_role("button", name="Confirm").click() state_text = (await card(page).locator(".status").first.inner_text()).strip() if "locked" in state_text and "no wallet yet" not in state_text: await card(page).locator("input[type=password]").fill(PASSPHRASE) await card(page).get_by_role("button", name="Unlock").click() await page.wait_for_timeout(1500) state_text = (await card(page).locator(".status").first.inner_text()).strip() say("wallet: " + " ".join(state_text.split())) await page.screenshot(path=os.path.join(HERE, "03-wallet.png"), full_page=True) started = time.time() await card(page).get_by_role("button", name="Sync").click() say("sync started; waiting up to %ds" % SECONDS) pause = card(page).get_by_role("button", name="Pause") finished = False deadline = time.time() + SECONDS while time.time() < deadline: if await pause.is_disabled(): finished = True break await asyncio.sleep(3) if not finished: await pause.click() text = await wait_text(page, "paused", 120) say("pause (%.1fs): %s" % (time.time() - started, " ".join(text.split()))) else: say("sync completed in %.1fs" % (time.time() - started)) say("result: " + " ".join((await card(page).locator(".status").last.inner_text()).split())) await page.screenshot(path=os.path.join(HERE, "04-synced.png"), full_page=True) state = await page.evaluate("(async () => await window.wallet.status(%r))()" % CHAIN) say("status " + str(state)) with open(os.path.join(HERE, "status-" + CHAIN + ".json"), "w") as handle: handle.write(str(state) + "\n") await context.close() asyncio.run(main())