/* * This file is part of the Flowee project * Copyright (C) 2019-2020 Tom Zander * * 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 . */ #ifndef WALLET_H #define WALLET_H #include #include /* * 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 #include #include #include #include class TransactionBuilder; class Wallet { public: Wallet(const QString &dbFile); ~Wallet(); void addKey(const PrivateKey &key, int blockheight = -1); void addOutput(int blockHeight, const uint256 &txid, int offsetInBlock, int outIndex, int64_t amount, const KeyId &destAddress, const CScript &script); 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(m_keys.size()); } const PrivateKey *privateKey(int keyId) const; int firstEmptyPubKey() const; std::vector publicKeys() const; const PublicKey &publicKey(int id) const; 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 &unspentOutputs() const; std::list::const_iterator spendOutput(const std::list::const_iterator &output); private: const QString m_dbFile; void loadKeys(); std::list > m_keys; // private keys. struct WalletPubKey { WalletPubKey(const PublicKey &pk) : pubKey(pk), bitcoinAddress(pk.getKeyId()) {} PublicKey pubKey; KeyId bitcoinAddress; // cache int64_t value = 0; }; std::map 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; }; std::list m_walletItems; // cache std::list m_unspentOutputs; uint256 m_lastCachedBlock; bool m_privKeysNeedsSave = false; }; #endif