6#ifndef BFI_BITCOIN_TRANSACTION_HPP
7#define BFI_BITCOIN_TRANSACTION_HPP
9#include <veriblock/bfi/bitcoin/serialize.hpp>
10#include <veriblock/pop/uint.hpp>
16static const int SERIALIZE_TRANSACTION_NO_WITNESS = 0x40000000;
18using Script = std::vector<uint8_t>;
19using ScriptWitness = std::vector<std::vector<uint8_t>>;
21using Amount = int64_t;
29 ADD_SERIALIZE_METHODS;
31 template <
typename Stream,
typename Operation>
32 inline void SerializationOp(Stream& s, Operation ser_action) {
38 return a.hash == b.hash && a.n == b.n;
56 ADD_SERIALIZE_METHODS;
58 template <
typename Stream,
typename Operation>
59 inline void SerializationOp(Stream& s, Operation ser_action) {
65 friend bool operator==(
const TxIn& a,
const TxIn& b) {
66 return a.prevout == b.prevout && a.scriptSig == b.scriptSig &&
70 friend bool operator!=(
const TxIn& a,
const TxIn& b) {
return !(a == b); }
80 ADD_SERIALIZE_METHODS;
82 template <
typename Stream,
typename Operation>
83 inline void SerializationOp(Stream& s, Operation ser_action) {
85 READWRITE(scriptPubKey);
88 friend bool operator==(
const TxOut& a,
const TxOut& b) {
89 return a.nValue == b.nValue && a.scriptPubKey == b.scriptPubKey;
92 friend bool operator!=(
const TxOut& a,
const TxOut& b) {
return !(a == b); }
104 std::vector<TxIn> vin;
105 std::vector<TxOut> vout;
109 template <
typename Stream>
110 inline void Serialize(Stream& s)
const {
111 SerializeTransaction(*
this, s);
114 template <
typename Stream>
115 inline void Unserialize(Stream& s) {
116 UnserializeTransaction(*
this, s);
120 return a.vin == b.vin && a.vout == b.vout && a.nVersion == b.nVersion &&
121 a.nLockTime == b.nLockTime;
128 bool HasWitness()
const {
129 for (
size_t i = 0; i < vin.size(); i++) {
130 if (!vin[i].scriptWitness.empty()) {
138template <
typename Stream>
139inline void UnserializeTransaction(
Transaction& tx, Stream& s) {
140 const bool fAllowWitness =
141 (s.getVersion() & SERIALIZE_TRANSACTION_NO_WITNESS) == 0;
143 Unserialize(s, tx.nVersion);
144 unsigned char flags = 0;
149 Unserialize(s, tx.vin);
150 if (tx.vin.size() == 0 && fAllowWitness) {
152 Unserialize(s, flags);
154 Unserialize(s, tx.vin);
155 Unserialize(s, tx.vout);
159 Unserialize(s, tx.vout);
161 if (((flags & 1) != 0) && fAllowWitness) {
164 for (
size_t i = 0; i < tx.vin.size(); i++) {
165 Unserialize(s, tx.vin[i].scriptWitness);
167 if (!tx.HasWitness()) {
169 throw std::ios_base::failure(
"Superfluous witness record");
174 throw std::ios_base::failure(
"Unknown transaction optional data");
176 Unserialize(s, tx.nLockTime);
179template <
typename Stream>
180inline void SerializeTransaction(
const Transaction& tx, Stream& s) {
181 const bool fAllowWitness =
182 (s.getVersion() & SERIALIZE_TRANSACTION_NO_WITNESS) == 0;
184 Serialize(s, tx.nVersion);
185 unsigned char flags = 0;
189 if (tx.HasWitness()) {
195 std::vector<TxIn> vinDummy;
196 Serialize(s, vinDummy);
199 Serialize(s, tx.vin);
200 Serialize(s, tx.vout);
202 for (
size_t i = 0; i < tx.vin.size(); i++) {
203 Serialize(s, tx.vin[i].scriptWitness);
206 Serialize(s, tx.nLockTime);
An outpoint - a combination of a transaction hash and an index n into its vout.
The basic transaction that is broadcasted on the network and contained in blocks.
An input of a transaction.
ScriptWitness scriptWitness
Only serialized through CTransaction.
An output of a transaction.