1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-17 21:22:10 +00:00

refactor(legacy): Extract node derivation from fill_input_script_pubkey().

This commit is contained in:
Andrew Kozlik 2023-01-03 20:09:48 +01:00 committed by matejcik
parent 2d18cad676
commit 34dcd53135
3 changed files with 37 additions and 49 deletions

View File

@ -907,6 +907,19 @@ static bool fill_input_script_sig(TxInputType *tinput) {
return true;
}
static bool fill_input_script_pubkey(TxInputType *in) {
if (!get_script_pubkey(coin, &node, in->has_multisig, &in->multisig,
in->script_type, in->script_pubkey.bytes,
&in->script_pubkey.size)) {
fsm_sendFailure(FailureType_Failure_ProcessError,
_("Failed to derive scriptPubKey"));
signing_abort();
return false;
}
in->has_script_pubkey = true;
return true;
}
static bool derive_node(TxInputType *tinput) {
if (!coin_path_check(coin, tinput->script_type, tinput->address_n_count,
tinput->address_n, tinput->has_multisig, false) &&
@ -1543,11 +1556,11 @@ static bool signing_add_input(TxInputType *txinput) {
}
}
if (!fill_input_script_pubkey(coin, &root, txinput)) {
fsm_sendFailure(FailureType_Failure_ProcessError,
_("Failed to derive scriptPubKey"));
signing_abort();
return false;
if (txinput->script_type != InputScriptType_EXTERNAL) {
// External inputs should have scriptPubKey set by the host.
if (!derive_node(txinput) || !fill_input_script_pubkey(txinput)) {
return false;
}
}
// Add input to BIP-143/BIP-341 computation.
@ -1767,11 +1780,11 @@ static bool signing_add_orig_input(TxInputType *orig_input) {
return false;
}
if (!fill_input_script_pubkey(coin, &root, orig_input)) {
fsm_sendFailure(FailureType_Failure_ProcessError,
_("Failed to derive scriptPubKey"));
signing_abort();
return false;
if (orig_input->script_type != InputScriptType_EXTERNAL) {
// External inputs should have scriptPubKey set by the host.
if (!derive_node(orig_input) || !fill_input_script_pubkey(orig_input)) {
return false;
}
}
// Verify that the original input matches the current input.
@ -3050,11 +3063,11 @@ void signing_txack(TransactionType *tx) {
memcpy(&input, tx->inputs, sizeof(TxInputType));
if (!fill_input_script_pubkey(coin, &root, &input)) {
fsm_sendFailure(FailureType_Failure_ProcessError,
_("Failed to derive scriptPubKey"));
signing_abort();
return;
if (input.script_type != InputScriptType_EXTERNAL) {
// External inputs should have scriptPubKey set by the host.
if (!derive_node(&input) || !fill_input_script_pubkey(&input)) {
return;
}
}
send_req_3_prev_meta();
@ -3449,10 +3462,7 @@ void signing_txack(TransactionType *tx) {
if (info.version == 4) {
signing_hash_zip243(&info, &tx->inputs[0], hash);
} else if (info.version == 5) {
if (!fill_input_script_pubkey(coin, &root, &tx->inputs[0])) {
fsm_sendFailure(FailureType_Failure_ProcessError,
_("Failed to derive scriptPubKey"));
signing_abort();
if (!fill_input_script_pubkey(&tx->inputs[0])) {
return;
}
signing_hash_zip244(&info, &tx->inputs[0], hash);

View File

@ -415,10 +415,10 @@ int compile_output(const CoinInfo *coin, AmountUnit amount_unit,
return out->script_pubkey.size;
}
int get_script_pubkey(const CoinInfo *coin, HDNode *node, bool has_multisig,
const MultisigRedeemScriptType *multisig,
InputScriptType script_type, uint8_t *script_pubkey,
pb_size_t *script_pubkey_size) {
bool get_script_pubkey(const CoinInfo *coin, HDNode *node, bool has_multisig,
const MultisigRedeemScriptType *multisig,
InputScriptType script_type, uint8_t *script_pubkey,
pb_size_t *script_pubkey_size) {
char address[MAX_ADDR_SIZE] = {0};
bool res = true;
res = res && (hdnode_fill_public_key(node) == 0);
@ -429,26 +429,6 @@ int get_script_pubkey(const CoinInfo *coin, HDNode *node, bool has_multisig,
return res;
}
int fill_input_script_pubkey(const CoinInfo *coin, const HDNode *root,
TxInputType *in) {
if (in->script_type == InputScriptType_EXTERNAL) {
// External inputs should have scriptPubKey set by the host.
return in->has_script_pubkey;
}
static CONFIDENTIAL HDNode node;
memcpy(&node, root, sizeof(HDNode));
int res = true;
res = res && hdnode_private_ckd_cached(&node, in->address_n,
in->address_n_count, NULL);
res = res && get_script_pubkey(coin, &node, in->has_multisig, &in->multisig,
in->script_type, in->script_pubkey.bytes,
&in->script_pubkey.size);
memzero(&node, sizeof(node));
in->has_script_pubkey = res;
return res;
}
uint32_t compile_script_sig(uint32_t address_type, const uint8_t *pubkeyhash,
uint8_t *out) {
if (coinByAddressType(address_type)) { // valid coin type

View File

@ -104,12 +104,10 @@ bool tx_sign_bip340(const uint8_t *private_key, const uint8_t *hash,
int compile_output(const CoinInfo *coin, AmountUnit amount_unit,
const HDNode *root, TxOutputType *in, TxOutputBinType *out,
bool needs_confirm);
int get_script_pubkey(const CoinInfo *coin, HDNode *node, bool has_multisig,
const MultisigRedeemScriptType *multisig,
InputScriptType script_type, uint8_t *script_pubkey,
pb_size_t *script_pubkey_size);
int fill_input_script_pubkey(const CoinInfo *coin, const HDNode *root,
TxInputType *in);
bool get_script_pubkey(const CoinInfo *coin, HDNode *node, bool has_multisig,
const MultisigRedeemScriptType *multisig,
InputScriptType script_type, uint8_t *script_pubkey,
pb_size_t *script_pubkey_size);
bool tx_input_check_hash(Hasher *hasher, const TxInputType *input);
uint32_t tx_prevout_hash(Hasher *hasher, const TxInputType *input);