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
#include "../Ethereum/Signer.h"
9

10
using namespace TW;
11
using namespace TW::Wanchain;
12

13 1
Ethereum::Proto::SigningOutput Signer::sign(const Ethereum::Proto::SigningInput& input) noexcept {
14 1
    auto signer = Signer(load(input.chain_id()));
15 1
    auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
16 1
    auto transaction = Ethereum::Signer::build(input);
17

18 1
    signer.sign(key, transaction);
19

20 1
    auto output = Ethereum::Proto::SigningOutput();
21

22 1
    auto encoded = signer.encode(transaction);
23 1
    output.set_encoded(encoded.data(), encoded.size());
24

25 1
    auto v = store(transaction.v);
26 1
    output.set_v(v.data(), v.size());
27

28 1
    auto r = store(transaction.r);
29 1
    output.set_r(r.data(), r.size());
30

31 1
    auto s = store(transaction.s);
32 1
    output.set_s(s.data(), s.size());
33

34 1
    return output;
35
}
36

37 1
void Signer::sign(const PrivateKey& privateKey, Ethereum::Transaction& transaction) const noexcept {
38 1
    transaction.v = chainID;
39 1
    transaction.r = 0;
40 1
    transaction.s = 0;
41 1
    auto hash = this->hash(transaction);
42 1
    auto tuple = Ethereum::Signer::sign(chainID, privateKey, hash);
43

44 1
    transaction.r = std::get<0>(tuple);
45 1
    transaction.s = std::get<1>(tuple);
46 1
    transaction.v = std::get<2>(tuple);
47
}
48

49 1
Data Signer::encode(const Ethereum::Transaction& transaction) const noexcept {
50 1
    auto encoded = Data();
51 1
    append(encoded, Ethereum::RLP::encode(1));
52 1
    append(encoded, Ethereum::RLP::encode(transaction.nonce));
53 1
    append(encoded, Ethereum::RLP::encode(transaction.gasPrice));
54 1
    append(encoded, Ethereum::RLP::encode(transaction.gasLimit));
55 1
    append(encoded, Ethereum::RLP::encode(transaction.to));
56 1
    append(encoded, Ethereum::RLP::encode(transaction.amount));
57 1
    append(encoded, Ethereum::RLP::encode(transaction.payload));
58 1
    append(encoded, Ethereum::RLP::encode(transaction.v));
59 1
    append(encoded, Ethereum::RLP::encode(transaction.r));
60 1
    append(encoded, Ethereum::RLP::encode(transaction.s));
61 1
    return Ethereum::RLP::encodeList(encoded);
62
}
63

64 1
Data Signer::hash(const Ethereum::Transaction& transaction) const noexcept {
65 1
    const auto encoded = Signer::encode(transaction);
66 1
    return Hash::keccak256(encoded);
67
}

Read our documentation on viewing source code .

Loading