refactor(core): call super().__init__() in subclasses

pull/1370/head
Martin Milata 4 years ago
parent fa1566cb71
commit ee64b65b26

@ -15,7 +15,7 @@ from ..common import SIGHASH_ALL, ecdsa_sign, input_is_external, input_is_segwit
from ..ownership import verify_nonownership from ..ownership import verify_nonownership
from ..verification import SignatureVerifier from ..verification import SignatureVerifier
from . import approvers, helpers, progress from . import approvers, helpers, progress
from .hash143 import Hash143 from .hash143 import Bip143Hash
from .tx_info import OriginalTxInfo, TxInfo from .tx_info import OriginalTxInfo, TxInfo
if False: if False:
@ -32,6 +32,8 @@ if False:
from apps.common.coininfo import CoinInfo from apps.common.coininfo import CoinInfo
from apps.common.keychain import Keychain from apps.common.keychain import Keychain
from .hash143 import Hash143
# the number of bytes to preallocate for serialized transaction chunks # the number of bytes to preallocate for serialized transaction chunks
_MAX_SERIALIZED_CHUNK_SIZE = const(2048) _MAX_SERIALIZED_CHUNK_SIZE = const(2048)
@ -108,7 +110,7 @@ class Bitcoin:
return HashWriter(sha256()) return HashWriter(sha256())
def create_hash143(self) -> Hash143: def create_hash143(self) -> Hash143:
return Hash143() return Bip143Hash()
async def step1_process_inputs(self) -> None: async def step1_process_inputs(self) -> None:
for i in range(self.tx_info.tx.inputs_count): for i in range(self.tx_info.tx.inputs_count):

