danicoin/src/wallet/LegacyKeysImporter.cpp

99 lines
3.3 KiB
C++
Raw Normal View History

2015-05-27 12:08:46 +00:00
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin 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.
//
// Bytecoin 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 Bytecoin. If not, see <http://www.gnu.org/licenses/>.
#include "LegacyKeysImporter.h"
#include <vector>
#include <system_error>
2015-05-27 12:08:46 +00:00
#include "Common/StringTools.h"
#include "cryptonote_core/Currency.h"
#include "cryptonote_core/account.h"
#include "serialization/binary_utils.h"
2015-07-15 12:23:00 +00:00
#include "serialization/SerializationTools.h"
#include "wallet/WalletSerializer.h"
#include "wallet/WalletUserTransactionsCache.h"
#include "wallet/WalletErrors.h"
namespace {
struct keys_file_data {
crypto::chacha8_iv iv;
std::string account_data;
BEGIN_SERIALIZE_OBJECT()
FIELD(iv)
FIELD(account_data)
2015-05-27 12:08:46 +00:00
END_SERIALIZE()
};
bool verify_keys(const crypto::secret_key& sec, const crypto::public_key& expected_pub) {
crypto::public_key pub;
bool r = crypto::secret_key_to_public_key(sec, pub);
return r && expected_pub == pub;
}
2015-05-27 12:08:46 +00:00
void loadKeysFromFile(const std::string& filename, const std::string& password, CryptoNote::account_base& account) {
keys_file_data keys_file_data;
std::string buf;
2015-05-27 12:08:46 +00:00
if (!Common::loadFileToString(filename, buf)) {
throw std::system_error(make_error_code(CryptoNote::error::INTERNAL_WALLET_ERROR), "failed to load \"" + filename + '\"');
}
if (!::serialization::parse_binary(buf, keys_file_data)) {
throw std::system_error(make_error_code(CryptoNote::error::INTERNAL_WALLET_ERROR), "failed to deserialize \"" + filename + '\"');
}
crypto::chacha8_key key;
crypto::cn_context cn_context;
crypto::generate_chacha8_key(cn_context, password, key);
std::string account_data;
account_data.resize(keys_file_data.account_data.size());
crypto::chacha8(keys_file_data.account_data.data(), keys_file_data.account_data.size(), key, keys_file_data.iv, &account_data[0]);
2015-07-15 12:23:00 +00:00
const CryptoNote::account_keys& keys = account.get_keys();
2015-05-27 12:08:46 +00:00
2015-07-15 12:23:00 +00:00
if (CryptoNote::loadFromBinaryKeyValue(account, account_data) &&
verify_keys(keys.m_view_secret_key, keys.m_account_address.m_viewPublicKey) &&
verify_keys(keys.m_spend_secret_key, keys.m_account_address.m_spendPublicKey)) {
return;
2015-05-27 12:08:46 +00:00
}
2015-07-15 12:23:00 +00:00
throw std::system_error(make_error_code(CryptoNote::error::WRONG_PASSWORD));
}
}
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
void importLegacyKeys(const std::string& legacyKeysFilename, const std::string& password, std::ostream& destination) {
2015-05-27 12:08:46 +00:00
CryptoNote::account_base account;
2015-05-27 12:08:46 +00:00
loadKeysFromFile(legacyKeysFilename, password, account);
CryptoNote::WalletUserTransactionsCache transactionsCache;
std::string cache;
CryptoNote::WalletSerializer importer(account, transactionsCache);
importer.serialize(destination, password, false, cache);
}
2015-05-27 12:08:46 +00:00
} //namespace CryptoNote