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 "../uint256.h"
10
#include "../BinaryCoding.h"
11
#include "../proto/NULS.pb.h"
12
#include "../HexCoding.h"
13
#include "Address.h"
14

15
using namespace TW;
16
using namespace TW::NULS;
17

18 1
static inline void serializerRemark(std::string& remark, Data& data) {
19 1
    encodeVarInt(remark.length(), data);
20 1
    std::copy(remark.begin(), remark.end(), std::back_inserter(data));
21
}
22

23 1
static inline void serializerInput(const Proto::TransactionCoinFrom& input, Data& data) {
24 1
    encodeVarInt(1, data);  //there is one coinFrom
25 1
    const auto& fromAddress = input.from_address();
26 1
    if (!NULS::Address::isValid(fromAddress)) {
27 0
        throw std::invalid_argument("Invalid address");
28
    }
29 1
    const auto& addr = NULS::Address(fromAddress);
30 1
    encodeVarInt(addr.bytes.size() - 1, data);
31 1
    std::copy(addr.bytes.begin(), addr.bytes.end() - 1, std::back_inserter(data));
32 1
    encode16LE(static_cast<uint16_t>(input.assets_chainid()), data);
33 1
    encode16LE(static_cast<uint16_t>(input.assets_id()), data);
34 1
    std::copy(input.id_amount().begin(), input.id_amount().end(), std::back_inserter(data));
35 1
    Data nonce = parse_hex(input.nonce());
36 1
    encodeVarInt(nonce.size(), data);
37 1
    append(data, nonce);
38 1
    data.push_back(static_cast<uint8_t>(input.locked()));
39
}
40

41 1
static inline void serializerOutput(const Proto::TransactionCoinTo& output, Data& data) {
42 1
    encodeVarInt(1, data); //there is one coinTo
43

44 1
    const auto& toAddress = output.to_address();
45 1
    if (!NULS::Address::isValid(toAddress)) {
46 0
        throw std::invalid_argument("Invalid address");
47
    }
48 1
    const auto& addr = NULS::Address(toAddress);
49 1
    encodeVarInt(addr.bytes.size() - 1, data);
50 1
    std::copy(addr.bytes.begin(), addr.bytes.end() - 1, std::back_inserter(data));
51 1
    encode16LE(static_cast<uint16_t>(output.assets_chainid()), data);
52 1
    encode16LE(static_cast<uint16_t>(output.assets_id()), data);
53 1
    std::copy(output.id_amount().begin(), output.id_amount().end(), std::back_inserter(data));
54 1
    encode64LE(output.lock_time(), data);
55
}
56

57 1
static inline Data calcTransactionDigest(Data& data) {
58 1
    Data hash1 = Hash::sha256(data);
59 1
    Data hash2 = Hash::sha256(hash1);
60 1
    return hash2;
61
}
62

63 1
static inline Data makeTransactionSignature(PrivateKey& privateKey, Data& txHash) {
64 1
    PublicKey pubKey = privateKey.getPublicKey(TWPublicKeyTypeSECP256k1);
65 1
    Data transactionSignature = Data();
66 1
    encodeVarInt(pubKey.bytes.size(), transactionSignature);
67 1
    std::copy(pubKey.bytes.begin(), pubKey.bytes.end(), std::back_inserter(transactionSignature));
68 1
    auto signature = privateKey.signAsDER(txHash, TWCurve::TWCurveSECP256k1);
69 1
    encodeVarInt(signature.size(), transactionSignature);
70 1
    std::copy(signature.begin(), signature.end(), std::back_inserter(transactionSignature));
71 1
    return transactionSignature;
72
}
73

74

Read our documentation on viewing source code .

Loading