1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-30 03:18:20 +00:00

simplify bip32 change logic

This commit is contained in:
Pavol Rusnak 2017-05-05 14:37:40 +02:00
parent b1995bb8d1
commit e31e55e505
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -319,8 +319,11 @@ void phase2_request_next_input(void)
} }
} }
void set_input_bip32_path(const TxInputType *tinput) void extract_input_bip32_path(const TxInputType *tinput)
{ {
if (in_address_n_count == (size_t) -1) {
return;
}
size_t count = tinput->address_n_count; size_t count = tinput->address_n_count;
if (count < 1) { if (count < 1) {
// no change address allowed // no change address allowed
@ -333,40 +336,46 @@ void set_input_bip32_path(const TxInputType *tinput)
if (count > 2) { // if longer than 2 elements, store first N - 2 if (count > 2) { // if longer than 2 elements, store first N - 2
memcpy(in_address_n, tinput->address_n, (count - 2) * sizeof(uint32_t)); memcpy(in_address_n, tinput->address_n, (count - 2) * sizeof(uint32_t));
} }
} else { return;
// check whether they are same length }
if (in_address_n_count != count) { // check whether they are same length
in_address_n_count = (size_t) -1; if (in_address_n_count != count) {
return; in_address_n_count = (size_t) -1;
} return;
if (count > 2 && memcmp(in_address_n, tinput->address_n, (count - 2) * sizeof(uint32_t)) != 0) { }
// mismatch -> no change address allowed if (count > 2 && memcmp(in_address_n, tinput->address_n, (count - 2) * sizeof(uint32_t)) != 0) {
in_address_n_count = (size_t) -1; // mismatch -> no change address allowed
return; in_address_n_count = (size_t) -1;
} return;
} }
} }
#define MAX_BIP32_LAST_ELEMENT 1000000
bool check_change_bip32_path(const TxOutputType *toutput) bool check_change_bip32_path(const TxOutputType *toutput)
{ {
size_t count = toutput->address_n_count; size_t count = toutput->address_n_count;
if (count < 1) { if (count < 1 || in_address_n_count < 1) {
return 0; return 0;
} }
if (count == 1) { if (count != in_address_n_count) {
return in_address_n_count == 1 && toutput->address_n[0] < 1000000; return 0;
}
if (toutput->address_n[count - 1] > MAX_BIP32_LAST_ELEMENT) {
return 0;
} }
if (count >= 2) { if (count >= 2) {
return in_address_n_count == count && if (0 != memcmp(in_address_n, toutput->address_n, (count - 2) * sizeof(uint32_t)) ||
0 == memcmp(in_address_n, toutput->address_n, (count - 2) * sizeof(uint32_t)) && toutput->address_n[count - 2] != 1) {
toutput->address_n[count - 2] == 1 && return 0;
toutput->address_n[count - 1] < 1000000; }
} }
return 0; return 1;
} }
bool compile_input_script_sig(TxInputType *tinput) bool compile_input_script_sig(TxInputType *tinput)
@ -463,7 +472,7 @@ static bool signing_check_input(TxInputType *txinput) {
} }
// remember the input bip32 path // remember the input bip32 path
// change addresses must use the same bip32 path as all inputs // change addresses must use the same bip32 path as all inputs
set_input_bip32_path(txinput); extract_input_bip32_path(txinput);
// compute segwit hashPrevouts & hashSequence // compute segwit hashPrevouts & hashSequence
tx_prevout_hash(&hashers[0], txinput); tx_prevout_hash(&hashers[0], txinput);
tx_sequence_hash(&hashers[1], txinput); tx_sequence_hash(&hashers[1], txinput);