forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrivateKey.h
More file actions
83 lines (61 loc) · 2.82 KB
/
Copy pathPrivateKey.h
File metadata and controls
83 lines (61 loc) · 2.82 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
// 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.
#pragma once
#include "Data.h"
#include "PublicKey.h"
#include <TrustWalletCore/TWCurve.h>
namespace TW {
class PrivateKey {
public:
/// The number of bytes in a private key.
static const size_t size = 32;
/// The number of bytes in an extended private key.
static const size_t extendedSize = 3 * 32;
/// The private key bytes.
Data bytes;
/// Optional extended part of the key (additional 32 bytes)
Data extensionBytes;
/// Optional chain code (additional 32 bytes)
Data chainCodeBytes;
/// Determines if a collection of bytes makes a valid private key.
static bool isValid(const Data& data);
/// Determines if a collection of bytes and curve make a valid private key.
static bool isValid(const Data& data, TWCurve curve);
/// Initializes a private key with an array of bytes. Size must be exact (normally 32, or 96 for extended)
explicit PrivateKey(const Data& data);
/// Initializes a private key from a string of bytes (convenience method).
explicit PrivateKey(const std::string& data) : PrivateKey(TW::data(data)) {}
/// Initializes an extended private key with key, extended key, and chain code.
explicit PrivateKey(const Data& data, const Data& ext, const Data& chainCode);
PrivateKey(const PrivateKey& other) = default;
PrivateKey& operator=(const PrivateKey& other) = default;
PrivateKey(PrivateKey&& other) = default;
PrivateKey& operator=(PrivateKey&& other) = default;
virtual ~PrivateKey();
/// Returns the public key for this private key.
PublicKey getPublicKey(enum TWPublicKeyType type) const;
/// Signs a digest using the given ECDSA curve.
Data sign(const Data& digest, TWCurve curve) const;
/// Signs a digest using the given ECDSA curve and prepends the recovery id (a la graphene)
/// Only a sig that passes canonicalChecker is returned
Data sign(const Data& digest, TWCurve curve, int(*canonicalChecker)(uint8_t by, uint8_t sig[64])) const;
/// Signs a digest using the given ECDSA curve. The result is encoded with
/// DER.
Data signAsDER(const Data& digest, TWCurve curve) const;
/// Signs a digest using given ECDSA curve, returns schnorr signature
Data signSchnorr(const Data& message, TWCurve curve) const;
};
inline bool operator==(const PrivateKey& lhs, const PrivateKey& rhs) {
return lhs.bytes == rhs.bytes;
}
inline bool operator!=(const PrivateKey& lhs, const PrivateKey& rhs) {
return lhs.bytes != rhs.bytes;
}
} // namespace TW
/// Wrapper for C interface.
struct TWPrivateKey {
TW::PrivateKey impl;
};