forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Signer.cpp
123 lines (98 loc) · 4.12 KB
/
Signer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright © 2017-2023 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#include "Signer.h"
#include "../BinaryCoding.h"
#include <limits>
namespace TW::Ripple {
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
auto output = Proto::SigningOutput();
auto transaction =
Transaction(
input.fee(),
input.flags(),
input.sequence(),
input.last_ledger_sequence(),
Address(input.account()));
switch (input.operation_oneof_case()) {
case Proto::SigningInput::kOpPayment:
signPayment(input, output, transaction);
break;
case Proto::SigningInput::kOpNftokenBurn:
transaction.createNFTokenBurn(input.op_nftoken_burn().nftoken_id());
break;
case Proto::SigningInput::kOpNftokenCreateOffer:
transaction.createNFTokenCreateOffer(
input.op_nftoken_create_offer().nftoken_id(),
input.op_nftoken_create_offer().destination());
break;
case Proto::SigningInput::kOpNftokenAcceptOffer:
transaction.createNFTokenAcceptOffer(input.op_nftoken_accept_offer().sell_offer());
break;
case Proto::SigningInput::kOpNftokenCancelOffer:
signNfTokenCancelOffer(input, transaction);
break;
case Proto::SigningInput::kOpTrustSet:
transaction.createTrustSet(
input.op_trust_set().limit_amount().currency(),
input.op_trust_set().limit_amount().value(),
input.op_trust_set().limit_amount().issuer());
break;
default:
break;
}
if (output.error()) {
return output;
}
auto signer = Signer();
auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
signer.sign(key, transaction);
auto encoded = transaction.serialize();
output.set_encoded(encoded.data(), encoded.size());
return output;
}
void Signer::sign(const PrivateKey& privateKey, Transaction& transaction) const noexcept {
/// See https://github.com/trezor/trezor-core/blob/master/src/apps/ripple/sign_tx.py#L59
transaction.pub_key = privateKey.getPublicKey(TWPublicKeyTypeSECP256k1).bytes;
auto unsignedTx = transaction.getPreImage();
auto hash = Hash::sha512(unsignedTx);
auto half = Data(hash.begin(), hash.begin() + 32);
transaction.signature = privateKey.signAsDER(half);
}
void Signer::signPayment(const Proto::SigningInput& input,
Proto::SigningOutput& output,
Transaction& transaction) noexcept {
const int64_t tag = input.op_payment().destination_tag();
if (tag > std::numeric_limits<uint32_t>::max() || tag < 0) {
output.set_error(Common::Proto::SigningError::Error_invalid_memo);
return;
}
switch (input.op_payment().amount_oneof_case()) {
case Proto::OperationPayment::kAmount:
transaction.createXrpPayment(
input.op_payment().amount(),
input.op_payment().destination(),
tag);
break;
case Proto::OperationPayment::kCurrencyAmount:
transaction.createTokenPayment(
input.op_payment().currency_amount().currency(),
input.op_payment().currency_amount().value(),
input.op_payment().currency_amount().issuer(),
input.op_payment().destination(),
tag);
break;
default:
break;
}
}
void Signer::signNfTokenCancelOffer(const Proto::SigningInput& input, Transaction& transaction) noexcept {
std::vector<std::string> token_offers;
for (int i = 0; i < input.op_nftoken_cancel_offer().token_offers_size(); i++) {
token_offers.emplace_back(input.op_nftoken_cancel_offer().token_offers(i));
}
transaction.createNFTokenCancelOffer(token_offers);
}
} // namespace TW::Ripple