forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransaction.h
More file actions
86 lines (65 loc) · 3.37 KB
/
Copy pathTransaction.h
File metadata and controls
86 lines (65 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#pragma once
#include "Script.h"
#include "TransactionInput.h"
#include "TransactionOutput.h"
#include "../Hash.h"
#include <TrustWalletCore/TWBitcoin.h>
#include <vector>
namespace TW::Bitcoin {
struct Transaction {
/// Transaction data format version (note, this is signed)
int32_t version = 1;
/// The block number or timestamp at which this transaction is unlocked
///
/// | Value | Description
/// |----------------|------------
/// | 0 | Not locked
/// | < 500000000 | Block number at which this transaction is unlocked
/// | >= 500000000 | UNIX timestamp at which this transaction is unlocked
///
/// If all inputs have final (`0xffffffff`) sequence numbers then `lockTime` is irrelevant. Otherwise, the
/// transaction may not be added to a block until after `lockTime`.
uint32_t lockTime = 0;
/// A list of 1 or more transaction inputs or sources for coins
std::vector<TransactionInput> inputs;
/// A list of 1 or more transaction outputs or destinations for coins
std::vector<TransactionOutput> outputs;
TW::Hash::Hasher hasher = TW::Hash::sha256d;
Transaction() = default;
Transaction(int32_t version, uint32_t lockTime, TW::Hash::Hasher hasher = TW::Hash::sha256d)
: version(version), lockTime(lockTime), inputs(), outputs(), hasher(hasher) {}
/// Whether the transaction is empty.
bool empty() const { return inputs.empty() && outputs.empty(); }
/// Generates the signature pre-image.
std::vector<uint8_t> getPreImage(const Script& scriptCode, size_t index, enum TWBitcoinSigHashType hashType,
uint64_t amount) const;
std::vector<uint8_t> getPrevoutHash() const;
std::vector<uint8_t> getSequenceHash() const;
std::vector<uint8_t> getOutputsHash() const;
/// Encodes the transaction into the provided buffer.
void encode(bool witness, std::vector<uint8_t>& data) const;
/// Generates the signature hash for this transaction.
std::vector<uint8_t> getSignatureHash(const Script& scriptCode, size_t index, enum TWBitcoinSigHashType hashType,
uint64_t amount, TWBitcoinSignatureVersion version) const;
void serializeInput(size_t subindex, const Script&, size_t index, enum TWBitcoinSigHashType hashType,
std::vector<uint8_t>& data) const;
/// Converts to Protobuf model
Proto::Transaction proto() const;
private:
/// Generates the signature hash for Witness version 0 scripts.
std::vector<uint8_t> getSignatureHashWitnessV0(const Script& scriptCode, size_t index,
enum TWBitcoinSigHashType hashType, uint64_t amount) const;
/// Generates the signature hash for for scripts other than witness scripts.
std::vector<uint8_t> getSignatureHashBase(const Script& scriptCode, size_t index,
enum TWBitcoinSigHashType hashType) const;
};
} // namespace TW::Bitcoin
/// Wrapper for C interface.
struct TWBitcoinTransaction {
TW::Bitcoin::Transaction impl;
};