receive.py
TEXT FILE1.1 KB · Public source code for developers, search crawlers and AI agents
1import json, os, urllib.request
2class NoRedirect(urllib.request.HTTPRedirectHandler):
3 def redirect_request(self, req, fp, code, msg, headers, newurl):
4 return None
5
6def receive(address_type: str) -> str:
7 if address_type not in ("segwit", "taproot"):
8 raise ValueError("unsupported address type")
9 key = os.environ["WAB_BTC_KEY"]
10 req = urllib.request.Request(
11 "http://127.0.0.1:7070/api/v5/btc/wallet/receive",
12 data=json.dumps({"net":"test", "type":address_type}).encode(),
13 headers={"Content-Type":"application/json", "X-BTC-KEY":key}, method="POST")
14 opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect())
15 with opener.open(req, timeout=10) as response:
16 raw = response.read(1048577)
17 if len(raw)>1048576:
18 raise ValueError("oversized response")
19 data=json.loads(raw)
20 if data.get("ok") is not True:
21 raise RuntimeError("receive unavailable")
22 address=data["result"]["address"]
23 if not isinstance(address,str) or not address.startswith("tb1q" if address_type=="segwit" else "tb1p"):
24 raise ValueError("unexpected address")
25 return addressRead-only source · Select a line number to link directly · Raw download above
