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

9
#include "../Ethereum/RLP.h"
10
#include "../Hash.h"
11

12
using namespace TW;
13
using namespace TW::Theta;
14
using RLP = Ethereum::RLP;
15

16 1
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
17 1
    auto pkFrom = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
18 1
    auto from = Ethereum::Address(pkFrom.getPublicKey(TWPublicKeyTypeSECP256k1Extended));
19

20 1
    auto transaction = Transaction(
21 1
        /* from: */ from,
22 1
        /* to: */ Ethereum::Address(input.to_address()),
23 1
        /* thetaAmount: */ load(input.theta_amount()),
24 1
        /* tfuelAmount: */ load(input.tfuel_amount()),
25 1
        /* sequence: */ input.sequence(),
26 1
        /* feeAmount: */ load(input.fee()));
27

28 1
    auto signer = Signer(input.chain_id());
29 1
    auto signature = signer.sign(pkFrom, transaction);
30

31 1
    auto output = Proto::SigningOutput();
32

33 1
    transaction.setSignature(from, signature);
34 1
    auto encoded = transaction.encode();
35 1
    output.set_encoded(encoded.data(), encoded.size());
36 1
    output.set_signature(signature.data(), signature.size());
37 1
    return output;
38
}
39

40 1
Data Signer::encode(const Transaction& transaction) noexcept {
41 1
    const uint64_t nonce = 0;
42 1
    const uint256_t gasPrice = 0;
43 1
    const uint64_t gasLimit = 0;
44 1
    const Ethereum::Address to = Ethereum::Address("0x0000000000000000000000000000000000000000");
45 1
    const uint256_t amount = 0;
46

47 1
    auto encoded = Data();
48
    /// Need to add the following prefix to the tx signbytes to be compatible with
49
    /// the Ethereum tx format
50 1
    append(encoded, RLP::encode(nonce));
51 1
    append(encoded, RLP::encode(gasPrice));
52 1
    append(encoded, RLP::encode(gasLimit));
53 1
    append(encoded, RLP::encode(to.bytes));
54 1
    append(encoded, RLP::encode(amount));
55
    /// Chain ID
56 1
    auto payload = Data();
57 1
    append(payload, RLP::encode(chainID));
58 1
    append(payload, transaction.encode());
59 1
    append(encoded, RLP::encode(payload));
60 1
    return RLP::encodeList(encoded);
61
}
62

63 1
Data Signer::sign(const PrivateKey& privateKey, const Transaction& transaction) noexcept {
64 1
    auto encoded = encode(transaction);
65 1
    auto hash = Hash::keccak256(encoded);
66 1
    auto signature = privateKey.sign(hash, TWCurveSECP256k1);
67 1
    return signature;
68
}

Read our documentation on viewing source code .

Loading