2014-04-29 05:26:51 -07:00
|
|
|
/*
|
|
|
|
* This file is part of the TREZOR project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
*
|
|
|
|
* This library is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "signing.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "layout2.h"
|
|
|
|
#include "messages.h"
|
|
|
|
#include "transaction.h"
|
|
|
|
#include "ecdsa.h"
|
|
|
|
#include "protect.h"
|
2014-12-10 09:04:51 -08:00
|
|
|
#include "crypto.h"
|
2014-04-29 05:26:51 -07:00
|
|
|
|
|
|
|
static uint32_t inputs_count;
|
|
|
|
static uint32_t outputs_count;
|
|
|
|
static const CoinType *coin;
|
|
|
|
static const HDNode *root;
|
|
|
|
static HDNode node;
|
|
|
|
static bool signing = false;
|
|
|
|
enum {
|
|
|
|
STAGE_REQUEST_1_INPUT,
|
|
|
|
STAGE_REQUEST_2_PREV_META,
|
|
|
|
STAGE_REQUEST_2_PREV_INPUT,
|
|
|
|
STAGE_REQUEST_2_PREV_OUTPUT,
|
|
|
|
STAGE_REQUEST_3_OUTPUT,
|
2015-02-11 08:38:32 -08:00
|
|
|
STAGE_REQUEST_4_INPUT,
|
|
|
|
STAGE_REQUEST_4_OUTPUT,
|
|
|
|
STAGE_REQUEST_5_OUTPUT
|
2014-04-29 05:26:51 -07:00
|
|
|
} signing_stage;
|
2015-02-11 08:38:32 -08:00
|
|
|
static uint32_t idx1, idx2;
|
2014-04-29 05:26:51 -07:00
|
|
|
static TxRequest resp;
|
|
|
|
static TxInputType input;
|
|
|
|
static TxOutputBinType bin_output;
|
|
|
|
static TxStruct to, tp, ti, tc;
|
|
|
|
static uint8_t hash[32], hash_check[32], privkey[32], pubkey[33], sig[64];
|
|
|
|
static uint64_t to_spend, spending, change_spend;
|
|
|
|
const uint32_t version = 1;
|
|
|
|
const uint32_t lock_time = 0;
|
|
|
|
static uint32_t progress, progress_total;
|
2014-12-21 09:58:56 -08:00
|
|
|
static bool multisig_fp_set, multisig_fp_mismatch;
|
|
|
|
static uint8_t multisig_fp[32];
|
2014-04-29 05:26:51 -07:00
|
|
|
|
|
|
|
/*
|
2015-02-11 08:38:32 -08:00
|
|
|
|
2014-04-29 05:26:51 -07:00
|
|
|
Workflow of streamed signing
|
2015-02-11 08:38:32 -08:00
|
|
|
The STAGE_ constants describe the signing_stage when request is sent.
|
2014-04-29 05:26:51 -07:00
|
|
|
|
|
|
|
I - input
|
|
|
|
O - output
|
|
|
|
|
2015-02-11 08:38:32 -08:00
|
|
|
Phase1 - check inputs, previous transactions, and outputs
|
|
|
|
- ask for confirmations
|
|
|
|
- check fee
|
|
|
|
=========================================================
|
2014-04-29 05:26:51 -07:00
|
|
|
|
2015-02-11 08:38:32 -08:00
|
|
|
foreach I (idx1):
|
|
|
|
Request I STAGE_REQUEST_1_INPUT
|
|
|
|
Add I to TransactionChecksum
|
2014-04-29 05:26:51 -07:00
|
|
|
Calculate amount of I:
|
|
|
|
Request prevhash I, META STAGE_REQUEST_2_PREV_META
|
2015-02-11 08:38:32 -08:00
|
|
|
foreach prevhash I (idx2):
|
|
|
|
Request prevhash I STAGE_REQUEST_2_PREV_INPUT
|
|
|
|
foreach prevhash O (idx2):
|
|
|
|
Request prevhash O STAGE_REQUEST_2_PREV_OUTPUT
|
|
|
|
Add amount of prevhash O (which is amount of I)
|
2014-04-29 05:26:51 -07:00
|
|
|
Calculate hash of streamed tx, compare to prevhash I
|
2015-02-11 08:38:32 -08:00
|
|
|
foreach O (idx1):
|
|
|
|
Request O STAGE_REQUEST_3_OUTPUT
|
|
|
|
Add O to TransactionChecksum
|
|
|
|
Display output
|
|
|
|
Ask for confirmation
|
|
|
|
|
|
|
|
Check tx fee
|
|
|
|
Ask for confirmation
|
|
|
|
|
|
|
|
Phase2: sign inputs, check that nothing changed
|
|
|
|
===============================================
|
2014-04-29 05:26:51 -07:00
|
|
|
|
2015-02-11 08:38:32 -08:00
|
|
|
foreach I (idx1): // input to sign
|
|
|
|
foreach I (idx2):
|
|
|
|
Request I STAGE_REQUEST_4_INPUT
|
|
|
|
If idx1 == idx2
|
|
|
|
Remember key for signing
|
2014-04-29 05:26:51 -07:00
|
|
|
Fill scriptsig
|
|
|
|
Add I to StreamTransactionSign
|
2015-02-11 08:38:32 -08:00
|
|
|
Add I to TransactionChecksum
|
|
|
|
foreach O (idx2):
|
|
|
|
Request O STAGE_REQUEST_4_OUTPUT
|
2014-04-29 05:26:51 -07:00
|
|
|
Add O to StreamTransactionSign
|
2015-02-11 08:38:32 -08:00
|
|
|
Add O to TransactionChecksum
|
2014-04-29 05:26:51 -07:00
|
|
|
|
2015-02-11 08:38:32 -08:00
|
|
|
Compare TransactionChecksum with checksum computed in Phase 1
|
|
|
|
If different:
|
|
|
|
Failure
|
2014-04-29 05:26:51 -07:00
|
|
|
Sign StreamTransactionSign
|
|
|
|
Return signed chunk
|
2015-02-11 08:38:32 -08:00
|
|
|
foreach O (idx1):
|
|
|
|
Request O STAGE_REQUEST_5_OUTPUT
|
|
|
|
Rewrite change address
|
|
|
|
Return O
|
|
|
|
*/
|
2014-04-29 05:26:51 -07:00
|
|
|
|
|
|
|
void send_req_1_input(void)
|
|
|
|
{
|
2014-12-21 09:58:56 -08:00
|
|
|
layoutProgress("Signing transaction", 1000 * progress / progress_total);
|
2014-04-29 05:26:51 -07:00
|
|
|
signing_stage = STAGE_REQUEST_1_INPUT;
|
|
|
|
resp.has_request_type = true;
|
|
|
|
resp.request_type = RequestType_TXINPUT;
|
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_request_index = true;
|
2015-02-11 08:38:32 -08:00
|
|
|
resp.details.request_index = idx1;
|
2014-04-29 05:26:51 -07:00
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_req_2_prev_meta(void)
|
|
|
|
{
|
2014-12-21 09:58:56 -08:00
|
|
|
layoutProgress("Signing transaction", 1000 * progress / progress_total);
|
2014-04-29 05:26:51 -07:00
|
|
|
signing_stage = STAGE_REQUEST_2_PREV_META;
|
|
|
|
resp.has_request_type = true;
|
|
|
|
resp.request_type = RequestType_TXMETA;
|
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_tx_hash = true;
|
|
|
|
resp.details.tx_hash.size = input.prev_hash.size;
|
|
|
|
memcpy(resp.details.tx_hash.bytes, input.prev_hash.bytes, input.prev_hash.size);
|
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_req_2_prev_input(void)
|
|
|
|
{
|
2014-12-21 09:58:56 -08:00
|
|
|
layoutProgress("Signing transaction", 1000 * progress / progress_total);
|
2014-04-29 05:26:51 -07:00
|
|
|
signing_stage = STAGE_REQUEST_2_PREV_INPUT;
|
|
|
|
resp.has_request_type = true;
|
|
|
|
resp.request_type = RequestType_TXINPUT;
|
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_request_index = true;
|
2015-02-11 08:38:32 -08:00
|
|
|
resp.details.request_index = idx2;
|
2014-04-29 05:26:51 -07:00
|
|
|
resp.details.has_tx_hash = true;
|
|
|
|
resp.details.tx_hash.size = input.prev_hash.size;
|
|
|
|
memcpy(resp.details.tx_hash.bytes, input.prev_hash.bytes, resp.details.tx_hash.size);
|
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_req_2_prev_output(void)
|
|
|
|
{
|
2014-12-21 09:58:56 -08:00
|
|
|
layoutProgress("Signing transaction", 1000 * progress / progress_total);
|
2014-04-29 05:26:51 -07:00
|
|
|
signing_stage = STAGE_REQUEST_2_PREV_OUTPUT;
|
|
|
|
resp.has_request_type = true;
|
|
|
|
resp.request_type = RequestType_TXOUTPUT;
|
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_request_index = true;
|
2015-02-11 08:38:32 -08:00
|
|
|
resp.details.request_index = idx2;
|
2014-04-29 05:26:51 -07:00
|
|
|
resp.details.has_tx_hash = true;
|
|
|
|
resp.details.tx_hash.size = input.prev_hash.size;
|
|
|
|
memcpy(resp.details.tx_hash.bytes, input.prev_hash.bytes, resp.details.tx_hash.size);
|
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
2015-02-11 08:38:32 -08:00
|
|
|
void send_req_3_output(void)
|
2014-04-29 05:26:51 -07:00
|
|
|
{
|
2014-12-21 09:58:56 -08:00
|
|
|
layoutProgress("Signing transaction", 1000 * progress / progress_total);
|
2015-02-11 08:38:32 -08:00
|
|
|
signing_stage = STAGE_REQUEST_3_OUTPUT;
|
2014-04-29 05:26:51 -07:00
|
|
|
resp.has_request_type = true;
|
2015-02-11 08:38:32 -08:00
|
|
|
resp.request_type = RequestType_TXOUTPUT;
|
2014-04-29 05:26:51 -07:00
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_request_index = true;
|
2015-02-11 08:38:32 -08:00
|
|
|
resp.details.request_index = idx1;
|
2014-04-29 05:26:51 -07:00
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
2015-02-11 08:38:32 -08:00
|
|
|
void send_req_4_input(void)
|
2014-04-29 05:26:51 -07:00
|
|
|
{
|
2014-12-21 09:58:56 -08:00
|
|
|
layoutProgress("Signing transaction", 1000 * progress / progress_total);
|
2015-02-11 08:38:32 -08:00
|
|
|
signing_stage = STAGE_REQUEST_4_INPUT;
|
2014-04-29 05:26:51 -07:00
|
|
|
resp.has_request_type = true;
|
2015-02-11 08:38:32 -08:00
|
|
|
resp.request_type = RequestType_TXINPUT;
|
2014-04-29 05:26:51 -07:00
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_request_index = true;
|
2015-02-11 08:38:32 -08:00
|
|
|
resp.details.request_index = idx2;
|
2014-04-29 05:26:51 -07:00
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_req_4_output(void)
|
|
|
|
{
|
2014-12-21 09:58:56 -08:00
|
|
|
layoutProgress("Signing transaction", 1000 * progress / progress_total);
|
2014-04-29 05:26:51 -07:00
|
|
|
signing_stage = STAGE_REQUEST_4_OUTPUT;
|
|
|
|
resp.has_request_type = true;
|
|
|
|
resp.request_type = RequestType_TXOUTPUT;
|
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_request_index = true;
|
2015-02-11 08:38:32 -08:00
|
|
|
resp.details.request_index = idx2;
|
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_req_5_output(void)
|
|
|
|
{
|
|
|
|
layoutProgress("Signing transaction", 1000 * progress / progress_total);
|
|
|
|
signing_stage = STAGE_REQUEST_5_OUTPUT;
|
|
|
|
resp.has_request_type = true;
|
|
|
|
resp.request_type = RequestType_TXOUTPUT;
|
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_request_index = true;
|
|
|
|
resp.details.request_index = idx1;
|
2014-04-29 05:26:51 -07:00
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_req_finished(void)
|
|
|
|
{
|
2014-12-21 09:58:56 -08:00
|
|
|
layoutProgress("Signing transaction", 1000 * progress / progress_total);
|
2014-04-29 05:26:51 -07:00
|
|
|
resp.has_request_type = true;
|
|
|
|
resp.request_type = RequestType_TXFINISHED;
|
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
2015-01-26 04:53:06 -08:00
|
|
|
void signing_init(uint32_t _inputs_count, uint32_t _outputs_count, const CoinType *_coin, const HDNode *_root)
|
2014-04-29 05:26:51 -07:00
|
|
|
{
|
|
|
|
inputs_count = _inputs_count;
|
|
|
|
outputs_count = _outputs_count;
|
|
|
|
coin = _coin;
|
|
|
|
root = _root;
|
|
|
|
|
2015-02-11 08:38:32 -08:00
|
|
|
idx1 = 0;
|
2014-04-29 05:26:51 -07:00
|
|
|
to_spend = 0;
|
|
|
|
spending = 0;
|
|
|
|
change_spend = 0;
|
|
|
|
memset(&input, 0, sizeof(TxInputType));
|
|
|
|
memset(&resp, 0, sizeof(TxRequest));
|
|
|
|
|
|
|
|
signing = true;
|
|
|
|
progress = 1;
|
2014-12-21 09:58:56 -08:00
|
|
|
progress_total = inputs_count * (1 + inputs_count + outputs_count) + outputs_count + 1;
|
|
|
|
|
|
|
|
multisig_fp_set = false;
|
|
|
|
multisig_fp_mismatch = false;
|
2014-04-29 05:26:51 -07:00
|
|
|
|
|
|
|
tx_init(&to, inputs_count, outputs_count, version, lock_time, false);
|
2015-02-11 08:38:32 -08:00
|
|
|
tx_init(&tc, inputs_count, outputs_count, version, lock_time, false);
|
2014-04-29 05:26:51 -07:00
|
|
|
|
2014-12-21 09:58:56 -08:00
|
|
|
layoutProgressSwipe("Signing transaction", 0);
|
|
|
|
|
2014-04-29 05:26:51 -07:00
|
|
|
send_req_1_input();
|
|
|
|
}
|
|
|
|
|
|
|
|
void signing_txack(TransactionType *tx)
|
|
|
|
{
|
|
|
|
if (!signing) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_UnexpectedMessage, "Not in Signing mode");
|
|
|
|
layoutHome();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-21 09:58:56 -08:00
|
|
|
layoutProgress("Signing transaction", 1000 * progress / progress_total);
|
2014-04-29 05:26:51 -07:00
|
|
|
|
2014-12-21 09:58:56 -08:00
|
|
|
int co;
|
2014-04-29 05:26:51 -07:00
|
|
|
memset(&resp, 0, sizeof(TxRequest));
|
|
|
|
|
|
|
|
switch (signing_stage) {
|
|
|
|
case STAGE_REQUEST_1_INPUT:
|
2014-12-21 09:58:56 -08:00
|
|
|
progress++;
|
2015-02-11 08:38:32 -08:00
|
|
|
/* compute multisig fingerprint */
|
|
|
|
/* (if all input share the same fingerprint, outputs having the same fingerprint will be considered as change outputs) */
|
|
|
|
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 (!tx_serialize_input_hash(&tc, tx->inputs)) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to serialize input");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2014-04-29 05:26:51 -07:00
|
|
|
memcpy(&input, tx->inputs, sizeof(TxInputType));
|
|
|
|
send_req_2_prev_meta();
|
|
|
|
return;
|
|
|
|
case STAGE_REQUEST_2_PREV_META:
|
|
|
|
tx_init(&tp, tx->inputs_cnt, tx->outputs_cnt, tx->version, tx->lock_time, false);
|
2015-02-11 08:38:32 -08:00
|
|
|
idx2 = 0;
|
2014-04-29 05:26:51 -07:00
|
|
|
send_req_2_prev_input();
|
|
|
|
return;
|
|
|
|
case STAGE_REQUEST_2_PREV_INPUT:
|
2014-12-21 10:39:20 -08:00
|
|
|
if (!tx_serialize_input_hash(&tp, tx->inputs)) {
|
2014-04-29 05:26:51 -07:00
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to serialize input");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2015-02-11 08:38:32 -08:00
|
|
|
if (idx2 < tp.inputs_len - 1) {
|
|
|
|
idx2++;
|
2014-04-29 05:26:51 -07:00
|
|
|
send_req_2_prev_input();
|
|
|
|
} else {
|
2015-02-11 08:38:32 -08:00
|
|
|
idx2 = 0;
|
2014-04-29 05:26:51 -07:00
|
|
|
send_req_2_prev_output();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case STAGE_REQUEST_2_PREV_OUTPUT:
|
2014-12-21 10:39:20 -08:00
|
|
|
if (!tx_serialize_output_hash(&tp, tx->bin_outputs)) {
|
2014-04-29 05:26:51 -07:00
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to serialize output");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2015-02-11 08:38:32 -08:00
|
|
|
if (idx2 == input.prev_index) {
|
2014-04-29 05:26:51 -07:00
|
|
|
to_spend += tx->bin_outputs[0].amount;
|
|
|
|
}
|
2015-02-11 08:38:32 -08:00
|
|
|
if (idx2 < tp.outputs_len - 1) {
|
|
|
|
/* Check prevtx of next input */
|
|
|
|
idx2++;
|
2014-04-29 05:26:51 -07:00
|
|
|
send_req_2_prev_output();
|
|
|
|
} else {
|
2015-02-11 08:38:32 -08:00
|
|
|
/* Check next output */
|
2014-04-29 05:26:51 -07:00
|
|
|
tx_hash_final(&tp, hash, true);
|
|
|
|
if (memcmp(hash, input.prev_hash.bytes, 32) != 0) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Encountered invalid prevhash");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2015-02-11 08:38:32 -08:00
|
|
|
if (idx1 < inputs_count - 1) {
|
|
|
|
idx1++;
|
|
|
|
send_req_1_input();
|
|
|
|
} else {
|
|
|
|
idx1 = 0;
|
|
|
|
send_req_3_output();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case STAGE_REQUEST_3_OUTPUT:
|
|
|
|
{
|
|
|
|
/* Downloaded output idx1 the first time.
|
|
|
|
* Add it to transaction check
|
|
|
|
* Ask for permission.
|
|
|
|
*/
|
|
|
|
bool is_change = false;
|
|
|
|
if (tx->outputs[0].script_type == OutputScriptType_PAYTOMULTISIG &&
|
|
|
|
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
|
|
|
|
if (tx->outputs[0].script_type == OutputScriptType_PAYTOADDRESS &&
|
|
|
|
tx->outputs[0].address_n_count > 0) {
|
|
|
|
is_change = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_change) {
|
|
|
|
if (change_spend == 0) { // not set
|
|
|
|
change_spend = tx->outputs[0].amount;
|
|
|
|
} else {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Only one change output allowed");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spending += tx->outputs[0].amount;
|
|
|
|
co = compile_output(coin, root, tx->outputs, &bin_output, !is_change);
|
|
|
|
if (!is_change) {
|
|
|
|
layoutProgress("Signing transaction", 1000 * progress / progress_total);
|
|
|
|
}
|
|
|
|
if (co < 0) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Signing cancelled by user");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
} else if (co == 0) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to compile output");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!tx_serialize_output_hash(&tc, &bin_output)) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to serialize output");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (idx1 < outputs_count - 1) {
|
|
|
|
idx1++;
|
|
|
|
send_req_3_output();
|
|
|
|
} else {
|
|
|
|
tx_hash_final(&tc, hash_check, false);
|
|
|
|
|
|
|
|
// check fees
|
|
|
|
if (spending > to_spend) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_NotEnoughFunds, "Not enough funds");
|
|
|
|
layoutHome();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint64_t fee = to_spend - spending;
|
|
|
|
uint32_t tx_est_size = transactionEstimateSizeKb(inputs_count, outputs_count);
|
|
|
|
if (fee > (uint64_t)tx_est_size * coin->maxfee_kb) {
|
|
|
|
layoutFeeOverThreshold(coin, fee, tx_est_size);
|
|
|
|
if (!protectButton(ButtonRequestType_ButtonRequest_FeeOverThreshold, false)) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_ActionCancelled, "Fee over threshold. Signing cancelled.");
|
|
|
|
layoutHome();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// last confirmation
|
|
|
|
layoutConfirmTx(coin, to_spend - change_spend, fee);
|
|
|
|
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_ActionCancelled, "Signing cancelled by user");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything was checked, now phase 2 begins and the transaction is signed.
|
|
|
|
idx1 = 0;
|
|
|
|
idx2 = 0;
|
|
|
|
send_req_4_input();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case STAGE_REQUEST_4_INPUT:
|
|
|
|
if (idx2 == 0) {
|
2014-04-29 05:26:51 -07:00
|
|
|
tx_init(&ti, inputs_count, outputs_count, version, lock_time, true);
|
2015-02-11 08:38:32 -08:00
|
|
|
tx_init(&tc, inputs_count, outputs_count, version, lock_time, false);
|
2014-04-29 05:26:51 -07:00
|
|
|
memset(privkey, 0, 32);
|
|
|
|
memset(pubkey, 0, 33);
|
|
|
|
}
|
2014-12-21 09:58:56 -08:00
|
|
|
progress++;
|
2014-12-21 10:39:20 -08:00
|
|
|
if (!tx_serialize_input_hash(&tc, tx->inputs)) {
|
2014-04-29 05:26:51 -07:00
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to serialize input");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2015-02-11 08:38:32 -08:00
|
|
|
if (idx2 == idx1) {
|
|
|
|
memcpy(&input, tx->inputs, sizeof(TxInputType));
|
2014-04-29 05:26:51 -07:00
|
|
|
memcpy(&node, root, sizeof(HDNode));
|
2015-01-26 11:24:07 -08:00
|
|
|
if (hdnode_private_ckd_cached(&node, tx->inputs[0].address_n, tx->inputs[0].address_n_count) == 0) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to derive private key");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
2014-04-29 05:26:51 -07:00
|
|
|
}
|
2014-12-07 04:11:29 -08:00
|
|
|
if (tx->inputs[0].script_type == InputScriptType_SPENDMULTISIG) {
|
|
|
|
if (!tx->inputs[0].has_multisig) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Multisig info not provided");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2014-12-06 10:12:55 -08:00
|
|
|
tx->inputs[0].script_sig.size = compile_script_multisig(&(tx->inputs[0].multisig), tx->inputs[0].script_sig.bytes);
|
2014-12-07 04:11:29 -08:00
|
|
|
} else { // SPENDADDRESS
|
2014-12-06 10:12:55 -08:00
|
|
|
ecdsa_get_pubkeyhash(node.public_key, hash);
|
|
|
|
tx->inputs[0].script_sig.size = compile_script_sig(coin->address_type, hash, tx->inputs[0].script_sig.bytes);
|
|
|
|
}
|
2014-04-29 05:26:51 -07:00
|
|
|
if (tx->inputs[0].script_sig.size == 0) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to compile input");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(privkey, node.private_key, 32);
|
|
|
|
memcpy(pubkey, node.public_key, 33);
|
|
|
|
} else {
|
|
|
|
tx->inputs[0].script_sig.size = 0;
|
|
|
|
}
|
2014-12-21 10:39:20 -08:00
|
|
|
if (!tx_serialize_input_hash(&ti, tx->inputs)) {
|
2014-04-29 05:26:51 -07:00
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to serialize input");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2015-02-11 08:38:32 -08:00
|
|
|
if (idx2 < inputs_count - 1) {
|
|
|
|
idx2++;
|
|
|
|
send_req_4_input();
|
2014-04-29 05:26:51 -07:00
|
|
|
} else {
|
2015-02-11 08:38:32 -08:00
|
|
|
idx2 = 0;
|
|
|
|
send_req_4_output();
|
2014-04-29 05:26:51 -07:00
|
|
|
}
|
|
|
|
return;
|
2015-02-11 08:38:32 -08:00
|
|
|
case STAGE_REQUEST_4_OUTPUT:
|
2014-12-21 09:58:56 -08:00
|
|
|
progress++;
|
2015-02-11 08:38:32 -08:00
|
|
|
co = compile_output(coin, root, tx->outputs, &bin_output, false);
|
2014-04-29 05:26:51 -07:00
|
|
|
if (co < 0) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Signing cancelled by user");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
} else if (co == 0) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to compile output");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2014-12-21 10:39:20 -08:00
|
|
|
if (!tx_serialize_output_hash(&tc, &bin_output)) {
|
2014-04-29 05:26:51 -07:00
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to serialize output");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2014-12-21 10:39:20 -08:00
|
|
|
if (!tx_serialize_output_hash(&ti, &bin_output)) {
|
2014-04-29 05:26:51 -07:00
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to serialize output");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2015-02-11 08:38:32 -08:00
|
|
|
if (idx2 < outputs_count - 1) {
|
|
|
|
idx2++;
|
|
|
|
send_req_4_output();
|
2014-04-29 05:26:51 -07:00
|
|
|
} else {
|
2015-02-11 08:38:32 -08:00
|
|
|
tx_hash_final(&tc, hash, false);
|
|
|
|
if (memcmp(hash, hash_check, 32) != 0) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Transaction has changed during signing");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
2014-04-29 05:26:51 -07:00
|
|
|
}
|
|
|
|
tx_hash_final(&ti, hash, false);
|
|
|
|
resp.has_serialized = true;
|
|
|
|
resp.serialized.has_signature_index = true;
|
2015-02-11 08:38:32 -08:00
|
|
|
resp.serialized.signature_index = idx1;
|
2014-04-29 05:26:51 -07:00
|
|
|
resp.serialized.has_signature = true;
|
|
|
|
resp.serialized.has_serialized_tx = true;
|
2014-12-08 12:21:44 -08:00
|
|
|
ecdsa_sign_digest(privkey, hash, sig, 0);
|
2014-04-29 05:26:51 -07:00
|
|
|
resp.serialized.signature.size = ecdsa_sig_to_der(sig, resp.serialized.signature.bytes);
|
2014-12-07 04:11:29 -08:00
|
|
|
if (input.script_type == InputScriptType_SPENDMULTISIG) {
|
|
|
|
if (!input.has_multisig) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Multisig info not provided");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2014-12-06 10:12:55 -08:00
|
|
|
// fill in the signature
|
2014-12-13 11:33:49 -08:00
|
|
|
int pubkey_idx = cryptoMultisigPubkeyIndex(&(input.multisig), pubkey);
|
2014-12-10 09:04:51 -08:00
|
|
|
if (pubkey_idx < 0) {
|
2014-12-06 10:12:55 -08:00
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Pubkey not found in multisig script");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(input.multisig.signatures[pubkey_idx].bytes, resp.serialized.signature.bytes, resp.serialized.signature.size);
|
|
|
|
input.multisig.signatures[pubkey_idx].size = resp.serialized.signature.size;
|
|
|
|
input.script_sig.size = serialize_script_multisig(&(input.multisig), input.script_sig.bytes);
|
2014-12-19 17:36:28 -08:00
|
|
|
if (input.script_sig.size == 0) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to serialize multisig script");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2014-12-07 04:11:29 -08:00
|
|
|
} else { // SPENDADDRESS
|
2014-12-06 10:12:55 -08:00
|
|
|
input.script_sig.size = serialize_script_sig(resp.serialized.signature.bytes, resp.serialized.signature.size, pubkey, 33, input.script_sig.bytes);
|
|
|
|
}
|
2014-12-21 10:39:20 -08:00
|
|
|
resp.serialized.serialized_tx.size = tx_serialize_input(&to, &input, resp.serialized.serialized_tx.bytes);
|
2015-02-11 08:38:32 -08:00
|
|
|
if (idx1 < inputs_count - 1) {
|
|
|
|
idx1++;
|
|
|
|
idx2 = 0;
|
|
|
|
send_req_4_input();
|
2014-04-29 05:26:51 -07:00
|
|
|
} else {
|
2015-02-11 08:38:32 -08:00
|
|
|
idx1 = 0;
|
|
|
|
send_req_5_output();
|
2014-04-29 05:26:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
2015-02-11 08:38:32 -08:00
|
|
|
case STAGE_REQUEST_5_OUTPUT:
|
2014-12-21 09:58:56 -08:00
|
|
|
progress++;
|
2015-02-11 08:38:32 -08:00
|
|
|
if (compile_output(coin, root, tx->outputs, &bin_output,false) <= 0) {
|
2014-04-29 05:26:51 -07:00
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Failed to compile output");
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resp.has_serialized = true;
|
|
|
|
resp.serialized.has_serialized_tx = true;
|
2014-12-21 10:39:20 -08:00
|
|
|
resp.serialized.serialized_tx.size = tx_serialize_output(&to, &bin_output, resp.serialized.serialized_tx.bytes);
|
2015-02-11 08:38:32 -08:00
|
|
|
if (idx1 < outputs_count - 1) {
|
|
|
|
idx1++;
|
|
|
|
send_req_5_output();
|
2014-04-29 05:26:51 -07:00
|
|
|
} else {
|
|
|
|
send_req_finished();
|
|
|
|
signing_abort();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fsm_sendFailure(FailureType_Failure_Other, "Signing error");
|
|
|
|
signing_abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void signing_abort(void)
|
|
|
|
{
|
|
|
|
if (signing) {
|
|
|
|
layoutHome();
|
|
|
|
signing = false;
|
|
|
|
}
|
|
|
|
}
|