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
#include "../../Data.h"
12

13
namespace TW::Ethereum::ABI {
14

15
/// Dynamic array of bytes "bytes"
16 1
class ParamByteArray: public ParamCollection
17
{
18
private:
19
    Data _bytes;
20
public:
21 1
    ParamByteArray() = default;
22 1
    ParamByteArray(const Data& val) : ParamCollection() { setVal(val); }
23 1
    void setVal(const Data& val) { _bytes = val; }
24 1
    const Data& getVal() const { return _bytes; }
25 1
    virtual std::string getType() const { return "bytes"; };
26 1
    virtual size_t getSize() const { return 32 + ValueEncoder::paddedTo32(_bytes.size()); }
27 1
    virtual bool isDynamic() const { return true; }
28 1
    virtual size_t getCount() const { return _bytes.size(); }
29
    static void encodeBytes(const Data& bytes, Data& data);
30 1
    virtual void encode(Data& data) const { encodeBytes(_bytes, data); }
31
    static bool decodeBytes(const Data& encoded, Data& decoded, size_t& offset_inout);
32 1
    virtual bool decode(const Data& encoded, size_t& offset_inout) {
33 1
        return decodeBytes(encoded, _bytes, offset_inout);
34
    }
35
};
36

37
/// Fixed-size array of bytes, "bytes<N>"
38 1
class ParamByteArrayFix: public ParamCollection
39
{
40
private:
41
    size_t _n;
42
    Data _bytes;
43
public:
44 1
    ParamByteArrayFix(size_t n): ParamCollection(), _n(n), _bytes(Data(_n)) {}
45 1
    ParamByteArrayFix(size_t n, const Data& val): ParamCollection(), _n(n), _bytes(Data(_n)) { setVal(val); }
46 1
    void setVal(const Data& val) { _bytes = val; }
47 1
    const std::vector<uint8_t>& getVal() const { return _bytes; }
48 1
    virtual std::string getType() const { return "bytes" + std::to_string(_n); };
49 1
    virtual size_t getSize() const { return ValueEncoder::paddedTo32(_bytes.size()); }
50 1
    virtual bool isDynamic() const { return false; }
51 1
    virtual size_t getCount() const { return _bytes.size(); }
52
    virtual void encode(Data& data) const;
53
    static bool decodeBytesFix(const Data& encoded, size_t n, Data& decoded, size_t& offset_inout);
54 1
    virtual bool decode(const Data& encoded, size_t& offset_inout) {
55 1
        return decodeBytesFix(encoded, _n, _bytes, offset_inout);
56
    }
57
};
58

59
/// Var-length string parameter
60 1
class ParamString: public ParamCollection
61
{
62
private:
63
    std::string _str;
64
public:
65 1
    ParamString() = default;
66 1
    ParamString(std::string val): ParamCollection() { setVal(val); }
67 1
    void setVal(const std::string& val) { _str = val; }
68 1
    const std::string& getVal() const { return _str; }
69 1
    virtual std::string getType() const { return "string"; };
70 1
    virtual size_t getSize() const { return 32 + ValueEncoder::paddedTo32(_str.size()); }
71 1
    virtual bool isDynamic() const { return true; }
72 0
    virtual size_t getCount() const { return _str.size(); }
73
    static void encodeString(const std::string& decoded, Data& data);
74 1
    virtual void encode(Data& data) const { ParamString::encodeString(_str, data); }
75
    static bool decodeString(const Data& encoded, std::string& decoded, size_t& offset_inout);
76 1
    virtual bool decode(const Data& encoded, size_t& offset_inout) {
77 1
        return decodeString(encoded, _str, offset_inout);
78
    }
79
};
80

81
} // namespace TW::Ethereum::ABI

Read our documentation on viewing source code .

Loading