forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPublicKey.h
More file actions
86 lines (64 loc) · 2.63 KB
/
Copy pathPublicKey.h
File metadata and controls
86 lines (64 loc) · 2.63 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
// 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 "Hash.h"
#include <TrustWalletCore/TWPublicKeyType.h>
#include <cassert>
#include <stdexcept>
namespace TW {
class PublicKey {
public:
/// The number of bytes in a secp256k1 and nist256p1 public key.
static const size_t secp256k1Size = 33;
/// The number of bytes in a ed25519 public key.
static const size_t ed25519Size = 32;
static const size_t ed25519ExtendedSize = 64;
/// The number of bytes in a secp256k1 and nist256p1 extended public key.
static const size_t secp256k1ExtendedSize = 65;
/// The public key bytes.
Data bytes;
/// The type of the public key.
///
/// This has information about the elliptic curve and other parameters
/// used when generating the public key.
enum TWPublicKeyType type;
/// Determines if a collection of bytes makes a valid public key of the
/// given type.
static bool isValid(const Data& data, enum TWPublicKeyType type);
/// Initializes a public key with a collection of bytes.
///
/// @throws std::invalid_argument if the data is not a valid public key.
explicit PublicKey(const Data& data, enum TWPublicKeyType type);
/// Determines if this is a compressed public key.
bool isCompressed() const {
return type != TWPublicKeyTypeSECP256k1Extended && type != TWPublicKeyTypeNIST256p1Extended;
}
/// Returns a compressed version of this public key.
PublicKey compressed() const;
/// Returns an extended version of this public key.
PublicKey extended() const;
/// Verifies a signature for the provided message.
bool verify(const Data& signature, const Data& message) const;
/// Verifies a schnorr signature for the provided message.
bool verifySchnorr(const Data& signature, const Data& message) const;
/// Computes the public key hash.
///
/// The public key hash is computed by applying the hasher to the public key
/// bytes and then prepending the prefix.
Data hash(const Data& prefix, Hash::Hasher hasher = Hash::sha256ripemd, bool skipTypeByte = false) const;
};
inline bool operator==(const PublicKey& lhs, const PublicKey& rhs) {
return lhs.bytes == rhs.bytes;
}
inline bool operator!=(const PublicKey& lhs, const PublicKey& rhs) {
return lhs.bytes != rhs.bytes;
}
} // namespace TW
/// Wrapper for C interface.
struct TWPublicKey {
TW::PublicKey impl;
};