2014-04-29 12:26:51 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __TRANSACTION_H__
|
|
|
|
#define __TRANSACTION_H__
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "bip32.h"
|
2017-10-30 21:13:09 +00:00
|
|
|
#include "coins.h"
|
2017-12-09 14:38:37 +00:00
|
|
|
#include "hasher.h"
|
2018-07-10 14:58:00 +00:00
|
|
|
#include "messages-bitcoin.pb.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "sha2.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2018-06-12 13:03:17 +00:00
|
|
|
#define TX_OVERWINTERED 0x80000000
|
|
|
|
|
2022-12-16 09:38:08 +00:00
|
|
|
#define OWNERSHIP_ID_SIZE 32
|
|
|
|
|
2022-12-19 11:45:36 +00:00
|
|
|
enum {
|
|
|
|
// Signature hash type with the same semantics as SIGHASH_ALL, but instead of
|
|
|
|
// having to include the byte in the signature, it is implied.
|
|
|
|
SIGHASH_ALL_TAPROOT = 0,
|
|
|
|
|
|
|
|
// Default signature hash type in Bitcoin which signs all inputs and all
|
|
|
|
// outputs of the transaction.
|
|
|
|
SIGHASH_ALL = 1,
|
|
|
|
|
|
|
|
// Signature hash flag used in some Bitcoin-like altcoins for replay
|
|
|
|
// protection.
|
|
|
|
SIGHASH_FORKID = 0x40,
|
|
|
|
};
|
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
typedef struct {
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t inputs_len;
|
|
|
|
uint32_t outputs_len;
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t version;
|
|
|
|
uint32_t version_group_id;
|
2019-12-05 03:32:13 +00:00
|
|
|
uint32_t timestamp;
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t lock_time;
|
|
|
|
uint32_t expiry;
|
2022-05-02 07:04:22 +00:00
|
|
|
uint32_t branch_id;
|
2019-03-29 16:10:31 +00:00
|
|
|
bool is_segwit;
|
|
|
|
bool is_decred;
|
2020-07-29 13:09:23 +00:00
|
|
|
bool is_zcashlike;
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t have_inputs;
|
|
|
|
uint32_t have_outputs;
|
2016-10-19 07:37:46 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t extra_data_len;
|
|
|
|
uint32_t extra_data_received;
|
2016-10-19 07:37:46 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t size;
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
Hasher hasher;
|
2014-04-29 12:26:51 +00:00
|
|
|
} TxStruct;
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
bool compute_address(const CoinInfo *coin, InputScriptType script_type,
|
|
|
|
const HDNode *node, bool has_multisig,
|
|
|
|
const MultisigRedeemScriptType *multisig,
|
|
|
|
char address[MAX_ADDR_SIZE]);
|
2023-01-02 10:24:46 +00:00
|
|
|
int address_to_script_pubkey(const CoinInfo *coin, const char *address,
|
|
|
|
uint8_t *script_pubkey, pb_size_t *size);
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t compile_script_sig(uint32_t address_type, const uint8_t *pubkeyhash,
|
|
|
|
uint8_t *out);
|
|
|
|
uint32_t compile_script_multisig(const CoinInfo *coin,
|
|
|
|
const MultisigRedeemScriptType *multisig,
|
|
|
|
uint8_t *out);
|
|
|
|
uint32_t compile_script_multisig_hash(const CoinInfo *coin,
|
|
|
|
const MultisigRedeemScriptType *multisig,
|
|
|
|
uint8_t *hash);
|
|
|
|
uint32_t serialize_script_sig(const uint8_t *signature, uint32_t signature_len,
|
|
|
|
const uint8_t *pubkey, uint32_t pubkey_len,
|
|
|
|
uint8_t sighash, uint8_t *out);
|
|
|
|
uint32_t serialize_script_multisig(const CoinInfo *coin,
|
|
|
|
const MultisigRedeemScriptType *multisig,
|
|
|
|
uint8_t sighash, uint8_t *out);
|
2022-12-19 11:45:36 +00:00
|
|
|
uint32_t serialize_p2wpkh_witness(const uint8_t *signature,
|
|
|
|
uint32_t signature_len,
|
|
|
|
const uint8_t *public_key,
|
|
|
|
uint32_t public_key_len, uint8_t sighash,
|
|
|
|
uint8_t *out);
|
|
|
|
uint32_t serialize_p2tr_witness(const uint8_t *signature,
|
|
|
|
uint32_t signature_len, uint8_t sighash,
|
|
|
|
uint8_t *out);
|
|
|
|
bool tx_sign_ecdsa(const ecdsa_curve *curve, const uint8_t *private_key,
|
|
|
|
const uint8_t *hash, uint8_t *out, pb_size_t *size);
|
|
|
|
bool tx_sign_bip340(const uint8_t *private_key, const uint8_t *hash,
|
|
|
|
uint8_t *out, pb_size_t *size);
|
2023-01-02 10:24:46 +00:00
|
|
|
void op_return_to_script_pubkey(const uint8_t *op_return_data,
|
|
|
|
size_t op_return_size, uint8_t *script_pubkey,
|
|
|
|
pb_size_t *script_pubkey_size);
|
2023-01-03 19:09:48 +00:00
|
|
|
bool get_script_pubkey(const CoinInfo *coin, HDNode *node, bool has_multisig,
|
|
|
|
const MultisigRedeemScriptType *multisig,
|
|
|
|
InputScriptType script_type, uint8_t *script_pubkey,
|
|
|
|
pb_size_t *script_pubkey_size);
|
2016-04-29 19:17:06 +00:00
|
|
|
|
2022-03-11 11:25:53 +00:00
|
|
|
bool tx_input_check_hash(Hasher *hasher, const TxInputType *input);
|
2018-08-27 17:06:11 +00:00
|
|
|
uint32_t tx_prevout_hash(Hasher *hasher, const TxInputType *input);
|
2021-11-10 11:43:03 +00:00
|
|
|
uint32_t tx_amount_hash(Hasher *hasher, const TxInputType *input);
|
2017-12-09 14:38:37 +00:00
|
|
|
uint32_t tx_script_hash(Hasher *hasher, uint32_t size, const uint8_t *data);
|
2018-08-27 17:06:11 +00:00
|
|
|
uint32_t tx_sequence_hash(Hasher *hasher, const TxInputType *input);
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t tx_output_hash(Hasher *hasher, const TxOutputBinType *output,
|
|
|
|
bool decred);
|
2016-04-29 20:15:55 +00:00
|
|
|
uint32_t tx_serialize_script(uint32_t size, const uint8_t *data, uint8_t *out);
|
2016-04-29 19:17:06 +00:00
|
|
|
|
2016-05-01 00:47:15 +00:00
|
|
|
uint32_t tx_serialize_footer(TxStruct *tx, uint8_t *out);
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t tx_serialize_input(TxStruct *tx, const TxInputType *input,
|
|
|
|
uint8_t *out);
|
|
|
|
uint32_t tx_serialize_output(TxStruct *tx, const TxOutputBinType *output,
|
|
|
|
uint8_t *out);
|
|
|
|
uint32_t tx_serialize_decred_witness(TxStruct *tx, const TxInputType *input,
|
|
|
|
uint8_t *out);
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
void tx_init(TxStruct *tx, uint32_t inputs_len, uint32_t outputs_len,
|
|
|
|
uint32_t version, uint32_t lock_time, uint32_t expiry,
|
2022-05-02 07:04:22 +00:00
|
|
|
uint32_t branch_id, uint32_t extra_data_len,
|
|
|
|
HasherType hasher_sign, bool is_zcashlike,
|
2019-12-05 03:32:13 +00:00
|
|
|
uint32_t version_group_id, uint32_t timestamp);
|
2016-10-21 11:20:57 +00:00
|
|
|
uint32_t tx_serialize_header_hash(TxStruct *tx);
|
2018-08-27 17:06:11 +00:00
|
|
|
uint32_t tx_serialize_input_hash(TxStruct *tx, const TxInputType *input);
|
|
|
|
uint32_t tx_serialize_output_hash(TxStruct *tx, const TxOutputBinType *output);
|
2019-03-29 16:10:31 +00:00
|
|
|
uint32_t tx_serialize_extra_data_hash(TxStruct *tx, const uint8_t *data,
|
|
|
|
uint32_t datalen);
|
|
|
|
uint32_t tx_serialize_decred_witness_hash(TxStruct *tx,
|
|
|
|
const TxInputType *input);
|
2014-04-29 12:26:51 +00:00
|
|
|
void tx_hash_final(TxStruct *t, uint8_t *hash, bool reverse);
|
|
|
|
|
2018-08-27 17:06:11 +00:00
|
|
|
uint32_t tx_input_weight(const CoinInfo *coin, const TxInputType *txinput);
|
|
|
|
uint32_t tx_output_weight(const CoinInfo *coin, const TxOutputType *txoutput);
|
|
|
|
uint32_t tx_decred_witness_weight(const TxInputType *txinput);
|
2022-12-16 09:38:08 +00:00
|
|
|
bool get_ownership_proof(const CoinInfo *coin, InputScriptType script_type,
|
|
|
|
const HDNode *node, uint8_t flags,
|
|
|
|
const uint8_t ownership_id[OWNERSHIP_ID_SIZE],
|
|
|
|
const uint8_t *script_pubkey,
|
|
|
|
size_t script_pubkey_size,
|
|
|
|
const uint8_t *commitment_data,
|
|
|
|
size_t commitment_data_size, OwnershipProof *out);
|
2022-12-21 16:29:53 +00:00
|
|
|
bool tx_input_verify_nonownership(
|
|
|
|
const CoinInfo *coin, const TxInputType *txinput,
|
|
|
|
const uint8_t ownership_id[OWNERSHIP_ID_SIZE]);
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
#endif
|