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:
davideandClaude Sonnet 5.5 committed 2026-11-16 15:38:29 +01:00
1 parent 3ee60a23da
commit 9c832fd3d2
16 files changed
+261 -36

No files matched your search

+16 -11
View File
@@ -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}")