forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSigner.cpp
More file actions
97 lines (80 loc) · 3.41 KB
/
Signer.cpp
File metadata and controls
97 lines (80 loc) · 3.41 KB
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
// Copyright © 2017-2020 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"
using namespace TW;
using namespace TW::Ethereum;
std::tuple<uint256_t, uint256_t, uint256_t> Signer::values(const uint256_t &chainID,
const Data &signature) noexcept {
boost::multiprecision::uint256_t r, s, v;
import_bits(r, signature.begin(), signature.begin() + 32);
import_bits(s, signature.begin() + 32, signature.begin() + 64);
import_bits(v, signature.begin() + 64, signature.begin() + 65);
v += 27;
boost::multiprecision::uint256_t newV;
if (chainID != 0) {
import_bits(newV, signature.begin() + 64, signature.begin() + 65);
newV += 35 + chainID + chainID;
} else {
newV = v;
}
return std::make_tuple(r, s, newV);
}
std::tuple<uint256_t, uint256_t, uint256_t>
Signer::sign(const uint256_t &chainID, const PrivateKey &privateKey, const Data &hash) noexcept {
auto signature = privateKey.sign(hash, TWCurveSECP256k1);
return values(chainID, signature);
}
Transaction Signer::build(const Proto::SigningInput &input) {
Data toAddress;
if (!input.to_address().empty()) {
toAddress.resize(20);
auto address = Address(input.to_address());
std::copy(address.bytes.begin(), address.bytes.end(), toAddress.data());
}
auto transaction = Transaction(
/* nonce: */ load(input.nonce()),
/* gasPrice: */ load(input.gas_price()),
/* gasLimit: */ load(input.gas_limit()),
/* to: */ toAddress,
/* amount: */ load(input.amount()),
/* payload: */ Data(input.payload().begin(), input.payload().end()));
return transaction;
}
Proto::SigningOutput Signer::sign(const Proto::SigningInput &input) const noexcept {
auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
auto transaction = Signer::build(input);
sign(key, transaction);
auto protoOutput = Proto::SigningOutput();
auto encoded = RLP::encode(transaction);
protoOutput.set_encoded(encoded.data(), encoded.size());
auto v = store(transaction.v);
protoOutput.set_v(v.data(), v.size());
auto r = store(transaction.r);
protoOutput.set_r(r.data(), r.size());
auto s = store(transaction.s);
protoOutput.set_s(s.data(), s.size());
return protoOutput;
}
void Signer::sign(const PrivateKey &privateKey, Transaction &transaction) const noexcept {
auto hash = this->hash(transaction);
auto tuple = Signer::sign(chainID, privateKey, hash);
transaction.r = std::get<0>(tuple);
transaction.s = std::get<1>(tuple);
transaction.v = std::get<2>(tuple);
}
Data Signer::hash(const Transaction &transaction) const noexcept {
auto encoded = Data();
append(encoded, RLP::encode(transaction.nonce));
append(encoded, RLP::encode(transaction.gasPrice));
append(encoded, RLP::encode(transaction.gasLimit));
append(encoded, RLP::encode(transaction.to));
append(encoded, RLP::encode(transaction.amount));
append(encoded, RLP::encode(transaction.payload));
append(encoded, RLP::encode(chainID));
append(encoded, RLP::encode(0));
append(encoded, RLP::encode(0));
return Hash::keccak256(RLP::encodeList(encoded));
}