mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-03 03:50:58 +00:00
core/sign_tx: Remove get_prevouts_hash(), get_sequence_hash(), get_outputs_hash() methods from signer classes, because they are only used internally.
This commit is contained in:
parent
22933587be
commit
8b89a30955
@ -114,11 +114,6 @@ class Bitcoin:
|
|||||||
# BIP-0143 transaction hashing
|
# BIP-0143 transaction hashing
|
||||||
self.init_hash143()
|
self.init_hash143()
|
||||||
|
|
||||||
def init_hash143(self) -> None:
|
|
||||||
self.h_prevouts = HashWriter(sha256())
|
|
||||||
self.h_sequence = HashWriter(sha256())
|
|
||||||
self.h_outputs = HashWriter(sha256())
|
|
||||||
|
|
||||||
def create_hash_writer(self) -> HashWriter:
|
def create_hash_writer(self) -> HashWriter:
|
||||||
return HashWriter(sha256())
|
return HashWriter(sha256())
|
||||||
|
|
||||||
@ -512,6 +507,11 @@ class Bitcoin:
|
|||||||
# BIP-0143
|
# BIP-0143
|
||||||
# ===
|
# ===
|
||||||
|
|
||||||
|
def init_hash143(self) -> None:
|
||||||
|
self.h_prevouts = HashWriter(sha256())
|
||||||
|
self.h_sequence = HashWriter(sha256())
|
||||||
|
self.h_outputs = HashWriter(sha256())
|
||||||
|
|
||||||
def hash143_add_input(self, txi: TxInputType) -> None:
|
def hash143_add_input(self, txi: TxInputType) -> None:
|
||||||
writers.write_bytes_reversed(
|
writers.write_bytes_reversed(
|
||||||
self.h_prevouts, txi.prev_hash, writers.TX_HASH_SIZE
|
self.h_prevouts, txi.prev_hash, writers.TX_HASH_SIZE
|
||||||
@ -522,15 +522,6 @@ class Bitcoin:
|
|||||||
def hash143_add_output(self, txo_bin: TxOutputBinType) -> None:
|
def hash143_add_output(self, txo_bin: TxOutputBinType) -> None:
|
||||||
writers.write_tx_output(self.h_outputs, txo_bin)
|
writers.write_tx_output(self.h_outputs, txo_bin)
|
||||||
|
|
||||||
def get_prevouts_hash(self) -> bytes:
|
|
||||||
return writers.get_tx_hash(self.h_prevouts, double=self.coin.sign_hash_double)
|
|
||||||
|
|
||||||
def get_sequence_hash(self) -> bytes:
|
|
||||||
return writers.get_tx_hash(self.h_sequence, double=self.coin.sign_hash_double)
|
|
||||||
|
|
||||||
def get_outputs_hash(self) -> bytes:
|
|
||||||
return writers.get_tx_hash(self.h_outputs, double=self.coin.sign_hash_double)
|
|
||||||
|
|
||||||
def hash143_preimage_hash(self, txi: TxInputType, pubkeyhash: bytes) -> bytes:
|
def hash143_preimage_hash(self, txi: TxInputType, pubkeyhash: bytes) -> bytes:
|
||||||
h_preimage = HashWriter(sha256())
|
h_preimage = HashWriter(sha256())
|
||||||
|
|
||||||
@ -538,14 +529,16 @@ class Bitcoin:
|
|||||||
writers.write_uint32(h_preimage, self.tx.version)
|
writers.write_uint32(h_preimage, self.tx.version)
|
||||||
|
|
||||||
# hashPrevouts
|
# hashPrevouts
|
||||||
writers.write_bytes_fixed(
|
prevouts_hash = writers.get_tx_hash(
|
||||||
h_preimage, self.get_prevouts_hash(), writers.TX_HASH_SIZE
|
self.h_prevouts, double=self.coin.sign_hash_double
|
||||||
)
|
)
|
||||||
|
writers.write_bytes_fixed(h_preimage, prevouts_hash, writers.TX_HASH_SIZE)
|
||||||
|
|
||||||
# hashSequence
|
# hashSequence
|
||||||
writers.write_bytes_fixed(
|
sequence_hash = writers.get_tx_hash(
|
||||||
h_preimage, self.get_sequence_hash(), writers.TX_HASH_SIZE
|
self.h_sequence, double=self.coin.sign_hash_double
|
||||||
)
|
)
|
||||||
|
writers.write_bytes_fixed(h_preimage, sequence_hash, writers.TX_HASH_SIZE)
|
||||||
|
|
||||||
# outpoint
|
# outpoint
|
||||||
writers.write_bytes_reversed(h_preimage, txi.prev_hash, writers.TX_HASH_SIZE)
|
writers.write_bytes_reversed(h_preimage, txi.prev_hash, writers.TX_HASH_SIZE)
|
||||||
@ -562,9 +555,10 @@ class Bitcoin:
|
|||||||
writers.write_uint32(h_preimage, txi.sequence)
|
writers.write_uint32(h_preimage, txi.sequence)
|
||||||
|
|
||||||
# hashOutputs
|
# hashOutputs
|
||||||
writers.write_bytes_fixed(
|
outputs_hash = writers.get_tx_hash(
|
||||||
h_preimage, self.get_outputs_hash(), writers.TX_HASH_SIZE
|
self.h_outputs, double=self.coin.sign_hash_double
|
||||||
)
|
)
|
||||||
|
writers.write_bytes_fixed(h_preimage, outputs_hash, writers.TX_HASH_SIZE)
|
||||||
|
|
||||||
# nLockTime
|
# nLockTime
|
||||||
writers.write_uint32(h_preimage, self.tx.lock_time)
|
writers.write_uint32(h_preimage, self.tx.lock_time)
|
||||||
|
@ -50,11 +50,6 @@ class Overwintered(Bitcoinlike):
|
|||||||
"Unsupported version for overwintered transaction",
|
"Unsupported version for overwintered transaction",
|
||||||
)
|
)
|
||||||
|
|
||||||
def init_hash143(self) -> None:
|
|
||||||
self.h_prevouts = HashWriter(blake2b(outlen=32, personal=b"ZcashPrevoutHash"))
|
|
||||||
self.h_sequence = HashWriter(blake2b(outlen=32, personal=b"ZcashSequencHash"))
|
|
||||||
self.h_outputs = HashWriter(blake2b(outlen=32, personal=b"ZcashOutputsHash"))
|
|
||||||
|
|
||||||
async def step7_finish(self) -> None:
|
async def step7_finish(self) -> None:
|
||||||
self.write_tx_footer(self.serialized_tx, self.tx)
|
self.write_tx_footer(self.serialized_tx, self.tx)
|
||||||
|
|
||||||
@ -91,14 +86,10 @@ class Overwintered(Bitcoinlike):
|
|||||||
# ZIP-0143 / ZIP-0243
|
# ZIP-0143 / ZIP-0243
|
||||||
# ===
|
# ===
|
||||||
|
|
||||||
def get_prevouts_hash(self) -> bytes:
|
def init_hash143(self) -> None:
|
||||||
return get_tx_hash(self.h_prevouts)
|
self.h_prevouts = HashWriter(blake2b(outlen=32, personal=b"ZcashPrevoutHash"))
|
||||||
|
self.h_sequence = HashWriter(blake2b(outlen=32, personal=b"ZcashSequencHash"))
|
||||||
def get_sequence_hash(self) -> bytes:
|
self.h_outputs = HashWriter(blake2b(outlen=32, personal=b"ZcashOutputsHash"))
|
||||||
return get_tx_hash(self.h_sequence)
|
|
||||||
|
|
||||||
def get_outputs_hash(self) -> bytes:
|
|
||||||
return get_tx_hash(self.h_outputs)
|
|
||||||
|
|
||||||
def hash143_preimage_hash(self, txi: TxInputType, pubkeyhash: bytes) -> bytes:
|
def hash143_preimage_hash(self, txi: TxInputType, pubkeyhash: bytes) -> bytes:
|
||||||
h_preimage = HashWriter(
|
h_preimage = HashWriter(
|
||||||
@ -113,11 +104,11 @@ class Overwintered(Bitcoinlike):
|
|||||||
# 2. nVersionGroupId
|
# 2. nVersionGroupId
|
||||||
write_uint32(h_preimage, self.tx.version_group_id)
|
write_uint32(h_preimage, self.tx.version_group_id)
|
||||||
# 3. hashPrevouts
|
# 3. hashPrevouts
|
||||||
write_bytes_fixed(h_preimage, self.get_prevouts_hash(), TX_HASH_SIZE)
|
write_bytes_fixed(h_preimage, get_tx_hash(self.h_prevouts), TX_HASH_SIZE)
|
||||||
# 4. hashSequence
|
# 4. hashSequence
|
||||||
write_bytes_fixed(h_preimage, self.get_sequence_hash(), TX_HASH_SIZE)
|
write_bytes_fixed(h_preimage, get_tx_hash(self.h_sequence), TX_HASH_SIZE)
|
||||||
# 5. hashOutputs
|
# 5. hashOutputs
|
||||||
write_bytes_fixed(h_preimage, self.get_outputs_hash(), TX_HASH_SIZE)
|
write_bytes_fixed(h_preimage, get_tx_hash(self.h_outputs), TX_HASH_SIZE)
|
||||||
|
|
||||||
if self.tx.version == 3:
|
if self.tx.version == 3:
|
||||||
# 6. hashJoinSplits
|
# 6. hashJoinSplits
|
||||||
|
@ -2,6 +2,7 @@ from common import *
|
|||||||
|
|
||||||
from apps.wallet.sign_tx.scripts import output_derive_script
|
from apps.wallet.sign_tx.scripts import output_derive_script
|
||||||
from apps.wallet.sign_tx.bitcoin import Bitcoin
|
from apps.wallet.sign_tx.bitcoin import Bitcoin
|
||||||
|
from apps.wallet.sign_tx.writers import get_tx_hash
|
||||||
from apps.common import coins
|
from apps.common import coins
|
||||||
from trezor.messages.SignTx import SignTx
|
from trezor.messages.SignTx import SignTx
|
||||||
from trezor.messages.TxInputType import TxInputType
|
from trezor.messages.TxInputType import TxInputType
|
||||||
@ -48,14 +49,16 @@ class TestSegwitBip143NativeP2WPKH(unittest.TestCase):
|
|||||||
bip143 = Bitcoin(self.tx, None, coin)
|
bip143 = Bitcoin(self.tx, None, coin)
|
||||||
bip143.hash143_add_input(self.inp1)
|
bip143.hash143_add_input(self.inp1)
|
||||||
bip143.hash143_add_input(self.inp2)
|
bip143.hash143_add_input(self.inp2)
|
||||||
self.assertEqual(hexlify(bip143.get_prevouts_hash()), b'96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37')
|
prevouts_hash = get_tx_hash(bip143.h_prevouts, double=coin.sign_hash_double)
|
||||||
|
self.assertEqual(hexlify(prevouts_hash), b'96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37')
|
||||||
|
|
||||||
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 = Bitcoin(self.tx, None, coin)
|
bip143 = Bitcoin(self.tx, None, coin)
|
||||||
bip143.hash143_add_input(self.inp1)
|
bip143.hash143_add_input(self.inp1)
|
||||||
bip143.hash143_add_input(self.inp2)
|
bip143.hash143_add_input(self.inp2)
|
||||||
self.assertEqual(hexlify(bip143.get_sequence_hash()), b'52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b')
|
sequence_hash = get_tx_hash(bip143.h_sequence, double=coin.sign_hash_double)
|
||||||
|
self.assertEqual(hexlify(sequence_hash), b'52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b')
|
||||||
|
|
||||||
def test_outputs(self):
|
def test_outputs(self):
|
||||||
|
|
||||||
@ -69,8 +72,8 @@ class TestSegwitBip143NativeP2WPKH(unittest.TestCase):
|
|||||||
txo_bin.script_pubkey = output_derive_script(txo, coin)
|
txo_bin.script_pubkey = output_derive_script(txo, coin)
|
||||||
bip143.hash143_add_output(txo_bin)
|
bip143.hash143_add_output(txo_bin)
|
||||||
|
|
||||||
self.assertEqual(hexlify(bip143.get_outputs_hash()),
|
outputs_hash = get_tx_hash(bip143.h_outputs, double=coin.sign_hash_double)
|
||||||
b'863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5')
|
self.assertEqual(hexlify(outputs_hash), b'863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5')
|
||||||
|
|
||||||
def test_preimage_testdata(self):
|
def test_preimage_testdata(self):
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ from common import *
|
|||||||
|
|
||||||
from apps.wallet.sign_tx.scripts import output_derive_script
|
from apps.wallet.sign_tx.scripts import output_derive_script
|
||||||
from apps.wallet.sign_tx.bitcoin import Bitcoin
|
from apps.wallet.sign_tx.bitcoin import Bitcoin
|
||||||
|
from apps.wallet.sign_tx.writers import get_tx_hash
|
||||||
from apps.common import coins
|
from apps.common import coins
|
||||||
from trezor.messages.SignTx import SignTx
|
from trezor.messages.SignTx import SignTx
|
||||||
from trezor.messages.TxInputType import TxInputType
|
from trezor.messages.TxInputType import TxInputType
|
||||||
@ -39,13 +40,15 @@ class TestSegwitBip143(unittest.TestCase):
|
|||||||
coin = coins.by_name(self.tx.coin_name)
|
coin = coins.by_name(self.tx.coin_name)
|
||||||
bip143 = Bitcoin(self.tx, None, coin)
|
bip143 = Bitcoin(self.tx, None, coin)
|
||||||
bip143.hash143_add_input(self.inp1)
|
bip143.hash143_add_input(self.inp1)
|
||||||
self.assertEqual(hexlify(bip143.get_prevouts_hash()), b'b0287b4a252ac05af83d2dcef00ba313af78a3e9c329afa216eb3aa2a7b4613a')
|
prevouts_hash = get_tx_hash(bip143.h_prevouts, double=coin.sign_hash_double)
|
||||||
|
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 = Bitcoin(self.tx, None, coin)
|
bip143 = Bitcoin(self.tx, None, coin)
|
||||||
bip143.hash143_add_input(self.inp1)
|
bip143.hash143_add_input(self.inp1)
|
||||||
self.assertEqual(hexlify(bip143.get_sequence_hash()), b'18606b350cd8bf565266bc352f0caddcf01e8fa789dd8a15386327cf8cabe198')
|
sequence_hash = get_tx_hash(bip143.h_sequence, double=coin.sign_hash_double)
|
||||||
|
self.assertEqual(hexlify(sequence_hash), b'18606b350cd8bf565266bc352f0caddcf01e8fa789dd8a15386327cf8cabe198')
|
||||||
|
|
||||||
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', '')
|
||||||
@ -58,8 +61,8 @@ class TestSegwitBip143(unittest.TestCase):
|
|||||||
txo_bin.script_pubkey = output_derive_script(txo, coin)
|
txo_bin.script_pubkey = output_derive_script(txo, coin)
|
||||||
bip143.hash143_add_output(txo_bin)
|
bip143.hash143_add_output(txo_bin)
|
||||||
|
|
||||||
self.assertEqual(hexlify(bip143.get_outputs_hash()),
|
outputs_hash = get_tx_hash(bip143.h_outputs, double=coin.sign_hash_double)
|
||||||
b'de984f44532e2173ca0d64314fcefe6d30da6f8cf27bafa706da61df8a226c83')
|
self.assertEqual(hexlify(outputs_hash), b'de984f44532e2173ca0d64314fcefe6d30da6f8cf27bafa706da61df8a226c83')
|
||||||
|
|
||||||
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', '')
|
||||||
|
@ -5,6 +5,7 @@ from trezor.messages.TxInputType import TxInputType
|
|||||||
from trezor.messages.TxOutputBinType import TxOutputBinType
|
from trezor.messages.TxOutputBinType import TxOutputBinType
|
||||||
|
|
||||||
from apps.common import coins
|
from apps.common import coins
|
||||||
|
from apps.wallet.sign_tx.writers import get_tx_hash
|
||||||
|
|
||||||
if not utils.BITCOIN_ONLY:
|
if not utils.BITCOIN_ONLY:
|
||||||
from apps.wallet.sign_tx.zcash import Overwintered
|
from apps.wallet.sign_tx.zcash import Overwintered
|
||||||
@ -170,9 +171,9 @@ class TestZcashZip143(unittest.TestCase):
|
|||||||
txo.script_pubkey = unhexlify(o["script_pubkey"])
|
txo.script_pubkey = unhexlify(o["script_pubkey"])
|
||||||
zip143.hash143_add_output(txo)
|
zip143.hash143_add_output(txo)
|
||||||
|
|
||||||
self.assertEqual(hexlify(zip143.get_prevouts_hash()), v["prevouts_hash"])
|
self.assertEqual(hexlify(get_tx_hash(zip143.h_prevouts)), v["prevouts_hash"])
|
||||||
self.assertEqual(hexlify(zip143.get_sequence_hash()), v["sequence_hash"])
|
self.assertEqual(hexlify(get_tx_hash(zip143.h_sequence)), v["sequence_hash"])
|
||||||
self.assertEqual(hexlify(zip143.get_outputs_hash()), v["outputs_hash"])
|
self.assertEqual(hexlify(get_tx_hash(zip143.h_outputs)), v["outputs_hash"])
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
hexlify(zip143.hash143_preimage_hash(txi, unhexlify(i["pubkeyhash"]))),
|
hexlify(zip143.hash143_preimage_hash(txi, unhexlify(i["pubkeyhash"]))),
|
||||||
v["preimage_hash"],
|
v["preimage_hash"],
|
||||||
|
@ -5,8 +5,10 @@ from trezor.messages.TxInputType import TxInputType
|
|||||||
from trezor.messages.TxOutputBinType import TxOutputBinType
|
from trezor.messages.TxOutputBinType import TxOutputBinType
|
||||||
|
|
||||||
from apps.common import coins
|
from apps.common import coins
|
||||||
|
from apps.wallet.sign_tx.writers import get_tx_hash
|
||||||
|
|
||||||
from apps.wallet.sign_tx.zcash import Overwintered
|
if not utils.BITCOIN_ONLY:
|
||||||
|
from apps.wallet.sign_tx.zcash import Overwintered
|
||||||
|
|
||||||
|
|
||||||
# test vectors inspired from https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/zip_0243.py
|
# test vectors inspired from https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/zip_0243.py
|
||||||
@ -203,9 +205,9 @@ class TestZcashZip243(unittest.TestCase):
|
|||||||
txo.script_pubkey = unhexlify(o["script_pubkey"])
|
txo.script_pubkey = unhexlify(o["script_pubkey"])
|
||||||
zip243.hash143_add_output(txo)
|
zip243.hash143_add_output(txo)
|
||||||
|
|
||||||
self.assertEqual(hexlify(zip243.get_prevouts_hash()), v["prevouts_hash"])
|
self.assertEqual(hexlify(get_tx_hash(zip243.h_prevouts)), v["prevouts_hash"])
|
||||||
self.assertEqual(hexlify(zip243.get_sequence_hash()), v["sequence_hash"])
|
self.assertEqual(hexlify(get_tx_hash(zip243.h_sequence)), v["sequence_hash"])
|
||||||
self.assertEqual(hexlify(zip243.get_outputs_hash()), v["outputs_hash"])
|
self.assertEqual(hexlify(get_tx_hash(zip243.h_outputs)), v["outputs_hash"])
|
||||||
self.assertEqual(hexlify(zip243.hash143_preimage_hash(txi, unhexlify(i["pubkeyhash"]))), v["preimage_hash"])
|
self.assertEqual(hexlify(zip243.hash143_preimage_hash(txi, unhexlify(i["pubkeyhash"]))), v["preimage_hash"])
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user