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

11
using namespace TW;
12
using namespace TW::Nebulas;
13

14 1
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
15 1
    auto signer = Signer(load(input.chain_id()));
16

17 1
    auto tx = Transaction(Address(input.from_address()),
18 1
        load(input.nonce()),
19 1
        load(input.gas_price()),
20 1
        load(input.gas_limit()),
21 1
        Address(input.to_address()),
22 1
        load(input.amount()),
23 1
        load(input.timestamp()),
24 1
        input.payload()
25
    );
26
    
27 1
    auto privateKey = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
28 1
    signer.sign(privateKey, tx);
29

30 1
    auto output = Proto::SigningOutput();
31 1
    output.set_algorithm(tx.algorithm);
32 1
    output.set_signature(tx.signature.data(), tx.signature.size());
33 1
    output.set_raw(TW::Base64::encode(tx.raw));
34 1
    return output;
35
}
36

37 1
void Signer::sign(const PrivateKey &privateKey, Transaction &transaction) const noexcept {
38 1
    transaction.hash = this->hash(transaction);
39 1
    transaction.chainID = chainID;
40 1
    transaction.algorithm = 1;
41 1
    transaction.signature = privateKey.sign(transaction.hash, TWCurveSECP256k1);
42 1
    transaction.serializeToRaw();
43
}
44

45 1
Data Signer::hash(const Transaction &transaction) const noexcept {
46 1
    auto encoded = Data();
47 1
    auto payload = Data();
48 1
    auto data = Transaction::newPayloadData(transaction.payload);
49 1
    payload.resize(data->ByteSize());
50 1
    data->SerializePartialToArray(payload.data(),(int)payload.size());
51 1
    delete data;
52

53 1
    encoded.insert(encoded.end(), transaction.from.bytes.begin(), transaction.from.bytes.end());
54 1
    encoded.insert(encoded.end(), transaction.to.bytes.begin(), transaction.to.bytes.end());
55 1
    encode256BE(encoded, transaction.amount, 128);
56 1
    encode256BE(encoded, transaction.nonce, 64);
57 1
    encode256BE(encoded, transaction.timestamp, 64);
58 1
    encoded.insert(encoded.end(), payload.begin(), payload.end());
59 1
    encode256BE(encoded, chainID, 32);
60 1
    encode256BE(encoded, transaction.gasPrice, 128);
61 1
    encode256BE(encoded, transaction.gasLimit, 128);
62 1
    return Hash::sha3_256(encoded);
63
}

Read our documentation on viewing source code .

Loading