Files
plm-lottery/plm_engine/address.py
T

33 lines
1.3 KiB
Python

"""Validation for PLM addresses supplied by the user (withdrawal destinations).
embit's `Script.from_address` accepts a well-formed bech32 address from *any*
chain — a Bitcoin `bc1...` parses fine and yields a perfectly valid witness
program — so parse-success alone is not a sufficient check here: a withdrawal
to a `bc1...` address would build, sign and broadcast normally on PLM and land
on a script nobody holds the key for. The HRP check below is what makes the
destination actually PLM, and it matches what the withdrawal form already
tells the user (bech32 `plm1q...` only).
"""
from embit import script
from embit.base import EmbitError
from plm_engine.network import MAINNET, Profile
def is_valid_address(address: str, profile: Profile) -> bool:
"""A well-formed segwit address on this profile's chain: its hrp is the profile's (ADR-58), so a mainnet
`plm1…` is refused on regtest and the other way round."""
if not address.startswith(profile.hrp + "1"):
return False
try:
script.Script.from_address(address)
except EmbitError:
return False
return True
def is_valid_plm_address(address: str) -> bool:
"""Mainnet only: what the old stack has always checked."""
return is_valid_address(address, MAINNET)