|
|
|
@ -13,7 +13,7 @@ from .multisig import (
|
|
|
|
|
multisig_get_pubkeys,
|
|
|
|
|
multisig_pubkey_index,
|
|
|
|
|
)
|
|
|
|
|
from .readers import read_bytes_prefixed, read_op_push
|
|
|
|
|
from .readers import read_memoryview_prefixed, read_op_push
|
|
|
|
|
from .writers import (
|
|
|
|
|
op_push_length,
|
|
|
|
|
write_bytes_fixed,
|
|
|
|
@ -23,6 +23,8 @@ from .writers import (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if False:
|
|
|
|
|
from typing import Sequence
|
|
|
|
|
|
|
|
|
|
from trezor.messages import MultisigRedeemScriptType, TxInput
|
|
|
|
|
|
|
|
|
|
from apps.common.coininfo import CoinInfo
|
|
|
|
@ -116,7 +118,11 @@ def output_derive_script(address: str, coin: CoinInfo) -> bytes:
|
|
|
|
|
# see https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#specification
|
|
|
|
|
# item 5 for details
|
|
|
|
|
def write_bip143_script_code_prefixed(
|
|
|
|
|
w: Writer, txi: TxInput, public_keys: list[bytes], threshold: int, coin: CoinInfo
|
|
|
|
|
w: Writer,
|
|
|
|
|
txi: TxInput,
|
|
|
|
|
public_keys: Sequence[bytes | memoryview],
|
|
|
|
|
threshold: int,
|
|
|
|
|
coin: CoinInfo,
|
|
|
|
|
) -> None:
|
|
|
|
|
if len(public_keys) > 1:
|
|
|
|
|
write_output_script_multisig(w, public_keys, threshold, prefixed=True)
|
|
|
|
@ -152,15 +158,15 @@ def write_input_script_p2pkh_or_p2sh_prefixed(
|
|
|
|
|
append_pubkey(w, pubkey)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_input_script_p2pkh(script_sig: bytes) -> tuple[bytes, bytes, int]:
|
|
|
|
|
def parse_input_script_p2pkh(script_sig: bytes) -> tuple[memoryview, memoryview, int]:
|
|
|
|
|
try:
|
|
|
|
|
r = utils.BufferReader(script_sig)
|
|
|
|
|
n = read_op_push(r)
|
|
|
|
|
signature = r.read(n - 1)
|
|
|
|
|
signature = r.read_memoryview(n - 1)
|
|
|
|
|
hash_type = r.get()
|
|
|
|
|
|
|
|
|
|
n = read_op_push(r)
|
|
|
|
|
pubkey = r.read()
|
|
|
|
|
pubkey = r.read_memoryview()
|
|
|
|
|
if len(pubkey) != n:
|
|
|
|
|
raise ValueError
|
|
|
|
|
except (ValueError, EOFError):
|
|
|
|
@ -286,7 +292,7 @@ def write_witness_p2wpkh(
|
|
|
|
|
write_bytes_prefixed(w, pubkey)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_witness_p2wpkh(witness: bytes) -> tuple[bytes, bytes, int]:
|
|
|
|
|
def parse_witness_p2wpkh(witness: bytes) -> tuple[memoryview, memoryview, int]:
|
|
|
|
|
try:
|
|
|
|
|
r = utils.BufferReader(witness)
|
|
|
|
|
|
|
|
|
@ -295,10 +301,10 @@ def parse_witness_p2wpkh(witness: bytes) -> tuple[bytes, bytes, int]:
|
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
|
|
n = read_bitcoin_varint(r)
|
|
|
|
|
signature = r.read(n - 1)
|
|
|
|
|
signature = r.read_memoryview(n - 1)
|
|
|
|
|
hash_type = r.get()
|
|
|
|
|
|
|
|
|
|
pubkey = read_bytes_prefixed(r)
|
|
|
|
|
pubkey = read_memoryview_prefixed(r)
|
|
|
|
|
if r.remaining_count():
|
|
|
|
|
raise ValueError
|
|
|
|
|
except (ValueError, EOFError):
|
|
|
|
@ -342,7 +348,9 @@ def write_witness_multisig(
|
|
|
|
|
write_output_script_multisig(w, pubkeys, multisig.m, prefixed=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_witness_multisig(witness: bytes) -> tuple[bytes, list[tuple[bytes, int]]]:
|
|
|
|
|
def parse_witness_multisig(
|
|
|
|
|
witness: bytes,
|
|
|
|
|
) -> tuple[memoryview, list[tuple[memoryview, int]]]:
|
|
|
|
|
try:
|
|
|
|
|
r = utils.BufferReader(witness)
|
|
|
|
|
|
|
|
|
@ -356,11 +364,11 @@ def parse_witness_multisig(witness: bytes) -> tuple[bytes, list[tuple[bytes, int
|
|
|
|
|
signatures = []
|
|
|
|
|
for i in range(item_count - 2):
|
|
|
|
|
n = read_bitcoin_varint(r)
|
|
|
|
|
signature = r.read(n - 1)
|
|
|
|
|
signature = r.read_memoryview(n - 1)
|
|
|
|
|
hash_type = r.get()
|
|
|
|
|
signatures.append((signature, hash_type))
|
|
|
|
|
|
|
|
|
|
script = read_bytes_prefixed(r)
|
|
|
|
|
script = read_memoryview_prefixed(r)
|
|
|
|
|
if r.remaining_count():
|
|
|
|
|
raise ValueError
|
|
|
|
|
except (ValueError, EOFError):
|
|
|
|
@ -416,7 +424,7 @@ def write_input_script_multisig_prefixed(
|
|
|
|
|
|
|
|
|
|
def parse_input_script_multisig(
|
|
|
|
|
script_sig: bytes,
|
|
|
|
|
) -> tuple[bytes, list[tuple[bytes, int]]]:
|
|
|
|
|
) -> tuple[memoryview, list[tuple[memoryview, int]]]:
|
|
|
|
|
try:
|
|
|
|
|
r = utils.BufferReader(script_sig)
|
|
|
|
|
|
|
|
|
@ -427,12 +435,12 @@ def parse_input_script_multisig(
|
|
|
|
|
signatures = []
|
|
|
|
|
n = read_op_push(r)
|
|
|
|
|
while r.remaining_count() > n:
|
|
|
|
|
signature = r.read(n - 1)
|
|
|
|
|
signature = r.read_memoryview(n - 1)
|
|
|
|
|
hash_type = r.get()
|
|
|
|
|
signatures.append((signature, hash_type))
|
|
|
|
|
n = read_op_push(r)
|
|
|
|
|
|
|
|
|
|
script = r.read()
|
|
|
|
|
script = r.read_memoryview()
|
|
|
|
|
if len(script) != n:
|
|
|
|
|
raise ValueError
|
|
|
|
|
except (ValueError, EOFError):
|
|
|
|
@ -449,7 +457,7 @@ def output_script_multisig(pubkeys: list[bytes], m: int) -> bytearray:
|
|
|
|
|
|
|
|
|
|
def write_output_script_multisig(
|
|
|
|
|
w: Writer,
|
|
|
|
|
pubkeys: list[bytes],
|
|
|
|
|
pubkeys: Sequence[bytes | memoryview],
|
|
|
|
|
m: int,
|
|
|
|
|
prefixed: bool = False,
|
|
|
|
|
) -> None:
|
|
|
|
@ -470,11 +478,11 @@ def write_output_script_multisig(
|
|
|
|
|
w.append(0xAE) # OP_CHECKMULTISIG
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def output_script_multisig_length(pubkeys: list[bytes], m: int) -> int:
|
|
|
|
|
def output_script_multisig_length(pubkeys: Sequence[bytes | memoryview], m: int) -> int:
|
|
|
|
|
return 1 + len(pubkeys) * (1 + 33) + 1 + 1 # see output_script_multisig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_output_script_multisig(script: bytes) -> tuple[list[bytes], int]:
|
|
|
|
|
def parse_output_script_multisig(script: bytes) -> tuple[list[memoryview], int]:
|
|
|
|
|
try:
|
|
|
|
|
r = utils.BufferReader(script)
|
|
|
|
|
|
|
|
|
@ -493,7 +501,7 @@ def parse_output_script_multisig(script: bytes) -> tuple[list[bytes], int]:
|
|
|
|
|
n = read_op_push(r)
|
|
|
|
|
if n != 33:
|
|
|
|
|
raise ValueError
|
|
|
|
|
public_keys.append(r.read(n))
|
|
|
|
|
public_keys.append(r.read_memoryview(n))
|
|
|
|
|
|
|
|
|
|
r.get() # ignore pubkey_count
|
|
|
|
|
if r.get() != 0xAE: # OP_CHECKMULTISIG
|
|
|
|
@ -550,9 +558,9 @@ def write_bip322_signature_proof(
|
|
|
|
|
w.append(0x00)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def read_bip322_signature_proof(r: utils.BufferReader) -> tuple[bytes, bytes]:
|
|
|
|
|
script_sig = read_bytes_prefixed(r)
|
|
|
|
|
witness = r.read()
|
|
|
|
|
def read_bip322_signature_proof(r: utils.BufferReader) -> tuple[memoryview, memoryview]:
|
|
|
|
|
script_sig = read_memoryview_prefixed(r)
|
|
|
|
|
witness = r.read_memoryview()
|
|
|
|
|
return script_sig, witness
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -572,6 +580,6 @@ def append_signature(w: Writer, signature: bytes, hash_type: int) -> None:
|
|
|
|
|
w.append(hash_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def append_pubkey(w: Writer, pubkey: bytes) -> None:
|
|
|
|
|
def append_pubkey(w: Writer, pubkey: bytes | memoryview) -> None:
|
|
|
|
|
write_op_push(w, len(pubkey))
|
|
|
|
|
write_bytes_unchecked(w, pubkey)
|
|
|
|
|