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
66 lines (52 loc) · 2.36 KB
/
Copy pathSigner.cpp
File metadata and controls
66 lines (52 loc) · 2.36 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
// 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"
#include "../Ethereum/Signer.h"
using namespace TW;
using namespace TW::Wanchain;
Ethereum::Proto::SigningOutput Signer::sign(const Ethereum::Proto::SigningInput &input) const noexcept {
auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
auto transaction = Ethereum::Signer::build(input);
sign(key, transaction);
auto protoOutput = Ethereum::Proto::SigningOutput();
auto encoded = 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, Ethereum::Transaction& transaction) const noexcept {
transaction.v = chainID;
transaction.r = 0;
transaction.s = 0;
auto hash = this->hash(transaction);
auto tuple = Ethereum::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::encode(const Ethereum::Transaction& transaction) const noexcept {
auto encoded = Data();
append(encoded, Ethereum::RLP::encode(1));
append(encoded, Ethereum::RLP::encode(transaction.nonce));
append(encoded, Ethereum::RLP::encode(transaction.gasPrice));
append(encoded, Ethereum::RLP::encode(transaction.gasLimit));
append(encoded, Ethereum::RLP::encode(transaction.to));
append(encoded, Ethereum::RLP::encode(transaction.amount));
append(encoded, Ethereum::RLP::encode(transaction.payload));
append(encoded, Ethereum::RLP::encode(transaction.v));
append(encoded, Ethereum::RLP::encode(transaction.r));
append(encoded, Ethereum::RLP::encode(transaction.s));
return Ethereum::RLP::encodeList(encoded);
}
Data Signer::hash(const Ethereum::Transaction& transaction) const noexcept {
const auto encoded = Signer::encode(transaction);
return Hash::keccak256(encoded);
}