LCOV - code coverage report
Current view: top level - src/jamidht/eth/libdevcrypto - Common.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 2 2 100.0 %
Date: 2024-04-19 08:05:40 Functions: 2 2 100.0 %

          Line data    Source code
       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             : */
      17             : /** @file Common.h
      18             :  * @author Alex Leverington <nessence@gmail.com>
      19             :  * @author Gav Wood <i@gavwood.com>
      20             :  * @date 2014
      21             :  *
      22             :  * Ethereum-specific data structures & algorithms.
      23             :  */
      24             : 
      25             : #pragma once
      26             : 
      27             : #include <mutex>
      28             : #include <libdevcore/Address.h>
      29             : #include <libdevcore/Common.h>
      30             : #include <libdevcore/FixedHash.h>
      31             : 
      32             : namespace dev {
      33             : 
      34             : using Secret = SecureFixedHash<32>;
      35             : 
      36             : /// A public key: 64 bytes.
      37             : /// @NOTE This is not endian-specific; it's just a bunch of bytes.
      38             : using Public = h512;
      39             : 
      40             : /// A signature: 65 bytes: r: [0, 32), s: [32, 64), v: 64.
      41             : /// @NOTE This is not endian-specific; it's just a bunch of bytes.
      42             : using Signature = h520;
      43             : 
      44             : struct SignatureStruct
      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             : 
      55             :     /// @returns true if r,s,v values are valid, otherwise false
      56             :     bool isValid() const noexcept;
      57             : 
      58             :     h256 r;
      59             :     h256 s;
      60             :     uint8_t v = 0;
      61             : };
      62             : 
      63             : /// A vector of secrets.
      64             : using Secrets = std::vector<Secret>;
      65             : 
      66             : /// Convert a secret key into the public key equivalent.
      67             : Public toPublic(Secret const& _secret);
      68             : 
      69             : /// Convert a public key to address.
      70             : Address toAddress(Public const& _public);
      71             : 
      72             : /// Convert a secret key into address of public key equivalent.
      73             : /// @returns 0 if it's not a valid secret key.
      74             : Address toAddress(Secret const& _secret);
      75             : 
      76             : /// Simple class that represents a "key pair".
      77             : /// All of the data of the class can be regenerated from the secret key (m_secret) alone.
      78             : /// Actually stores a tuplet of secret, public and address (the right 160-bits of the public).
      79             : class KeyPair
      80             : {
      81             : public:
      82             :     KeyPair() = default;
      83             :     /// Normal constructor - populates object from the given secret key.
      84             :     /// If the secret key is invalid the constructor succeeds, but public key
      85             :     /// and address stay "null".
      86             :     KeyPair(Secret const& _sec);
      87             : 
      88             :     /// Create a new, randomly generated object.
      89             :     static KeyPair create();
      90             : 
      91             :     /// Create from an encrypted seed.
      92             :     // static KeyPair fromEncryptedSeed(bytesConstRef _seed, std::string const& _password);
      93             : 
      94         730 :     Secret const& secret() const { return m_secret; }
      95             : 
      96             :     /// Retrieve the public key.
      97             :     Public const& pub() const { return m_public; }
      98             : 
      99             :     /// Retrieve the associated address of the public key.
     100        1505 :     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             : 
     105             : private:
     106             :     Secret m_secret;
     107             :     Public m_public;
     108             :     Address m_address;
     109             : };
     110             : 
     111             : namespace crypto {
     112             : 
     113             : class InvalidState : public std::runtime_error
     114             : {
     115             : public:
     116             :     InvalidState()
     117             :         : runtime_error("invalid state") {};
     118             : };
     119             : class CryptoException : public std::runtime_error
     120             : {
     121             : public:
     122             :     CryptoException(const char* err)
     123             :         : runtime_error(err) {};
     124             : };
     125             : 
     126             : } // namespace crypto
     127             : 
     128             : } // namespace dev

Generated by: LCOV version 1.14