Repository navigation
Expand file tree
/
Copy pathArray.cpp
More file actions
83 lines (72 loc) · 2.59 KB
/
Copy pathArray.cpp
File metadata and controls
83 lines (72 loc) · 2.59 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
// 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 "Array.h"
#include <cassert>
using namespace TW::Ethereum::ABI;
int ParamArray::addParam(const std::shared_ptr<ParamBase>& param) {
assert(param != nullptr);
if (param == nullptr) { return -1; }
if (_params.getCount() >= 1 && param->getType() != getFirstType()) { return -2; } // do not add different types
return _params.addParam(param);
}
void ParamArray::addParams(const std::vector<std::shared_ptr<ParamBase>>& params) {
for (auto p: params) { addParam(p); }
}
std::string ParamArray::getFirstType() const {
if (_params.getCount() == 0) { return "empty"; }
return _params.getParamUnsafe(0)->getType();
}
void ParamArray::encode(Data& data) const {
size_t n = _params.getCount();
ABI::encode(uint256_t(n), data);
size_t headSize = 0;
for (auto i = 0; i < n; ++i) {
auto p = _params.getParamUnsafe(i);
if (p->isDynamic()) {
headSize += 32;
} else {
headSize += p->getSize();
}
}
size_t dynamicOffset = 0;
for (auto i = 0; i < n; ++i) {
auto p = _params.getParamUnsafe(i);
if (p->isDynamic()) {
ABI::encode(uint256_t(headSize + dynamicOffset), data);
dynamicOffset += p->getSize();
} else {
p->encode(data);
}
}
for (auto i = 0; i < n; ++i) {
auto p = _params.getParamUnsafe(i);
if (p->isDynamic()) {
p->encode(data);
}
}
}
bool ParamArray::decode(const Data& encoded, size_t& offset_inout) {
size_t origOffset = offset_inout;
// read length
uint256_t len256;
if (!ABI::decode(encoded, len256, offset_inout)) { return false; }
// check if length is in the size_t range
size_t len = static_cast<size_t>(len256);
if (len256 != static_cast<uint256_t>(len)) { return false; }
// read values
auto n = _params.getCount();
if (n != len) {
// Element number mismatch: the proto has to have exact same number of values as in the encoded form
// Note: this could be handles in a smarter way, and create more elements as needed
return false;
}
for (auto i = 0; i < n; ++i) {
if (!_params.getParamUnsafe(i)->decode(encoded, offset_inout)) { return false; }
}
// padding
offset_inout = origOffset + Util::paddedTo32(offset_inout - origOffset);
return true;
}