mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-05 13:01:12 +00:00
implement change logic for multisig
This commit is contained in:
parent
0898c707d9
commit
03a053c944
@ -285,26 +285,24 @@ int cryptoMultisigPubkeyIndex(const MultisigRedeemScriptType *multisig, const ui
|
|||||||
int cryptoMultisigFingerprint(const MultisigRedeemScriptType *multisig, uint8_t *hash)
|
int cryptoMultisigFingerprint(const MultisigRedeemScriptType *multisig, uint8_t *hash)
|
||||||
{
|
{
|
||||||
const uint32_t n = multisig->pubkeys_count;
|
const uint32_t n = multisig->pubkeys_count;
|
||||||
const int max_pubkeys = pb_arraysize(MultisigRedeemScriptType, pubkeys);
|
if (n > 15) {
|
||||||
uint8_t order[max_pubkeys], swap;
|
return 0;
|
||||||
|
}
|
||||||
|
const HDNodePathType *ptr[n], *swap;
|
||||||
uint32_t i, j;
|
uint32_t i, j;
|
||||||
const HDNodeType *a, *b;
|
|
||||||
// check sanity
|
// check sanity
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
order[i] = i;
|
ptr[i] = &(multisig->pubkeys[i]);
|
||||||
a = &(multisig->pubkeys[i].node);
|
if (!ptr[i]->node.has_public_key || ptr[i]->node.public_key.size != 33) return 0;
|
||||||
if (!a->has_public_key || a->public_key.size != 33) return 0;
|
if (ptr[i]->node.chain_code.size != 32) return 0;
|
||||||
if (a->chain_code.size != 32) return 0;
|
|
||||||
}
|
}
|
||||||
// (bubble) sort according to pubkey
|
// minsort according to pubkey
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n - 1; i++) {
|
||||||
for (j = i; j < n; j++) {
|
for (j = n - 1; j > i; j--) {
|
||||||
a = &(multisig->pubkeys[order[i]].node);
|
if (memcmp(ptr[i]->node.public_key.bytes, ptr[j]->node.public_key.bytes, 33) > 0) {
|
||||||
b = &(multisig->pubkeys[order[j]].node);
|
swap = ptr[i];
|
||||||
if (memcmp(a->public_key.bytes, b->public_key.bytes, 33) > 0) {
|
ptr[i] = ptr[j];
|
||||||
swap = order[i];
|
ptr[j] = swap;
|
||||||
order[i] = order[j];
|
|
||||||
order[j] = swap;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -312,12 +310,11 @@ int cryptoMultisigFingerprint(const MultisigRedeemScriptType *multisig, uint8_t
|
|||||||
SHA256_CTX ctx;
|
SHA256_CTX ctx;
|
||||||
sha256_Init(&ctx);
|
sha256_Init(&ctx);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
a = &(multisig->pubkeys[order[i]].node);
|
sha256_Update(&ctx, (const uint8_t *)&(ptr[i]->node.depth), sizeof(uint32_t));
|
||||||
sha256_Update(&ctx, (const uint8_t *)a->depth, sizeof(uint32_t));
|
sha256_Update(&ctx, (const uint8_t *)&(ptr[i]->node.fingerprint), sizeof(uint32_t));
|
||||||
sha256_Update(&ctx, (const uint8_t *)a->fingerprint, sizeof(uint32_t));
|
sha256_Update(&ctx, (const uint8_t *)&(ptr[i]->node.child_num), sizeof(uint32_t));
|
||||||
sha256_Update(&ctx, (const uint8_t *)a->child_num, sizeof(uint32_t));
|
sha256_Update(&ctx, ptr[i]->node.chain_code.bytes, 32);
|
||||||
sha256_Update(&ctx, a->chain_code.bytes, 32);
|
sha256_Update(&ctx, ptr[i]->node.public_key.bytes, 33);
|
||||||
sha256_Update(&ctx, a->public_key.bytes, 33);
|
|
||||||
}
|
}
|
||||||
sha256_Final(hash, &ctx);
|
sha256_Final(hash, &ctx);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -218,6 +218,8 @@ void signing_txack(TransactionType *tx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int co;
|
int co;
|
||||||
|
static bool multisig_fp_set, multisig_fp_mismatch;
|
||||||
|
static uint8_t multisig_fp[32];
|
||||||
|
|
||||||
memset(&resp, 0, sizeof(TxRequest));
|
memset(&resp, 0, sizeof(TxRequest));
|
||||||
|
|
||||||
@ -225,6 +227,8 @@ void signing_txack(TransactionType *tx)
|
|||||||
case STAGE_REQUEST_1_INPUT:
|
case STAGE_REQUEST_1_INPUT:
|
||||||
layoutProgress("Preparing", 1000 * progress / progress_total, progress); progress++;
|
layoutProgress("Preparing", 1000 * progress / progress_total, progress); progress++;
|
||||||
memcpy(&input, tx->inputs, sizeof(TxInputType));
|
memcpy(&input, tx->inputs, sizeof(TxInputType));
|
||||||
|
multisig_fp_set = false;
|
||||||
|
multisig_fp_mismatch = false;
|
||||||
send_req_2_prev_meta();
|
send_req_2_prev_meta();
|
||||||
return;
|
return;
|
||||||
case STAGE_REQUEST_2_PREV_META:
|
case STAGE_REQUEST_2_PREV_META:
|
||||||
@ -277,6 +281,29 @@ void signing_txack(TransactionType *tx)
|
|||||||
signing_abort();
|
signing_abort();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (idx1i == 0) {
|
||||||
|
if (tx->inputs[0].script_type == InputScriptType_SPENDMULTISIG &&
|
||||||
|
tx->inputs[0].has_multisig && !multisig_fp_mismatch) {
|
||||||
|
if (multisig_fp_set) {
|
||||||
|
uint8_t h[32];
|
||||||
|
if (cryptoMultisigFingerprint(&(tx->inputs[0].multisig), h) == 0) {
|
||||||
|
fsm_sendFailure(FailureType_Failure_Other, "Error computing multisig fingeprint");
|
||||||
|
signing_abort();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (memcmp(multisig_fp, h, 32) != 0) {
|
||||||
|
multisig_fp_mismatch = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (cryptoMultisigFingerprint(&(tx->inputs[0].multisig), multisig_fp) == 0) {
|
||||||
|
fsm_sendFailure(FailureType_Failure_Other, "Error computing multisig fingeprint");
|
||||||
|
signing_abort();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
multisig_fp_set = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (idx3i == idx1i) {
|
if (idx3i == idx1i) {
|
||||||
memcpy(&node, root, sizeof(HDNode));
|
memcpy(&node, root, sizeof(HDNode));
|
||||||
uint32_t k;
|
uint32_t k;
|
||||||
@ -324,10 +351,21 @@ void signing_txack(TransactionType *tx)
|
|||||||
layoutProgress("Signing", 1000 * progress / progress_total, progress); progress++;
|
layoutProgress("Signing", 1000 * progress / progress_total, progress); progress++;
|
||||||
bool is_change = false;
|
bool is_change = false;
|
||||||
if (idx1i == 0) {
|
if (idx1i == 0) {
|
||||||
if (tx->outputs[0].has_multisig) {
|
if (tx->outputs[0].script_type == OutputScriptType_PAYTOMULTISIG &&
|
||||||
is_change = false; // TODO: detect when not needed
|
tx->outputs[0].has_multisig &&
|
||||||
|
multisig_fp_set && !multisig_fp_mismatch) {
|
||||||
|
uint8_t h[32];
|
||||||
|
if (cryptoMultisigFingerprint(&(tx->outputs[0].multisig), h) == 0) {
|
||||||
|
fsm_sendFailure(FailureType_Failure_Other, "Error computing multisig fingeprint");
|
||||||
|
signing_abort();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (memcmp(multisig_fp, h, 32) == 0) {
|
||||||
|
is_change = true;
|
||||||
|
}
|
||||||
} else
|
} else
|
||||||
if (tx->outputs[0].address_n_count > 0) { // address_n set -> change address
|
if (tx->outputs[0].script_type == OutputScriptType_PAYTOADDRESS &&
|
||||||
|
tx->outputs[0].address_n_count > 0) {
|
||||||
is_change = true;
|
is_change = true;
|
||||||
}
|
}
|
||||||
if (is_change) {
|
if (is_change) {
|
||||||
|
Loading…
Reference in New Issue
Block a user