Stagehand: AI-assisted Web Data Validation
Stagehand: AI-assisted Web Data Validation
Research question
How can a browser workflow collect Bitcoin invoice information without turning untrusted website content into spending authority? A web page can contain misleading amounts, hidden instructions or manipulated checkout details. Even an accurate extraction does not establish authorization.
Platform-specific design
Use Stagehand extraction to produce a typed invoice candidate in the browser process. Serialize that candidate to JSON for the independent Python validator. Schema-conforming extraction is not proof that the merchant or invoice is authentic.
Source implementation
1# SPDX-License-Identifier: 0BSD
2import json
3import re
4
5
6def unique_object(pairs):
7 result = {}
8 for key, value in pairs:
9 if key in result:
10 raise ValueError("duplicate JSON key")
11 result[key] = value
12 return result
13
14
15def parse_sats(text):
16 if not re.fullmatch(r"[1-9][0-9]{0,15}", text):
17 raise ValueError("invalid integer sats")
18 return int(text)
19
20
21def validate(candidate):
22 if type(candidate) is not dict or set(candidate) != {"invoice_id", "amount_sat", "network"}:
23 raise ValueError("invalid invoice shape")
24 if type(candidate["invoice_id"]) is not str or not re.fullmatch(r"[A-Za-z0-9_-]{1,64}", candidate["invoice_id"]):
25 raise ValueError("invalid invoice identifier")
26 amount = candidate["amount_sat"]
27 if type(amount) is not int or not 1 <= amount <= 100000:
28 raise ValueError("outside illustrative review limit")
29 if candidate["network"] != "test":
30 raise ValueError("Testnet only")
31 return {"invoice": dict(candidate), "status": "review_required"}
32
33
34def extract(raw_json):
35 if len(raw_json) > 4096:
36 raise ValueError("oversized extraction")
37 return json.loads(raw_json, object_pairs_hook=unique_object)Local review contract
Pass the result of extract() to validate(). The validator accepts exactly three fields: invoice_id, amount_sat and network. It rejects non-integer amounts, booleans, unknown fields and networks other than test. The illustrative 100,000 sat review ceiling is a local example choice, not a wallet fee law or an authorization policy. The output is review_required, never paid or approved. No destination is collected because this study does not construct a transaction.
Connecting to the Bitcoin engine
A separate trusted operator process may query POST /api/v5/btc/wallet/balance with X-BTC-KEY and JSON body {"net":"test","type":"segwit"}. Keep the key outside the browser process, model context, page scripts and browser logs. A balance observation does not establish payment or permission to spend. Before any future SEND integration, independently verify merchant identity, destination, network and amount, obtain explicit authorization bound to the exact transaction intent, and preserve idempotency across retries. Never treat page text or a successful browser task as that authorization.
Failure cases and limitations
Reject missing fields, ambiguous units, unexpected network and stale task data. A production integration must also enforce origin and navigation policy, authenticate the extraction channel and protect against prompt injection and browser access to local services. The sample is a reference fragment, not a complete browser agent or payment integration. No live browser, engine or platform compatibility tests were performed. No engine or signed fee law changes are required.
References
- Platform documentation: https://docs.stagehand.dev/
- Engine API: https://thamer.org/api
- Engine: https://thamer.org/engine
- Source repository: https://thamer.org/repo/
