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 <Data.h>
10
#include <uint256.h>
11

12
#include <string>
13

14
namespace TW::Ethereum::ABI {
15

16
/// Encode a type according to EIP712, into 32 bytes.
17
/// Result is appended to the provided Data object.
18
/// Values are padded by 0 on the left -- unless otherwise mentioned.
19
/// See: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md
20
class ValueEncoder {
21
public:
22
    static const int encodedIntSize = 32;
23

24
    static void encodeBool(bool value, Data& inout);
25
    static void encodeInt32(int32_t value, Data& inout);
26
    static void encodeUInt32(uint32_t value, Data& inout);
27
    static void encodeInt256(const int256_t& value, Data& inout);
28
    static void encodeUInt256(const uint256_t& value, Data& inout);
29
    /// Encode the 20 bytes of an address
30
    static void encodeAddress(const Data& value, Data& inout);
31
    /// Encode a string by encoding its hash
32
    static void encodeString(const std::string& value, Data& inout);
33
    /// Encode a number of bytes, up to 32 bytes, padded on the right.  Longer arrays are truncated.
34
    static void encodeBytes(const Data& value, Data& inout);
35
    /// Encode a dynamic number of bytes by encoding its hash
36
    static void encodeBytesDyn(const Data& value, Data& inout);
37

38
    // uint256_t -> int256_t conversion, non trivial
39
    static int256_t int256FromUint256(uint256_t x);
40
    static uint256_t uint256FromInt256(int256_t x);
41

42
    /// Compute size when padded to 32-byte boundary; round up to multiple of 32.
43 1
    static inline size_t paddedTo32(size_t len) { return ((len + 31) / 32) * 32; }
44

45
    /// Compute padding needed to next 32-byte boundary.
46 1
    static inline size_t padNeeded32(size_t len) { return ((len + 31) / 32) * 32 - len; }
47
};
48
} // namespace TW::Ethereum::ABI

Read our documentation on viewing source code .

Loading