Files

129 lines
3.7 KiB
C++
Raw Permalink Normal View History

2019-03-17 22:52:09 +01:00
/*
* This file is part of the Flowee project
2021-06-20 22:44:44 +02:00
* Copyright (C) 2019-2020 Tom Zander <tom@flowee.org>
2019-03-17 22:52:09 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef WALLET_H
#define WALLET_H
2020-10-26 21:53:49 +01:00
#include <QString>
2019-03-17 22:52:09 +01:00
#include <map>
/*
* it stores private keys
I guess encryption of this may be nice.
* it stores public keys
* it stores transactions applicable to the keys it owns.
store by block-height and offset-in-block
* it stores the last block it saw (block-id)
* I should also be able to fund a Transaction Creator, which selects from
funds and then signs.
when starting I connect it and it starts by checking which blocks it has not
seen yet and download those and find any transactions in them.
I need some way to get a list of unspend outputs
*/
#include <boost/filesystem/path.hpp>
2023-11-24 18:16:32 +01:00
#include <primitives/PrivateKey.h>
2023-11-24 18:01:36 +01:00
#include <primitives/PublicKey.h>
2019-11-13 15:11:39 +01:00
#include <primitives/script.h>
2019-03-17 22:52:09 +01:00
#include <primitives/Tx.h>
2019-03-17 22:52:09 +01:00
class TransactionBuilder;
class Wallet
{
public:
2020-10-26 21:53:49 +01:00
Wallet(const QString &dbFile);
2019-03-17 22:52:09 +01:00
~Wallet();
2022-07-06 22:12:33 +02:00
void addKey(const PrivateKey &key, int blockheight = -1);
2022-07-06 21:52:47 +02:00
void addOutput(int blockHeight, const uint256 &txid, int offsetInBlock, int outIndex, int64_t amount, const KeyId &destAddress, const CScript &script);
2019-03-17 22:52:09 +01:00
void addOutput(const uint256 &txid, int outIndex, int64_t amount, int keyId, short unconfirmedDepth, const CScript &script);
void clearUnconfirmedUTXOs();
int keyCount() const {
return static_cast<int>(m_keys.size());
}
2022-07-06 22:12:33 +02:00
const PrivateKey *privateKey(int keyId) const;
2019-03-17 22:52:09 +01:00
int firstEmptyPubKey() const;
std::vector<int> publicKeys() const;
2022-07-06 21:56:34 +02:00
const PublicKey &publicKey(int id) const;
2019-03-17 22:52:09 +01:00
void saveKeys();
void saveCache();
uint256 lastCachedBlock() const;
void setLastCachedBlock(const uint256 &lastCachedBlock);
struct UnspentOutput {
uint256 prevTxId;
short index, keyId, unconfirmedDepth;
int coinbaseHeight;
int64_t amount;
CScript prevOutScript;
};
const std::list<UnspentOutput> &unspentOutputs() const;
std::list<UnspentOutput>::const_iterator spendOutput(const std::list<UnspentOutput>::const_iterator &output);
private:
2020-10-26 21:53:49 +01:00
const QString m_dbFile;
2019-03-17 22:52:09 +01:00
void loadKeys();
2022-07-06 22:12:33 +02:00
std::list<std::pair<int, PrivateKey> > m_keys; // private keys.
2019-03-17 22:52:09 +01:00
struct WalletPubKey {
2022-07-06 21:56:34 +02:00
WalletPubKey(const PublicKey &pk) : pubKey(pk), bitcoinAddress(pk.getKeyId()) {}
PublicKey pubKey;
2022-07-06 21:52:47 +02:00
KeyId bitcoinAddress;
2019-03-17 22:52:09 +01:00
// cache
int64_t value = 0;
};
std::map<short, WalletPubKey> m_pubkeys;
struct ValueTransfer {
ValueTransfer(int keyId, int64_t amount) : keyId(keyId), amount(amount) {}
int keyId = -1;
int64_t amount = 0;
};
struct WalletItem { // aka a tx that touches one of our private-keys.
int blockHeight = -1;
uint32_t byteOffsetInblock = 0;
// cache
uint256 txid;
std::vector<ValueTransfer> valueTransfer;
};
std::list<WalletItem> m_walletItems;
// cache
std::list<UnspentOutput> m_unspentOutputs;
uint256 m_lastCachedBlock;
bool m_privKeysNeedsSave = false;
};
#endif