1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 14:58:09 +00:00

refactor(core): Rename bitcoin_varint to compact_size.

This commit is contained in:
Andrew Kozlik 2022-01-17 13:03:21 +01:00 committed by Andrew Kozlik
parent a5824ed1ff
commit f17a435cdf
17 changed files with 91 additions and 81 deletions

View File

@ -7,13 +7,13 @@ from trezor.enums import InputScriptType
from trezor.utils import HashWriter
from apps.bitcoin.writers import (
write_bitcoin_varint,
write_bytes_fixed,
write_bytes_prefixed,
write_compact_size,
write_uint8,
)
from apps.common.keychain import Keychain
from apps.common.readers import read_bitcoin_varint
from apps.common.readers import read_compact_size
from . import common
from .scripts import read_bip322_signature_proof, write_bip322_signature_proof
@ -51,7 +51,7 @@ def generate_proof(
write_bytes_fixed(proof, _VERSION_MAGIC, 4)
write_uint8(proof, flags)
write_bitcoin_varint(proof, len(ownership_ids))
write_compact_size(proof, len(ownership_ids))
for ownership_id in ownership_ids:
write_bytes_fixed(proof, ownership_id, _OWNERSHIP_ID_LEN)
@ -94,7 +94,7 @@ def verify_nonownership(
raise wire.DataError("Unknown flags in proof of ownership")
# Determine whether our ownership ID appears in the proof.
id_count = read_bitcoin_varint(r)
id_count = read_compact_size(r)
ownership_id = get_identifier(script_pubkey, keychain)
not_owned = True
for _ in range(id_count):
@ -134,7 +134,7 @@ def read_scriptsig_witness(ownership_proof: bytes) -> tuple[memoryview, memoryvi
raise wire.DataError("Unknown flags in proof of ownership")
# Skip ownership IDs.
id_count = read_bitcoin_varint(r)
id_count = read_compact_size(r)
r.read_memoryview(_OWNERSHIP_ID_LEN * id_count)
return read_bip322_signature_proof(r)

View File

@ -1,10 +1,10 @@
from trezor.utils import BufferReader
from apps.common.readers import read_bitcoin_varint
from apps.common.readers import read_compact_size
def read_memoryview_prefixed(r: BufferReader) -> memoryview:
n = read_bitcoin_varint(r)
n = read_compact_size(r)
return r.read_memoryview(n)

View File

@ -6,8 +6,8 @@ from trezor.crypto.hashlib import sha256
from trezor.enums import InputScriptType
from apps.common import address_type
from apps.common.readers import read_bitcoin_varint
from apps.common.writers import write_bitcoin_varint
from apps.common.readers import read_compact_size
from apps.common.writers import write_compact_size
from . import common
from .common import SigHashType
@ -155,7 +155,7 @@ def write_bip143_script_code_prefixed(
def write_input_script_p2pkh_or_p2sh_prefixed(
w: Writer, pubkey: bytes, signature: bytes, sighash_type: SigHashType
) -> None:
write_bitcoin_varint(w, 1 + len(signature) + 1 + 1 + len(pubkey))
write_compact_size(w, 1 + len(signature) + 1 + 1 + len(pubkey))
append_signature(w, signature, sighash_type)
append_pubkey(w, pubkey)
@ -183,7 +183,7 @@ def write_output_script_p2pkh(
w: Writer, pubkeyhash: bytes, prefixed: bool = False
) -> None:
if prefixed:
write_bitcoin_varint(w, 25)
write_compact_size(w, 25)
w.append(0x76) # OP_DUP
w.append(0xA9) # OP_HASH160
w.append(0x14) # OP_DATA_20
@ -281,7 +281,7 @@ def write_input_script_p2wpkh_in_p2sh(
# 16 00 14 <pubkeyhash>
# Signature is moved to the witness.
if prefixed:
write_bitcoin_varint(w, 23)
write_compact_size(w, 23)
w.append(0x16) # length of the data
w.append(0x00) # witness version byte
@ -303,7 +303,7 @@ def write_input_script_p2wsh_in_p2sh(
# 22 00 20 <redeem script hash>
# Signature is moved to the witness.
if prefixed:
write_bitcoin_varint(w, 35)
write_compact_size(w, 35)
w.append(0x22) # length of the data
w.append(0x00) # witness version byte
@ -318,7 +318,7 @@ def write_input_script_p2wsh_in_p2sh(
def write_witness_p2wpkh(
w: Writer, signature: bytes, pubkey: bytes, sighash_type: SigHashType
) -> None:
write_bitcoin_varint(w, 0x02) # num of segwit items, in P2WPKH it's always 2
write_compact_size(w, 0x02) # num of segwit items, in P2WPKH it's always 2
write_signature_prefixed(w, signature, sighash_type)
write_bytes_prefixed(w, pubkey)
@ -331,7 +331,7 @@ def parse_witness_p2wpkh(witness: bytes) -> tuple[memoryview, memoryview, SigHas
# num of stack items, in P2WPKH it's always 2
raise ValueError
n = read_bitcoin_varint(r)
n = read_compact_size(r)
signature = r.read_memoryview(n - 1)
sighash_type = SigHashType.from_int(r.get())
@ -363,12 +363,12 @@ def write_witness_multisig(
# witness program + signatures + redeem script
num_of_witness_items = 1 + sum(1 for s in signatures if s) + 1
write_bitcoin_varint(w, num_of_witness_items)
write_compact_size(w, num_of_witness_items)
# Starts with OP_FALSE because of an old OP_CHECKMULTISIG bug, which
# consumes one additional item on the stack:
# https://bitcoin.org/en/developer-guide#standard-transactions
write_bitcoin_varint(w, 0)
write_compact_size(w, 0)
for s in signatures:
if s:
@ -386,7 +386,7 @@ def parse_witness_multisig(
r = utils.BufferReader(witness)
# Get number of witness stack items.
item_count = read_bitcoin_varint(r)
item_count = read_compact_size(r)
# Skip over OP_FALSE, which is due to the old OP_CHECKMULTISIG bug.
if r.get() != 0:
@ -394,7 +394,7 @@ def parse_witness_multisig(
signatures = []
for _ in range(item_count - 2):
n = read_bitcoin_varint(r)
n = read_compact_size(r)
signature = r.read_memoryview(n - 1)
sighash_type = SigHashType.from_int(r.get())
signatures.append((signature, sighash_type))
@ -414,7 +414,7 @@ def parse_witness_multisig(
def write_witness_p2tr(w: Writer, signature: bytes, sighash_type: SigHashType) -> None:
# Taproot key path spending without annex.
write_bitcoin_varint(w, 0x01) # num of segwit items
write_compact_size(w, 0x01) # num of segwit items
write_signature_prefixed(w, signature, sighash_type)
@ -426,7 +426,7 @@ def parse_witness_p2tr(witness: bytes) -> tuple[memoryview, SigHashType]:
# Only Taproot key path spending without annex is supported.
raise ValueError
n = read_bitcoin_varint(r)
n = read_compact_size(r)
if n not in (64, 65):
raise ValueError
@ -473,7 +473,7 @@ def write_input_script_multisig_prefixed(
if s:
total_length += 1 + len(s) + 1 # length, signature, sighash_type
total_length += op_push_length(redeem_script_length) + redeem_script_length
write_bitcoin_varint(w, total_length)
write_compact_size(w, total_length)
# Starts with OP_FALSE because of an old OP_CHECKMULTISIG bug, which
# consumes one additional item on the stack:
@ -536,7 +536,7 @@ def write_output_script_multisig(
raise wire.DataError("Invalid multisig parameters")
if prefixed:
write_bitcoin_varint(w, output_script_multisig_length(pubkeys, m))
write_compact_size(w, output_script_multisig_length(pubkeys, m))
w.append(0x50 + m) # numbers 1 to 16 are pushed as 0x50 + value
for p in pubkeys:
@ -644,7 +644,7 @@ def write_signature_prefixed(
if sighash_type != SigHashType.SIGHASH_ALL_TAPROOT:
length += 1
write_bitcoin_varint(w, length)
write_compact_size(w, length)
write_bytes_unchecked(w, signature)
if sighash_type != SigHashType.SIGHASH_ALL_TAPROOT:
w.append(sighash_type)

View File

@ -15,7 +15,7 @@ from .scripts import ( # noqa: F401
write_output_script_multisig,
write_output_script_p2pkh,
)
from .writers import op_push_length, write_bitcoin_varint, write_op_push
from .writers import op_push_length, write_compact_size, write_op_push
if TYPE_CHECKING:
from trezor.messages import MultisigRedeemScriptType
@ -73,7 +73,7 @@ def write_input_script_multisig_prefixed(
if s:
total_length += 1 + len(s) + 1 # length, signature, hash_type
total_length += op_push_length(redeem_script_length) + redeem_script_length
write_bitcoin_varint(w, total_length)
write_compact_size(w, total_length)
for s in signatures:
if s:
@ -113,7 +113,7 @@ def output_script_sstxchange(addr: str) -> bytearray:
# Spend from a stake revocation.
def write_output_script_ssrtx_prefixed(w: Writer, pkh: bytes) -> None:
utils.ensure(len(pkh) == 20)
write_bitcoin_varint(w, 26)
write_compact_size(w, 26)
w.append(0xBC) # OP_SSRTX
scripts.write_output_script_p2pkh(w, pkh)
@ -121,7 +121,7 @@ def write_output_script_ssrtx_prefixed(w: Writer, pkh: bytes) -> None:
# Spend from a stake generation.
def write_output_script_ssgen_prefixed(w: Writer, pkh: bytes) -> None:
utils.ensure(len(pkh) == 20)
write_bitcoin_varint(w, 26)
write_compact_size(w, 26)
w.append(0xBB) # OP_SSGEN
scripts.write_output_script_p2pkh(w, pkh)

View File

@ -7,7 +7,7 @@ from trezor.enums import InputScriptType, OutputScriptType
from trezor.messages import TxRequest, TxRequestDetailsType, TxRequestSerializedType
from trezor.utils import HashWriter, empty_bytearray, ensure
from apps.common.writers import write_bitcoin_varint
from apps.common.writers import write_compact_size
from .. import addresses, common, multisig, scripts, writers
from ..common import (
@ -241,7 +241,7 @@ class Bitcoin:
async def step4_serialize_inputs(self) -> None:
self.write_tx_header(self.serialized_tx, self.tx_info.tx, bool(self.segwit))
write_bitcoin_varint(self.serialized_tx, self.tx_info.tx.inputs_count)
write_compact_size(self.serialized_tx, self.tx_info.tx.inputs_count)
for i in range(self.tx_info.tx.inputs_count):
progress.advance()
if i in self.external:
@ -252,7 +252,7 @@ class Bitcoin:
await self.sign_nonsegwit_input(i)
async def step5_serialize_outputs(self) -> None:
write_bitcoin_varint(self.serialized_tx, self.tx_info.tx.outputs_count)
write_compact_size(self.serialized_tx, self.tx_info.tx.outputs_count)
for i in range(self.tx_info.tx.outputs_count):
progress.advance()
await self.serialize_output(i)
@ -626,7 +626,7 @@ class Bitcoin:
h_check = HashWriter(sha256())
self.write_tx_header(h_sign, tx_info.tx, witness_marker=False)
write_bitcoin_varint(h_sign, tx_info.tx.inputs_count)
write_compact_size(h_sign, tx_info.tx.inputs_count)
txi_sign = None
node = None
@ -664,7 +664,7 @@ class Bitcoin:
if txi_sign is None:
raise RuntimeError # index >= tx_info.tx.inputs_count
write_bitcoin_varint(h_sign, tx_info.tx.outputs_count)
write_compact_size(h_sign, tx_info.tx.outputs_count)
for i in range(tx_info.tx.outputs_count):
# STAGE_REQUEST_4_OUTPUT in legacy
@ -722,7 +722,7 @@ class Bitcoin:
# witnesses are not included in txid hash
self.write_tx_header(txh, tx, witness_marker=False)
write_bitcoin_varint(txh, tx.inputs_count)
write_compact_size(txh, tx.inputs_count)
for i in range(tx.inputs_count):
# STAGE_REQUEST_3_PREV_INPUT in legacy
@ -731,7 +731,7 @@ class Bitcoin:
)
self.write_tx_input(txh, txi, txi.script_sig)
write_bitcoin_varint(txh, tx.outputs_count)
write_compact_size(txh, tx.outputs_count)
script_pubkey: bytes | None = None
for i in range(tx.outputs_count):
@ -822,8 +822,8 @@ class Bitcoin:
) -> None:
writers.write_uint32(w, tx.version) # nVersion
if witness_marker:
write_bitcoin_varint(w, 0x00) # segwit witness marker
write_bitcoin_varint(w, 0x01) # segwit witness flag
write_compact_size(w, 0x00) # segwit witness marker
write_compact_size(w, 0x01) # segwit witness flag
def write_tx_footer(self, w: writers.Writer, tx: SignTx | PrevTx) -> None:
writers.write_uint32(w, tx.lock_time)

View File

@ -3,7 +3,7 @@ from typing import TYPE_CHECKING
from trezor import wire
from trezor.messages import PrevTx, SignTx, TxInput
from apps.common.writers import write_bitcoin_varint
from apps.common.writers import write_compact_size
from .. import multisig, writers
from ..common import NONSEGWIT_INPUT_SCRIPT_TYPES, SigHashType
@ -79,8 +79,8 @@ class Bitcoinlike(Bitcoin):
assert tx.timestamp is not None # checked in sanitize_*
writers.write_uint32(w, tx.timestamp)
if witness_marker:
write_bitcoin_varint(w, 0x00) # segwit witness marker
write_bitcoin_varint(w, 0x01) # segwit witness flag
write_compact_size(w, 0x00) # segwit witness marker
write_compact_size(w, 0x01) # segwit witness flag
async def write_prev_tx_footer(
self, w: writers.Writer, tx: PrevTx, prev_hash: bytes

View File

@ -7,7 +7,7 @@ from trezor.enums import DecredStakingSpendType, InputScriptType
from trezor.messages import PrevOutput
from trezor.utils import HashWriter, ensure
from apps.common.writers import write_bitcoin_varint
from apps.common.writers import write_compact_size
from .. import multisig, scripts_decred, writers
from ..common import SigHashType, ecdsa_hash_pubkey, ecdsa_sign
@ -96,12 +96,12 @@ class Decred(Bitcoin):
super().__init__(tx, keychain, coin, approver)
self.write_tx_header(self.serialized_tx, self.tx_info.tx, witness_marker=True)
write_bitcoin_varint(self.serialized_tx, self.tx_info.tx.inputs_count)
write_compact_size(self.serialized_tx, self.tx_info.tx.inputs_count)
writers.write_uint32(
self.h_prefix, self.tx_info.tx.version | DECRED_SERIALIZE_NO_WITNESS
)
write_bitcoin_varint(self.h_prefix, self.tx_info.tx.inputs_count)
write_compact_size(self.h_prefix, self.tx_info.tx.inputs_count)
def create_hash_writer(self) -> HashWriter:
return HashWriter(blake256())
@ -110,8 +110,8 @@ class Decred(Bitcoin):
return DecredSigHasher(self.h_prefix)
async def step2_approve_outputs(self) -> None:
write_bitcoin_varint(self.serialized_tx, self.tx_info.tx.outputs_count)
write_bitcoin_varint(self.h_prefix, self.tx_info.tx.outputs_count)
write_compact_size(self.serialized_tx, self.tx_info.tx.outputs_count)
write_compact_size(self.h_prefix, self.tx_info.tx.outputs_count)
if self.tx_info.tx.decred_staking_ticket:
await self.approve_staking_ticket()
@ -143,7 +143,7 @@ class Decred(Bitcoin):
self.write_tx_output(self.serialized_tx, txo, script_pubkey)
async def step4_serialize_inputs(self) -> None:
write_bitcoin_varint(self.serialized_tx, self.tx_info.tx.inputs_count)
write_compact_size(self.serialized_tx, self.tx_info.tx.inputs_count)
prefix_hash = self.h_prefix.get_digest()
@ -161,7 +161,7 @@ class Decred(Bitcoin):
writers.write_uint32(
h_witness, self.tx_info.tx.version | DECRED_SERIALIZE_WITNESS_SIGNING
)
write_bitcoin_varint(h_witness, self.tx_info.tx.inputs_count)
write_compact_size(h_witness, self.tx_info.tx.inputs_count)
for ii in range(self.tx_info.tx.inputs_count):
if ii == i_sign:
@ -190,7 +190,7 @@ class Decred(Bitcoin):
else:
raise wire.DataError("Unsupported input script type")
else:
write_bitcoin_varint(h_witness, 0)
write_compact_size(h_witness, 0)
witness_hash = writers.get_tx_hash(
h_witness, double=self.coin.sign_hash_double, reverse=False

View File

@ -50,7 +50,7 @@ class PaymentRequestVerifier:
writers.write_bytes_fixed(self.h_pr, b"SL\x00\x24", 4)
writers.write_bytes_prefixed(self.h_pr, nonce)
writers.write_bytes_prefixed(self.h_pr, msg.recipient_name.encode())
writers.write_bitcoin_varint(self.h_pr, len(msg.memos))
writers.write_compact_size(self.h_pr, len(msg.memos))
for m in msg.memos:
if m.text_memo is not None:
memo = m.text_memo

View File

@ -143,7 +143,7 @@ class OriginalTxInfo(TxInfoBase):
# Transaction hasher to compute the TXID.
self.h_tx = signer.create_hash_writer()
signer.write_tx_header(self.h_tx, tx, witness_marker=False)
writers.write_bitcoin_varint(self.h_tx, tx.inputs_count)
writers.write_compact_size(self.h_tx, tx.inputs_count)
# The input which will be used for verification and its index in the original transaction.
self.verification_input: TxInput | None = None
@ -162,7 +162,7 @@ class OriginalTxInfo(TxInfoBase):
super().add_output(txo, script_pubkey)
if self.index == 0:
writers.write_bitcoin_varint(self.h_tx, self.tx.outputs_count)
writers.write_compact_size(self.h_tx, self.tx.outputs_count)
writers.write_tx_output(self.h_tx, txo, script_pubkey)

View File

@ -58,9 +58,9 @@ class TxWeightCalculator:
n = len(i.multisig.nodes) if i.multisig.nodes else len(i.multisig.pubkeys)
multisig_script_size = _TXSIZE_MULTISIGSCRIPT + n * (1 + _TXSIZE_PUBKEY)
if i.script_type in common.SEGWIT_INPUT_SCRIPT_TYPES:
multisig_script_size += self.varint_size(multisig_script_size)
multisig_script_size += self.compact_size_len(multisig_script_size)
else:
multisig_script_size += self.op_push_size(multisig_script_size)
multisig_script_size += self.op_push_len(multisig_script_size)
input_script_size = (
1 # the OP_FALSE bug in multisig
@ -75,7 +75,7 @@ class TxWeightCalculator:
self.counter += 4 * _TXSIZE_INPUT
if i.script_type in common.NONSEGWIT_INPUT_SCRIPT_TYPES:
input_script_size += self.varint_size(input_script_size)
input_script_size += self.compact_size_len(input_script_size)
self.counter += 4 * input_script_size
elif i.script_type in common.SEGWIT_INPUT_SCRIPT_TYPES:
self.segwit_inputs_count += 1
@ -102,20 +102,22 @@ class TxWeightCalculator:
if witness_size > 1:
self.segwit_inputs_count += 1
self.counter += 4 * (self.varint_size(script_sig_size) + script_sig_size)
self.counter += 4 * (
self.compact_size_len(script_sig_size) + script_sig_size
)
self.counter += witness_size
else:
raise wire.DataError("Invalid script type")
def add_output(self, script: bytes) -> None:
self.outputs_count += 1
script_size = self.varint_size(len(script)) + len(script)
script_size = self.compact_size_len(len(script)) + len(script)
self.counter += 4 * (_TXSIZE_OUTPUT + script_size)
def get_total(self) -> int:
total = self.counter
total += 4 * self.varint_size(self.inputs_count)
total += 4 * self.varint_size(self.outputs_count)
total += 4 * self.compact_size_len(self.inputs_count)
total += 4 * self.compact_size_len(self.outputs_count)
if self.segwit_inputs_count:
total += _TXSIZE_SEGWIT_OVERHEAD
# add one byte of witness stack item count per non-segwit input
@ -124,7 +126,7 @@ class TxWeightCalculator:
return total
@staticmethod
def varint_size(length: int) -> int:
def compact_size_len(length: int) -> int:
if length < 253:
return 1
if length < 0x1_0000:
@ -132,7 +134,7 @@ class TxWeightCalculator:
return 5
@staticmethod
def op_push_size(length: int) -> int:
def op_push_len(length: int) -> int:
if length < 0x4C:
return 1
if length < 0x100:

View File

@ -9,7 +9,7 @@ from trezor.utils import HashWriter, ensure
from apps.common.coininfo import CoinInfo
from apps.common.keychain import Keychain
from apps.common.writers import write_bitcoin_varint
from apps.common.writers import write_compact_size
from ..scripts import write_bip143_script_code_prefixed
from ..writers import (
@ -135,9 +135,9 @@ class Zcashlike(Bitcoinlike):
self.write_tx_footer(self.serialized_tx, self.tx_info.tx)
write_uint64(self.serialized_tx, 0) # valueBalance
write_bitcoin_varint(self.serialized_tx, 0) # nShieldedSpend
write_bitcoin_varint(self.serialized_tx, 0) # nShieldedOutput
write_bitcoin_varint(self.serialized_tx, 0) # nJoinSplit
write_compact_size(self.serialized_tx, 0) # nShieldedSpend
write_compact_size(self.serialized_tx, 0) # nShieldedOutput
write_compact_size(self.serialized_tx, 0) # nJoinSplit
await helpers.request_tx_finish(self.tx_req)

View File

@ -5,10 +5,10 @@ from trezor.crypto.hashlib import sha256
from trezor.utils import ensure
from apps.common.writers import ( # noqa: F401
write_bitcoin_varint,
write_bytes_fixed,
write_bytes_reversed,
write_bytes_unchecked,
write_compact_size,
write_uint8,
write_uint16_le,
write_uint32_le,
@ -34,7 +34,7 @@ TX_HASH_SIZE = const(32)
def write_bytes_prefixed(w: Writer, b: bytes) -> None:
write_bitcoin_varint(w, len(b))
write_compact_size(w, len(b))
write_bytes_unchecked(w, b)

View File

@ -3,7 +3,7 @@ from typing import TYPE_CHECKING
from trezor import utils, wire
from trezor.crypto import hashlib, hmac
from .writers import write_bitcoin_varint, write_bytes_unchecked, write_uint32_le
from .writers import write_bytes_unchecked, write_compact_size, write_uint32_le
if TYPE_CHECKING:
from apps.common.keychain import Keychain
@ -27,6 +27,6 @@ def get_address_mac(address: str, slip44: int, keychain: Keychain) -> bytes:
mac = utils.HashWriter(hmac(hmac.SHA256, node.key()))
address_bytes = address.encode()
write_uint32_le(mac, slip44)
write_bitcoin_varint(mac, len(address_bytes))
write_compact_size(mac, len(address_bytes))
write_bytes_unchecked(mac, address_bytes)
return mac.get_digest()

View File

@ -1,7 +1,7 @@
from trezor.utils import BufferReader
def read_bitcoin_varint(r: BufferReader) -> int:
def read_compact_size(r: BufferReader) -> int:
prefix = r.get()
if prefix < 253:
n = prefix
@ -13,6 +13,15 @@ def read_bitcoin_varint(r: BufferReader) -> int:
n += r.get() << 8
n += r.get() << 16
n += r.get() << 24
elif prefix == 255:
n = r.get()
n += r.get() << 8
n += r.get() << 16
n += r.get() << 24
n += r.get() << 32
n += r.get() << 40
n += r.get() << 48
n += r.get() << 56
else:
raise ValueError
return n

View File

@ -4,7 +4,7 @@ from ubinascii import hexlify
from trezor import utils, wire
from trezor.crypto.hashlib import blake256, sha256
from apps.common.writers import write_bitcoin_varint
from apps.common.writers import write_compact_size
if TYPE_CHECKING:
from apps.common.coininfo import CoinInfo
@ -17,9 +17,9 @@ def message_digest(coin: CoinInfo, message: bytes) -> bytes:
h = utils.HashWriter(sha256())
if not coin.signed_message_header:
raise wire.DataError("Empty message header not allowed.")
write_bitcoin_varint(h, len(coin.signed_message_header))
write_compact_size(h, len(coin.signed_message_header))
h.extend(coin.signed_message_header.encode())
write_bitcoin_varint(h, len(message))
write_compact_size(h, len(message))
h.extend(message)
ret = h.get_digest()
if coin.sign_hash_double:

View File

@ -87,20 +87,19 @@ def write_bytes_reversed(w: Writer, b: bytes, length: int) -> int:
return length
def write_bitcoin_varint(w: Writer, n: int) -> None:
def write_compact_size(w: Writer, n: int) -> None:
ensure(0 <= n <= 0xFFFF_FFFF)
if n < 253:
w.append(n & 0xFF)
elif n < 0x1_0000:
w.append(253)
w.append(n & 0xFF)
w.append((n >> 8) & 0xFF)
else:
write_uint16_le(w, n)
elif n < 0x1_0000_0000:
w.append(254)
w.append(n & 0xFF)
w.append((n >> 8) & 0xFF)
w.append((n >> 16) & 0xFF)
w.append((n >> 24) & 0xFF)
write_uint32_le(w, n)
else:
w.append(255)
write_uint64_le(w, n)
def write_uvarint(w: Writer, n: int) -> None:

View File

@ -134,7 +134,7 @@ class TestApprover(unittest.TestCase):
writers.write_bytes_fixed(h_pr, b"SL\x00\x24", 4)
writers.write_bytes_prefixed(h_pr, b"") # Empty nonce.
writers.write_bytes_prefixed(h_pr, self.coordinator_name.encode())
writers.write_bitcoin_varint(h_pr, 0) # No memos.
writers.write_compact_size(h_pr, 0) # No memos.
writers.write_uint32(h_pr, self.coin.slip44)
h_outputs = HashWriter(sha256())
for txo in outputs: