Repository navigation
Expand file tree
/
Copy pathForging.cpp
More file actions
135 lines (118 loc) · 4.55 KB
/
Copy pathForging.cpp
File metadata and controls
135 lines (118 loc) · 4.55 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
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#include "Address.h"
#include "BinaryCoding.h"
#include "../Base58.h"
#include "../Data.h"
#include "../HexCoding.h"
#include "../proto/Tezos.pb.h"
#include <sstream>
using namespace TW;
using namespace TW::Tezos;
using namespace TW::Tezos::Proto;
// Forge the given boolean into a hex encoded string.
Data forgeBool(bool input) {
unsigned char result = input ? 0xff : 0x00;
return Data{result};
}
// Forge the given public key hash into a hex encoded string.
// Note: This function supports tz1, tz2 and tz3 addresses.
Data forgePublicKeyHash(const std::string& publicKeyHash) {
Data forged = Data();
// Adjust prefix based on tz1, tz2 or tz3.
switch ((char)publicKeyHash[2]) {
case '1':
forged.push_back(0x00);
break;
case '2':
forged.push_back(0x01);
break;
case '3':
forged.push_back(0x02);
break;
default:
throw std::invalid_argument("Invalid Prefix");
}
const auto decoded = Base58::bitcoin.decodeCheck(publicKeyHash);
const auto prefixSize = 3;
forged.insert(forged.end(), decoded.begin() + prefixSize, decoded.end());
return forged;
}
// Forge the given public key into a hex encoded string.
Data forgePublicKey(PublicKey publicKey) {
std::array<uint8_t, 4> prefix = {13, 15, 37, 217};
auto data = Data(prefix.begin(), prefix.end());
auto bytes = Data(publicKey.bytes.begin(), publicKey.bytes.end());
append(data, bytes);
auto pk = Base58::bitcoin.encodeCheck(data);
auto decoded = "00" + base58ToHex(pk, 4, prefix.data());
return parse_hex(decoded);
}
// Forge the given zarith hash into a hex encoded string.
Data forgeZarith(uint64_t input) {
Data forged = Data();
while (input >= 0x80) {
forged.push_back(static_cast<byte>((input & 0xff) | 0x80));
input >>= 7;
}
forged.push_back(static_cast<byte>(input));
return forged;
}
// Forge the given operation.
Data forgeOperation(const Operation& operation) {
auto forged = Data();
auto source = Address(operation.source());
auto forgedSource = source.forge();
auto forgedFee = forgeZarith(operation.fee());
auto forgedCounter = forgeZarith(operation.counter());
auto forgedGasLimit = forgeZarith(operation.gas_limit());
auto forgedStorageLimit = forgeZarith(operation.storage_limit());
if (operation.kind() == Operation_OperationKind_REVEAL) {
auto publicKey = PublicKey(data(operation.reveal_operation_data().public_key()), TWPublicKeyTypeED25519);
auto forgedPublicKey = forgePublicKey(publicKey);
forged.push_back(Operation_OperationKind_REVEAL);
append(forged, forgedSource);
append(forged, forgedFee);
append(forged, forgedCounter);
append(forged, forgedGasLimit);
append(forged, forgedStorageLimit);
append(forged, forgedPublicKey);
return forged;
}
if (operation.kind() == Operation_OperationKind_DELEGATION) {
auto delegate = operation.delegation_operation_data().delegate();
forged.push_back(Operation_OperationKind_DELEGATION);
append(forged, forgedSource);
append(forged, forgedFee);
append(forged, forgedCounter);
append(forged, forgedGasLimit);
append(forged, forgedStorageLimit);
if (!delegate.empty()) {
auto forgedPublicKeyHash = forgePublicKeyHash(delegate);
append(forged, forgeBool(true));
append(forged, forgedPublicKeyHash);
} else {
append(forged, forgeBool(false));
}
return forged;
}
if (operation.kind() == Operation_OperationKind_TRANSACTION) {
auto forgedAmount = forgeZarith(operation.transaction_operation_data().amount());
auto forgedDestination = Address(operation.transaction_operation_data().destination()).forge();
forged.push_back(Operation_OperationKind_TRANSACTION);
append(forged, forgedSource);
append(forged, forgedFee);
append(forged, forgedCounter);
append(forged, forgedGasLimit);
append(forged, forgedStorageLimit);
append(forged, forgedAmount);
append(forged, forgeBool(false));
append(forged, forgedDestination);
append(forged, forgeBool(false));
return forged;
}
throw std::invalid_argument("Invalid operation kind");
}