-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15_address_types.py
More file actions
33 lines (27 loc) · 1.5 KB
/
Copy path15_address_types.py
File metadata and controls
33 lines (27 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# SPDX-License-Identifier: MIT
"""15 - See the Bitcoin address types your node can make: legacy, SegWit, Taproot.
Asks the node for one address of each type (getnewaddress with an address_type). The
JavaScript recipe 15 derives all three from a single key offline, with no node. Run this
against a recent Bitcoin node (testnet4); bech32m/Taproot needs Bitcoin Core 23+.
Testnet only.
RPC_USER=... RPC_PASS=... python 15_address_types.py
"""
import requests
from config import RPC_URL, RPC_USER, RPC_PASS
def rpc(method, params=None):
payload = {"jsonrpc": "1.0", "id": "ex", "method": method, "params": params or []}
try:
r = requests.post(RPC_URL, json=payload, auth=(RPC_USER, RPC_PASS), timeout=15)
except requests.exceptions.RequestException:
raise SystemExit(f"could not reach your node at {RPC_URL}. is it running with RPC "
"enabled? see testnethub.com/run-node")
if r.status_code == 401:
raise SystemExit("the node rejected the login: set RPC_USER / RPC_PASS (see testnethub.com/rpc-safety)")
body = r.json()
if body.get("error"):
raise SystemExit(f"the node rejected the call: {body['error']['message']}")
return body["result"]
for kind, label in [("legacy", "legacy (P2PKH)"), ("bech32", "segwit (P2WPKH)"), ("bech32m", "taproot (P2TR)")]:
print(f"{label} : {rpc('getnewaddress', ['', kind])}")
print("all three receive coins; SegWit and Taproot cost less to spend from.")
print("more: https://testnethub.com/keys-and-seeds")