2014-04-29 12:26:51 +00:00
|
|
|
/*
|
2017-11-05 16:46:34 +00:00
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
2014-04-29 12:26:51 +00:00
|
|
|
*
|
|
|
|
* 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 17:04:51 +00:00
|
|
|
#include "crypto.h"
|
2015-06-03 19:21:16 +00:00
|
|
|
#include "secp256k1.h"
|
2017-06-18 20:22:33 +00:00
|
|
|
#include "gettext.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
static uint32_t inputs_count;
|
|
|
|
static uint32_t outputs_count;
|
2017-10-30 21:13:09 +00:00
|
|
|
static const CoinInfo *coin;
|
2014-04-29 12:26:51 +00:00
|
|
|
static const HDNode *root;
|
2017-08-08 10:59:39 +00:00
|
|
|
static CONFIDENTIAL HDNode node;
|
2014-04-29 12:26:51 +00:00
|
|
|
static bool signing = false;
|
|
|
|
enum {
|
|
|
|
STAGE_REQUEST_1_INPUT,
|
|
|
|
STAGE_REQUEST_2_PREV_META,
|
|
|
|
STAGE_REQUEST_2_PREV_INPUT,
|
|
|
|
STAGE_REQUEST_2_PREV_OUTPUT,
|
2016-10-19 07:37:46 +00:00
|
|
|
STAGE_REQUEST_2_PREV_EXTRADATA,
|
2014-04-29 12:26:51 +00:00
|
|
|
STAGE_REQUEST_3_OUTPUT,
|
2015-02-11 16:38:32 +00:00
|
|
|
STAGE_REQUEST_4_INPUT,
|
|
|
|
STAGE_REQUEST_4_OUTPUT,
|
2016-04-29 20:15:55 +00:00
|
|
|
STAGE_REQUEST_SEGWIT_INPUT,
|
|
|
|
STAGE_REQUEST_5_OUTPUT,
|
|
|
|
STAGE_REQUEST_SEGWIT_WITNESS
|
2014-04-29 12:26:51 +00:00
|
|
|
} signing_stage;
|
2015-02-11 16:38:32 +00:00
|
|
|
static uint32_t idx1, idx2;
|
2017-04-08 20:36:32 +00:00
|
|
|
static uint32_t signatures;
|
2014-04-29 12:26:51 +00:00
|
|
|
static TxRequest resp;
|
|
|
|
static TxInputType input;
|
|
|
|
static TxOutputBinType bin_output;
|
2015-02-24 14:14:44 +00:00
|
|
|
static TxStruct to, tp, ti;
|
2017-12-09 14:38:37 +00:00
|
|
|
static Hasher hashers[3];
|
2017-08-08 10:59:39 +00:00
|
|
|
static uint8_t CONFIDENTIAL privkey[32];
|
|
|
|
static uint8_t pubkey[33], sig[64];
|
2016-04-29 19:17:06 +00:00
|
|
|
static uint8_t hash_prevouts[32], hash_sequence[32],hash_outputs[32];
|
2016-04-29 20:15:55 +00:00
|
|
|
static uint8_t hash_check[32];
|
2017-07-25 20:35:09 +00:00
|
|
|
static uint64_t to_spend, authorized_amount, spending, change_spend;
|
2016-04-16 02:47:09 +00:00
|
|
|
static uint32_t version = 1;
|
|
|
|
static uint32_t lock_time = 0;
|
2016-04-29 20:15:55 +00:00
|
|
|
static uint32_t next_nonsegwit_input;
|
2015-02-25 15:23:24 +00:00
|
|
|
static uint32_t progress, progress_step, progress_meta_step;
|
2014-12-21 17:58:56 +00:00
|
|
|
static bool multisig_fp_set, multisig_fp_mismatch;
|
|
|
|
static uint8_t multisig_fp[32];
|
2017-03-29 14:43:48 +00:00
|
|
|
static uint32_t in_address_n[8];
|
|
|
|
static size_t in_address_n_count;
|
2017-11-08 00:13:21 +00:00
|
|
|
static uint32_t tx_weight;
|
2017-03-29 14:43:48 +00:00
|
|
|
|
2017-10-30 11:20:47 +00:00
|
|
|
/* A marker for in_address_n_count to indicate a mismatch in bip32 paths in
|
2017-10-10 14:05:42 +00:00
|
|
|
input */
|
|
|
|
#define BIP32_NOCHANGEALLOWED 1
|
2017-10-30 11:20:47 +00:00
|
|
|
/* The number of bip32 levels used in a wallet (chain and address) */
|
|
|
|
#define BIP32_WALLET_DEPTH 2
|
|
|
|
/* The chain id used for change */
|
|
|
|
#define BIP32_CHANGE_CHAIN 1
|
|
|
|
/* The maximum allowed change address. This should be large enough for normal
|
|
|
|
use and still allow to quickly brute-force the correct bip32 path. */
|
|
|
|
#define BIP32_MAX_LAST_ELEMENT 1000000
|
2017-10-10 14:05:42 +00:00
|
|
|
|
2017-11-08 00:13:21 +00:00
|
|
|
/* transaction header size: 4 byte version */
|
|
|
|
#define TXSIZE_HEADER 4
|
|
|
|
/* transaction footer size: 4 byte lock time */
|
|
|
|
#define TXSIZE_FOOTER 4
|
|
|
|
/* transaction segwit overhead 2 marker */
|
|
|
|
#define TXSIZE_SEGWIT_OVERHEAD 2
|
|
|
|
|
2017-07-25 14:24:24 +00:00
|
|
|
enum {
|
|
|
|
SIGHASH_ALL = 1,
|
|
|
|
SIGHASH_FORKID = 0x40,
|
|
|
|
};
|
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2017-04-08 19:46:43 +00:00
|
|
|
/* progress_step/meta_step are fixed point numbers, giving the
|
2015-02-25 15:23:24 +00:00
|
|
|
* progress per input in permille with these many additional bits.
|
|
|
|
*/
|
|
|
|
#define PROGRESS_PRECISION 16
|
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
/*
|
2015-02-11 16:38:32 +00:00
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
Workflow of streamed signing
|
2015-02-11 16:38:32 +00:00
|
|
|
The STAGE_ constants describe the signing_stage when request is sent.
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
I - input
|
|
|
|
O - output
|
|
|
|
|
2015-02-11 16:38:32 +00:00
|
|
|
Phase1 - check inputs, previous transactions, and outputs
|
|
|
|
- ask for confirmations
|
|
|
|
- check fee
|
|
|
|
=========================================================
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2015-02-11 16:38:32 +00:00
|
|
|
foreach I (idx1):
|
|
|
|
Request I STAGE_REQUEST_1_INPUT
|
2017-04-08 19:46:43 +00:00
|
|
|
Add I to segwit hash_prevouts, hash_sequence
|
|
|
|
Add I to TransactionChecksum (prevout and type)
|
|
|
|
If not segwit, Calculate amount of I:
|
2014-04-29 12:26:51 +00:00
|
|
|
Request prevhash I, META STAGE_REQUEST_2_PREV_META
|
2015-02-11 16:38:32 +00: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)
|
2016-10-19 07:37:46 +00:00
|
|
|
Request prevhash extra data (if applicable) STAGE_REQUEST_2_PREV_EXTRADATA
|
2014-04-29 12:26:51 +00:00
|
|
|
Calculate hash of streamed tx, compare to prevhash I
|
2015-02-11 16:38:32 +00: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 12:26:51 +00:00
|
|
|
|
2015-02-11 16:38:32 +00:00
|
|
|
foreach I (idx1): // input to sign
|
2017-04-08 19:46:43 +00:00
|
|
|
if (idx1 is segwit)
|
|
|
|
Request I STAGE_REQUEST_SEGWIT_INPUT
|
2017-05-04 16:54:54 +00:00
|
|
|
Return serialized input chunk
|
2017-04-08 19:46:43 +00:00
|
|
|
|
2017-05-04 16:54:54 +00:00
|
|
|
else
|
2017-04-08 19:46:43 +00:00
|
|
|
foreach I (idx2):
|
|
|
|
Request I STAGE_REQUEST_4_INPUT
|
|
|
|
If idx1 == idx2
|
|
|
|
Remember key for signing
|
|
|
|
Fill scriptsig
|
|
|
|
Add I to StreamTransactionSign
|
|
|
|
Add I to TransactionChecksum
|
|
|
|
foreach O (idx2):
|
|
|
|
Request O STAGE_REQUEST_4_OUTPUT
|
|
|
|
Add O to StreamTransactionSign
|
|
|
|
Add O to TransactionChecksum
|
|
|
|
|
|
|
|
Compare TransactionChecksum with checksum computed in Phase 1
|
|
|
|
If different:
|
|
|
|
Failure
|
|
|
|
Sign StreamTransactionSign
|
|
|
|
Return signed chunk
|
|
|
|
|
2015-02-11 16:38:32 +00:00
|
|
|
foreach O (idx1):
|
|
|
|
Request O STAGE_REQUEST_5_OUTPUT
|
|
|
|
Rewrite change address
|
|
|
|
Return O
|
2017-04-08 19:46:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
Phase3: sign segwit inputs, check that nothing changed
|
|
|
|
===============================================
|
|
|
|
|
|
|
|
foreach I (idx1): // input to sign
|
|
|
|
Request I STAGE_REQUEST_SEGWIT_WITNESS
|
|
|
|
Check amount
|
|
|
|
Sign segwit prevhash, sequence, amount, outputs
|
|
|
|
Return witness
|
2015-02-11 16:38:32 +00:00
|
|
|
*/
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
void send_req_1_input(void)
|
|
|
|
{
|
|
|
|
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 16:38:32 +00:00
|
|
|
resp.details.request_index = idx1;
|
2014-04-29 12:26:51 +00:00
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_req_2_prev_meta(void)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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 16:38:32 +00:00
|
|
|
resp.details.request_index = idx2;
|
2014-04-29 12:26:51 +00: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)
|
|
|
|
{
|
|
|
|
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 16:38:32 +00:00
|
|
|
resp.details.request_index = idx2;
|
2014-04-29 12:26:51 +00: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);
|
|
|
|
}
|
|
|
|
|
2016-10-19 07:37:46 +00:00
|
|
|
void send_req_2_prev_extradata(uint32_t chunk_offset, uint32_t chunk_len)
|
|
|
|
{
|
|
|
|
signing_stage = STAGE_REQUEST_2_PREV_EXTRADATA;
|
|
|
|
resp.has_request_type = true;
|
|
|
|
resp.request_type = RequestType_TXEXTRADATA;
|
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_extra_data_offset = true;
|
|
|
|
resp.details.extra_data_offset = chunk_offset;
|
|
|
|
resp.details.has_extra_data_len = true;
|
|
|
|
resp.details.extra_data_len = chunk_len;
|
|
|
|
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 16:38:32 +00:00
|
|
|
void send_req_3_output(void)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
2015-02-11 16:38:32 +00:00
|
|
|
signing_stage = STAGE_REQUEST_3_OUTPUT;
|
2014-04-29 12:26:51 +00:00
|
|
|
resp.has_request_type = true;
|
2015-02-11 16:38:32 +00:00
|
|
|
resp.request_type = RequestType_TXOUTPUT;
|
2014-04-29 12:26:51 +00:00
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_request_index = true;
|
2015-02-11 16:38:32 +00:00
|
|
|
resp.details.request_index = idx1;
|
2014-04-29 12:26:51 +00:00
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
2015-02-11 16:38:32 +00:00
|
|
|
void send_req_4_input(void)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
2015-02-11 16:38:32 +00:00
|
|
|
signing_stage = STAGE_REQUEST_4_INPUT;
|
2014-04-29 12:26:51 +00:00
|
|
|
resp.has_request_type = true;
|
2015-02-11 16:38:32 +00:00
|
|
|
resp.request_type = RequestType_TXINPUT;
|
2014-04-29 12:26:51 +00:00
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_request_index = true;
|
2015-02-11 16:38:32 +00:00
|
|
|
resp.details.request_index = idx2;
|
2014-04-29 12:26:51 +00:00
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_req_4_output(void)
|
|
|
|
{
|
|
|
|
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 16:38:32 +00:00
|
|
|
resp.details.request_index = idx2;
|
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
2016-04-29 20:15:55 +00:00
|
|
|
void send_req_segwit_input(void)
|
|
|
|
{
|
|
|
|
signing_stage = STAGE_REQUEST_SEGWIT_INPUT;
|
|
|
|
resp.has_request_type = true;
|
|
|
|
resp.request_type = RequestType_TXINPUT;
|
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_request_index = true;
|
|
|
|
resp.details.request_index = idx1;
|
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_req_segwit_witness(void)
|
|
|
|
{
|
|
|
|
signing_stage = STAGE_REQUEST_SEGWIT_WITNESS;
|
|
|
|
resp.has_request_type = true;
|
|
|
|
resp.request_type = RequestType_TXINPUT;
|
|
|
|
resp.has_details = true;
|
|
|
|
resp.details.has_request_index = true;
|
|
|
|
resp.details.request_index = idx1;
|
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
2015-02-11 16:38:32 +00:00
|
|
|
void send_req_5_output(void)
|
|
|
|
{
|
|
|
|
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 12:26:51 +00:00
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_req_finished(void)
|
|
|
|
{
|
|
|
|
resp.has_request_type = true;
|
|
|
|
resp.request_type = RequestType_TXFINISHED;
|
|
|
|
msg_write(MessageType_MessageType_TxRequest, &resp);
|
|
|
|
}
|
|
|
|
|
2016-04-29 20:15:55 +00:00
|
|
|
void phase1_request_next_input(void)
|
|
|
|
{
|
|
|
|
if (idx1 < inputs_count - 1) {
|
|
|
|
idx1++;
|
|
|
|
send_req_1_input();
|
|
|
|
} else {
|
|
|
|
// compute segwit hashPrevouts & hashSequence
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Double(&hashers[0], hash_prevouts);
|
|
|
|
hasher_Double(&hashers[1], hash_sequence);
|
|
|
|
hasher_Final(&hashers[2], hash_check);
|
2016-04-29 20:15:55 +00:00
|
|
|
// init hashOutputs
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Reset(&hashers[0]);
|
2016-04-29 20:15:55 +00:00
|
|
|
idx1 = 0;
|
|
|
|
send_req_3_output();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void phase2_request_next_input(void)
|
|
|
|
{
|
|
|
|
if (idx1 == next_nonsegwit_input) {
|
|
|
|
idx2 = 0;
|
|
|
|
send_req_4_input();
|
|
|
|
} else {
|
|
|
|
send_req_segwit_input();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 12:37:40 +00:00
|
|
|
void extract_input_bip32_path(const TxInputType *tinput)
|
2017-03-29 14:43:48 +00:00
|
|
|
{
|
2017-10-10 14:05:42 +00:00
|
|
|
if (in_address_n_count == BIP32_NOCHANGEALLOWED) {
|
2017-05-05 12:37:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-03-29 14:43:48 +00:00
|
|
|
size_t count = tinput->address_n_count;
|
2017-10-30 11:20:47 +00:00
|
|
|
if (count < BIP32_WALLET_DEPTH) {
|
2017-03-29 14:43:48 +00:00
|
|
|
// no change address allowed
|
2017-10-10 14:05:42 +00:00
|
|
|
in_address_n_count = BIP32_NOCHANGEALLOWED;
|
2017-05-04 16:15:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (in_address_n_count == 0) {
|
2017-03-29 14:43:48 +00:00
|
|
|
// initialize in_address_n on first input seen
|
|
|
|
in_address_n_count = count;
|
2017-10-30 11:20:47 +00:00
|
|
|
// store the bip32 path up to the account
|
|
|
|
memcpy(in_address_n, tinput->address_n,
|
|
|
|
(count - BIP32_WALLET_DEPTH) * sizeof(uint32_t));
|
2017-05-05 12:37:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-30 11:20:47 +00:00
|
|
|
// check that all addresses use a path of same length
|
2017-05-05 12:37:40 +00:00
|
|
|
if (in_address_n_count != count) {
|
2017-10-10 14:05:42 +00:00
|
|
|
in_address_n_count = BIP32_NOCHANGEALLOWED;
|
2017-05-05 12:37:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-30 11:20:47 +00:00
|
|
|
// check that the bip32 path up to the account matches
|
|
|
|
if (memcmp(in_address_n, tinput->address_n,
|
|
|
|
(count - BIP32_WALLET_DEPTH) * sizeof(uint32_t)) != 0) {
|
2017-05-05 12:37:40 +00:00
|
|
|
// mismatch -> no change address allowed
|
2017-10-10 14:05:42 +00:00
|
|
|
in_address_n_count = BIP32_NOCHANGEALLOWED;
|
2017-05-05 12:37:40 +00:00
|
|
|
return;
|
2017-03-29 14:43:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool check_change_bip32_path(const TxOutputType *toutput)
|
|
|
|
{
|
|
|
|
size_t count = toutput->address_n_count;
|
|
|
|
|
2017-10-30 11:20:47 +00:00
|
|
|
// Check that the change path has the same bip32 path length,
|
|
|
|
// the same path up to the account, and that the wallet components
|
|
|
|
// (chain id and address) are as expected.
|
|
|
|
// Note: count >= BIP32_WALLET_DEPTH and count == in_address_n_count
|
2017-10-10 14:05:42 +00:00
|
|
|
// imply that in_address_n_count != BIP32_NOCHANGEALLOWED
|
2017-10-30 11:20:47 +00:00
|
|
|
return (count >= BIP32_WALLET_DEPTH
|
2017-10-10 14:05:42 +00:00
|
|
|
&& count == in_address_n_count
|
2017-10-30 11:20:47 +00:00
|
|
|
&& 0 == memcmp(in_address_n, toutput->address_n,
|
|
|
|
(count - BIP32_WALLET_DEPTH) * sizeof(uint32_t))
|
|
|
|
&& toutput->address_n[count - 2] == BIP32_CHANGE_CHAIN
|
|
|
|
&& toutput->address_n[count - 1] <= BIP32_MAX_LAST_ELEMENT);
|
2017-03-29 14:43:48 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 20:15:55 +00:00
|
|
|
bool compile_input_script_sig(TxInputType *tinput)
|
|
|
|
{
|
|
|
|
if (!multisig_fp_mismatch) {
|
|
|
|
// check that this is still multisig
|
|
|
|
uint8_t h[32];
|
2017-09-08 13:29:55 +00:00
|
|
|
if (!tinput->has_multisig
|
2016-04-29 20:15:55 +00:00
|
|
|
|| cryptoMultisigFingerprint(&(tinput->multisig), h) == 0
|
|
|
|
|| memcmp(multisig_fp, h, 32) != 0) {
|
|
|
|
// Transaction has changed during signing
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 14:05:42 +00:00
|
|
|
if (in_address_n_count != BIP32_NOCHANGEALLOWED) {
|
2017-07-27 19:21:47 +00:00
|
|
|
// check that input address didn't change
|
|
|
|
size_t count = tinput->address_n_count;
|
2017-10-10 14:05:42 +00:00
|
|
|
if (count < 2
|
|
|
|
|| count != in_address_n_count
|
|
|
|
|| 0 != memcmp(in_address_n, tinput->address_n, (count - 2) * sizeof(uint32_t))) {
|
2017-07-27 19:21:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 20:15:55 +00:00
|
|
|
memcpy(&node, root, sizeof(HDNode));
|
2017-01-06 15:53:05 +00:00
|
|
|
if (hdnode_private_ckd_cached(&node, tinput->address_n, tinput->address_n_count, NULL) == 0) {
|
2016-04-29 20:15:55 +00:00
|
|
|
// Failed to derive private key
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-10 17:06:00 +00:00
|
|
|
hdnode_fill_public_key(&node);
|
2016-07-04 21:04:40 +00:00
|
|
|
if (tinput->has_multisig) {
|
2016-04-29 20:15:55 +00:00
|
|
|
tinput->script_sig.size = compile_script_multisig(&(tinput->multisig), tinput->script_sig.bytes);
|
|
|
|
} else { // SPENDADDRESS
|
|
|
|
uint8_t hash[20];
|
2017-12-09 18:28:53 +00:00
|
|
|
ecdsa_get_pubkeyhash(node.public_key, coin->curve->hasher_type, hash);
|
2016-04-29 20:15:55 +00:00
|
|
|
tinput->script_sig.size = compile_script_sig(coin->address_type, hash, tinput->script_sig.bytes);
|
|
|
|
}
|
|
|
|
return tinput->script_sig.size > 0;
|
|
|
|
}
|
|
|
|
|
2017-10-30 21:13:09 +00:00
|
|
|
void signing_init(uint32_t _inputs_count, uint32_t _outputs_count, const CoinInfo *_coin, const HDNode *_root, uint32_t _version, uint32_t _lock_time)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
|
|
|
inputs_count = _inputs_count;
|
|
|
|
outputs_count = _outputs_count;
|
|
|
|
coin = _coin;
|
|
|
|
root = _root;
|
2016-04-16 02:47:09 +00:00
|
|
|
version = _version;
|
|
|
|
lock_time = _lock_time;
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2017-11-08 00:13:21 +00:00
|
|
|
tx_weight = 4 * (TXSIZE_HEADER + TXSIZE_FOOTER
|
|
|
|
+ ser_length_size(inputs_count)
|
|
|
|
+ ser_length_size(outputs_count));
|
|
|
|
|
2017-04-08 20:36:32 +00:00
|
|
|
signatures = 0;
|
2015-02-11 16:38:32 +00:00
|
|
|
idx1 = 0;
|
2014-04-29 12:26:51 +00:00
|
|
|
to_spend = 0;
|
|
|
|
spending = 0;
|
|
|
|
change_spend = 0;
|
2017-07-25 20:35:09 +00:00
|
|
|
authorized_amount = 0;
|
2014-04-29 12:26:51 +00:00
|
|
|
memset(&input, 0, sizeof(TxInputType));
|
|
|
|
memset(&resp, 0, sizeof(TxRequest));
|
|
|
|
|
|
|
|
signing = true;
|
2015-02-25 15:23:24 +00:00
|
|
|
progress = 0;
|
|
|
|
// we step by 500/inputs_count per input in phase1 and phase2
|
|
|
|
// this means 50 % per phase.
|
|
|
|
progress_step = (500 << PROGRESS_PRECISION) / inputs_count;
|
2014-12-21 17:58:56 +00:00
|
|
|
|
2017-03-29 14:43:48 +00:00
|
|
|
in_address_n_count = 0;
|
2014-12-21 17:58:56 +00:00
|
|
|
multisig_fp_set = false;
|
|
|
|
multisig_fp_mismatch = false;
|
2016-04-29 20:15:55 +00:00
|
|
|
next_nonsegwit_input = 0xffffffff;
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2017-12-09 18:28:53 +00:00
|
|
|
tx_init(&to, inputs_count, outputs_count, version, lock_time, 0, coin->curve->hasher_type);
|
2016-04-29 19:17:06 +00:00
|
|
|
// segwit hashes for hashPrevouts and hashSequence
|
2017-12-09 18:28:53 +00:00
|
|
|
hasher_Init(&hashers[0], coin->curve->hasher_type);
|
|
|
|
hasher_Init(&hashers[1], coin->curve->hasher_type);
|
|
|
|
hasher_Init(&hashers[2], coin->curve->hasher_type);
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutProgressSwipe(_("Signing transaction"), 0);
|
2014-12-21 17:58:56 +00:00
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
send_req_1_input();
|
|
|
|
}
|
|
|
|
|
2016-10-19 07:37:46 +00:00
|
|
|
#define MIN(a,b) (((a)<(b))?(a):(b))
|
|
|
|
|
2017-04-08 19:46:43 +00:00
|
|
|
static bool signing_check_input(TxInputType *txinput) {
|
|
|
|
/* compute multisig fingerprint */
|
|
|
|
/* (if all input share the same fingerprint, outputs having the same fingerprint will be considered as change outputs) */
|
2017-09-08 13:29:55 +00:00
|
|
|
if (txinput->has_multisig && !multisig_fp_mismatch) {
|
2017-04-08 19:46:43 +00:00
|
|
|
uint8_t h[32];
|
|
|
|
if (cryptoMultisigFingerprint(&txinput->multisig, h) == 0) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Error computing multisig fingerprint"));
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (multisig_fp_set) {
|
|
|
|
if (memcmp(multisig_fp, h, 32) != 0) {
|
|
|
|
multisig_fp_mismatch = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
memcpy(multisig_fp, h, 32);
|
|
|
|
multisig_fp_set = true;
|
|
|
|
}
|
|
|
|
} else { // single signature
|
|
|
|
multisig_fp_mismatch = true;
|
|
|
|
}
|
|
|
|
// remember the input bip32 path
|
|
|
|
// change addresses must use the same bip32 path as all inputs
|
2017-05-05 12:37:40 +00:00
|
|
|
extract_input_bip32_path(txinput);
|
2017-04-08 19:46:43 +00:00
|
|
|
// compute segwit hashPrevouts & hashSequence
|
|
|
|
tx_prevout_hash(&hashers[0], txinput);
|
|
|
|
tx_sequence_hash(&hashers[1], txinput);
|
|
|
|
// hash prevout and script type to check it later (relevant for fee computation)
|
|
|
|
tx_prevout_hash(&hashers[2], txinput);
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Update(&hashers[2], &txinput->script_type, sizeof(&txinput->script_type));
|
2017-04-08 19:46:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the hash of the prevtx matches
|
|
|
|
static bool signing_check_prevtx_hash(void) {
|
|
|
|
uint8_t hash[32];
|
|
|
|
tx_hash_final(&tp, hash, true);
|
|
|
|
if (memcmp(hash, input.prev_hash.bytes, 32) != 0) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Encountered invalid prevhash"));
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
phase1_request_next_input();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool signing_check_output(TxOutputType *txoutput) {
|
|
|
|
// Phase1: Check outputs
|
|
|
|
// add it to hash_outputs
|
|
|
|
// ask user for permission
|
|
|
|
|
|
|
|
// check for change address
|
|
|
|
bool is_change = false;
|
|
|
|
if (txoutput->address_n_count > 0) {
|
|
|
|
if (txoutput->has_address) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Address in change output"));
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-29 19:58:49 +00:00
|
|
|
/*
|
|
|
|
* For multisig check that all inputs are multisig
|
|
|
|
*/
|
|
|
|
if (txoutput->has_multisig) {
|
2017-04-08 19:46:43 +00:00
|
|
|
uint8_t h[32];
|
2017-05-06 17:52:49 +00:00
|
|
|
if (multisig_fp_set && !multisig_fp_mismatch
|
2017-07-29 19:58:49 +00:00
|
|
|
&& cryptoMultisigFingerprint(&(txoutput->multisig), h)
|
|
|
|
&& memcmp(multisig_fp, h, 32) == 0) {
|
2017-05-06 17:52:49 +00:00
|
|
|
is_change = check_change_bip32_path(txoutput);
|
2017-04-08 19:46:43 +00:00
|
|
|
}
|
2017-07-29 19:58:49 +00:00
|
|
|
} else {
|
2017-04-08 19:46:43 +00:00
|
|
|
is_change = check_change_bip32_path(txoutput);
|
|
|
|
}
|
2017-07-29 19:58:49 +00:00
|
|
|
/*
|
|
|
|
* only allow segwit change if amount is smaller than what segwit inputs paid.
|
2017-11-24 11:35:16 +00:00
|
|
|
* this was added during the times segwit was not yet fully activated
|
|
|
|
* to make sure the user is not tricked to use witness change output
|
|
|
|
* instead of regular one therefore creating ANYONECANSPEND output
|
2017-07-29 19:58:49 +00:00
|
|
|
*/
|
|
|
|
if ((txoutput->script_type == OutputScriptType_PAYTOWITNESS
|
|
|
|
|| txoutput->script_type == OutputScriptType_PAYTOP2SHWITNESS)
|
|
|
|
&& txoutput->amount > authorized_amount) {
|
|
|
|
is_change = false;
|
|
|
|
}
|
2017-04-08 19:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_change) {
|
|
|
|
if (change_spend == 0) { // not set
|
|
|
|
change_spend = txoutput->amount;
|
|
|
|
} else {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Only one change output allowed"));
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spending + txoutput->amount < spending) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Value overflow"));
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
2017-04-08 20:36:32 +00:00
|
|
|
return false;
|
2017-04-08 19:46:43 +00:00
|
|
|
}
|
|
|
|
spending += txoutput->amount;
|
|
|
|
int co = compile_output(coin, root, txoutput, &bin_output, !is_change);
|
|
|
|
if (!is_change) {
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutProgress(_("Signing transaction"), progress);
|
2017-04-08 19:46:43 +00:00
|
|
|
}
|
|
|
|
if (co < 0) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
|
|
|
return false;
|
|
|
|
} else if (co == 0) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to compile output"));
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// compute segwit hashOuts
|
|
|
|
tx_output_hash(&hashers[0], &bin_output);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool signing_check_fee(void) {
|
|
|
|
// check fees
|
|
|
|
if (spending > to_spend) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_NotEnoughFunds, _("Not enough funds"));
|
2017-04-08 20:36:32 +00:00
|
|
|
signing_abort();
|
2017-04-08 19:46:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint64_t fee = to_spend - spending;
|
2017-11-08 00:13:21 +00:00
|
|
|
if (fee > ((uint64_t) tx_weight * coin->maxfee_kb)/4000) {
|
2017-05-15 10:08:54 +00:00
|
|
|
layoutFeeOverThreshold(coin, fee);
|
2017-04-08 19:46:43 +00:00
|
|
|
if (!protectButton(ButtonRequestType_ButtonRequest_FeeOverThreshold, false)) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
|
2017-04-08 20:36:32 +00:00
|
|
|
signing_abort();
|
2017-04-08 19:46:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// last confirmation
|
|
|
|
layoutConfirmTx(coin, to_spend - change_spend, fee);
|
|
|
|
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-31 20:49:01 +00:00
|
|
|
static uint32_t signing_hash_type(void) {
|
|
|
|
uint32_t hash_type = SIGHASH_ALL;
|
|
|
|
|
|
|
|
if (coin->has_forkid) {
|
|
|
|
hash_type |= (coin->forkid << 8) | SIGHASH_FORKID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash_type;
|
|
|
|
}
|
|
|
|
|
2017-04-08 19:46:43 +00:00
|
|
|
static void phase1_request_next_output(void) {
|
|
|
|
if (idx1 < outputs_count - 1) {
|
|
|
|
idx1++;
|
|
|
|
send_req_3_output();
|
|
|
|
} else {
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Double(&hashers[0], hash_outputs);
|
2017-04-08 19:46:43 +00:00
|
|
|
if (!signing_check_fee()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Everything was checked, now phase 2 begins and the transaction is signed.
|
|
|
|
progress_meta_step = progress_step / (inputs_count + outputs_count);
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutProgress(_("Signing transaction"), progress);
|
2017-04-08 19:46:43 +00:00
|
|
|
idx1 = 0;
|
|
|
|
phase2_request_next_input();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-31 20:49:01 +00:00
|
|
|
static void signing_hash_bip143(const TxInputType *txinput, uint8_t *hash) {
|
|
|
|
uint32_t hash_type = signing_hash_type();
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Reset(&hashers[0]);
|
|
|
|
hasher_Update(&hashers[0], (const uint8_t *)&version, 4);
|
|
|
|
hasher_Update(&hashers[0], hash_prevouts, 32);
|
|
|
|
hasher_Update(&hashers[0], hash_sequence, 32);
|
2017-07-25 14:24:24 +00:00
|
|
|
tx_prevout_hash(&hashers[0], txinput);
|
|
|
|
tx_script_hash(&hashers[0], txinput->script_sig.size, txinput->script_sig.bytes);
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Update(&hashers[0], (const uint8_t*) &txinput->amount, 8);
|
2017-07-25 14:24:24 +00:00
|
|
|
tx_sequence_hash(&hashers[0], txinput);
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Update(&hashers[0], hash_outputs, 32);
|
|
|
|
hasher_Update(&hashers[0], (const uint8_t*) &lock_time, 4);
|
|
|
|
hasher_Update(&hashers[0], (const uint8_t*) &hash_type, 4);
|
|
|
|
hasher_Double(&hashers[0], hash);
|
2017-07-25 14:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 20:49:01 +00:00
|
|
|
static bool signing_sign_hash(TxInputType *txinput, const uint8_t* private_key, const uint8_t *public_key, const uint8_t *hash) {
|
2017-04-08 19:46:43 +00:00
|
|
|
resp.serialized.has_signature_index = true;
|
|
|
|
resp.serialized.signature_index = idx1;
|
|
|
|
resp.serialized.has_signature = true;
|
|
|
|
resp.serialized.has_serialized_tx = true;
|
2017-07-25 20:35:09 +00:00
|
|
|
if (ecdsa_sign_digest(&secp256k1, private_key, hash, sig, NULL, NULL) != 0) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Signing failed"));
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
resp.serialized.signature.size = ecdsa_sig_to_der(sig, resp.serialized.signature.bytes);
|
|
|
|
|
2017-10-31 20:49:01 +00:00
|
|
|
uint8_t sighash = signing_hash_type() & 0xff;
|
2017-07-25 20:35:09 +00:00
|
|
|
if (txinput->has_multisig) {
|
2017-04-08 19:46:43 +00:00
|
|
|
// fill in the signature
|
2017-07-25 20:35:09 +00:00
|
|
|
int pubkey_idx = cryptoMultisigPubkeyIndex(&(txinput->multisig), public_key);
|
2017-04-08 19:46:43 +00:00
|
|
|
if (pubkey_idx < 0) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Pubkey not found in multisig script"));
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-25 20:35:09 +00:00
|
|
|
memcpy(txinput->multisig.signatures[pubkey_idx].bytes, resp.serialized.signature.bytes, resp.serialized.signature.size);
|
|
|
|
txinput->multisig.signatures[pubkey_idx].size = resp.serialized.signature.size;
|
|
|
|
txinput->script_sig.size = serialize_script_multisig(&(txinput->multisig), sighash, txinput->script_sig.bytes);
|
|
|
|
if (txinput->script_sig.size == 0) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to serialize multisig script"));
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else { // SPENDADDRESS
|
2017-07-25 20:35:09 +00:00
|
|
|
txinput->script_sig.size = serialize_script_sig(resp.serialized.signature.bytes, resp.serialized.signature.size, public_key, 33, sighash, txinput->script_sig.bytes);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool signing_sign_input(void) {
|
|
|
|
uint8_t hash[32];
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Double(&hashers[0], hash);
|
2017-07-25 20:35:09 +00:00
|
|
|
if (memcmp(hash, hash_outputs, 32) != 0) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Transaction has changed during signing"));
|
|
|
|
signing_abort();
|
|
|
|
return false;
|
2017-04-08 19:46:43 +00:00
|
|
|
}
|
2017-07-25 20:35:09 +00:00
|
|
|
|
2017-10-31 21:21:51 +00:00
|
|
|
uint32_t hash_type = signing_hash_type();
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Update(&ti.hasher, (const uint8_t *)&hash_type, 4);
|
2017-07-25 20:35:09 +00:00
|
|
|
tx_hash_final(&ti, hash, false);
|
|
|
|
resp.has_serialized = true;
|
2017-10-31 20:49:01 +00:00
|
|
|
if (!signing_sign_hash(&input, privkey, pubkey, hash))
|
2017-07-25 20:35:09 +00:00
|
|
|
return false;
|
2017-04-08 19:46:43 +00:00
|
|
|
resp.serialized.serialized_tx.size = tx_serialize_input(&to, &input, resp.serialized.serialized_tx.bytes);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool signing_sign_segwit_input(TxInputType *txinput) {
|
|
|
|
// idx1: index to sign
|
|
|
|
uint8_t hash[32];
|
|
|
|
|
|
|
|
if (txinput->script_type == InputScriptType_SPENDWITNESS
|
|
|
|
|| txinput->script_type == InputScriptType_SPENDP2SHWITNESS) {
|
|
|
|
if (!compile_input_script_sig(txinput)) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to compile input"));
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-25 20:35:09 +00:00
|
|
|
if (txinput->amount > authorized_amount) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Transaction has changed during signing"));
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_abort();
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-25 20:35:09 +00:00
|
|
|
authorized_amount -= txinput->amount;
|
2017-04-08 19:46:43 +00:00
|
|
|
|
2017-10-31 20:49:01 +00:00
|
|
|
signing_hash_bip143(txinput, hash);
|
2017-04-08 19:46:43 +00:00
|
|
|
|
|
|
|
resp.has_serialized = true;
|
2017-10-31 20:49:01 +00:00
|
|
|
if (!signing_sign_hash(txinput, node.private_key, node.public_key, hash))
|
2017-04-08 19:46:43 +00:00
|
|
|
return false;
|
2017-11-07 01:33:53 +00:00
|
|
|
|
|
|
|
uint8_t sighash = signing_hash_type() & 0xff;
|
2017-04-08 19:46:43 +00:00
|
|
|
if (txinput->has_multisig) {
|
2017-07-23 20:20:51 +00:00
|
|
|
uint32_t r = 1; // skip number of items (filled in later)
|
2017-04-08 19:46:43 +00:00
|
|
|
resp.serialized.serialized_tx.bytes[r] = 0; r++;
|
2017-07-23 20:20:51 +00:00
|
|
|
int nwitnesses = 2;
|
|
|
|
for (uint32_t i = 0; i < txinput->multisig.signatures_count; i++) {
|
2017-04-08 19:46:43 +00:00
|
|
|
if (txinput->multisig.signatures[i].size == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nwitnesses++;
|
2017-11-07 01:33:53 +00:00
|
|
|
txinput->multisig.signatures[i].bytes[txinput->multisig.signatures[i].size] = sighash;
|
2017-04-08 19:46:43 +00:00
|
|
|
r += tx_serialize_script(txinput->multisig.signatures[i].size + 1, txinput->multisig.signatures[i].bytes, resp.serialized.serialized_tx.bytes + r);
|
|
|
|
}
|
2017-07-23 20:20:51 +00:00
|
|
|
uint32_t script_len = compile_script_multisig(&txinput->multisig, 0);
|
2017-04-08 19:46:43 +00:00
|
|
|
r += ser_length(script_len, resp.serialized.serialized_tx.bytes + r);
|
|
|
|
r += compile_script_multisig(&txinput->multisig, resp.serialized.serialized_tx.bytes + r);
|
|
|
|
resp.serialized.serialized_tx.bytes[0] = nwitnesses;
|
|
|
|
resp.serialized.serialized_tx.size = r;
|
|
|
|
} else { // single signature
|
|
|
|
uint32_t r = 0;
|
|
|
|
r += ser_length(2, resp.serialized.serialized_tx.bytes + r);
|
2017-11-07 01:33:53 +00:00
|
|
|
resp.serialized.signature.bytes[resp.serialized.signature.size] = sighash;
|
2017-04-08 19:46:43 +00:00
|
|
|
r += tx_serialize_script(resp.serialized.signature.size + 1, resp.serialized.signature.bytes, resp.serialized.serialized_tx.bytes + r);
|
|
|
|
r += tx_serialize_script(33, node.public_key, resp.serialized.serialized_tx.bytes + r);
|
|
|
|
resp.serialized.serialized_tx.size = r;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// empty witness
|
|
|
|
resp.has_serialized = true;
|
|
|
|
resp.serialized.has_signature_index = false;
|
|
|
|
resp.serialized.has_signature = false;
|
|
|
|
resp.serialized.has_serialized_tx = true;
|
|
|
|
resp.serialized.serialized_tx.bytes[0] = 0;
|
|
|
|
resp.serialized.serialized_tx.size = 1;
|
|
|
|
}
|
|
|
|
// if last witness add tx footer
|
|
|
|
if (idx1 == inputs_count - 1) {
|
|
|
|
uint32_t r = resp.serialized.serialized_tx.size;
|
|
|
|
r += tx_serialize_footer(&to, resp.serialized.serialized_tx.bytes + r);
|
|
|
|
resp.serialized.serialized_tx.size = r;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-19 15:40:57 +00:00
|
|
|
#define ENABLE_SEGWIT_NONSEGWIT_MIXING 1
|
2017-05-09 21:26:18 +00:00
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
void signing_txack(TransactionType *tx)
|
|
|
|
{
|
|
|
|
if (!signing) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_UnexpectedMessage, _("Not in Signing mode"));
|
2014-04-29 12:26:51 +00:00
|
|
|
layoutHome();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-25 15:23:24 +00:00
|
|
|
static int update_ctr = 0;
|
2015-04-13 16:53:58 +00:00
|
|
|
if (update_ctr++ == 20) {
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutProgress(_("Signing transaction"), progress);
|
2015-02-25 15:23:24 +00:00
|
|
|
update_ctr = 0;
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
memset(&resp, 0, sizeof(TxRequest));
|
|
|
|
|
|
|
|
switch (signing_stage) {
|
|
|
|
case STAGE_REQUEST_1_INPUT:
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_check_input(&tx->inputs[0]);
|
2017-11-08 00:13:21 +00:00
|
|
|
tx_weight += tx_input_weight(&tx->inputs[0]);
|
2016-04-29 20:15:55 +00:00
|
|
|
if (tx->inputs[0].script_type == InputScriptType_SPENDMULTISIG
|
|
|
|
|| tx->inputs[0].script_type == InputScriptType_SPENDADDRESS) {
|
|
|
|
memcpy(&input, tx->inputs, sizeof(TxInputType));
|
2017-05-09 21:26:18 +00:00
|
|
|
#if !ENABLE_SEGWIT_NONSEGWIT_MIXING
|
|
|
|
// don't mix segwit and non-segwit inputs
|
|
|
|
if (idx1 > 0 && to.is_segwit == true) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Mixing segwit and non-segwit inputs is not allowed"));
|
2017-05-09 21:26:18 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2017-07-25 15:49:55 +00:00
|
|
|
|
2017-10-31 20:55:29 +00:00
|
|
|
if (coin->force_bip143) {
|
2017-07-25 15:49:55 +00:00
|
|
|
if (!tx->inputs[0].has_amount) {
|
2017-10-31 20:55:29 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("BIP 143 input without amount"));
|
2017-07-25 15:49:55 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (to_spend + tx->inputs[0].amount < to_spend) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Value overflow"));
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
to_spend += tx->inputs[0].amount;
|
2017-07-25 20:35:09 +00:00
|
|
|
authorized_amount += tx->inputs[0].amount;
|
2017-07-25 15:49:55 +00:00
|
|
|
phase1_request_next_input();
|
|
|
|
} else {
|
2017-07-25 20:35:09 +00:00
|
|
|
// remember the first non-segwit input -- this is the first input
|
|
|
|
// we need to sign during phase2
|
|
|
|
if (next_nonsegwit_input == 0xffffffff)
|
|
|
|
next_nonsegwit_input = idx1;
|
2017-07-25 15:49:55 +00:00
|
|
|
send_req_2_prev_meta();
|
|
|
|
}
|
2017-04-08 18:29:58 +00:00
|
|
|
} else if (tx->inputs[0].script_type == InputScriptType_SPENDWITNESS
|
|
|
|
|| tx->inputs[0].script_type == InputScriptType_SPENDP2SHWITNESS) {
|
2017-10-30 21:13:09 +00:00
|
|
|
if (!coin->has_segwit) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Segwit not enabled on this coin"));
|
2017-05-04 16:54:54 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
2017-05-12 23:03:03 +00:00
|
|
|
}
|
2017-04-08 18:29:58 +00:00
|
|
|
if (!tx->inputs[0].has_amount) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Segwit input without amount"));
|
2017-04-08 18:29:58 +00:00
|
|
|
signing_abort();
|
2017-04-08 18:32:14 +00:00
|
|
|
return;
|
2017-04-08 18:29:58 +00:00
|
|
|
}
|
2016-04-29 20:15:55 +00:00
|
|
|
if (to_spend + tx->inputs[0].amount < to_spend) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Value overflow"));
|
2016-04-29 20:15:55 +00:00
|
|
|
signing_abort();
|
2017-04-08 18:32:14 +00:00
|
|
|
return;
|
2016-04-29 20:15:55 +00:00
|
|
|
}
|
2017-11-08 00:13:21 +00:00
|
|
|
if (!to.is_segwit) {
|
|
|
|
tx_weight += TXSIZE_SEGWIT_OVERHEAD + to.inputs_len;
|
|
|
|
}
|
2017-05-09 21:26:18 +00:00
|
|
|
#if !ENABLE_SEGWIT_NONSEGWIT_MIXING
|
|
|
|
// don't mix segwit and non-segwit inputs
|
|
|
|
if (idx1 == 0) {
|
|
|
|
to.is_segwit = true;
|
|
|
|
} else if (to.is_segwit == false) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Mixing segwit and non-segwit inputs is not allowed"));
|
2017-05-09 21:26:18 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
to.is_segwit = true;
|
|
|
|
#endif
|
2016-04-29 20:15:55 +00:00
|
|
|
to_spend += tx->inputs[0].amount;
|
2017-07-25 20:35:09 +00:00
|
|
|
authorized_amount += tx->inputs[0].amount;
|
2016-04-29 20:15:55 +00:00
|
|
|
phase1_request_next_input();
|
|
|
|
} else {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Wrong input script type"));
|
2016-04-29 20:15:55 +00:00
|
|
|
signing_abort();
|
2017-04-08 18:32:14 +00:00
|
|
|
return;
|
2016-04-29 20:15:55 +00:00
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
return;
|
|
|
|
case STAGE_REQUEST_2_PREV_META:
|
2017-12-09 18:28:53 +00:00
|
|
|
tx_init(&tp, tx->inputs_cnt, tx->outputs_cnt, tx->version, tx->lock_time, tx->extra_data_len, coin->curve->hasher_type);
|
2015-02-25 15:23:24 +00:00
|
|
|
progress_meta_step = progress_step / (tp.inputs_len + tp.outputs_len);
|
2015-02-11 16:38:32 +00:00
|
|
|
idx2 = 0;
|
2016-10-21 11:20:57 +00:00
|
|
|
if (tp.inputs_len > 0) {
|
|
|
|
send_req_2_prev_input();
|
|
|
|
} else {
|
|
|
|
tx_serialize_header_hash(&tp);
|
|
|
|
send_req_2_prev_output();
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
return;
|
|
|
|
case STAGE_REQUEST_2_PREV_INPUT:
|
2015-02-25 15:23:24 +00:00
|
|
|
progress = (idx1 * progress_step + idx2 * progress_meta_step) >> PROGRESS_PRECISION;
|
2014-12-21 18:39:20 +00:00
|
|
|
if (!tx_serialize_input_hash(&tp, tx->inputs)) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to serialize input"));
|
2014-04-29 12:26:51 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2015-02-11 16:38:32 +00:00
|
|
|
if (idx2 < tp.inputs_len - 1) {
|
|
|
|
idx2++;
|
2014-04-29 12:26:51 +00:00
|
|
|
send_req_2_prev_input();
|
|
|
|
} else {
|
2015-02-11 16:38:32 +00:00
|
|
|
idx2 = 0;
|
2014-04-29 12:26:51 +00:00
|
|
|
send_req_2_prev_output();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case STAGE_REQUEST_2_PREV_OUTPUT:
|
2015-02-25 15:23:24 +00:00
|
|
|
progress = (idx1 * progress_step + (tp.inputs_len + idx2) * progress_meta_step) >> PROGRESS_PRECISION;
|
2014-12-21 18:39:20 +00:00
|
|
|
if (!tx_serialize_output_hash(&tp, tx->bin_outputs)) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to serialize output"));
|
2014-04-29 12:26:51 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2015-02-11 16:38:32 +00:00
|
|
|
if (idx2 == input.prev_index) {
|
2016-04-29 20:15:55 +00:00
|
|
|
if (to_spend + tx->bin_outputs[0].amount < to_spend) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Value overflow"));
|
2016-04-29 20:15:55 +00:00
|
|
|
signing_abort();
|
2017-04-08 18:32:14 +00:00
|
|
|
return;
|
2016-04-29 20:15:55 +00:00
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
to_spend += tx->bin_outputs[0].amount;
|
|
|
|
}
|
2015-02-11 16:38:32 +00:00
|
|
|
if (idx2 < tp.outputs_len - 1) {
|
|
|
|
/* Check prevtx of next input */
|
|
|
|
idx2++;
|
2014-04-29 12:26:51 +00:00
|
|
|
send_req_2_prev_output();
|
2017-04-08 19:46:43 +00:00
|
|
|
} else if (tp.extra_data_len > 0) { // has extra data
|
|
|
|
send_req_2_prev_extradata(0, MIN(1024, tp.extra_data_len));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
/* prevtx is done */
|
|
|
|
signing_check_prevtx_hash();
|
2016-10-19 07:37:46 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
case STAGE_REQUEST_2_PREV_EXTRADATA:
|
|
|
|
if (!tx_serialize_extra_data_hash(&tp, tx->extra_data.bytes, tx->extra_data.size)) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to serialize extra data"));
|
2016-10-19 07:37:46 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tp.extra_data_received < tp.extra_data_len) { // still some data remanining
|
|
|
|
send_req_2_prev_extradata(tp.extra_data_received, MIN(1024, tp.extra_data_len - tp.extra_data_received));
|
2014-04-29 12:26:51 +00:00
|
|
|
} else {
|
2017-04-08 19:46:43 +00:00
|
|
|
signing_check_prevtx_hash();
|
2015-02-11 16:38:32 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
case STAGE_REQUEST_3_OUTPUT:
|
2017-04-08 19:46:43 +00:00
|
|
|
if (!signing_check_output(&tx->outputs[0])) {
|
2015-02-11 16:38:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-08 00:13:21 +00:00
|
|
|
tx_weight += tx_output_weight(coin, &tx->outputs[0]);
|
2017-04-08 19:46:43 +00:00
|
|
|
phase1_request_next_output();
|
2015-02-11 16:38:32 +00:00
|
|
|
return;
|
|
|
|
case STAGE_REQUEST_4_INPUT:
|
2017-04-08 20:36:32 +00:00
|
|
|
progress = 500 + ((signatures * progress_step + idx2 * progress_meta_step) >> PROGRESS_PRECISION);
|
2015-02-11 16:38:32 +00:00
|
|
|
if (idx2 == 0) {
|
2017-12-09 18:28:53 +00:00
|
|
|
tx_init(&ti, inputs_count, outputs_count, version, lock_time, 0, coin->curve->hasher_type);
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Reset(&hashers[0]);
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
2016-04-29 20:15:55 +00:00
|
|
|
// check prevouts and script type
|
|
|
|
tx_prevout_hash(&hashers[0], tx->inputs);
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Update(&hashers[0], &tx->inputs[0].script_type, sizeof(&tx->inputs[0].script_type));
|
2015-02-11 16:38:32 +00:00
|
|
|
if (idx2 == idx1) {
|
2016-04-29 20:15:55 +00:00
|
|
|
if (!compile_input_script_sig(&tx->inputs[0])) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to compile input"));
|
2014-04-29 12:26:51 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2016-04-29 20:15:55 +00:00
|
|
|
memcpy(&input, &tx->inputs[0], sizeof(input));
|
2014-04-29 12:26:51 +00:00
|
|
|
memcpy(privkey, node.private_key, 32);
|
|
|
|
memcpy(pubkey, node.public_key, 33);
|
|
|
|
} else {
|
2016-04-29 20:15:55 +00:00
|
|
|
if (next_nonsegwit_input == idx1 && idx2 > idx1
|
|
|
|
&& (tx->inputs[0].script_type == InputScriptType_SPENDADDRESS
|
|
|
|
|| tx->inputs[0].script_type == InputScriptType_SPENDMULTISIG)) {
|
|
|
|
next_nonsegwit_input = idx2;
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
tx->inputs[0].script_sig.size = 0;
|
|
|
|
}
|
2014-12-21 18:39:20 +00:00
|
|
|
if (!tx_serialize_input_hash(&ti, tx->inputs)) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to serialize input"));
|
2014-04-29 12:26:51 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2015-02-11 16:38:32 +00:00
|
|
|
if (idx2 < inputs_count - 1) {
|
|
|
|
idx2++;
|
|
|
|
send_req_4_input();
|
2014-04-29 12:26:51 +00:00
|
|
|
} else {
|
2016-04-29 20:15:55 +00:00
|
|
|
uint8_t hash[32];
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Final(&hashers[0], hash);
|
2016-04-29 20:15:55 +00:00
|
|
|
if (memcmp(hash, hash_check, 32) != 0) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Transaction has changed during signing"));
|
2016-04-29 20:15:55 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2017-12-09 14:38:37 +00:00
|
|
|
hasher_Reset(&hashers[0]);
|
2015-02-11 16:38:32 +00:00
|
|
|
idx2 = 0;
|
|
|
|
send_req_4_output();
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
return;
|
2015-02-11 16:38:32 +00:00
|
|
|
case STAGE_REQUEST_4_OUTPUT:
|
2017-04-08 20:36:32 +00:00
|
|
|
progress = 500 + ((signatures * progress_step + (inputs_count + idx2) * progress_meta_step) >> PROGRESS_PRECISION);
|
|
|
|
if (compile_output(coin, root, tx->outputs, &bin_output, false) <= 0) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to compile output"));
|
2014-04-29 12:26:51 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2016-04-29 20:15:55 +00:00
|
|
|
// check hashOutputs
|
|
|
|
tx_output_hash(&hashers[0], &bin_output);
|
2014-12-21 18:39:20 +00:00
|
|
|
if (!tx_serialize_output_hash(&ti, &bin_output)) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to serialize output"));
|
2014-04-29 12:26:51 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
2015-02-11 16:38:32 +00:00
|
|
|
if (idx2 < outputs_count - 1) {
|
|
|
|
idx2++;
|
|
|
|
send_req_4_output();
|
2014-04-29 12:26:51 +00:00
|
|
|
} else {
|
2017-04-08 19:46:43 +00:00
|
|
|
if (!signing_sign_input()) {
|
2016-11-08 14:48:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-02-25 15:23:24 +00:00
|
|
|
// since this took a longer time, update progress
|
2017-04-08 20:36:32 +00:00
|
|
|
signatures++;
|
|
|
|
progress = 500 + ((signatures * progress_step) >> PROGRESS_PRECISION);
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutProgress(_("Signing transaction"), progress);
|
2015-02-25 15:23:24 +00:00
|
|
|
update_ctr = 0;
|
2015-02-11 16:38:32 +00:00
|
|
|
if (idx1 < inputs_count - 1) {
|
|
|
|
idx1++;
|
2016-04-29 20:15:55 +00:00
|
|
|
phase2_request_next_input();
|
2014-04-29 12:26:51 +00:00
|
|
|
} else {
|
2015-02-11 16:38:32 +00:00
|
|
|
idx1 = 0;
|
|
|
|
send_req_5_output();
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
2016-04-29 20:15:55 +00:00
|
|
|
|
|
|
|
case STAGE_REQUEST_SEGWIT_INPUT:
|
|
|
|
resp.has_serialized = true;
|
|
|
|
resp.serialized.has_signature_index = false;
|
|
|
|
resp.serialized.has_signature = false;
|
|
|
|
resp.serialized.has_serialized_tx = true;
|
2017-07-29 19:58:49 +00:00
|
|
|
if (tx->inputs[0].script_type == InputScriptType_SPENDMULTISIG
|
|
|
|
|| tx->inputs[0].script_type == InputScriptType_SPENDADDRESS) {
|
2017-10-31 20:55:29 +00:00
|
|
|
if (!coin->force_bip143) {
|
2017-07-25 20:35:09 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Transaction has changed during signing"));
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!compile_input_script_sig(&tx->inputs[0])) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to compile input"));
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tx->inputs[0].amount > authorized_amount) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_DataError, _("Transaction has changed during signing"));
|
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
authorized_amount -= tx->inputs[0].amount;
|
|
|
|
|
|
|
|
uint8_t hash[32];
|
2017-10-31 20:49:01 +00:00
|
|
|
signing_hash_bip143(&tx->inputs[0], hash);
|
|
|
|
if (!signing_sign_hash(&tx->inputs[0], node.private_key, node.public_key, hash))
|
2017-07-25 20:35:09 +00:00
|
|
|
return;
|
|
|
|
// since this took a longer time, update progress
|
|
|
|
signatures++;
|
|
|
|
progress = 500 + ((signatures * progress_step) >> PROGRESS_PRECISION);
|
|
|
|
layoutProgress(_("Signing transaction"), progress);
|
|
|
|
update_ctr = 0;
|
|
|
|
} else if (tx->inputs[0].script_type == InputScriptType_SPENDP2SHWITNESS
|
|
|
|
&& !tx->inputs[0].has_multisig) {
|
2016-07-03 14:11:56 +00:00
|
|
|
if (!compile_input_script_sig(&tx->inputs[0])) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to compile input"));
|
2016-07-03 14:11:56 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// fixup normal p2pkh script into witness 0 p2wpkh script for p2sh
|
|
|
|
// we convert 76 A9 14 <digest> 88 AC to 16 00 14 <digest>
|
|
|
|
// P2SH input pushes witness 0 script
|
|
|
|
tx->inputs[0].script_sig.size = 0x17; // drops last 2 bytes.
|
|
|
|
tx->inputs[0].script_sig.bytes[0] = 0x16; // push 22 bytes; replaces OP_DUP
|
|
|
|
tx->inputs[0].script_sig.bytes[1] = 0x00; // witness 0 script ; replaces OP_HASH160
|
|
|
|
// digest is already in right place.
|
2016-07-04 21:04:40 +00:00
|
|
|
} else if (tx->inputs[0].script_type == InputScriptType_SPENDP2SHWITNESS) {
|
2016-07-03 14:11:56 +00:00
|
|
|
// Prepare P2SH witness script.
|
|
|
|
tx->inputs[0].script_sig.size = 0x23; // 35 bytes long:
|
|
|
|
tx->inputs[0].script_sig.bytes[0] = 0x22; // push 34 bytes (full witness script)
|
|
|
|
tx->inputs[0].script_sig.bytes[1] = 0x00; // witness 0 script
|
|
|
|
tx->inputs[0].script_sig.bytes[2] = 0x20; // push 32 bytes (digest)
|
2016-07-04 21:04:40 +00:00
|
|
|
// compute digest of multisig script
|
2017-12-09 18:28:53 +00:00
|
|
|
if (!compile_script_multisig_hash(&tx->inputs[0].multisig, coin->curve->hasher_type, tx->inputs[0].script_sig.bytes + 3)) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to compile input"));
|
2016-07-03 14:11:56 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// direct witness scripts require zero scriptSig
|
|
|
|
tx->inputs[0].script_sig.size = 0;
|
|
|
|
}
|
2016-04-29 20:15:55 +00:00
|
|
|
resp.serialized.serialized_tx.size = tx_serialize_input(&to, &tx->inputs[0], resp.serialized.serialized_tx.bytes);
|
|
|
|
if (idx1 < inputs_count - 1) {
|
|
|
|
idx1++;
|
|
|
|
phase2_request_next_input();
|
|
|
|
} else {
|
|
|
|
idx1 = 0;
|
|
|
|
send_req_5_output();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
2015-02-11 16:38:32 +00:00
|
|
|
case STAGE_REQUEST_5_OUTPUT:
|
|
|
|
if (compile_output(coin, root, tx->outputs, &bin_output,false) <= 0) {
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to compile output"));
|
2014-04-29 12:26:51 +00:00
|
|
|
signing_abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resp.has_serialized = true;
|
|
|
|
resp.serialized.has_serialized_tx = true;
|
2014-12-21 18:39:20 +00:00
|
|
|
resp.serialized.serialized_tx.size = tx_serialize_output(&to, &bin_output, resp.serialized.serialized_tx.bytes);
|
2015-02-11 16:38:32 +00:00
|
|
|
if (idx1 < outputs_count - 1) {
|
|
|
|
idx1++;
|
|
|
|
send_req_5_output();
|
2016-04-29 20:15:55 +00:00
|
|
|
} else if (to.is_segwit) {
|
|
|
|
idx1 = 0;
|
|
|
|
send_req_segwit_witness();
|
|
|
|
} else {
|
|
|
|
send_req_finished();
|
|
|
|
signing_abort();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case STAGE_REQUEST_SEGWIT_WITNESS:
|
2017-04-08 19:46:43 +00:00
|
|
|
if (!signing_sign_segwit_input(&tx->inputs[0])) {
|
|
|
|
return;
|
2016-05-01 00:47:15 +00:00
|
|
|
}
|
2017-04-08 20:36:32 +00:00
|
|
|
signatures++;
|
|
|
|
progress = 500 + ((signatures * progress_step) >> PROGRESS_PRECISION);
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutProgress(_("Signing transaction"), progress);
|
2016-04-29 20:15:55 +00:00
|
|
|
update_ctr = 0;
|
|
|
|
if (idx1 < inputs_count - 1) {
|
|
|
|
idx1++;
|
|
|
|
send_req_segwit_witness();
|
2014-04-29 12:26:51 +00:00
|
|
|
} else {
|
|
|
|
send_req_finished();
|
|
|
|
signing_abort();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-04-08 19:46:43 +00:00
|
|
|
|
2017-06-18 20:22:33 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError, _("Signing error"));
|
2014-04-29 12:26:51 +00:00
|
|
|
signing_abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void signing_abort(void)
|
|
|
|
{
|
|
|
|
if (signing) {
|
|
|
|
layoutHome();
|
|
|
|
signing = false;
|
|
|
|
}
|
|
|
|
}
|