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
61 lines (53 loc) · 2.31 KB
/
Copy pathSigner.cpp
File metadata and controls
61 lines (53 loc) · 2.31 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
// 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 "Base64.h"
#include "../HexCoding.h"
using namespace TW;
using namespace TW::Nebulas;
Proto::SigningOutput Signer::sign(Proto::SigningInput &input) const noexcept {
Transaction tx(Address(input.from_address()),
load(input.nonce()),
load(input.gas_price()),
load(input.gas_limit()),
Address(input.to_address()),
load(input.amount()),
load(input.timestamp()),
input.payload()
);
auto privateKey = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
sign(privateKey, tx);
auto protoOutput = Proto::SigningOutput();
protoOutput.set_algorithm(tx.algorithm);
protoOutput.set_signature(reinterpret_cast<const char *>(tx.signature.data()), tx.signature.size());
protoOutput.set_raw(TW::Base64::encode(tx.raw));
return protoOutput;
}
void Signer::sign(const PrivateKey &privateKey, Transaction &transaction) const noexcept {
transaction.hash = this->hash(transaction);
transaction.chainID = chainID;
transaction.algorithm = 1;
transaction.signature = privateKey.sign(transaction.hash, TWCurveSECP256k1);
transaction.serializeToRaw();
}
Data Signer::hash(const Transaction &transaction) const noexcept {
auto encoded = Data();
auto payload = Data();
auto data = Transaction::newPayloadData(transaction.payload);
payload.resize(data->ByteSize());
data->SerializePartialToArray(payload.data(),(int)payload.size());
delete data;
encoded.insert(encoded.end(), transaction.from.bytes.begin(), transaction.from.bytes.end());
encoded.insert(encoded.end(), transaction.to.bytes.begin(), transaction.to.bytes.end());
encode256BE(encoded, transaction.amount, 128);
encode256BE(encoded, transaction.nonce, 64);
encode256BE(encoded, transaction.timestamp, 64);
encoded.insert(encoded.end(), payload.begin(), payload.end());
encode256BE(encoded, chainID, 32);
encode256BE(encoded, transaction.gasPrice, 128);
encode256BE(encoded, transaction.gasLimit, 128);
return Hash::sha3_256(encoded);
}