From 3de565c33c3647d8ae776af2c02bc39bd602604d Mon Sep 17 00:00:00 2001 From: Andrew Kozlik Date: Tue, 5 May 2020 16:06:48 +0200 Subject: [PATCH] core/sign_tx: In write_tx_header() rename has_segwit parameter to witness_marker and clarify usage. --- core/src/apps/wallet/sign_tx/bitcoin.py | 13 ++++++++----- core/src/apps/wallet/sign_tx/bitcoinlike.py | 7 +++++-- core/src/apps/wallet/sign_tx/decred.py | 15 ++++++++++----- core/src/apps/wallet/sign_tx/zcash.py | 2 +- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/core/src/apps/wallet/sign_tx/bitcoin.py b/core/src/apps/wallet/sign_tx/bitcoin.py index f120274a1..3336b4711 100644 --- a/core/src/apps/wallet/sign_tx/bitcoin.py +++ b/core/src/apps/wallet/sign_tx/bitcoin.py @@ -291,7 +291,7 @@ class Bitcoin: # should come out the same as h_confirmed, checked before signing the digest h_check = self.create_hash_writer() - self.write_tx_header(h_sign, self.tx, has_segwit=False) + self.write_tx_header(h_sign, self.tx, witness_marker=False) writers.write_varint(h_sign, self.tx.inputs_count) for i in range(self.tx.inputs_count): @@ -374,8 +374,8 @@ class Bitcoin: txh = self.create_hash_writer() - # TODO set has_segwit correctly - self.write_tx_header(txh, tx, has_segwit=False) + # witnesses are not included in txid hash + self.write_tx_header(txh, tx, witness_marker=False) writers.write_varint(txh, tx.inputs_cnt) for i in range(tx.inputs_cnt): @@ -431,10 +431,13 @@ class Bitcoin: writers.write_tx_output(w, txo, script_pubkey) def write_tx_header( - self, w: writers.Writer, tx: Union[SignTx, TransactionType], has_segwit: bool + self, + w: writers.Writer, + tx: Union[SignTx, TransactionType], + witness_marker: bool, ) -> None: writers.write_uint32(w, tx.version) # nVersion - if has_segwit: + if witness_marker: writers.write_varint(w, 0x00) # segwit witness marker writers.write_varint(w, 0x01) # segwit witness flag diff --git a/core/src/apps/wallet/sign_tx/bitcoinlike.py b/core/src/apps/wallet/sign_tx/bitcoinlike.py index 02b8e39da..a49bd43a0 100644 --- a/core/src/apps/wallet/sign_tx/bitcoinlike.py +++ b/core/src/apps/wallet/sign_tx/bitcoinlike.py @@ -65,12 +65,15 @@ class Bitcoinlike(Bitcoin): return hashtype def write_tx_header( - self, w: writers.Writer, tx: Union[SignTx, TransactionType], has_segwit: bool + self, + w: writers.Writer, + tx: Union[SignTx, TransactionType], + witness_marker: bool, ) -> None: writers.write_uint32(w, tx.version) # nVersion if self.coin.timestamp: writers.write_uint32(w, tx.timestamp) - if has_segwit: + if witness_marker: writers.write_varint(w, 0x00) # segwit witness marker writers.write_varint(w, 0x01) # segwit witness flag diff --git a/core/src/apps/wallet/sign_tx/decred.py b/core/src/apps/wallet/sign_tx/decred.py index 4aa9dfa35..eee0dd457 100644 --- a/core/src/apps/wallet/sign_tx/decred.py +++ b/core/src/apps/wallet/sign_tx/decred.py @@ -32,7 +32,7 @@ class Decred(Bitcoin): ensure(coin.decred) super().__init__(tx, keychain, coin) - self.write_tx_header(self.serialized_tx, self.tx, has_segwit=False) + self.write_tx_header(self.serialized_tx, self.tx, witness_marker=True) writers.write_varint(self.serialized_tx, self.tx.inputs_count) def init_hash143(self) -> None: @@ -163,12 +163,17 @@ class Decred(Bitcoin): writers.write_tx_output_decred(w, txo, script_pubkey) def write_tx_header( - self, w: writers.Writer, tx: Union[SignTx, TransactionType], has_segwit: bool + self, + w: writers.Writer, + tx: Union[SignTx, TransactionType], + witness_marker: bool, ) -> None: - if isinstance(tx, TransactionType): - version = tx.version | DECRED_SERIALIZE_NO_WITNESS - else: + # The upper 16 bits of the transaction version specify the serialization + # format and the lower 16 bits specify the version number. + if witness_marker: version = tx.version | DECRED_SERIALIZE_FULL + else: + version = tx.version | DECRED_SERIALIZE_NO_WITNESS writers.write_uint32(w, version) diff --git a/core/src/apps/wallet/sign_tx/zcash.py b/core/src/apps/wallet/sign_tx/zcash.py index 34d13fe6f..ec10ca7a0 100644 --- a/core/src/apps/wallet/sign_tx/zcash.py +++ b/core/src/apps/wallet/sign_tx/zcash.py @@ -77,7 +77,7 @@ class Overwintered(Bitcoinlike): await self.sign_nonsegwit_bip143_input(i_sign) def write_tx_header( - self, w: Writer, tx: Union[SignTx, TransactionType], has_segwit: bool + self, w: Writer, tx: Union[SignTx, TransactionType], witness_marker: bool ) -> None: # nVersion | fOverwintered write_uint32(w, tx.version | OVERWINTERED)