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 "RLP.h"
10
#include "Transaction.h"
11
#include "../Data.h"
12
#include "../Hash.h"
13
#include "../PrivateKey.h"
14
#include "../proto/Ethereum.pb.h"
15
#include "../uint256.h"
16

17
#include <boost/multiprecision/cpp_int.hpp>
18
#include <cstdint>
19
#include <tuple>
20
#include <vector>
21

22
namespace TW::Ethereum {
23

24
/// Helper class that performs Ethereum transaction signing.
25
class Signer {
26
  public:
27
    /// Signs a Proto::SigningInput transaction
28
    static Proto::SigningOutput sign(const Proto::SigningInput& input) noexcept;
29
    /// Signs a json Proto::SigningInput with private key
30
    static std::string signJSON(const std::string& json, const Data& key);
31

32
  public:
33
    uint256_t chainID;
34

35
    /// Initializes a signer with a chain identifier.
36 1
    explicit Signer(uint256_t chainID) : chainID(std::move(chainID)) {}
37

38
    /// Signs the given transaction.
39
    void sign(const PrivateKey &privateKey, Transaction &transaction) const noexcept;
40

41
  public:
42
    /// build Transaction from signing input
43
    static Transaction build(const Proto::SigningInput &input);
44

45
    /// Signs a hash with the given private key for the given chain identifier.
46
    ///
47
    /// @returns the r, s, and v values of the transaction signature
48
    static std::tuple<uint256_t, uint256_t, uint256_t>
49
    sign(const uint256_t &chainID, const PrivateKey &privateKey, const Data& hash) noexcept;
50

51
    /// R, S, and V values for the given chain identifier and signature.
52
    ///
53
    /// @returns the r, s, and v values of the transaction signature
54
    static std::tuple<uint256_t, uint256_t, uint256_t> values(const uint256_t &chainID,
55
                                                              const Data& signature) noexcept;
56

57
  protected:
58
    /// Computes the transaction hash.
59
    Data hash(const Transaction &transaction) const noexcept;
60
};
61

62
} // namespace TW::Ethereum
63

64
/// Wrapper for C interface.
65
struct TWEthereumSigner {
66
    TW::Ethereum::Signer impl;
67
};

Read our documentation on viewing source code .

Loading