@ -12,7 +12,6 @@ from .. import multisig, scripts, writers
from ..common import ecdsa_hash_pubkey, ecdsa_sign from ..common import ecdsa_hash_pubkey, ecdsa_sign
from . import approvers, helpers, progress from . import approvers, helpers, progress
from .bitcoin import Bitcoin from .bitcoin import Bitcoin
from .hash143 import Hash143
DECRED_SERIALIZE_FULL = const(0 << 16) DECRED_SERIALIZE_FULL = const(0 << 16)
DECRED_SERIALIZE_NO_WITNESS = const(1 << 16) DECRED_SERIALIZE_NO_WITNESS = const(1 << 16)
@ -22,7 +21,7 @@ DECRED_SCRIPT_VERSION = const(0)
DECRED_SIGHASH_ALL = const(1) DECRED_SIGHASH_ALL = const(1)
if False: if False:
from typing import Optional, Union from typing import Optional, Union, List
from trezor.messages.SignTx import SignTx from trezor.messages.SignTx import SignTx
from trezor.messages.TxInput import TxInput from trezor.messages.TxInput import TxInput
@ -33,8 +32,10 @@ if False:
from apps.common.coininfo import CoinInfo from apps.common.coininfo import CoinInfo
from apps.common.keychain import Keychain from apps.common.keychain import Keychain
from .hash143 import Hash143
class DecredHash(Hash143):
class DecredHash:
def __init__(self, h_prefix: HashWriter) -> None: def __init__(self, h_prefix: HashWriter) -> None:
self.h_prefix = h_prefix self.h_prefix = h_prefix
@ -44,6 +45,17 @@ class DecredHash(Hash143):
def add_output(self, txo: TxOutput, script_pubkey: bytes) -> None: def add_output(self, txo: TxOutput, script_pubkey: bytes) -> None:
Decred.write_tx_output(self.h_prefix, txo, script_pubkey) Decred.write_tx_output(self.h_prefix, txo, script_pubkey)
def preimage_hash(
self,
txi: TxInput,
public_keys: List[bytes],
threshold: int,
tx: Union[SignTx, PrevTx],
coin: CoinInfo,
sighash_type: int,
) -> bytes:
raise NotImplementedError
class Decred(Bitcoin): class Decred(Bitcoin):
def __init__( def __init__(

@ -10,11 +10,29 @@ from apps.common import coininfo
from .. import scripts, writers from .. import scripts, writers
if False: if False:
from typing import List, Union from typing import List, Union, Protocol
class Hash143(Protocol):
def add_input(self, txi: TxInput) -> None:
...
def add_output(self, txo: TxOutput, script_pubkey: bytes) -> None:
...
def preimage_hash(
self,
txi: TxInput,
public_keys: List[bytes],
threshold: int,
tx: Union[SignTx, PrevTx],
coin: coininfo.CoinInfo,
sighash_type: int,
) -> bytes:
...
# BIP-0143 hash # BIP-0143 hash
class Hash143: class Bip143Hash:
def __init__(self) -> None: def __init__(self) -> None:
self.h_prevouts = HashWriter(sha256()) self.h_prevouts = HashWriter(sha256())
self.h_sequence = HashWriter(sha256()) self.h_sequence = HashWriter(sha256())

@ -7,6 +7,7 @@ from trezor.messages import InputScriptType
from trezor.messages.PrevTx import PrevTx from trezor.messages.PrevTx import PrevTx
from trezor.messages.SignTx import SignTx from trezor.messages.SignTx import SignTx
from trezor.messages.TxInput import TxInput from trezor.messages.TxInput import TxInput
from trezor.messages.TxOutput import TxOutput
from trezor.utils import HashWriter, ensure from trezor.utils import HashWriter, ensure
from apps.common.coininfo import CoinInfo from apps.common.coininfo import CoinInfo
@ -21,28 +22,37 @@ from ..writers import (
write_bytes_fixed, write_bytes_fixed,
write_bytes_prefixed, write_bytes_prefixed,
write_bytes_reversed, write_bytes_reversed,
write_tx_output,
write_uint32, write_uint32,
write_uint64, write_uint64,
) )
from . import approvers, helpers from . import approvers, helpers
from .bitcoinlike import Bitcoinlike from .bitcoinlike import Bitcoinlike
from .hash143 import Hash143
if False: if False:
from apps.common import coininfo from apps.common import coininfo
from typing import List, Optional, Union from typing import List, Optional, Union
from .hash143 import Hash143
from .tx_info import OriginalTxInfo, TxInfo from .tx_info import OriginalTxInfo, TxInfo
from ..writers import Writer from ..writers import Writer
OVERWINTERED = const(0x8000_0000) OVERWINTERED = const(0x8000_0000)
class Zip243Hash(Hash143): class Zip243Hash:
def __init__(self) -> None: def __init__(self) -> None:
self.h_prevouts = HashWriter(blake2b(outlen=32, personal=b"ZcashPrevoutHash")) self.h_prevouts = HashWriter(blake2b(outlen=32, personal=b"ZcashPrevoutHash"))
self.h_sequence = HashWriter(blake2b(outlen=32, personal=b"ZcashSequencHash")) self.h_sequence = HashWriter(blake2b(outlen=32, personal=b"ZcashSequencHash"))
self.h_outputs = HashWriter(blake2b(outlen=32, personal=b"ZcashOutputsHash")) self.h_outputs = HashWriter(blake2b(outlen=32, personal=b"ZcashOutputsHash"))
def add_input(self, txi: TxInput) -> None:
write_bytes_reversed(self.h_prevouts, txi.prev_hash, TX_HASH_SIZE)
write_uint32(self.h_prevouts, txi.prev_index)
write_uint32(self.h_sequence, txi.sequence)
def add_output(self, txo: TxOutput, script_pubkey: bytes) -> None:
write_tx_output(self.h_outputs, txo, script_pubkey)
def preimage_hash( def preimage_hash(
self, self,
txi: TxInput, txi: TxInput,

@ -237,6 +237,7 @@ _last_good_auth_check_cid = 0
class CborError(Exception): class CborError(Exception):
def __init__(self, code: int): def __init__(self, code: int):
super().__init__()
self.code = code self.code = code

@ -152,10 +152,6 @@ class MessageType:
def get_fields(cls) -> "FieldDict": def get_fields(cls) -> "FieldDict":
return {} return {}
def __init__(self, **kwargs: Any) -> None:
for kw in kwargs:
setattr(self, kw, kwargs[kw])
def __eq__(self, rhs: Any) -> bool: def __eq__(self, rhs: Any) -> bool:
return self.__class__ is rhs.__class__ and self.__dict__ == rhs.__dict__ return self.__class__ is rhs.__class__ and self.__dict__ == rhs.__dict__

@ -280,6 +280,7 @@ class Result(Exception):
""" """
def __init__(self, value: ResultValue) -> None: def __init__(self, value: ResultValue) -> None:
super().__init__()
self.value = value self.value = value

@ -299,6 +299,7 @@ class Context:
class UnexpectedMessageError(Exception): class UnexpectedMessageError(Exception):
def __init__(self, msg: codec_v1.Message) -> None: def __init__(self, msg: codec_v1.Message) -> None:
super().__init__()
self.msg = msg self.msg = msg

@ -2,7 +2,7 @@ from common import *
from apps.bitcoin.common import SIGHASH_ALL from apps.bitcoin.common import SIGHASH_ALL
from apps.bitcoin.scripts import output_derive_script from apps.bitcoin.scripts import output_derive_script
from apps.bitcoin.sign_tx.bitcoin import Hash143 from apps.bitcoin.sign_tx.bitcoin import Bip143Hash
from apps.bitcoin.writers import get_tx_hash from apps.bitcoin.writers import get_tx_hash
from apps.common import coins from apps.common import coins
from apps.common.keychain import Keychain from apps.common.keychain import Keychain
@ -49,7 +49,7 @@ class TestSegwitBip143NativeP2WPKH(unittest.TestCase):
def test_prevouts(self): def test_prevouts(self):
coin = coins.by_name(self.tx.coin_name) coin = coins.by_name(self.tx.coin_name)
bip143 = Hash143() bip143 = Bip143Hash()
bip143.add_input(self.inp1) bip143.add_input(self.inp1)
bip143.add_input(self.inp2) bip143.add_input(self.inp2)
prevouts_hash = get_tx_hash(bip143.h_prevouts, double=coin.sign_hash_double) prevouts_hash = get_tx_hash(bip143.h_prevouts, double=coin.sign_hash_double)
@ -57,7 +57,7 @@ class TestSegwitBip143NativeP2WPKH(unittest.TestCase):
def test_sequence(self): def test_sequence(self):
coin = coins.by_name(self.tx.coin_name) coin = coins.by_name(self.tx.coin_name)
bip143 = Hash143() bip143 = Bip143Hash()
bip143.add_input(self.inp1) bip143.add_input(self.inp1)
bip143.add_input(self.inp2) bip143.add_input(self.inp2)
sequence_hash = get_tx_hash(bip143.h_sequence, double=coin.sign_hash_double) sequence_hash = get_tx_hash(bip143.h_sequence, double=coin.sign_hash_double)
@ -67,7 +67,7 @@ class TestSegwitBip143NativeP2WPKH(unittest.TestCase):
seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '') seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '')
coin = coins.by_name(self.tx.coin_name) coin = coins.by_name(self.tx.coin_name)
bip143 = Hash143() bip143 = Bip143Hash()
for txo in [self.out1, self.out2]: for txo in [self.out1, self.out2]:
script_pubkey = output_derive_script(txo.address, coin) script_pubkey = output_derive_script(txo.address, coin)
@ -81,7 +81,7 @@ class TestSegwitBip143NativeP2WPKH(unittest.TestCase):
seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '') seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '')
coin = coins.by_name(self.tx.coin_name) coin = coins.by_name(self.tx.coin_name)
bip143 = Hash143() bip143 = Bip143Hash()
bip143.add_input(self.inp1) bip143.add_input(self.inp1)
bip143.add_input(self.inp2) bip143.add_input(self.inp2)

