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 "Serialization.h"
8

9
#include "../BinaryCoding.h"
10
#include "../PrivateKey.h"
11

12
using namespace TW;
13
using namespace TW::NEAR;
14
using namespace TW::NEAR::Proto;
15

16

17 1
static void writeU8(Data& data, uint8_t number) {
18 1
    data.push_back(number);
19
}
20

21 1
static void writeU32(Data& data, uint32_t number) {
22 1
    encode32LE(number, data);
23
}
24

25 1
static void writeU64(Data& data, uint64_t number) {
26 1
    encode64LE(number, data);
27
}
28

29 1
static void writeU128(Data& data, const std::string& numberData) {
30 1
    data.insert(std::end(data), std::begin(numberData), std::end(numberData));
31
}
32

33 1
template <class T> static void writeRawBuffer(Data& data, const T& buf) {
34 1
    data.insert(std::end(data), std::begin(buf), std::end(buf));
35
}
36

37 1
static void writeString(Data& data, const std::string& str) {
38 1
    writeU32(data, static_cast<uint32_t>(str.length()));
39 1
    writeRawBuffer(data, str);
40
}
41

42 1
static void writePublicKey(Data& data, const Proto::PublicKey& publicKey) {
43 1
    writeU8(data, static_cast<uint8_t>(publicKey.key_type()));
44 1
    const auto& keyData = publicKey.data();
45 1
    writeRawBuffer(data, keyData);
46
}
47

48 1
static void writeTransfer(Data& data, const Proto::Transfer& transfer) {
49 1
    writeU128(data, transfer.deposit());
50
}
51

52 1
static void writeAction(Data& data, const Proto::Action& action) {
53 1
    writeU8(data, action.payload_case() - Proto::Action::kCreateAccount);
54 1
    switch (action.payload_case()) {
55
        case Proto::Action::kTransfer:
56 1
            writeTransfer(data, action.transfer());
57 1
            return;
58
        default:
59 0
            return;
60
    }
61
}
62

63 1
Data TW::NEAR::transactionData(const Proto::SigningInput& input) {
64 1
    Data data;
65 1
    writeString(data, input.signer_id());
66 1
    auto key = PrivateKey(input.private_key());
67 1
    auto public_key = key.getPublicKey(TWPublicKeyTypeED25519);
68 1
    auto public_key_proto = Proto::PublicKey();
69 1
    public_key_proto.set_data(public_key.bytes.data(), public_key.bytes.size());
70 1
    writePublicKey(data, public_key_proto);
71 1
    writeU64(data, input.nonce());
72 1
    writeString(data, input.receiver_id());
73 1
    const auto& block_hash = input.block_hash();
74 1
    writeRawBuffer(data, block_hash);
75 1
    writeU32(data, input.actions_size());
76 1
    for (const auto& action : input.actions()) {
77 1
        writeAction(data, action);
78
    }
79 1
    return data;
80
}
81

82 1
Data TW::NEAR::signedTransactionData(const Data& transactionData, const Data& signatureData) {
83 1
    Data data;
84 1
    writeRawBuffer(data, transactionData);
85 1
    writeU8(data, 0);
86 1
    writeRawBuffer(data, signatureData);
87 1
    return data;
88
}

Read our documentation on viewing source code .

Loading