Seed: refactor

This commit is contained in:
tobtoht
2022-02-25 16:29:33 +01:00
parent f8bc335720
commit c6264ed3a0
13 changed files with 869 additions and 695 deletions
-117
View File
@@ -1,117 +0,0 @@
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-FileCopyrightText: 2020-2022 The Monero Project
#ifndef FEATHER_FEATHERSEED_H
#define FEATHER_FEATHERSEED_H
#include "libwalletqt/WalletManager.h"
#include "libwalletqt/Wallet.h"
#include "utils/AppData.h"
#include <sstream>
#include "RestoreHeightLookup.h"
enum SeedType {
MONERO = 0, // 25 word seeds
TEVADOR // 14 word seeds
};
struct FeatherSeed {
// TODO: this is spaghetti, needs refactor
NetworkType::Type netType;
QString coin;
QString language;
SeedType seedType;
QStringList mnemonic;
QString spendKey;
QString correction;
time_t time;
int restoreHeight = 0;
QString errorString;
explicit FeatherSeed(NetworkType::Type networkType = NetworkType::MAINNET,
const QString &coin = "monero",
const QString &language = "English",
const QStringList &mnemonic = {})
: netType(networkType), coin(coin), language(language), mnemonic(mnemonic)
{
// Generate a new mnemonic if none was given
if (mnemonic.length() == 0) {
this->time = std::time(nullptr);
monero_seed seed(this->time, coin.toStdString());
std::stringstream buffer;
buffer << seed;
this->mnemonic = QString::fromStdString(buffer.str()).split(" ");
buffer.str(std::string());
buffer << seed.key();
this->spendKey = QString::fromStdString(buffer.str());
this->setRestoreHeight();
}
if (mnemonic.length() == 25) {
this->seedType = SeedType::MONERO;
}
else if (mnemonic.length() == 14) {
this->seedType = SeedType::TEVADOR;
} else {
this->errorString = "Mnemonic seed does not match known type";
return;
}
if (seedType == SeedType::TEVADOR) {
try {
monero_seed seed(mnemonic.join(" ").toStdString(), coin.toStdString());
this->time = seed.date();
this->setRestoreHeight();
std::stringstream buffer;
buffer << seed.key();
this->spendKey = QString::fromStdString(buffer.str());
this->correction = QString::fromStdString(seed.correction());
if (!this->correction.isEmpty()) {
buffer.str(std::string());
buffer << seed;
int index = this->mnemonic.indexOf("xxxx");
this->mnemonic.replace(index, this->correction);
}
}
catch (const std::exception &e) {
this->errorString = e.what();
return;
}
}
}
void setRestoreHeight() {
if (this->time == 0)
this->restoreHeight = 1;
this->restoreHeight = appData()->restoreHeights[netType]->dateToHeight(this->time);
}
int setRestoreHeight(int height) {
auto now = std::time(nullptr);
auto nowClearance = 3600 * 24;
auto currentBlockHeight = appData()->restoreHeights[netType]->dateToHeight(now - nowClearance);
if (height >= currentBlockHeight + nowClearance) {
qCritical() << "unrealistic restore height detected, setting to current blockheight instead: " << currentBlockHeight;
this->restoreHeight = currentBlockHeight;
} else {
this->restoreHeight = height;
}
return this->restoreHeight;
}
};
#endif //FEATHER_FEATHERSEED_H
+31 -20
View File
@@ -14,10 +14,10 @@
struct RestoreHeightLookup {
NetworkType::Type type;
QMap<int, int> data;
QMap<time_t, int> data;
explicit RestoreHeightLookup(NetworkType::Type type) : type(type) {}
int dateToHeight(int date) {
int dateToHeight(time_t date) {
// restore height based on a given timestamp using a lookup
// table. If it cannot find the date in the lookup table, it
// will calculate the blockheight based off the last known
@@ -28,34 +28,43 @@ struct RestoreHeightLookup {
}
int blockTime = 120;
int blocksPerDay = 86400 / blockTime;
int blocksPerDay = 720;
int blockCalcClearance = blocksPerDay * 5;
QList<int> values = this->data.keys();
if (date <= values.at(0))
return this->data[values.at(0)];
QList<time_t> values = this->data.keys();
// If timestamp is before epoch, return genesis height.
if (date <= values.at(0)) {
return 1;
}
for (int i = 0; i != values.count(); i++) {
if(values[i] > date) {
return i - 1 < 0 ? this->data[values[i]] : this->data[values[i-1]] - blockCalcClearance;
if (values[i] > date) {
if (i == 0) {
return 1;
}
return this->data[values[i-1]] - blockCalcClearance;
}
}
// lookup failed, calculate blockheight from last known checkpoint
int lastBlockHeightTime = values.at(values.count() - 1);
time_t lastBlockHeightTime = values.last();
int lastBlockHeight = this->data[lastBlockHeightTime];
int deltaTime = date - lastBlockHeightTime;
time_t deltaTime = date - lastBlockHeightTime;
int deltaBlocks = deltaTime / blockTime;
int blockHeight = (lastBlockHeight + deltaBlocks) - blockCalcClearance;
qDebug() << "Calculated blockheight: " << blockHeight << " from epoch " << date;
return blockHeight;
}
int heightToTimestamp(int height) {
time_t heightToTimestamp(int height) {
// @TODO: most likely inefficient, refactor
QMap<int, int>::iterator i;
int timestamp = 0;
QMap<time_t, int>::iterator i;
time_t timestamp = 0;
int heightData = 1;
for (i = this->data.begin(); i != this->data.end(); ++i) {
int ts = i.key();
time_t ts = i.key();
if (i.value() > height)
return timestamp;
timestamp = ts;
@@ -77,12 +86,14 @@ struct RestoreHeightLookup {
static RestoreHeightLookup *fromFile(const QString &fn, NetworkType::Type type) {
// initialize this class using a lookup table, e.g `:/assets/restore_heights_monero_mainnet.txt`/
auto rtn = new RestoreHeightLookup(type);
auto data = Utils::barrayToString(Utils::fileOpen(fn));
QMap<int, int> _data;
for(const auto &line: data.split('\n')) {
if(line.trimmed().isEmpty()) continue;
auto data= Utils::barrayToString(Utils::fileOpen(fn));
for (const auto &line: data.split('\n')) {
if (line.trimmed().isEmpty()) {
continue;
}
auto spl = line.trimmed().split(':');
rtn->data[spl.at(0).toInt()] = spl.at(1).toInt();
rtn->data[(time_t)spl.at(0).toInt()] = spl.at(1).toInt();
}
return rtn;
}
+106
View File
@@ -0,0 +1,106 @@
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-FileCopyrightText: 2020-2022 The Monero Project
#include "Seed.h"
Seed::Seed(Type type, NetworkType::Type networkType, QString language)
: type(type), networkType(networkType), language(std::move(language))
{
// We only support the creation of Tevador-style seeds for now.
if (this->type != Type::TEVADOR) {
this->errorString = "Unsupported seed type";
return;
}
// We only support the creation of English language seeds for now.
if (this->language != "English") {
this->errorString = "Unsupported seed language";
return;
}
this->time = std::time(nullptr);
try {
monero_seed seed(this->time, constants::coinName);
std::stringstream buffer;
buffer << seed;
this->mnemonic = QString::fromStdString(buffer.str()).split(" ");
buffer.str(std::string());
buffer << seed.key();
this->spendKey = QString::fromStdString(buffer.str());
}
catch (const std::exception &e) {
this->errorString = QString::fromStdString(e.what());
return;
}
// Basic check against seed library implementation issues
if (m_insecureSeeds.contains(this->spendKey)) {
this->errorString = "Insecure spendkey";
return;
}
this->setRestoreHeight();
}
Seed::Seed(Type type, QStringList mnemonic, NetworkType::Type networkType)
: type(type), mnemonic(std::move(mnemonic)), networkType(networkType)
{
if (m_seedLength[this->type] != this->mnemonic.length()) {
this->errorString = "Invalid seed length";
return;
}
if (this->type == Type::POLYSEED) {
this->errorString = "Unsupported seed type";
return;
}
if (this->type == Type::TEVADOR) {
try {
monero_seed seed(this->mnemonic.join(" ").toStdString(), constants::coinName);
this->time = seed.date();
this->setRestoreHeight();
std::stringstream buffer;
buffer << seed.key();
this->spendKey = QString::fromStdString(buffer.str());
// Tevador style seeds have built-in error correction
// Any word can be replaced with 'xxxx' to recover the original word
this->correction = QString::fromStdString(seed.correction());
if (!this->correction.isEmpty()) {
buffer.str(std::string());
buffer << seed;
int index = this->mnemonic.indexOf("xxxx");
this->mnemonic.replace(index, this->correction);
}
}
catch (const std::exception &e) {
this->errorString = e.what();
return;
}
}
}
void Seed::setRestoreHeight(int height) {
auto now = std::time(nullptr);
auto nowClearance = 3600 * 24;
auto currentBlockHeight = appData()->restoreHeights[this->networkType]->dateToHeight(now - nowClearance);
if (height >= currentBlockHeight + nowClearance) {
qWarning() << "unrealistic restore height detected, setting to current blockheight instead: " << currentBlockHeight;
this->restoreHeight = currentBlockHeight;
} else {
this->restoreHeight = height;
}
}
void Seed::setRestoreHeight() {
// Ignore the embedded restore date, new wallets should sync from the current block height.
this->restoreHeight = appData()->restoreHeights[networkType]->dateToHeight(this->time);
}
Seed::Seed() = default;
+62
View File
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-FileCopyrightText: 2020-2022 The Monero Project
#ifndef FEATHER_SEED_H
#define FEATHER_SEED_H
#include "constants.h"
#include "libwalletqt/Wallet.h"
#include "monero_seed/monero_seed.hpp"
#include "utils/AppData.h"
#include <QString>
#include <QStringList>
#include <sstream>
#include <utility>
struct Seed {
enum Type {
MONERO = 0, // 25 word seeds
TEVADOR, // 14 word seeds
POLYSEED, // 16 word seeds
};
NetworkType::Type networkType = NetworkType::MAINNET;
QString coin;
QString language;
Type type = Type::TEVADOR;
QStringList mnemonic;
QString spendKey;
QString correction;
time_t time{};
int restoreHeight = 0;
QString errorString;
explicit Seed();
explicit Seed(Type type, NetworkType::Type networkType = NetworkType::MAINNET, QString language = "English");
explicit Seed(Type type, QStringList mnemonic, NetworkType::Type networkType = NetworkType::MAINNET);
void setRestoreHeight(int height);
private:
void setRestoreHeight();
QStringList m_insecureSeeds = {
"0000000000000000000000000000000000000000000000000000000000000000",
"1111111111111111111111111111111111111111111111111111111111111111",
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
};
QMap<Seed::Type, int> m_seedLength {
{Type::MONERO, 25},
{Type::TEVADOR, 14},
{Type::POLYSEED, 16}
};
};
#endif //FEATHER_SEED_H