1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#pragma once
8

9
#include "ParamBase.h"
10
#include "ValueEncoder.h"
11

12
#include "../../Data.h"
13
#include "../../uint256.h"
14

15
#include <string>
16

17
namespace TW::Ethereum::ABI {
18

19

20 1
inline bool decode(const Data& encoded, uint256_t& decoded, size_t& offset_inout)
21
{
22 1
    decoded = 0u;
23 1
    if (encoded.empty() || (encoded.size() < (ValueEncoder::encodedIntSize + offset_inout))) {
24 1
        return false;
25
    }
26 1
    decoded = loadWithOffset(encoded, offset_inout);
27 1
    offset_inout += ValueEncoder::encodedIntSize;
28 1
    return true;
29
}
30

31
/// Generic parameter class for numeric types, like bool, uint32, int64, etc.  All are stored on 256 bits.
32
template<typename T> 
33 1
class ParamNumberType : public ParamBase
34
{
35
public:
36
    ParamNumberType() = default;
37 1
    ParamNumberType(T val) { _val = val; }
38 1
    void setVal(T val) { _val = val; }
39 1
    T getVal() const { return _val; }
40
    virtual std::string getType() const = 0;
41 1
    virtual size_t getSize() const { return ValueEncoder::encodedIntSize; }
42 1
    virtual bool isDynamic() const { return false; }
43 1
    virtual void encode(Data& data) const {
44
        // cast up
45 1
        ValueEncoder::encodeUInt256(static_cast<uint256_t>(_val), data);
46
    }
47 1
    static bool decodeNumber(const Data& encoded, T& decoded, size_t& offset_inout) {
48 1
        uint256_t val256;
49 1
        if (!ABI::decode(encoded, val256, offset_inout)) { return false; }
50
        // cast down
51 1
        decoded = static_cast<T>(val256);
52 1
        return true;
53
    }
54 1
    virtual bool decode(const Data& encoded, size_t& offset_inout) {
55 1
        return decodeNumber(encoded, _val, offset_inout);
56
    }
57
private:
58
    T _val;
59
};
60

61 1
class ParamUInt256 : public ParamNumberType<uint256_t>
62
{
63
public:
64 1
    ParamUInt256() : ParamNumberType<uint256_t>(uint256_t(0)) {}
65 1
    ParamUInt256(uint256_t val) : ParamNumberType<uint256_t>(val) {}
66 1
    virtual std::string getType() const { return "uint256"; }
67 1
    uint256_t getVal() const { return ParamNumberType<uint256_t>::getVal(); }
68
};
69

70 1
class ParamInt256 : public ParamNumberType<int256_t>
71
{
72
public:
73 1
    ParamInt256() : ParamNumberType<int256_t>(int256_t(0)) {}
74 1
    ParamInt256(int256_t val) : ParamNumberType<int256_t>(val) {}
75 1
    virtual std::string getType() const { return "int256"; }
76 1
    int256_t getVal() const { return ParamNumberType<int256_t>::getVal(); }
77
};
78

79 1
class ParamBool : public ParamNumberType<bool>
80
{
81
public:
82 1
    ParamBool() : ParamNumberType<bool>(false) {}
83 1
    ParamBool(bool val) : ParamNumberType<bool>(val) {}
84 1
    virtual std::string getType() const { return "bool"; }
85 1
    bool getVal() const { return ParamNumberType<bool>::getVal(); }
86
};
87

88 1
class ParamUInt8 : public ParamNumberType<uint8_t>
89
{
90
public:
91 1
    ParamUInt8() : ParamNumberType<uint8_t>(0) {}
92 1
    ParamUInt8(uint8_t val) : ParamNumberType<uint8_t>(val) {}
93 1
    virtual std::string getType() const { return "uint8"; }
94
};
95

96 1
class ParamInt8 : public ParamNumberType<int8_t>
97
{
98
public:
99 1
    ParamInt8() : ParamNumberType<int8_t>(0) {}
100 1
    ParamInt8(int8_t val) : ParamNumberType<int8_t>(val) {}
101 1
    virtual std::string getType() const { return "int8"; }
102
};
103

104 1
class ParamUInt16 : public ParamNumberType<uint16_t>
105
{
106
public:
107 1
    ParamUInt16() : ParamNumberType<uint16_t>(0) {}
108 1
    ParamUInt16(uint16_t val) : ParamNumberType<uint16_t>(val) {}
109 1
    virtual std::string getType() const { return "uint16"; }
110
};
111

112 1
class ParamInt16 : public ParamNumberType<int16_t>
113
{
114
public:
115 1
    ParamInt16() : ParamNumberType<int16_t>(0) {}
116 1
    ParamInt16(int16_t val) : ParamNumberType<int16_t>(val) {}
117 1
    virtual std::string getType() const { return "int16"; }
118
};
119

120 1
class ParamUInt32 : public ParamNumberType<uint32_t>
121
{
122
public:
123 1
    ParamUInt32() : ParamNumberType<uint32_t>(0) {}
124 1
    ParamUInt32(uint32_t val) : ParamNumberType<uint32_t>(val) {}
125 1
    virtual std::string getType() const { return "uint32"; }
126
};
127

128 1
class ParamInt32 : public ParamNumberType<int32_t>
129
{
130
public:
131 1
    ParamInt32() : ParamNumberType<int32_t>(0) {}
132 1
    ParamInt32(int32_t val) : ParamNumberType<int32_t>(val) {}
133 1
    virtual std::string getType() const { return "int32"; }
134
};
135

136 1
class ParamUInt64 : public ParamNumberType<uint64_t>
137
{
138
public:
139 1
    ParamUInt64() : ParamNumberType<uint64_t>(0) {}
140 1
    ParamUInt64(uint64_t val) : ParamNumberType<uint64_t>(val) {}
141 1
    virtual std::string getType() const { return "uint64"; }
142
};
143

144 1
class ParamInt64 : public ParamNumberType<int64_t>
145
{
146
public:
147 1
    ParamInt64() : ParamNumberType<int64_t>(0) {}
148 1
    ParamInt64(int64_t val) : ParamNumberType<int64_t>(val) {}
149 1
    virtual std::string getType() const { return "int64"; }
150
};
151

152
/// Generic parameter class for all other bit sizes, like UInt24, 40, 48, ... 248.
153
/// For predefined sizes (8, 16, 32, 64, 256) use the sepcial types like UInt32.
154
/// Stored on 256 bits.
155 1
class ParamUIntN : public ParamBase
156
{
157
public:
158
    const size_t bits;
159 1
    ParamUIntN(size_t bits_in) : bits(bits_in) { init(); }
160 1
    ParamUIntN(size_t bits_in, uint256_t val) : bits(bits_in) { init(); setVal(val); }
161
    void setVal(uint256_t val);
162 1
    uint256_t getVal() const { return _val; }
163 1
    virtual std::string getType() const { return "uint" + std::to_string(bits); }
164 1
    virtual size_t getSize() const { return ValueEncoder::encodedIntSize; }
165 1
    virtual bool isDynamic() const { return false; }
166 1
    virtual void encode(Data& data) const { ValueEncoder::encodeUInt256(_val, data); }
167 1
    static bool decodeNumber(const Data& encoded, uint256_t& decoded, size_t& offset_inout) {
168 1
        return ABI::decode(encoded, decoded, offset_inout);
169
    }
170
    virtual bool decode(const Data& encoded, size_t& offset_inout);
171
    static uint256_t maskForBits(size_t bits);
172

173
private:
174
    void init();
175
    uint256_t _val;
176
    uint256_t _mask;
177
};
178

179
/// Generic parameter class for all other bit sizes, like Int24, 40, 48, ... 248.
180
/// For predefined sizes (8, 16, 32, 64, 256) use the sepcial types like Int32.
181
/// Stored on 256 bits.
182 1
class ParamIntN : public ParamBase
183
{
184
public:
185
    const size_t bits;
186 1
    ParamIntN(size_t bits_in) : bits(bits_in) { init(); }
187 1
    ParamIntN(size_t bits_in, int256_t val) : bits(bits_in) { init(); setVal(val); }
188
    void setVal(int256_t val);
189 1
    int256_t getVal() const { return _val; }
190 1
    virtual std::string getType() const { return "int" + std::to_string(bits); }
191 1
    virtual size_t getSize() const { return ValueEncoder::encodedIntSize; }
192 1
    virtual bool isDynamic() const { return false; }
193 1
    virtual void encode(Data& data) const { ValueEncoder::encodeUInt256((uint256_t)_val, data); }
194
    static bool decodeNumber(const Data& encoded, int256_t& decoded, size_t& offset_inout);
195
    virtual bool decode(const Data& encoded, size_t& offset_inout);
196

197
private:
198
    void init();
199
    int256_t _val;
200
    uint256_t _mask;
201
};
202

203
} // namespace TW::Ethereum::ABI

Read our documentation on viewing source code .

Loading