"""Signalbound CIAG HTTPS client. Supply an existing wallet signing callback.""" import hashlib import json import re import secrets import time import urllib.error import urllib.request API = "https://api.signalbound.art/api/" ASSET = "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913" class NoRedirect(urllib.request.HTTPRedirectHandler): def redirect_request(self, *args): return None def request(path, data=None): body = None if data is None else json.dumps(data).encode() req = urllib.request.Request(API + path, data=body, headers={"Content-Type": "application/json"}) try: with urllib.request.build_opener(NoRedirect()).open(req, timeout=20) as response: return response.status, json.load(response) except urllib.error.HTTPError as error: return error.code, {"status": error.code} def payment_terms(): code, terms = request("ciag-billing") if code != 200 or (terms.get("network"), terms.get("asset"), terms.get("amount")) != ("eip155:8453", ASSET, "100000"): raise RuntimeError("CIAG pricing unavailable or changed") recipient = terms.get("payTo", "") if not re.fullmatch(r"0x[0-9a-fA-F]{40}", recipient): raise ValueError("Invalid recipient") memo = b"SIGNALBOUND-CIAG-CREDIT-V1".hex().ljust(64, "0") data = "0xa9059cbb" + recipient[2:].lower().zfill(64) + format(100000, "064x") + memo return {**terms, "transaction": {"to": ASSET, "data": data, "value": "0x0"}} def claim_credit(tx): if not re.fullmatch(r"0x[0-9a-f]{64}", tx): raise ValueError("Invalid transaction hash") return request("ciag-billing", {"tx": tx}) def new_order(): return "0x" + secrets.token_hex(32) def call(target, action, proof, payer, order, sign_typed_data): if target not in ("ciag-file-check", "ciag-billing", "ciag-drills", "ciag-lab") and not re.fullmatch(r"[0-9a-f]{64}", target): raise ValueError("Invalid target") if not re.fullmatch(r"0x[0-9a-f]{64}", order): raise ValueError("Invalid order") terms = payment_terms() fields = [("action", "string"), ("merchant", "address"), ("priceUSDCAtomic", "uint256"), ("requestHash", "bytes32"), ("order", "bytes32"), ("expires", "uint256")] domain = [("name", "string"), ("version", "string"), ("chainId", "uint256"), ("verifyingContract", "address")] schema = lambda pairs: [{"name": name, "type": kind} for name, kind in pairs] payload = json.dumps([target, action, proof], sort_keys=True, separators=(",", ":"), ensure_ascii=False) expires = int(time.time()) + 600 typed = {"primaryType": "ServiceRequest", "types": {"EIP712Domain": schema(domain), "ServiceRequest": schema(fields)}, "domain": {"name": "Signalbound CIAG", "version": "1", "chainId": 8453, "verifyingContract": ASSET}, "message": {"action": action, "merchant": terms["payTo"], "priceUSDCAtomic": 0 if action in ("balance", "watchlist", "watch-remove", "team-read", "team-grant", "team-revoke", "drill-resume", "drill-file", "lab-resume") else 100000, "requestHash": "0x" + hashlib.sha256(payload.encode()).hexdigest(), "order": order, "expires": expires}} auth = {"payer": payer, "order": order, "expires": expires, "signature": sign_typed_data(typed)} path = target if target.startswith("ciag-") else "ciag-recovery/" + target return request(path, {"action": action, "proof": proof, "billing": auth})