mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-26 23:13:25 +00:00
bitcoin.py: change API of address_to_hash
This commit is contained in:
parent
cc4aa1812d
commit
66c264f613
2 changed files with 32 additions and 11 deletions
|
@ -25,7 +25,8 @@
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
from typing import List, Tuple, TYPE_CHECKING, Optional, Union
|
from typing import List, Tuple, TYPE_CHECKING, Optional, Union
|
||||||
from enum import IntEnum
|
import enum
|
||||||
|
from enum import IntEnum, Enum
|
||||||
|
|
||||||
from .util import bfh, bh2u, BitcoinException, assert_bytes, to_bytes, inv_dict
|
from .util import bfh, bh2u, BitcoinException, assert_bytes, to_bytes, inv_dict
|
||||||
from . import version
|
from . import version
|
||||||
|
@ -432,20 +433,39 @@ def address_to_script(addr: str, *, net=None) -> str:
|
||||||
raise BitcoinException(f'unknown address type: {addrtype}')
|
raise BitcoinException(f'unknown address type: {addrtype}')
|
||||||
return script
|
return script
|
||||||
|
|
||||||
def address_to_hash(addr: str, *, net=None) -> Tuple[int, bytes]:
|
|
||||||
"""Return the pubkey hash / witness program of an address"""
|
class OnchainOutputType(Enum):
|
||||||
|
"""Opaque types of scriptPubKeys.
|
||||||
|
In case of p2sh, p2wsh and similar, no knowledge of redeem script, etc.
|
||||||
|
"""
|
||||||
|
P2PKH = enum.auto()
|
||||||
|
P2SH = enum.auto()
|
||||||
|
WITVER0_P2WPKH = enum.auto()
|
||||||
|
WITVER0_P2WSH = enum.auto()
|
||||||
|
|
||||||
|
|
||||||
|
def address_to_hash(addr: str, *, net=None) -> Tuple[OnchainOutputType, bytes]:
|
||||||
|
"""Return (type, pubkey hash / witness program) for an address."""
|
||||||
if net is None: net = constants.net
|
if net is None: net = constants.net
|
||||||
if not is_address(addr, net=net):
|
if not is_address(addr, net=net):
|
||||||
raise BitcoinException(f"invalid bitcoin address: {addr}")
|
raise BitcoinException(f"invalid bitcoin address: {addr}")
|
||||||
witver, witprog = segwit_addr.decode(net.SEGWIT_HRP, addr)
|
witver, witprog = segwit_addr.decode(net.SEGWIT_HRP, addr)
|
||||||
if witprog is not None:
|
if witprog is not None:
|
||||||
|
if witver != 0:
|
||||||
|
raise BitcoinException(f"not implemented handling for witver={witver}")
|
||||||
if len(witprog) == 20:
|
if len(witprog) == 20:
|
||||||
return WIF_SCRIPT_TYPES['p2wpkh'], bytes(witprog)
|
return OnchainOutputType.WITVER0_P2WPKH, bytes(witprog)
|
||||||
return WIF_SCRIPT_TYPES['p2wsh'], bytes(witprog)
|
elif len(witprog) == 32:
|
||||||
|
return OnchainOutputType.WITVER0_P2WSH, bytes(witprog)
|
||||||
|
else:
|
||||||
|
raise BitcoinException(f"unexpected length for segwit witver=0 witprog: len={len(witprog)}")
|
||||||
addrtype, hash_160_ = b58_address_to_hash160(addr)
|
addrtype, hash_160_ = b58_address_to_hash160(addr)
|
||||||
if addrtype == net.ADDRTYPE_P2PKH:
|
if addrtype == net.ADDRTYPE_P2PKH:
|
||||||
return WIF_SCRIPT_TYPES['p2pkh'], hash_160_
|
return OnchainOutputType.P2PKH, hash_160_
|
||||||
return WIF_SCRIPT_TYPES['p2sh'], hash_160_
|
elif addrtype == net.ADDRTYPE_P2SH:
|
||||||
|
return OnchainOutputType.P2SH, hash_160_
|
||||||
|
raise BitcoinException(f"unknown address type: {addrtype}")
|
||||||
|
|
||||||
|
|
||||||
def address_to_scripthash(addr: str) -> str:
|
def address_to_scripthash(addr: str) -> str:
|
||||||
script = address_to_script(addr)
|
script = address_to_script(addr)
|
||||||
|
|
|
@ -17,6 +17,7 @@ from electrum.plugin import Device, DeviceInfo
|
||||||
from electrum.simple_config import SimpleConfig
|
from electrum.simple_config import SimpleConfig
|
||||||
from electrum.json_db import StoredDict
|
from electrum.json_db import StoredDict
|
||||||
from electrum.storage import get_derivation_used_for_hw_device_encryption
|
from electrum.storage import get_derivation_used_for_hw_device_encryption
|
||||||
|
from electrum.bitcoin import OnchainOutputType
|
||||||
|
|
||||||
import electrum.bitcoin as bitcoin
|
import electrum.bitcoin as bitcoin
|
||||||
import electrum.ecc as ecc
|
import electrum.ecc as ecc
|
||||||
|
@ -411,13 +412,13 @@ class BitBox02Client(HardwareClientBase):
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
addrtype, pubkey_hash = bitcoin.address_to_hash(txout.address)
|
addrtype, pubkey_hash = bitcoin.address_to_hash(txout.address)
|
||||||
if addrtype == bitcoin.WIF_SCRIPT_TYPES["p2pkh"]:
|
if addrtype == OnchainOutputType.P2PKH:
|
||||||
output_type = bitbox02.btc.P2PKH
|
output_type = bitbox02.btc.P2PKH
|
||||||
elif addrtype == bitcoin.WIF_SCRIPT_TYPES["p2sh"]:
|
elif addrtype == OnchainOutputType.P2SH:
|
||||||
output_type = bitbox02.btc.P2SH
|
output_type = bitbox02.btc.P2SH
|
||||||
elif addrtype == bitcoin.WIF_SCRIPT_TYPES["p2wpkh"]:
|
elif addrtype == OnchainOutputType.WITVER0_P2WPKH:
|
||||||
output_type = bitbox02.btc.P2WPKH
|
output_type = bitbox02.btc.P2WPKH
|
||||||
elif addrtype == bitcoin.WIF_SCRIPT_TYPES["p2wsh"]:
|
elif addrtype == OnchainOutputType.WITVER0_P2WSH:
|
||||||
output_type = bitbox02.btc.P2WSH
|
output_type = bitbox02.btc.P2WSH
|
||||||
else:
|
else:
|
||||||
raise UserFacingException(
|
raise UserFacingException(
|
||||||
|
|
Loading…
Add table
Reference in a new issue