1
0
mirror of https://github.com/bitcoin/bips.git synced 2026-05-18 16:59:30 +00:00

BIP352: complete return type annotations in bitcoin_utils

The serialization helpers in bip-0352/bitcoin_utils.py were partially
typed: ser_uint32, hash160, is_p2tr, is_p2wpkh, is_p2sh and is_p2pkh
already declare argument and return types, but the surrounding
from_hex / ser_uint256 / deser_uint256 / deser_txid / deser_compact_size
/ deser_string / deser_string_vector helpers omit them.

Annotate the missing return types (and fill in the obvious argument
types) so the file is consistent and so static analysis can flow types
through callers in reference.py. No behavior changes.
This commit is contained in:
omipheo
2026-05-08 07:39:17 +02:00
parent 9fce983a96
commit 3b76c84d6f

View File

@@ -3,10 +3,10 @@ import struct
from io import BytesIO
from ripemd160 import ripemd160
from secp256k1lab.secp256k1 import Scalar
from typing import Union
from typing import List, Union
def from_hex(hex_string):
def from_hex(hex_string: str) -> BytesIO:
"""Deserialize from a hex string representation (e.g. from RPC)"""
return BytesIO(bytes.fromhex(hex_string))
@@ -15,15 +15,15 @@ def ser_uint32(u: int) -> bytes:
return u.to_bytes(4, "big")
def ser_uint256(u):
def ser_uint256(u: int) -> bytes:
return u.to_bytes(32, 'little')
def deser_uint256(f):
def deser_uint256(f: BytesIO) -> int:
return int.from_bytes(f.read(32), 'little')
def deser_txid(txid: str):
def deser_txid(txid: str) -> bytes:
# recall that txids are serialized little-endian, but displayed big-endian
# this means when converting from a human readable hex txid, we need to first
# reverse it before deserializing it
@@ -31,7 +31,7 @@ def deser_txid(txid: str):
return bytes.fromhex(dixt)
def deser_compact_size(f: BytesIO):
def deser_compact_size(f: BytesIO) -> int:
view = f.getbuffer()
nbytes = view.nbytes
view.release()
@@ -48,12 +48,12 @@ def deser_compact_size(f: BytesIO):
return nit
def deser_string(f: BytesIO):
def deser_string(f: BytesIO) -> bytes:
nit = deser_compact_size(f)
return f.read(nit)
def deser_string_vector(f: BytesIO):
def deser_string_vector(f: BytesIO) -> List[bytes]:
nit = deser_compact_size(f)
r = []
for _ in range(nit):