Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Mnemonic support for wallet entropy #117

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ BLSCT_H = \
blsct/wallet/address.h \
blsct/wallet/hdchain.h \
blsct/wallet/helpers.h \
blsct/wallet/import_wallet_type.h \
blsct/wallet/keyman.h \
blsct/wallet/keyring.h \
blsct/wallet/txfactory.h \
Expand Down
2 changes: 1 addition & 1 deletion src/bench/wallet_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static void WalletCreate(benchmark::Bench& bench, bool encrypted)

fs::path wallet_path = test_setup->m_path_root / strprintf("test_wallet_%d", random.rand32()).c_str();
bench.run([&] {
auto wallet = CreateWallet(context, wallet_path.utf8string(), /*load_on_start=*/std::nullopt, options, status, error_string, warnings);
auto wallet = CreateWallet(context, wallet_path.utf8string(), {}, blsct::IMPORT_MASTER_KEY, /*load_on_start=*/std::nullopt, options, status, error_string, warnings);
assert(status == DatabaseStatus::SUCCESS);
assert(wallet != nullptr);

Expand Down
1 change: 1 addition & 0 deletions src/bitcoin-wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
argsman.AddArg("-debug=<category>", "Output debugging information (default: 0).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
argsman.AddArg("-descriptors", "Create descriptors wallet. Only for 'create'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-blsct", "Create blsct wallet. Only for 'create'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-seed", "Seed used for the wallet creation. Only for 'create'. Can be a master seed or an audit key.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-legacy", "Create legacy wallet. Only for 'create'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-format=<format>", "The format of the wallet file to create. Either \"bdb\" or \"sqlite\". Only used with 'createfromdump'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
Expand Down
16 changes: 16 additions & 0 deletions src/blsct/wallet/import_wallet_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2023 The Navio developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef NAVIO_BLSCT_IMPORT_WALLET_TYPE_H
#define NAVIO_BLSCT_IMPORT_WALLET_TYPE_H

namespace blsct {
enum SeedType {
IMPORT_MASTER_KEY,
IMPORT_VIEW_KEY
};

} // namespace blsct

#endif // NAVIO_BLSCT_KEYMAN_H
48 changes: 36 additions & 12 deletions src/blsct/wallet/keyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void KeyMan::SetHDSeed(const PrivateKey& key)
throw std::runtime_error(std::string(__func__) + ": AddSpendKey failed");

if (!AddViewKey(viewKey, viewKey.GetPublicKey()))
throw std::runtime_error(std::string(__func__) + ": AddKeyPubKey failed");
throw std::runtime_error(std::string(__func__) + ": AddViewKey failed");

if (!AddKeyPubKey(tokenKey, tokenKey.GetPublicKey()))
throw std::runtime_error(std::string(__func__) + ": AddKeyPubKey failed");
Expand All @@ -261,13 +261,39 @@ void KeyMan::SetHDSeed(const PrivateKey& key)
wallet::WalletBatch batch(m_storage.GetDatabase());
}

bool KeyMan::SetupGeneration(bool force)
bool KeyMan::SetupGeneration(const std::vector<unsigned char>& seed, const SeedType& type, bool force)
{
if ((CanGenerateKeys() && !force) || m_storage.IsLocked()) {
return false;
}

SetHDSeed(GenerateNewSeed());
if (seed.size() == 32) {
if (type == IMPORT_MASTER_KEY) {
MclScalar scalarSeed;
scalarSeed.SetVch(seed);
SetHDSeed(scalarSeed);
}
} else if (seed.size() == 80) {
if (type == IMPORT_VIEW_KEY) {
std::vector<unsigned char> viewVch(seed.begin(), seed.begin() + 32);
std::vector<unsigned char> spendingVch(seed.begin() + 32, seed.end());

MclScalar scalarView;
scalarView.SetVch(viewVch);

MclG1Point pointSpending;
pointSpending.SetVch(spendingVch);

if (!AddViewKey(scalarView, PrivateKey(scalarView).GetPublicKey()))
throw std::runtime_error(std::string(__func__) + ": AddViewKey failed");

if (!AddSpendKey(pointSpending))
throw std::runtime_error(std::string(__func__) + ": AddSpendKey failed");
}
} else {
SetHDSeed(GenerateNewSeed());
}

if (!NewSubAddressPool() || !NewSubAddressPool(-1) || !NewSubAddressPool(-2)) {
return false;
}
Expand Down Expand Up @@ -460,17 +486,15 @@ blsct::PrivateKey KeyMan::GetMasterSeedKey() const

blsct::PrivateKey KeyMan::GetPrivateViewKey() const
{
if (!IsHDEnabled())
throw std::runtime_error(strprintf("%s: the wallet has no HD enabled"));
if (!fViewKeyDefined)
throw std::runtime_error(strprintf("%s: the wallet has no view key available"));

auto viewId = m_hd_chain.view_id;

PrivateKey ret;

if (!GetKey(viewId, ret))
throw std::runtime_error(strprintf("%s: could not access the private view key", __func__));
return viewKey;
}

return ret;
blsct::PublicKey KeyMan::GetPublicSpendingKey() const
{
return spendPublicKey;
}

blsct::PrivateKey KeyMan::GetSpendingKey() const
Expand Down
6 changes: 4 additions & 2 deletions src/blsct/wallet/keyman.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <blsct/wallet/address.h>
#include <blsct/wallet/hdchain.h>
#include <blsct/wallet/helpers.h>
#include <blsct/wallet/import_wallet_type.h>
#include <blsct/wallet/keyring.h>
#include <logging.h>
#include <wallet/crypter.h>
Expand All @@ -34,7 +35,7 @@ class Manager
explicit Manager(wallet::WalletStorage& storage) : m_storage(storage) {}
virtual ~Manager(){};

virtual bool SetupGeneration(bool force = false) { return false; }
virtual bool SetupGeneration(const std::vector<unsigned char>& seed, const SeedType& type, bool force = false) { return false; }

/* Returns true if HD is enabled */
virtual bool IsHDEnabled() const { return false; }
Expand Down Expand Up @@ -72,7 +73,7 @@ class KeyMan : public Manager, public KeyRing
KeyMan(wallet::WalletStorage& storage, int64_t keypool_size)
: Manager(storage), KeyRing(), m_keypool_size(keypool_size) {}

bool SetupGeneration(bool force = false) override;
bool SetupGeneration(const std::vector<unsigned char>& seed, const SeedType& type = IMPORT_MASTER_KEY, bool force = false) override;
bool IsHDEnabled() const override;

/* Returns true if the wallet can generate new keys */
Expand Down Expand Up @@ -136,6 +137,7 @@ class KeyMan : public Manager, public KeyRing
CTxDestination GetDestination(const CTxOut& txout) const;
blsct::PrivateKey GetMasterSeedKey() const;
blsct::PrivateKey GetPrivateViewKey() const;
blsct::PublicKey GetPublicSpendingKey() const;
blsct::PrivateKey GetSpendingKey() const;
blsct::PrivateKey GetSpendingKeyForOutput(const CTxOut& out) const;
blsct::PrivateKey GetSpendingKeyForOutput(const CTxOut& out, const CKeyID& id) const;
Expand Down
15 changes: 10 additions & 5 deletions src/blsct/wallet/txfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@ class TxFactoryBase
{
protected:
CMutableTransaction tx;
std::map<TokenId, std::vector<UnsignedOutput>> vOutputs;
std::map<TokenId, std::vector<UnsignedInput>> vInputs;
std::map<TokenId, Amounts> nAmounts;
std::map<TokenId, std::vector<UnsignedOutput>>
vOutputs;
std::map<TokenId, std::vector<UnsignedInput>>
vInputs;
std::map<TokenId, Amounts>
nAmounts;

public:
TxFactoryBase(){};

void AddOutput(const SubAddress& destination, const CAmount& nAmount, std::string sMemo, const TokenId& token_id = TokenId(), const CreateTransactionType& type = NORMAL, const CAmount& minStake = 0, const bool& fSubtractFeeFromAmount = false);
bool AddInput(const CAmount& amount, const MclScalar& gamma, const blsct::PrivateKey& spendingKey, const TokenId& token_id, const COutPoint& outpoint, const bool& rbf = false);
std::optional<CMutableTransaction> BuildTx(const blsct::DoublePublicKey& changeDestination, const CAmount& minStake = 0, const CreateTransactionType& type = NORMAL, const bool& fSubtractedFee = false);
static std::optional<CMutableTransaction> CreateTransaction(const std::vector<InputCandidates>& inputCandidates, const blsct::DoublePublicKey& changeDestination, const SubAddress& destination, const CAmount& nAmount, std::string sMemo, const TokenId& token_id = TokenId(), const CreateTransactionType& type = NORMAL, const CAmount& minStake = 0);
std::optional<CMutableTransaction>
BuildTx(const blsct::DoublePublicKey& changeDestination, const CAmount& minStake = 0, const CreateTransactionType& type = NORMAL, const bool& fSubtractedFee = false);
static std::optional<CMutableTransaction>
CreateTransaction(const std::vector<InputCandidates>& inputCandidates, const blsct::DoublePublicKey& changeDestination, const SubAddress& destination, const CAmount& nAmount, std::string sMemo, const TokenId& token_id = TokenId(), const CreateTransactionType& type = NORMAL, const CAmount& minStake = 0);
static void AddAvailableCoins(wallet::CWallet* wallet, blsct::KeyMan* blsct_km, const wallet::CoinFilterParams& coins_params, std::vector<InputCandidates>& inputCandidates) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet);
static void AddAvailableCoins(wallet::CWallet* wallet, blsct::KeyMan* blsct_km, const TokenId& token_id, const CreateTransactionType& type, std::vector<InputCandidates>& inputCandidates) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet);
};
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define BITCOIN_INTERFACES_WALLET_H

#include <addresstype.h>
#include <blsct/wallet/import_wallet_type.h>
#include <consensus/amount.h>
#include <interfaces/chain.h>
#include <pubkey.h>
Expand Down Expand Up @@ -323,7 +324,7 @@ class WalletLoader : public ChainClient
{
public:
//! Create new wallet.
virtual util::Result<std::unique_ptr<Wallet>> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, std::vector<bilingual_str>& warnings) = 0;
virtual util::Result<std::unique_ptr<Wallet>> createWallet(const std::string& name, const SecureString& passphrase, const std::vector<unsigned char>& seed, const blsct::SeedType& type, uint64_t wallet_creation_flags, std::vector<bilingual_str>& warnings) = 0;

//! Load existing wallet.
virtual util::Result<std::unique_ptr<Wallet>> loadWallet(const std::string& name, std::vector<bilingual_str>& warnings) = 0;
Expand Down
27 changes: 27 additions & 0 deletions src/mnemonic/language.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2023-2023 The Navcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_MNEMONIC_LANGUAGE_H
#define BITCOIN_MNEMONIC_LANGUAGE_H

namespace mnemonic {

typedef enum class language
{
en,
es,
it,
fr,
cs,
pt,
ja,
ko,
zh_Hans,
zh_Hant,
none
} language;

} // namespace mnemonic

#endif // BITCOIN_MNEMONIC_LANGUAGE_H
9 changes: 9 additions & 0 deletions src/mnemonic/lexicon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2023-2023 The Navcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <mnemonic/lexicon.h>

namespace mnemonic {

} // namespace mnemonic
33 changes: 33 additions & 0 deletions src/mnemonic/lexicon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2023-2023 The Navcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_MNEMONIC_LEXICON_H
#define BITCOIN_MNEMONIC_LEXICON_H

#include <array>

#include <mnemonic/language.h>

namespace mnemonic {

template <std::size_t Size>
class lexicon final
{
public:
typedef std::array<const char*, Size> words;

private:
// This dictionary creates only this one word of state.
const language identifier_;

// Arrays of words are declared statically and held by reference here.
// The array type is POD, so no words are copied into the array. Only
// this wrapper dictionary object is created for each word list, for
// each dictionaries object constructed by various mnemonic classes.
const words& words_;
};

} // namespace mnemonic

#endif // BITCOIN_MNEMONIC_LEXICON_H
Loading
Loading