Ring Daemon 16.0.0
Loading...
Searching...
No Matches
libdevcrypto/Common.h
Go to the documentation of this file.
1/*
2 This file is part of cpp-ethereum.
3
4 cpp-ethereum is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 cpp-ethereum is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16*/
25#pragma once
26
27#include <mutex>
28#include <libdevcore/Address.h>
29#include <libdevcore/Common.h>
31
32namespace dev {
33
35
38using Public = h512;
39
43
45{
46 SignatureStruct() = default;
47 SignatureStruct(Signature const& _s) { *(h520*) this = _s; }
48 SignatureStruct(h256 const& _r, h256 const& _s, uint8_t _v)
49 : r(_r)
50 , s(_s)
51 , v(_v)
52 {}
53 operator Signature() const { return *(h520 const*) this; }
54
56 bool isValid() const noexcept;
57
60 uint8_t v = 0;
61};
62
64using Secrets = std::vector<Secret>;
65
67Public toPublic(Secret const& _secret);
68
70Address toAddress(Public const& _public);
71
74Address toAddress(Secret const& _secret);
75
80{
81public:
82 KeyPair() = default;
86 KeyPair(Secret const& _sec);
87
89 static KeyPair create();
90
92 // static KeyPair fromEncryptedSeed(bytesConstRef _seed, std::string const& _password);
93
94 Secret const& secret() const { return m_secret; }
95
97 Public const& pub() const { return m_public; }
98
100 Address const& address() const { return m_address; }
101
102 bool operator==(KeyPair const& _c) const { return m_public == _c.m_public; }
103 bool operator!=(KeyPair const& _c) const { return m_public != _c.m_public; }
104
105private:
106 Secret m_secret;
107 Public m_public;
108 Address m_address;
109};
110
111namespace crypto {
112
113class InvalidState : public std::runtime_error
114{
115public:
116 InvalidState()
117 : runtime_error("invalid state") {};
118};
119class CryptoException : public std::runtime_error
120{
121public:
122 CryptoException(const char* err)
123 : runtime_error(err) {};
124};
125
126} // namespace crypto
127
128} // namespace dev
This file defined Address alias for FixedHash of 160 bits and some special Address constants.
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition FixedHash.h:53
Simple class that represents a "key pair".
bool operator==(KeyPair const &_c) const
Secret const & secret() const
Create from an encrypted seed.
bool operator!=(KeyPair const &_c) const
Public const & pub() const
Retrieve the public key.
KeyPair()=default
Address const & address() const
Retrieve the associated address of the public key.
Definition Address.h:25
h520 Signature
A signature: 65 bytes: r: [0, 32), s: [32, 64), v: 64.
Address toAddress(Public const &_public)
Convert a public key to address.
Definition Common.cpp:78
FixedHash< 65 > h520
Definition FixedHash.h:491
Public toPublic(Secret const &_secret)
Convert a secret key into the public key equivalent.
Definition Common.cpp:56
std::vector< Secret > Secrets
A vector of secrets.
FixedHash< 64 > h512
Definition FixedHash.h:492
SignatureStruct(Signature const &_s)
bool isValid() const noexcept
Definition Common.cpp:47
SignatureStruct()=default
SignatureStruct(h256 const &_r, h256 const &_s, uint8_t _v)