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 "Asset.h"
9
#include "PackedTransaction.h"
10
#include "../HexCoding.h"
11

12
#include <TrezorCrypto/ecdsa.h>
13
#include <TrezorCrypto/nist256p1.h>
14
#include <TrezorCrypto/secp256k1.h>
15

16
using namespace TW;
17
using namespace TW::EOS;
18

19 1
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
20 1
    Proto::SigningOutput output;
21
    try {
22
        // create an asset object
23 1
        auto assetData = input.asset();
24 1
        auto asset = Asset(assetData.amount(), static_cast<uint8_t>(assetData.decimals()),
25 1
                           assetData.symbol());
26

27
        // create a transfer action
28 1
        auto action = TransferAction(input.currency(), input.sender(), input.recipient(), asset,
29 1
                                     input.memo());
30

31
        // create a Transaction and add the transfer action
32 1
        auto tx = Transaction(Data(input.reference_block_id().begin(), input.reference_block_id().end()),
33 1
                        input.reference_block_time());
34 1
        tx.actions.push_back(action);
35

36
        // get key type
37 1
        EOS::Type type = Type::Legacy;
38 1
        switch (input.private_key_type()) {
39
        case Proto::KeyType::LEGACY:
40 1
            type = Type::Legacy;
41 1
            break;
42

43
        case Proto::KeyType::MODERNK1:
44 1
            type = Type::ModernK1;
45 1
            break;
46

47
        case Proto::KeyType::MODERNR1:
48 0
            type = Type::ModernR1;
49 0
            break;
50
        default:
51 0
            break;
52
        }
53

54
        // sign the transaction with a Signer
55 1
        auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
56 1
        auto chainId = Data(input.chain_id().begin(), input.chain_id().end());
57 1
        Signer(chainId).sign(key, type, tx);
58

59
        // Pack the transaction and add the json encoding to Signing outputput
60 1
        PackedTransaction ptx{tx, CompressionType::None};
61

62 1
        output.set_json_encoded(ptx.serialize().dump());
63 1
        return output;
64 1
    } catch (const std::exception& e) {
65 1
        output.set_error(e.what());
66
    }
67 1
    return output;
68
}
69

70 1
void Signer::sign(const PrivateKey& privateKey, Type type, Transaction& transaction) const {
71 1
    if (!transaction.isValid()) {
72 1
        throw std::invalid_argument("Invalid transaction!");
73
    }
74

75
    // values for Legacy and ModernK1
76 1
    TWCurve curve = TWCurveSECP256k1;
77 1
    auto canonicalChecker = isCanonical;
78

79
    //  Values for ModernR1
80 1
    if (type == Type::ModernR1) {
81 1
        curve = TWCurveNIST256p1;
82 1
        canonicalChecker = nullptr;
83
    }
84

85 1
    const Data result = privateKey.sign(hash(transaction), curve, canonicalChecker);
86

87 1
    transaction.signatures.push_back(Signature(result, type));
88
}
89

90 1
TW::Data Signer::hash(const Transaction& transaction) const noexcept {
91 1
    Data hashInput(chainID);
92 1
    transaction.serialize(hashInput);
93

94 1
    Data cfdHash(Hash::sha256Size); // default value for empty cfd
95 1
    if (transaction.contextFreeData.size()) {
96 0
        cfdHash = Hash::sha256(transaction.contextFreeData);
97
    }
98

99 1
    append(hashInput, cfdHash);
100 1
    return Hash::sha256(hashInput);
101
}
102

103
// canonical check for EOS
104 1
int Signer::isCanonical(uint8_t by, uint8_t sig[64]) {
105 1
    return !(sig[0] & 0x80) 
106 1
        && !(sig[0] == 0 && !(sig[1] & 0x80))
107 1
        && !(sig[32] & 0x80)
108 1
        && !(sig[32] == 0 && !(sig[33] & 0x80));
109
}

Read our documentation on viewing source code .

Loading