-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathContractId.cc
More file actions
240 lines (210 loc) · 6.52 KB
/
ContractId.cc
File metadata and controls
240 lines (210 loc) · 6.52 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// SPDX-License-Identifier: Apache-2.0
#include "ContractId.h"
#include "Client.h"
#include "LedgerId.h"
#include "exceptions/IllegalStateException.h"
#include "impl/EntityIdHelper.h"
#include "impl/HexConverter.h"
#include "impl/Utilities.h"
#include <basic_types.pb.h>
#include <limits>
namespace Hiero
{
//-----
ContractId::ContractId(uint64_t num)
: mContractNum(num)
{
}
//-----
ContractId::ContractId(const EvmAddress& address)
: mEvmAddress(address)
{
}
//-----
ContractId::ContractId(uint64_t shard, uint64_t realm, uint64_t num, std::string_view checksum)
: mShardNum(shard)
, mRealmNum(realm)
, mContractNum(num)
, mChecksum(checksum)
{
}
//-----
ContractId::ContractId(uint64_t shard, uint64_t realm, const EvmAddress& address)
: mShardNum(shard)
, mRealmNum(realm)
, mEvmAddress(address)
{
}
//-----
bool ContractId::operator==(const ContractId& other) const
{
return (mShardNum == other.mShardNum) && (mRealmNum == other.mRealmNum) &&
((mContractNum && other.mContractNum && *mContractNum == *other.mContractNum) ||
(mEvmAddress && other.mEvmAddress && mEvmAddress->toBytes() == other.mEvmAddress->toBytes()) ||
(!mContractNum && !other.mContractNum && !mEvmAddress && !other.mEvmAddress));
}
//-----
ContractId ContractId::fromString(std::string_view id)
{
// Get the shard and realm numbers.
const uint64_t shard = internal::EntityIdHelper::getShardNum(id);
const uint64_t realm = internal::EntityIdHelper::getRealmNum(id);
// Determine what the entity ID number is. First try to see if it's just a contract number. Get the entity number
// string before the try-block to verify the input ID isn't malformed.
const std::string_view entityNum = internal::EntityIdHelper::getEntityNumStr(id);
const std::string_view checksum = internal::EntityIdHelper::getChecksum(id);
try
{
return ContractId(shard, realm, internal::EntityIdHelper::getNum(entityNum), checksum);
}
catch (const std::invalid_argument&)
{
// If the entity number isn't a contract number, it's an EvmAddress. An EvmAddress cannot have checksums, so verify
// that first.
if (!checksum.empty())
{
throw std::invalid_argument("Contract IDs with EVM addresses can't have checksums");
}
// Try the entity number as an EVM address.
try
{
return ContractId(shard, realm, EvmAddress::fromString(entityNum));
}
catch (const std::invalid_argument&)
{
// If not an EVM address, the entity ID cannot be realized.
throw std::invalid_argument(std::string("Contract number/EVM address cannot be realized from ") +
entityNum.data());
}
}
}
//-----
ContractId ContractId::fromEvmAddress(std::string_view evmAddress, uint64_t shard, uint64_t realm)
{
return fromEvmAddress(EvmAddress::fromString(evmAddress), shard, realm);
}
//-----
ContractId ContractId::fromEvmAddress(const EvmAddress& evmAddress, uint64_t shard, uint64_t realm)
{
return ContractId(shard, realm, evmAddress);
}
//-----
ContractId ContractId::fromSolidityAddress(std::string_view address)
{
return fromEvmAddress(address);
}
//-----
ContractId ContractId::fromProtobuf(const proto::ContractID& proto)
{
ContractId contractId;
contractId.mShardNum = static_cast<uint64_t>(proto.shardnum());
contractId.mRealmNum = static_cast<uint64_t>(proto.realmnum());
if (proto.contract_case() == proto::ContractID::ContractCase::kContractNum)
{
contractId.mContractNum = static_cast<uint64_t>(proto.contractnum());
}
else if (proto.contract_case() == proto::ContractID::ContractCase::kEvmAddress)
{
contractId.mEvmAddress = EvmAddress::fromBytes(internal::Utilities::stringToByteVector(proto.evm_address()));
}
return contractId;
}
//-----
ContractId ContractId::fromBytes(const std::vector<std::byte>& bytes)
{
proto::ContractID proto;
proto.ParseFromArray(bytes.data(), static_cast<int>(bytes.size()));
return fromProtobuf(proto);
}
//-----
std::unique_ptr<Key> ContractId::clone() const
{
return std::make_unique<ContractId>(*this);
}
//-----
std::unique_ptr<proto::Key> ContractId::toProtobufKey() const
{
auto proto = std::make_unique<proto::Key>();
proto->set_allocated_contractid(toProtobuf().release());
return proto;
}
//-----
std::vector<std::byte> ContractId::toBytes() const
{
return internal::Utilities::stringToByteVector(toProtobuf()->SerializeAsString());
}
//-----
void ContractId::validateChecksum(const Client& client) const
{
if (mContractNum.has_value() && !mChecksum.empty())
{
internal::EntityIdHelper::validate(mShardNum, mRealmNum, mContractNum.value(), client, mChecksum);
}
}
//-----
std::unique_ptr<proto::ContractID> ContractId::toProtobuf() const
{
auto proto = std::make_unique<proto::ContractID>();
proto->set_shardnum(static_cast<int64_t>(mShardNum));
proto->set_realmnum(static_cast<int64_t>(mRealmNum));
if (mContractNum.has_value())
{
proto->set_contractnum(static_cast<int64_t>(mContractNum.value()));
}
else if (mEvmAddress.has_value())
{
proto->set_evm_address(internal::Utilities::byteVectorToString(mEvmAddress->toBytes()));
}
return proto;
}
//-----
std::string ContractId::toSolidityAddress() const
{
if (mEvmAddress.has_value())
{
return internal::HexConverter::bytesToHex(mEvmAddress->toBytes());
}
else if (mContractNum.has_value())
{
return internal::EntityIdHelper::toSolidityAddress(mShardNum, mRealmNum, mContractNum.value());
}
else
{
throw IllegalStateException(
"ContractId must contain a contract number of EVM address to generate a Solidity address");
}
}
//-----
std::string ContractId::toString() const
{
std::string str = std::to_string(mShardNum) + '.' + std::to_string(mRealmNum) + '.';
if (mContractNum.has_value())
{
return str + std::to_string(mContractNum.value());
}
else if (mEvmAddress.has_value())
{
return str + mEvmAddress->toString();
}
else
{
// Uninitialized case
return str + '0';
}
}
//-----
std::string ContractId::toStringWithChecksum(const Client& client) const
{
// Checksums are only valid for contracts using a contract number.
if (!mContractNum.has_value())
{
throw IllegalStateException("Checksums can only be generated for ContractIds that contain a contract number");
}
if (mChecksum.empty())
{
mChecksum = internal::EntityIdHelper::checksum(
internal::EntityIdHelper::toString(mShardNum, mRealmNum, mContractNum.value()), client.getLedgerId());
}
return internal::EntityIdHelper::toString(mShardNum, mRealmNum, mContractNum.value(), mChecksum);
}
} // namespace Hiero