Skip to content

Commit

Permalink
#624 stored nonce seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
anatolse committed May 27, 2019
1 parent 729f87e commit 3e52f20
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
42 changes: 34 additions & 8 deletions wallet/base_transaction.cpp
Expand Up @@ -68,9 +68,16 @@ namespace beam::wallet

///

LocalPrivateKeyKeeper::LocalPrivateKeyKeeper(Key::IKdf::Ptr kdf)
: m_MasterKdf(kdf)
namespace
{
const char* LOCAL_NONCE_SEEDS = "NonceSeeds";
}

LocalPrivateKeyKeeper::LocalPrivateKeyKeeper(IWalletDB::Ptr walletDB)
: m_WalletDB(walletDB)
, m_MasterKdf(walletDB->get_MasterKdf())
{
LoadNonceSeeds();
}

void LocalPrivateKeyKeeper::GenerateKey(const vector<Key::IDV>& ids, bool createCoinKey, Callback<PublicKeys>&& resultCallback, ExceptionCallback&& exceptionCallback)
Expand Down Expand Up @@ -103,10 +110,6 @@ namespace beam::wallet

size_t LocalPrivateKeyKeeper::AllocateNonceSlot()
{
if (m_Nonces.empty())
{
return 0;
}
if (m_Nonces.size() == numeric_limits<uint8_t>::max())
{
throw runtime_error("has no place for nonces");
Expand Down Expand Up @@ -135,7 +138,10 @@ namespace beam::wallet
NoLeak<Hash::Value> hvRandom;
ECC::GenRandom(hvRandom.V);

m_Nonces.insert({ i, hvRandom });
m_Nonces.insert({ i, hvRandom.V });

SaveNonceSeeds();

return i;
}

Expand Down Expand Up @@ -216,6 +222,26 @@ namespace beam::wallet
return Scalar(partialSignature);
}

void LocalPrivateKeyKeeper::LoadNonceSeeds()
{
ByteBuffer buffer;
if (m_WalletDB->getBlob(LOCAL_NONCE_SEEDS, buffer) && !buffer.empty())
{
Deserializer d;
d.reset(buffer);
d & m_Nonces;
}
}

void LocalPrivateKeyKeeper::SaveNonceSeeds()
{
Serializer s;
s& m_Nonces;
ByteBuffer buffer;
s.swap_buf(buffer);
m_WalletDB->setVarRaw(LOCAL_NONCE_SEEDS, buffer.data(), buffer.size());
}

////

ECC::uintBig LocalPrivateKeyKeeper::GetSeedKid(Key::IPKdf& tagKdf, const Point& commitment) const
Expand Down Expand Up @@ -245,7 +271,7 @@ namespace beam::wallet
auto randomValue = m_Nonces[slot];

NoLeak<Scalar::Native> nonce;
m_MasterKdf->DeriveKey(nonce.V, randomValue.V);
m_MasterKdf->DeriveKey(nonce.V, randomValue);
return nonce.V;
}

Expand Down
6 changes: 4 additions & 2 deletions wallet/base_transaction.h
Expand Up @@ -125,7 +125,7 @@ namespace beam::wallet
class LocalPrivateKeyKeeper : public IPrivateKeyKeeper
{
public:
LocalPrivateKeyKeeper(Key::IKdf::Ptr kdf);
LocalPrivateKeyKeeper(IWalletDB::Ptr walletDB);
private:
void GenerateKey(const std::vector<Key::IDV>& ids, bool createCoinKey, Callback<PublicKeys>&& resultCallback, ExceptionCallback&& exceptionCallback) override;
//void GenerateRangeProof(Height schemeHeight, const std::vector<Key::IDV>& ids, Callback<RangeProofs>&&, ExceptionCallback&&) override;
Expand All @@ -143,11 +143,13 @@ namespace beam::wallet
Key::IKdf::Ptr GetChildKdf(Key::Index iKdf) const;
ECC::Scalar::Native GetNonce(size_t slot);
ECC::Scalar::Native GetExcess(const std::vector<Key::IDV>& inputs, const std::vector<Key::IDV>& outputs, const ECC::Scalar::Native& offset) const;
void LoadNonceSeeds();
void SaveNonceSeeds();
private:
IWalletDB::Ptr m_WalletDB;
Key::IKdf::Ptr m_MasterKdf;

std::map<size_t, ECC::NoLeak<ECC::Hash::Value>> m_Nonces;
std::map<size_t, ECC::Hash::Value> m_Nonces;
};

std::string GetFailureMessage(TxFailureReason reason);
Expand Down
2 changes: 1 addition & 1 deletion wallet/unittests/wallet_test_environment.cpp
Expand Up @@ -346,7 +346,7 @@ struct TestWalletRig
{
TestWalletRig(const string& name, IWalletDB::Ptr walletDB, Wallet::TxCompletedAction&& action = Wallet::TxCompletedAction(), bool coldWallet = false, bool oneTimeBbsEndpoint = false)
: m_WalletDB{ walletDB }
, m_KeyKeeper{ make_shared<wallet::LocalPrivateKeyKeeper>(walletDB->get_MasterKdf()) }
, m_KeyKeeper{ make_shared<wallet::LocalPrivateKeyKeeper>(walletDB) }
, m_Wallet{ m_WalletDB, move(action), coldWallet ? []() {io::Reactor::get_Current().stop(); } : Wallet::UpdateCompletedAction() }
{
if (m_WalletDB->get_MasterKdf()) // can create secrets
Expand Down
2 changes: 1 addition & 1 deletion wallet/wallet.cpp
Expand Up @@ -102,7 +102,7 @@ namespace beam

Wallet::Wallet(IWalletDB::Ptr walletDB, TxCompletedAction&& action, UpdateCompletedAction&& updateCompleted)
: m_WalletDB{ walletDB }
, m_KeyKeeper{ walletDB->get_MasterKdf() ? make_shared<LocalPrivateKeyKeeper>(walletDB->get_MasterKdf()): IPrivateKeyKeeper::Ptr() }
, m_KeyKeeper{ walletDB->get_MasterKdf() ? make_shared<LocalPrivateKeyKeeper>(walletDB): IPrivateKeyKeeper::Ptr() }
, m_TxCompletedAction{move(action)}
, m_UpdateCompleted{move(updateCompleted)}
, m_LastSyncTotal(0)
Expand Down

0 comments on commit 3e52f20

Please sign in to comment.