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
89 lines (74 loc) · 2.65 KB
/
Copy pathSigner.cpp
File metadata and controls
89 lines (74 loc) · 2.65 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
// 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 "../Hash.h"
#include "../HexCoding.h"
#include "../PrivateKey.h"
#include "../uint256.h"
#include <boost/multiprecision/cpp_int.hpp>
#include <nlohmann/json.hpp>
#include <algorithm>
#include <iostream>
#include <sstream>
using namespace TW;
using namespace TW::Icon;
std::string to_hex(int64_t i) {
std::stringstream ss;
ss << "0x" << std::hex << i;
return ss.str();
}
std::string to_hex(const std::string& n) {
auto s = hex(n);
auto start = std::find_if(s.begin(), s.end(), [](auto c) { return c != '0'; });
if (start == s.end()) {
return "0x0";
}
return "0x" + std::string(start, s.end());
}
std::map<std::string, std::string> Signer::parameters() const noexcept {
auto params = std::map<std::string, std::string>();
params["from"] = input.from_address();
params["to"] = input.to_address();
params["timestamp"] = to_hex(input.timestamp());
params["nonce"] = to_hex(input.nonce());
params["stepLimit"] = to_hex(input.step_limit());
params["value"] = to_hex(input.value());
params["nid"] = to_hex(input.network_id());
params["version"] = "0x3";
return params;
}
std::string Signer::preImage() const noexcept {
std::string txHash = "icx_sendTransaction";
const auto params = parameters();
for (auto [key, value] : params) {
txHash += "." + key + "." + value;
}
return txHash;
}
std::string Signer::encode(const Data& signature) const noexcept {
auto json = nlohmann::json();
json["from"] = input.from_address();
json["to"] = input.to_address();
json["timestamp"] = to_hex(input.timestamp());
json["nonce"] = to_hex(input.nonce());
json["stepLimit"] = to_hex(input.step_limit());
json["value"] = to_hex(input.value());
json["nid"] = to_hex(input.network_id());
json["version"] = "0x3";
json["signature"] = Base64::encode(signature);
return json.dump();
}
Proto::SigningOutput Signer::sign() const noexcept {
const auto hash = Hash::sha3_256(Signer::preImage());
const auto key = PrivateKey(input.private_key());
const auto signature = key.sign(hash, TWCurveSECP256k1);
auto output = Proto::SigningOutput();
output.set_signature(signature.data(), signature.size());
auto encoded = encode(Data(signature.begin(), signature.end()));
output.set_encoded(encoded.data(), encoded.size());
return output;
}