1
// Copyright © 2017-2020 Trust.
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 "HexCoding.h"
9
#include <google/protobuf/util/json_util.h>
10

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

14 1
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
15
    // Load private key and transaction from Protobuf input.
16 1
    auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
17 1
    auto pubkey = key.getPublicKey(TWPublicKeyTypeSECP256k1Extended);
18 1
    Address from_address(pubkey);
19 1
    Address to_address(input.to());
20 1
    Transaction transaction(
21 1
        /* to */ to_address,
22 1
        /* from */ from_address,
23 1
        /* nonce */ input.nonce(),
24 1
        /* value */ load(input.value()),
25 1
        /* gasLimit */ input.gas_limit(),
26 1
        /* gasFeeCap */ load(input.gas_fee_cap()),
27 1
        /* gasPremium */ load(input.gas_premium())
28
    );
29

30
    // Sign transaction.
31 1
    auto signature = sign(key, transaction);
32 1
    const auto json = transaction.serialize(signature);
33

34
    // Return Protobuf output.
35 1
    Proto::SigningOutput output;
36 1
    output.set_json(json.data(), json.size());
37 1
    return output;
38
}
39

40 1
Data Signer::sign(const PrivateKey& privateKey, Transaction& transaction) noexcept {
41 1
    Data toSign = Hash::blake2b(transaction.cid(), 32);
42 1
    auto signature = privateKey.sign(toSign, TWCurveSECP256k1);
43 1
    return Data(signature.begin(), signature.end());
44
}
45

46 1
std::string Signer::signJSON(const std::string& json, const Data& key) {
47 1
    auto input = Proto::SigningInput();
48 1
    google::protobuf::util::JsonStringToMessage(json, &input);
49 1
    input.set_private_key(key.data(), key.size());
50 1
    auto output = Signer::sign(input);
51 1
    return output.json();
52
}

Read our documentation on viewing source code .

Loading