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
#include <cstring>
15

16
namespace TW::Bitcoin {
17

18
/// Bitcoin transaction out-point reference.
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
    /// Initializes an out-point reference with a hash and an index.
28
    template <typename T>
29 1
    OutPoint(const T& h, uint32_t index) {
30 1
        std::copy(std::begin(h), std::end(h), hash.begin());
31 1
        this->index = index;
32
    }
33

34
    /// Initializes an out-point from a Protobuf out-point.
35 1
    OutPoint(const Proto::OutPoint& other) {
36 1
        assert(other.hash().size() == 32);
37 1
        std::copy(other.hash().begin(), other.hash().end(), hash.begin());
38 1
        index = other.index();
39
    }
40

41
    /// Encodes the out-point into the provided buffer.
42
    void encode(std::vector<uint8_t>& data) const;
43

44
    friend bool operator<(const OutPoint& a, const OutPoint& b) {
45
        int cmp = std::memcmp(a.hash.data(), b.hash.data(), 32);
46
        return cmp < 0 || (cmp == 0 && a.index < b.index);
47
    }
48

49
    friend bool operator==(const OutPoint& a, const OutPoint& b) {
50
        int cmp = std::memcmp(a.hash.data(), b.hash.data(), 32);
51
        return (cmp == 0 && a.index == b.index);
52
    }
53

54
    friend bool operator!=(const OutPoint& a, const OutPoint& b) { return !(a == b); }
55
};
56

57
} // namespace TW::Bitcoin

Read our documentation on viewing source code .

Loading