mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-02-22 12:32:02 +00:00
core/sign_tx: Move Overwintered class to zcash.
This commit is contained in:
parent
60dbec95ac
commit
e9900df18d
@ -17,7 +17,7 @@ from apps.wallet.sign_tx import (
|
||||
)
|
||||
|
||||
if not utils.BITCOIN_ONLY:
|
||||
from apps.wallet.sign_tx import decred, bitcoinlike
|
||||
from apps.wallet.sign_tx import bitcoinlike, decred, zcash
|
||||
|
||||
if False:
|
||||
from typing import Union
|
||||
@ -29,7 +29,7 @@ async def sign_tx(ctx: wire.Context, msg: SignTx, keychain: seed.Keychain) -> Tx
|
||||
if not utils.BITCOIN_ONLY and coin.decred:
|
||||
coinsig = decred.Decred() # type: signing.Bitcoin
|
||||
elif not utils.BITCOIN_ONLY and coin.overwintered:
|
||||
coinsig = bitcoinlike.Overwintered()
|
||||
coinsig = zcash.Overwintered()
|
||||
elif not utils.BITCOIN_ONLY and coin_name not in ("Bitcoin", "Regtest", "Testnet"):
|
||||
coinsig = bitcoinlike.Bitcoinlike()
|
||||
else:
|
||||
|
@ -9,7 +9,7 @@ from trezor.messages.TxInputType import TxInputType
|
||||
from trezor.messages.TxOutputType import TxOutputType
|
||||
from trezor.messages.TxRequestSerializedType import TxRequestSerializedType
|
||||
|
||||
from apps.wallet.sign_tx import addresses, helpers, multisig, signing, writers, zcash
|
||||
from apps.wallet.sign_tx import addresses, helpers, multisig, signing, writers
|
||||
|
||||
if False:
|
||||
from typing import Union
|
||||
@ -141,52 +141,3 @@ class Bitcoinlike(signing.Bitcoin):
|
||||
)
|
||||
writers.write_bytes_unchecked(w, data)
|
||||
ofs += len(data)
|
||||
|
||||
|
||||
class Overwintered(Bitcoinlike):
|
||||
def init_hash143(self) -> None:
|
||||
if self.coin.overwintered:
|
||||
if self.tx.version == 3:
|
||||
branch_id = self.tx.branch_id or 0x5BA81B19 # Overwinter
|
||||
self.hash143 = zcash.Zip143(branch_id) # ZIP-0143 transaction hashing
|
||||
elif self.tx.version == 4:
|
||||
branch_id = self.tx.branch_id or 0x76B809BB # Sapling
|
||||
self.hash143 = zcash.Zip243(branch_id) # ZIP-0243 transaction hashing
|
||||
else:
|
||||
raise signing.SigningError(
|
||||
FailureType.DataError,
|
||||
"Unsupported version for overwintered transaction",
|
||||
)
|
||||
else:
|
||||
super().init_hash143()
|
||||
|
||||
async def phase1_process_nonsegwit_input(self, i: int, txi: TxInputType) -> None:
|
||||
await self.phase1_process_bip143_input(i, txi)
|
||||
|
||||
async def phase2_sign_nonsegwit_input(self, i_sign: int) -> None:
|
||||
await self.phase2_sign_bip143_input(i_sign)
|
||||
|
||||
def write_tx_header(
|
||||
self, w: writers.Writer, tx: Union[SignTx, TransactionType], has_segwit: bool
|
||||
) -> None:
|
||||
# nVersion | fOverwintered
|
||||
writers.write_uint32(w, tx.version | zcash.OVERWINTERED)
|
||||
writers.write_uint32(w, tx.version_group_id) # nVersionGroupId
|
||||
|
||||
def write_sign_tx_footer(self, w: writers.Writer) -> None:
|
||||
super().write_sign_tx_footer(w)
|
||||
|
||||
if self.tx.version == 3:
|
||||
writers.write_uint32(w, self.tx.expiry) # expiryHeight
|
||||
writers.write_varint(w, 0) # nJoinSplit
|
||||
elif self.tx.version == 4:
|
||||
writers.write_uint32(w, self.tx.expiry) # expiryHeight
|
||||
writers.write_uint64(w, 0) # valueBalance
|
||||
writers.write_varint(w, 0) # nShieldedSpend
|
||||
writers.write_varint(w, 0) # nShieldedOutput
|
||||
writers.write_varint(w, 0) # nJoinSplit
|
||||
else:
|
||||
raise signing.SigningError(
|
||||
FailureType.DataError,
|
||||
"Unsupported version for overwintered transaction",
|
||||
)
|
||||
|
@ -4,13 +4,17 @@ from micropython import const
|
||||
from trezor.crypto.hashlib import blake2b
|
||||
from trezor.messages import FailureType, InputScriptType
|
||||
from trezor.messages.SignTx import SignTx
|
||||
from trezor.messages.TransactionType import TransactionType
|
||||
from trezor.messages.TxInputType import TxInputType
|
||||
from trezor.messages.TxOutputBinType import TxOutputBinType
|
||||
from trezor.utils import HashWriter, ensure
|
||||
|
||||
from apps.common.coininfo import CoinInfo
|
||||
from apps.common.seed import Keychain
|
||||
from apps.wallet.sign_tx.bitcoinlike import Bitcoinlike
|
||||
from apps.wallet.sign_tx.multisig import multisig_get_pubkeys
|
||||
from apps.wallet.sign_tx.scripts import output_script_multisig, output_script_p2pkh
|
||||
from apps.wallet.sign_tx.signing import SigningError
|
||||
from apps.wallet.sign_tx.writers import (
|
||||
TX_HASH_SIZE,
|
||||
get_tx_hash,
|
||||
@ -20,8 +24,13 @@ from apps.wallet.sign_tx.writers import (
|
||||
write_tx_output,
|
||||
write_uint32,
|
||||
write_uint64,
|
||||
write_varint,
|
||||
)
|
||||
|
||||
if False:
|
||||
from typing import Union
|
||||
from apps.wallet.sign_tx.writers import Writer
|
||||
|
||||
OVERWINTERED = const(0x80000000)
|
||||
|
||||
|
||||
@ -171,3 +180,53 @@ class Zip243(Zip143):
|
||||
write_uint32(h_preimage, txi.sequence) # 13d. nSequence
|
||||
|
||||
return get_tx_hash(h_preimage)
|
||||
|
||||
|
||||
class Overwintered(Bitcoinlike):
|
||||
def initialize(self, tx: SignTx, keychain: Keychain, coin: CoinInfo) -> None:
|
||||
ensure(coin.overwintered)
|
||||
super().initialize(tx, keychain, coin)
|
||||
|
||||
def init_hash143(self) -> None:
|
||||
if self.tx.version == 3:
|
||||
branch_id = self.tx.branch_id or 0x5BA81B19 # Overwinter
|
||||
self.hash143 = Zip143(branch_id) # ZIP-0143 transaction hashing
|
||||
elif self.tx.version == 4:
|
||||
branch_id = self.tx.branch_id or 0x76B809BB # Sapling
|
||||
self.hash143 = Zip243(branch_id) # ZIP-0243 transaction hashing
|
||||
else:
|
||||
raise SigningError(
|
||||
FailureType.DataError,
|
||||
"Unsupported version for overwintered transaction",
|
||||
)
|
||||
|
||||
async def phase1_process_nonsegwit_input(self, i: int, txi: TxInputType) -> None:
|
||||
await self.phase1_process_bip143_input(i, txi)
|
||||
|
||||
async def phase2_sign_nonsegwit_input(self, i_sign: int) -> None:
|
||||
await self.phase2_sign_bip143_input(i_sign)
|
||||
|
||||
def write_tx_header(
|
||||
self, w: Writer, tx: Union[SignTx, TransactionType], has_segwit: bool
|
||||
) -> None:
|
||||
# nVersion | fOverwintered
|
||||
write_uint32(w, tx.version | OVERWINTERED)
|
||||
write_uint32(w, tx.version_group_id) # nVersionGroupId
|
||||
|
||||
def write_sign_tx_footer(self, w: Writer) -> None:
|
||||
super().write_sign_tx_footer(w)
|
||||
|
||||
if self.tx.version == 3:
|
||||
write_uint32(w, self.tx.expiry) # expiryHeight
|
||||
write_varint(w, 0) # nJoinSplit
|
||||
elif self.tx.version == 4:
|
||||
write_uint32(w, self.tx.expiry) # expiryHeight
|
||||
write_uint64(w, 0) # valueBalance
|
||||
write_varint(w, 0) # nShieldedSpend
|
||||
write_varint(w, 0) # nShieldedOutput
|
||||
write_varint(w, 0) # nJoinSplit
|
||||
else:
|
||||
raise SigningError(
|
||||
FailureType.DataError,
|
||||
"Unsupported version for overwintered transaction",
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user