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

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

14
static const int64_t fullyCanonical = 0x80000000;
15

16 1
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
17 1
    auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
18 1
    auto transaction = Transaction(
19 1
        /* amount */input.amount(),
20 1
        /* fee */input.fee(),
21 1
        /* flags */input.flags(),
22 1
        /* sequence */input.sequence(),
23 1
        /* last_ledger_sequence */input.last_ledger_sequence(),
24 1
        /* account */Address(input.account()),
25 1
        /* destination */input.destination(),
26 1
        /* destination_tag*/input.destination_tag()
27
    );
28

29 1
    auto signer = Signer();
30 1
    signer.sign(key, transaction);
31

32 1
    auto output = Proto::SigningOutput();
33 1
    auto encoded = transaction.serialize();
34 1
    output.set_encoded(encoded.data(), encoded.size());
35 1
    return output;
36
}
37

38 1
void Signer::sign(const PrivateKey& privateKey, Transaction& transaction) const noexcept {
39
    /// See https://github.com/trezor/trezor-core/blob/master/src/apps/ripple/sign_tx.py#L59
40 1
    transaction.flags |= fullyCanonical;
41 1
    transaction.pub_key = privateKey.getPublicKey(TWPublicKeyTypeSECP256k1).bytes;
42

43 1
    auto unsignedTx = transaction.getPreImage();
44 1
    auto hash = Hash::sha512(unsignedTx);
45 1
    auto half = Data(hash.begin(), hash.begin() + 32);
46

47 1
    transaction.signature = privateKey.signAsDER(half, TWCurveSECP256k1);
48
}

Read our documentation on viewing source code .

Loading