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
56 lines (44 loc) · 1.67 KB
/
Signer.cpp
File metadata and controls
56 lines (44 loc) · 1.67 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
// 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 "../HexCoding.h"
#include <TrezorCrypto/ecdsa.h>
#include <TrezorCrypto/secp256k1.h>
#include <TrezorCrypto/nist256p1.h>
using namespace TW;
using namespace TW::EOS;
void Signer::sign(const PrivateKey& privateKey, Type type, Transaction& transaction) const {
if (!transaction.isValid()) {
throw std::invalid_argument("Invalid transaction!");;
}
// values for Legacy and ModernK1
TWCurve curve = TWCurveSECP256k1;
auto canonicalChecker = is_canonical;
// Values for ModernR1
if (type == Type::ModernR1) {
curve = TWCurveNIST256p1;
canonicalChecker = nullptr;
}
const Data result = privateKey.sign(hash(transaction), curve, canonicalChecker);
transaction.signatures.push_back(Signature(result, type));
}
TW::Data Signer::hash(const Transaction& transaction) const noexcept {
Data hashInput(chainID);
transaction.serialize(hashInput);
Data cfdHash(Hash::sha256Size); // default value for empty cfd
if (transaction.contextFreeData.size()) {
cfdHash = Hash::sha256(transaction.contextFreeData);
}
append(hashInput, cfdHash);
return Hash::sha256(hashInput);
}
// canonical check for EOS
int Signer::is_canonical(uint8_t by, uint8_t sig[64]) {
return !(sig[0] & 0x80)
&& !(sig[0] == 0 && !(sig[1] & 0x80))
&& !(sig[32] & 0x80)
&& !(sig[32] == 0 && !(sig[33] & 0x80));
}