text: // Copyright (c) 2012-2017, The CryptoNote developers, The Bytecoin developers
  // Copyright (c) 2018-2022, The Deocoin Group.
  //
  // This file is part of Deocoin.
  //
  // Deocoin is free software: you can redistribute it and/or modify
  // it under the terms of the GNU Lesser General Public License as published by
  // the Free Software Foundation, either version 3 of the License, or
  // (at your option) any later version.
  //
  // Deocoin is distributed in the hope that it will be useful,
  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  // GNU Lesser General Public License for more details.
  //
  // You should have received a copy of the GNU Lesser General Public License
  // along with Deocoin.  If not, see <http://www.gnu.org/licenses/>.
  
  #include <crypto/crypto.h>
  #include <CryptoNoteCore/Account.h>
  #include <CryptoNoteCore/CryptoNoteSerialization.h>
  
  extern "C" {
  #include <crypto/keccak.h>
  } // extern "C"
  
  namespace CryptoNote {
  
  AccountBase::AccountBase()
  {
      setNull();
  }
  
  void AccountBase::setNull()
  {
      m_keys = AccountKeys();
  }
  
  void AccountBase::generate()
  {
      Crypto::generate_keys(m_keys.address.spendPublicKey, m_keys.spendSecretKey);
      Crypto::generate_keys(m_keys.address.viewPublicKey, m_keys.viewSecretKey);
      m_creation_timestamp = time(nullptr);
  }
  
  void AccountBase::generateDeterministic()
  {
      Crypto::SecretKey second;
      Crypto::generate_keys(m_keys.address.spendPublicKey, m_keys.spendSecretKey);
      auto ssk = (uint8_t *)&m_keys.spendSecretKey;
      keccak(ssk, sizeof(Crypto::SecretKey), (uint8_t *)&second, sizeof(Crypto::SecretKey));
      Crypto::generate_deterministic_keys(m_keys.address.viewPublicKey, m_keys.viewSecretKey, second);
      m_creation_timestamp = time(nullptr);
  }
  
  Crypto::SecretKey AccountBase::generate_key(
      const Crypto::SecretKey &recovery_key,
      bool recover,
      bool two_random)
  {
      Crypto::SecretKey first = generate_m_keys(
          m_keys.address.spendPublicKey,
          m_keys.spendSecretKey,
          recovery_key,
          recover
      );
  
      // rng for generating second set of keys is hash of first rng.
      // means only one set of electrum-style words needed for recovery.
      Crypto::SecretKey second;
      keccak((uint8_t*)&first,sizeof(Crypto::SecretKey),(uint8_t*)&second,sizeof(Crypto::SecretKey));
  
      generate_m_keys(m_keys.address.viewPublicKey, m_keys.viewSecretKey, second, !two_random);
  
      struct tm timestamp;
      timestamp.tm_year = 2016 - 1900; // year 2016
      timestamp.tm_mon = 5 - 1; // month May
      timestamp.tm_mday = 30; // 30 of May
      timestamp.tm_hour = 0;
      timestamp.tm_min = 0;
      timestamp.tm_sec = 0;
  
      if (recover) {
          m_creation_timestamp = mktime(&timestamp);
      } else {
          m_creation_timestamp = time(nullptr);
      }
  
      return first;
  }
  
  void AccountBase::generateViewFromSpend(
      Crypto::SecretKey &spend,
      Crypto::SecretKey &viewSecret,
      Crypto::PublicKey &viewPublic)
  {
      Crypto::SecretKey viewKeySeed;
      keccak((uint8_t *)&spend, sizeof(spend), (uint8_t *)&viewKeySeed, sizeof(viewKeySeed));
      Crypto::generate_deterministic_keys(viewPublic, viewSecret, viewKeySeed);
  }
  
  void AccountBase::generateViewFromSpend(Crypto::SecretKey &spend, Crypto::SecretKey &viewSecret)
  {
      // If we don't need the pub key
      Crypto::PublicKey unused_dummy_variable;
      generateViewFromSpend(spend, viewSecret, unused_dummy_variable);
  }
  
  const AccountKeys &AccountBase::getAccountKeys() const
  {
      return m_keys;
  }
  
  void AccountBase::setAccountKeys(const AccountKeys &keys)
  {
      m_keys = keys;
  }
  
  void AccountBase::serialize(ISerializer &s)
  {
      s(m_keys, "m_keys");
      s(m_creation_timestamp, "m_creation_timestamp");
  }
  
  } // namespace CryptoNote
  