B2: network profile for mainnet and regtest, explicit confirmation for mainnet (ADR-58, ELC-30)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3ee60a23da
commit
9c832fd3d2
16 files changed
+261
-36
No files matched your search
+10
-5
@@ -12,16 +12,21 @@ tells the user (bech32 `plm1q...` only).
|
||||
from embit import script
|
||||
from embit.base import EmbitError
|
||||
|
||||
from plm_engine.network import PLM_MAINNET
|
||||
|
||||
_BECH32_PREFIX = PLM_MAINNET["bech32"] + "1"
|
||||
from plm_engine.network import MAINNET, Profile
|
||||
|
||||
|
||||
def is_valid_plm_address(address: str) -> bool:
|
||||
if not address.startswith(_BECH32_PREFIX):
|
||||
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)
|
||||
@@ -158,11 +158,14 @@ class ElectrumClient:
|
||||
notification queues nobody will ever fill again.
|
||||
"""
|
||||
|
||||
def __init__(self, host: str, port: int, use_ssl: bool = True, pin: str | None = None):
|
||||
def __init__(
|
||||
self, host: str, port: int, use_ssl: bool = True, pin: str | None = None, genesis_hash: str = GENESIS_HASH
|
||||
):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.use_ssl = use_ssl
|
||||
self.pin = pin.lower() if pin else None
|
||||
self.genesis_hash = genesis_hash # the profile's (ADR-58, ELC-30)
|
||||
self._reader: asyncio.StreamReader | None = None
|
||||
self._writer: asyncio.StreamWriter | None = None
|
||||
self._id_counter = itertools.count(1)
|
||||
@@ -206,7 +209,7 @@ class ElectrumClient:
|
||||
Bitcoin's genesis and powLimit, so only `server.features` tells them apart (B-99, ELC-30)."""
|
||||
features = await self.request("server.features")
|
||||
genesis = features.get("genesis_hash") if isinstance(features, dict) else None
|
||||
if genesis != GENESIS_HASH:
|
||||
if genesis != self.genesis_hash:
|
||||
raise ElectrumError(f"{self.host}:{self.port} is not on the PLM chain (genesis {genesis!r})")
|
||||
|
||||
def _check_pin(self) -> None:
|
||||
|
||||
+16
-11
@@ -9,24 +9,29 @@ from embit import script
|
||||
from embit.bip32 import HDKey
|
||||
from embit.ec import PrivateKey
|
||||
|
||||
from .network import PLM_MAINNET
|
||||
from .network import MAINNET, Profile
|
||||
|
||||
|
||||
def account_key(master: HDKey, profile: Profile = MAINNET) -> HDKey:
|
||||
"""The account key of the profile's chain: `m/84'/<coin type>'/0'` under the master key."""
|
||||
return master.derive(profile.account_path)
|
||||
|
||||
|
||||
def user_key(account: HDKey, derivation_index: int) -> HDKey:
|
||||
return account.derive(f"0/{derivation_index}")
|
||||
|
||||
|
||||
def address_of(key: HDKey) -> str:
|
||||
return script.p2wpkh(key.to_public()).address(network=PLM_MAINNET)
|
||||
def address_of(key: HDKey, profile: Profile = MAINNET) -> str:
|
||||
return script.p2wpkh(key.to_public()).address(network=profile.embit)
|
||||
|
||||
|
||||
def user_address(account: HDKey, derivation_index: int) -> str:
|
||||
return address_of(user_key(account, derivation_index))
|
||||
def user_address(account: HDKey, derivation_index: int, profile: Profile = MAINNET) -> str:
|
||||
return address_of(user_key(account, derivation_index), profile)
|
||||
|
||||
|
||||
def user_wif(account: HDKey, derivation_index: int) -> str:
|
||||
def user_wif(account: HDKey, derivation_index: int, profile: Profile = MAINNET) -> str:
|
||||
key = user_key(account, derivation_index)
|
||||
return PrivateKey(key.secret, compressed=True, network=PLM_MAINNET).wif(network=PLM_MAINNET)
|
||||
return PrivateKey(key.secret, compressed=True, network=profile.embit).wif(network=profile.embit)
|
||||
|
||||
|
||||
def pool_key(account: HDKey, round_id: int | None = None) -> HDKey:
|
||||
@@ -38,15 +43,15 @@ def pool_key(account: HDKey, round_id: int | None = None) -> HDKey:
|
||||
return account.derive(f"2/{round_id}")
|
||||
|
||||
|
||||
def pool_address(account: HDKey, round_id: int | None = None) -> str:
|
||||
return address_of(pool_key(account, round_id))
|
||||
def pool_address(account: HDKey, round_id: int | None = None, profile: Profile = MAINNET) -> str:
|
||||
return address_of(pool_key(account, round_id), profile)
|
||||
|
||||
|
||||
def round_pool_key(account: HDKey, round_id: int, stored_address: str) -> HDKey:
|
||||
def round_pool_key(account: HDKey, round_id: int, stored_address: str, profile: Profile = MAINNET) -> HDKey:
|
||||
"""The key that controls `stored_address`, the address stored on round `round_id`: the round's own, or
|
||||
the reserve key for a round opened before B-80. A mismatch fails loudly instead of building a transaction
|
||||
that spends someone else's coins."""
|
||||
for key in (pool_key(account, round_id), pool_key(account)):
|
||||
if address_of(key) == stored_address:
|
||||
if address_of(key, profile) == stored_address:
|
||||
return key
|
||||
raise ValueError(f"round {round_id}: no pool key controls the stored address {stored_address!r}")
|
||||
@@ -58,7 +58,7 @@ def _target_from_bits(bits: int) -> int:
|
||||
return mantissa << (8 * (exponent - 3))
|
||||
|
||||
|
||||
def header_meets_its_own_target(header_hex: str) -> bool:
|
||||
def header_meets_its_own_target(header_hex: str, pow_limit: int | None = None) -> bool:
|
||||
"""Whether this header's hash satisfies the difficulty target *it claims for
|
||||
itself* (the `bits` field), and that target is no easier than the chain's powLimit
|
||||
(B-74, ELC-11 — without that, a fabricated header just claims `bits = 0x207fffff`). Rejects a header that was never actually mined —
|
||||
@@ -73,13 +73,15 @@ def header_meets_its_own_target(header_hex: str) -> bool:
|
||||
the configured servers at a fixed height before using it as the seed (see
|
||||
ElectrumListener.agreed_block_hash and rounds/scheduler.py:_wait_for_draw_block)
|
||||
rather than relying on this check alone.
|
||||
|
||||
`pow_limit` is the profile's floor (ADR-58); without it the module default (mainnet) applies.
|
||||
"""
|
||||
header_bytes = bytes.fromhex(header_hex)
|
||||
if len(header_bytes) != _HEADER_LENGTH_BYTES:
|
||||
return False
|
||||
bits = int.from_bytes(header_bytes[_BITS_OFFSET : _BITS_OFFSET + _BITS_LENGTH], "little")
|
||||
target = _target_from_bits(bits)
|
||||
if target <= 0 or target > POW_LIMIT: # B-74: not easier than the chain's powLimit
|
||||
if target <= 0 or target > (POW_LIMIT if pow_limit is None else pow_limit): # B-74: not easier than the chain's powLimit
|
||||
return False
|
||||
digest = hashlib.sha256(hashlib.sha256(header_bytes).digest()).digest()
|
||||
# The hash as the integer comparable against `target`: this is the same digest
|
||||
|
||||
+62
-4
@@ -1,10 +1,14 @@
|
||||
"""PLM mainnet params for embit, verified against PalladiumWallet/src/Core/Chain/ChainProfiles.cs.
|
||||
"""Network profiles (ADR-58): everything that differs between PLM mainnet and the regtest of the alpha.
|
||||
|
||||
Threaded explicitly through every embit call via `network=PLM_MAINNET` rather than
|
||||
registered globally, since this is a long-lived async server (embit has no
|
||||
concept of "current network" beyond what you pass in).
|
||||
A `Profile` fixes the bech32 hrp, the base58 and extended-key prefixes, the genesis hash the Electrum
|
||||
handshake expects (ELC-30), the proof-of-work floor and the BIP44 coin type. The values come from
|
||||
`palladiumcore/src/chainparams.cpp` (mainnet and `CRegTestParams`) and the PalladiumWallet chain profiles.
|
||||
The profile is threaded explicitly through every call (`network=profile.embit`), never registered globally:
|
||||
this is a long-lived server and embit has no notion of a current network beyond what it is passed.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
PLM_MAINNET = {
|
||||
"name": "PLM Mainnet",
|
||||
"wif": bytes([0x80]),
|
||||
@@ -30,3 +34,57 @@ ACCOUNT_PATH = f"m/84h/{BIP44_COIN_TYPE}h/0h"
|
||||
# PLM shares Bitcoin's genesis block (palladiumcore chainparams.cpp) and its powLimit, so a
|
||||
# Bitcoin Electrum server would pass every header check; the handshake asks for this instead.
|
||||
GENESIS_HASH = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
|
||||
|
||||
MAINNET_POW_LIMIT = int("00000000" + "ff" * 28, 16)
|
||||
REGTEST_POW_LIMIT = int("7f" + "ff" * 31, 16)
|
||||
|
||||
# Bitcoin's testnet SLIP-132 variants, which Palladium's testnet and regtest share.
|
||||
PLM_REGTEST = {
|
||||
"name": "PLM Regtest",
|
||||
"wif": bytes([0xFF]),
|
||||
"p2pkh": bytes([127]),
|
||||
"p2sh": bytes([115]),
|
||||
"bech32": "rplm",
|
||||
"xprv": bytes.fromhex("04358394"),
|
||||
"xpub": bytes.fromhex("043587cf"),
|
||||
"yprv": bytes.fromhex("044a4e28"),
|
||||
"ypub": bytes.fromhex("044a5262"),
|
||||
"zprv": bytes.fromhex("045f18bc"),
|
||||
"zpub": bytes.fromhex("045f1cf6"),
|
||||
"Yprv": bytes.fromhex("024285b5"),
|
||||
"Ypub": bytes.fromhex("024289ef"),
|
||||
"Zprv": bytes.fromhex("02575048"),
|
||||
"Zpub": bytes.fromhex("02575483"),
|
||||
"bip32": 1,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Profile:
|
||||
name: str # the value of PLM_NETWORK
|
||||
embit: dict # what embit takes as `network=`
|
||||
genesis_hash: str
|
||||
pow_limit: int
|
||||
coin_type: int
|
||||
|
||||
@property
|
||||
def hrp(self) -> str:
|
||||
return self.embit["bech32"]
|
||||
|
||||
@property
|
||||
def account_path(self) -> str:
|
||||
return f"m/84h/{self.coin_type}h/0h"
|
||||
|
||||
|
||||
MAINNET = Profile("mainnet", PLM_MAINNET, GENESIS_HASH, MAINNET_POW_LIMIT, BIP44_COIN_TYPE)
|
||||
REGTEST = Profile(
|
||||
"regtest", PLM_REGTEST, "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206", REGTEST_POW_LIMIT, 1
|
||||
)
|
||||
PROFILES = {profile.name: profile for profile in (MAINNET, REGTEST)}
|
||||
|
||||
|
||||
def get_profile(name: str) -> Profile:
|
||||
try:
|
||||
return PROFILES[name]
|
||||
except KeyError:
|
||||
raise ValueError(f"unknown network {name!r}: expected one of {sorted(PROFILES)}") from None
|
||||
Reference in new issue
Block a user