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
#include "Function.h"
8

9
#include "../../Data.h"
10

11
#include <string>
12

13
using namespace TW;
14
using namespace TW::Ethereum::ABI;
15

16 1
Data Function::getSignature() const {
17 1
    auto typ = getType();
18 1
    auto hash = Hash::keccak256(Data(typ.begin(), typ.end()));
19 1
    auto signature = Data(hash.begin(), hash.begin() + 4);
20 1
    return signature;
21
}
22

23 1
void Function::encode(Data& data) const {
24 1
    Data signature = getSignature();
25 1
    append(data, signature);
26 1
    _inParams.encode(data);
27
}
28

29 1
bool Function::decodeOutput(const Data& encoded, size_t& offset_inout) {
30
    // read parameter values
31 1
    if (!_outParams.decode(encoded, offset_inout)) { return false; }
32 1
    return true;
33
}
34

35 1
bool Function::decodeInput(const Data& encoded, size_t& offset_inout) {
36
    // read 4-byte hash
37 1
    auto p = ParamByteArrayFix(4);
38 1
    if (!p.decode(encoded, offset_inout)) { return false; }
39 1
    std::vector<byte> hash = p.getVal();
40
    // adjust offset; hash is NOT padded to 32 bytes
41 1
    offset_inout = offset_inout - 32 + 4;
42
    // verify hash
43 1
    Data hashExpect = getSignature();
44 1
    if (hash != hashExpect) {
45
        // invalid hash
46 0
        return false;
47
    }
48
    // read parameters
49 1
    if (!_inParams.decode(encoded, offset_inout)) { return false; }
50 1
    return true;
51
}

Read our documentation on viewing source code .

Loading