-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcfdcore_coin.cpp
114 lines (93 loc) · 3.08 KB
/
cfdcore_coin.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2019 CryptoGarage
/**
* @file cfdcore_coin.cpp
*
* @brief Classes related to Coin(UTXO)
*/
#include "cfdcore/cfdcore_coin.h"
#include <string>
#include <vector>
#include "cfdcore/cfdcore_exception.h"
#include "cfdcore/cfdcore_logger.h"
#include "cfdcore/cfdcore_util.h"
namespace cfd {
namespace core {
using logger::warn;
// -----------------------------------------------------------------------------
// Txid
// -----------------------------------------------------------------------------
Txid::Txid() : data_(ByteData()) {
// do nothing
}
Txid::Txid(const std::string& hex) : data_() {
const std::vector<uint8_t>& data = StringUtil::StringToByte(hex);
std::vector<uint8_t> reverse_buffer(data.crbegin(), data.crend());
if (reverse_buffer.size() != kByteData256Length) {
warn(CFD_LOG_SOURCE, "Txid size Invalid. size={}.", reverse_buffer.size());
throw CfdException(
CfdError::kCfdIllegalArgumentError, "Txid size Invalid.");
}
data_ = ByteData(reverse_buffer);
}
Txid::Txid(const ByteData256& data) : data_(ByteData(data.GetBytes())) {
// do nothing
}
Txid::Txid(const Txid& object) { data_ = object.data_; }
Txid& Txid::operator=(const Txid& object) {
if (this != &object) {
data_ = object.data_;
}
return *this;
}
const std::string Txid::GetHex() const {
const std::vector<uint8_t>& data = data_.GetBytes();
std::vector<uint8_t> reverse_buffer(data.crbegin(), data.crend());
return StringUtil::ByteToString(reverse_buffer);
}
const ByteData Txid::GetData() const { return data_; }
bool Txid::Equals(const Txid& txid) const {
if (data_.Equals(txid.data_)) {
return true;
}
return false;
}
bool Txid::IsValid() const {
return (data_.GetDataSize() == kByteData256Length);
}
// -----------------------------------------------------------------------------
// BlockHash
// -----------------------------------------------------------------------------
BlockHash::BlockHash(const std::string& hex) : data_() {
const std::vector<uint8_t>& data = StringUtil::StringToByte(hex);
std::vector<uint8_t> reverse_buffer(data.crbegin(), data.crend());
if (reverse_buffer.size() != kByteData256Length) {
warn(
CFD_LOG_SOURCE, "BlockHash size Invalid. size={}.",
reverse_buffer.size());
throw CfdException(
CfdError::kCfdIllegalArgumentError, "BlockHash size Invalid.");
}
data_ = ByteData(reverse_buffer);
}
BlockHash::BlockHash(const ByteData256& data)
: data_(ByteData(data.GetBytes())) {
// do nothing
}
BlockHash::BlockHash(const BlockHash& object) { data_ = object.data_; }
BlockHash& BlockHash::operator=(const BlockHash& object) {
if (this != &object) {
data_ = object.data_;
}
return *this;
}
const std::string BlockHash::GetHex() const {
const std::vector<uint8_t>& data = data_.GetBytes();
std::vector<uint8_t> reverse_buffer(data.crbegin(), data.crend());
return StringUtil::ByteToString(reverse_buffer);
}
const ByteData BlockHash::GetData() const { return data_; }
bool BlockHash::IsValid() const {
return (data_.GetDataSize() == kByteData256Length);
}
} // namespace core
} // namespace cfd