-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsignedtransaction.cpp
More file actions
149 lines (120 loc) · 4.29 KB
/
signedtransaction.cpp
File metadata and controls
149 lines (120 loc) · 4.29 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "signedtransaction.h"
#include "eosbytewriter.h"
#include "../Crypto/libbase58.h"
#include "../utility/utils.h"
#include "../ec/sha256.h"
extern "C"
{
#include "../Crypto/sha2.h"
#include "../Crypto/uECC.h"
#include "../Crypto/rmd160.h"
}
SignedTransaction::SignedTransaction()
{
}
QJsonValue SignedTransaction::toJson() const
{
QJsonObject obj = Transaction::toJson().toObject();
QJsonArray signaturesArr, ctxFreeDataArr;
for (const auto& s : signatures) {
signaturesArr.append(QJsonValue(s.c_str()));
}
for (const auto& d : context_free_data) {
ctxFreeDataArr.append(QJsonValue(d.c_str()));
}
obj.insert("signatures", signaturesArr);
obj.insert("context_free_data", ctxFreeDataArr);
return QJsonValue(obj);
}
void SignedTransaction::fromJson(const QJsonValue &value)
{
QJsonObject obj = value.toObject();
if (obj.isEmpty()) {
return;
}
QJsonArray signaturesArr = obj.value("signatures").toArray();
if (!signaturesArr.isEmpty()) {
for (int i = 0; i < signaturesArr.size(); ++i) {
signatures.push_back(signaturesArr.at(i).toString().toStdString());
}
}
QJsonArray ctxFreeDataArr = obj.value("context_free_data").toArray();
if (!ctxFreeDataArr.isEmpty()) {
for (int i = 0; i < ctxFreeDataArr.size(); ++i) {
context_free_data.push_back(ctxFreeDataArr.at(i).toString().toStdString());
}
}
Transaction::fromJson(value);
}
std::vector<std::string> SignedTransaction::getSignatures() const
{
return signatures;
}
std::vector<std::string> SignedTransaction::getCtxFreeData() const
{
return context_free_data;
}
void SignedTransaction::setSignatures(const std::vector<std::string> &signatures)
{
this->signatures = signatures;
}
void SignedTransaction::sign(const std::vector<unsigned char> &pri_key, const TypeChainId &cid)
{
std::vector<unsigned char> packedBytes = getDigestForSignature(cid);
uint8_t packedSha256[SHA256_DIGEST_LENGTH];
sha256_Raw(packedBytes.data(), packedBytes.size(), packedSha256);
uint8_t signature[uECC_BYTES * 2] = { 0 };
int recId = uECC_sign_forbc(pri_key.data(), packedSha256, signature);
if (recId == -1) {
// could not find recid, data probably already signed by the key before?
return;
} else {
unsigned char bin[65+4] = { 0 };
unsigned char *rmdhash = nullptr;
int binlen = 65+4;
int headerBytes = recId + 27 + 4;
bin[0] = (unsigned char)headerBytes;
memcpy(bin + 1, signature, uECC_BYTES * 2);
unsigned char temp[67] = { 0 };
memcpy(temp, bin, 65);
memcpy(temp + 65, "K1", 2);
rmdhash = RMD(temp, 67);
memcpy(bin + 1 + uECC_BYTES * 2, rmdhash, 4);
char sigbin[100] = { 0 };
size_t sigbinlen = 100;
b58enc(sigbin, &sigbinlen, bin, binlen);
std::string sig = "SIG_K1_";
sig += sigbin;
signatures.push_back(sig);
}
}
bool SignedTransaction::signTest(const std::vector<unsigned char> &signatureHex, const std::vector<unsigned char> &pubKey, const TypeChainId &cid)
{
std::vector<unsigned char> packedBytes = getDigestForSignature(cid);
uint8_t packedSha256[SHA256_DIGEST_LENGTH];
sha256_Raw(packedBytes.data(), packedBytes.size(), packedSha256);
std::vector<unsigned char> signature = Utils::convertHexStrToBytes(signatureHex);
signature.erase(signature.begin()); //remove headerBytes
return checkSignature(pubKey.data(), packedSha256, signature.data()) != -1;
}
std::vector<unsigned char> SignedTransaction::getDigestForSignature(const TypeChainId &cid)
{
EOSByteWriter writer(255);
writer.putBytes(cid.chainId(), cid.size());
serialize(&writer);
std::vector<unsigned char> free_data;
for (auto str = context_free_data.cbegin(); str != context_free_data.cend(); ++str) {
for (auto it = (*str).cbegin(); it != (*str).cend(); ++it) {
free_data.push_back(*it);
}
}
if (free_data.size()) {
std::string str(free_data.begin(), free_data.end());
sha256 h = sha256::hash(str);
writer.putBytes((const unsigned char *)h.data(), h.data_size());
} else {
sha256 h;
writer.putBytes((const unsigned char *)h.data(), h.data_size());
}
return writer.toBytes();
}