1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#pragma once
8

9
#include "../Data.h"
10
#include "../proto/Bitcoin.pb.h"
11

12
#include <algorithm>
13
#include <array>
14

15
namespace TW::Decred {
16

17
/// OutPoint defines a Decred data type that is used to track previous
18
/// transaction outputs.
19
class OutPoint {
20
  public:
21
    /// The hash of the referenced transaction.
22
    std::array<byte, 32> hash;
23

24
    /// The index of the specific output in the transaction.
25
    uint32_t index;
26

27
    /// Which tree the output being spent is in. This is required because there
28
    /// is more than one tree used to locate transactions in a block.
29
    int8_t tree;
30

31
    OutPoint() = default;
32

33
    /// Initializes an out-point reference.
34 1
    OutPoint(std::array<byte, 32>&& hash, uint32_t index, int8_t tree)
35 1
        : hash(hash), index(index), tree(tree) {}
36

37
    /// Initializes an out-point reference.
38 1
    OutPoint(const Data& hash, uint32_t index, int8_t tree) : hash(), index(index), tree(tree) {
39 1
        std::copy(hash.begin(), hash.end(), this->hash.begin());
40
    }
41

42
    /// Initializes an out-point from a Protobuf out-point.
43 1
    OutPoint(const Bitcoin::Proto::OutPoint& other) {
44 1
        std::copy(other.hash().begin(), other.hash().begin() + hash.size(), hash.begin());
45 1
        index = other.index();
46 1
        tree = 0;
47
    }
48

49
    /// Encodes the out-point into the provided buffer.
50
    void encode(Data& data) const;
51

52
    friend bool operator<(const OutPoint& a, const OutPoint& b) {
53
        int cmp = std::memcmp(a.hash.data(), b.hash.data(), 32);
54
        return cmp < 0 || (cmp == 0 && a.index < b.index);
55
    }
56

57
    friend bool operator==(const OutPoint& a, const OutPoint& b) {
58
        int cmp = std::memcmp(a.hash.data(), b.hash.data(), 32);
59
        return (cmp == 0 && a.index == b.index);
60
    }
61

62
    friend bool operator!=(const OutPoint& a, const OutPoint& b) { return !(a == b); }
63
};
64

65
} // namespace TW::Decred

Read our documentation on viewing source code .

Loading