@ -2,7 +2,7 @@ from common import *
from apps.bitcoin.common import SIGHASH_ALL from apps.bitcoin.common import SIGHASH_ALL
from apps.bitcoin.scripts import output_derive_script from apps.bitcoin.scripts import output_derive_script
from apps.bitcoin.sign_tx.bitcoin import Hash143 from apps.bitcoin.sign_tx.bitcoin import Bip143Hash
from apps.bitcoin.writers import get_tx_hash from apps.bitcoin.writers import get_tx_hash
from apps.common import coins from apps.common import coins
from apps.common.keychain import Keychain from apps.common.keychain import Keychain
@ -41,14 +41,14 @@ class TestSegwitBip143(unittest.TestCase):
def test_bip143_prevouts(self): def test_bip143_prevouts(self):
coin = coins.by_name(self.tx.coin_name) coin = coins.by_name(self.tx.coin_name)
bip143 = Hash143() bip143 = Bip143Hash()
bip143.add_input(self.inp1) bip143.add_input(self.inp1)
prevouts_hash = get_tx_hash(bip143.h_prevouts, double=coin.sign_hash_double) prevouts_hash = get_tx_hash(bip143.h_prevouts, double=coin.sign_hash_double)
self.assertEqual(hexlify(prevouts_hash), b'b0287b4a252ac05af83d2dcef00ba313af78a3e9c329afa216eb3aa2a7b4613a') self.assertEqual(hexlify(prevouts_hash), b'b0287b4a252ac05af83d2dcef00ba313af78a3e9c329afa216eb3aa2a7b4613a')
def test_bip143_sequence(self): def test_bip143_sequence(self):
coin = coins.by_name(self.tx.coin_name) coin = coins.by_name(self.tx.coin_name)
bip143 = Hash143() bip143 = Bip143Hash()
bip143.add_input(self.inp1) bip143.add_input(self.inp1)
sequence_hash = get_tx_hash(bip143.h_sequence, double=coin.sign_hash_double) sequence_hash = get_tx_hash(bip143.h_sequence, double=coin.sign_hash_double)
self.assertEqual(hexlify(sequence_hash), b'18606b350cd8bf565266bc352f0caddcf01e8fa789dd8a15386327cf8cabe198') self.assertEqual(hexlify(sequence_hash), b'18606b350cd8bf565266bc352f0caddcf01e8fa789dd8a15386327cf8cabe198')
@ -56,7 +56,7 @@ class TestSegwitBip143(unittest.TestCase):
def test_bip143_outputs(self): def test_bip143_outputs(self):
seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '') seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '')
coin = coins.by_name(self.tx.coin_name) coin = coins.by_name(self.tx.coin_name)
bip143 = Hash143() bip143 = Bip143Hash()
for txo in [self.out1, self.out2]: for txo in [self.out1, self.out2]:
script_pubkey = output_derive_script(txo.address, coin) script_pubkey = output_derive_script(txo.address, coin)
@ -69,7 +69,7 @@ class TestSegwitBip143(unittest.TestCase):
def test_bip143_preimage_testdata(self): def test_bip143_preimage_testdata(self):
seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '') seed = bip39.seed('alcohol woman abuse must during monitor noble actual mixed trade anger aisle', '')
coin = coins.by_name(self.tx.coin_name) coin = coins.by_name(self.tx.coin_name)
bip143 = Hash143() bip143 = Bip143Hash()
bip143.add_input(self.inp1) bip143.add_input(self.inp1)
for txo in [self.out1, self.out2]: for txo in [self.out1, self.out2]:
script_pubkey = output_derive_script(txo.address, coin) script_pubkey = output_derive_script(txo.address, coin)

Loading…
Cancel
Save