ledger-core
BitcoinLikeUtxoPicker.h
1 /*
2  *
3  * BitcoinLikeUtxoPicker.h
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 28/03/2018.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2017 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 
32 #ifndef LEDGER_CORE_BITCOINLIKEUTXOPICKER_H
33 #define LEDGER_CORE_BITCOINLIKEUTXOPICKER_H
34 
35 #include <async/DedicatedContext.hpp>
36 #include "BitcoinLikeTransactionBuilder.h"
37 #include <wallet/bitcoin/keychains/BitcoinLikeKeychain.hpp>
38 #include <wallet/bitcoin/types.h>
39 #include <wallet/bitcoin/explorers/BitcoinLikeBlockchainExplorer.hpp>
40 #include <api/Currency.hpp>
41 #include <async/Future.hpp>
42 #include <api/BitcoinLikeOutput.hpp>
43 
44 
45 namespace ledger {
46  namespace core {
47  class BitcoinLikeTransactionApi;
48  class BitcoinLikeWritableInputApi;
49  using BitcoinLikeGetUtxoFunction = std::function<Future<std::vector<std::shared_ptr<api::BitcoinLikeOutput>>> ()>;
50  using BitcoinLikeGetTxFunction = std::function<FuturePtr<BitcoinLikeBlockchainExplorerTransaction> (const std::string&)>;
51 
52  class BitcoinLikeUtxoPicker : public DedicatedContext, public std::enable_shared_from_this<BitcoinLikeUtxoPicker> {
53  public:
55  const std::shared_ptr<api::ExecutionContext> &context,
56  const api::Currency& currency
57  );
58  virtual BitcoinLikeTransactionBuildFunction getBuildFunction(
59  const BitcoinLikeGetUtxoFunction& getUtxo,
60  const BitcoinLikeGetTxFunction& getTransaction,
61  const std::shared_ptr<BitcoinLikeBlockchainExplorer>& explorer,
62  const std::shared_ptr<BitcoinLikeKeychain>& keychain,
63  const uint64_t currentBlockHeight,
64  const std::shared_ptr<spdlog::logger>& logger,
65  bool partial);
66  const api::Currency& getCurrency() const;
67 
68  protected:
69  using UTXODescriptor = std::tuple<std::string, int32_t, uint32_t >;
70  using UTXODescriptorList = std::vector<UTXODescriptor>;
71  struct Buddy {
72  Buddy(
74  const BitcoinLikeGetUtxoFunction& g,
75  const BitcoinLikeGetTxFunction& tx,
76  const std::shared_ptr<BitcoinLikeBlockchainExplorer>& e,
77  const std::shared_ptr<BitcoinLikeKeychain>& k,
78  const std::shared_ptr<spdlog::logger>& l,
79  std::shared_ptr<BitcoinLikeTransactionApi> t,
80  bool partial) : request(r), explorer(e), keychain(k), transaction(t), getUtxo(g),
81  getTransaction(tx), logger(l), isPartial(partial)
82  {
83  if(request.wipe) {
84  outputAmount = ledger::core::BigInt::ZERO;
85  } else {
86  for (auto& output : r.outputs)
87  outputAmount = outputAmount + *std::get<0>(output);
88  }
89  }
91  BitcoinLikeGetUtxoFunction getUtxo;
92  BitcoinLikeGetTxFunction getTransaction;
93  std::shared_ptr<BitcoinLikeBlockchainExplorer> explorer;
94  std::shared_ptr<BitcoinLikeKeychain> keychain;
95  std::shared_ptr<BitcoinLikeTransactionApi> transaction;
96  BigInt outputAmount;
97  std::shared_ptr<spdlog::logger> logger;
98  BigInt changeAmount;
99  bool isPartial;
100  };
101 
102  virtual Future<Unit> fillInputs(const std::shared_ptr<Buddy>& buddy);
103  virtual Future<UTXODescriptorList> filterInputs(const std::shared_ptr<Buddy>& buddy) = 0;
104  virtual Future<Unit> fillOutputs(const std::shared_ptr<Buddy>& buddy);
105  virtual Future<Unit> fillTransactionInfo(const std::shared_ptr<Buddy>& buddy);
106 
107  private:
108  Future<Unit> fillInput(const std::shared_ptr<Buddy>& buddy, const UTXODescriptor& desc);
109  BitcoinLikeGetUtxoFunction createFilteredUtxoFunction(const BitcoinLikeTransactionBuildRequest& buddy,
110  const BitcoinLikeGetUtxoFunction& getUtxo);
111  private:
112  api::Currency _currency;
113  };
114  }
115 }
116 
117 
118 #endif //LEDGER_CORE_BITCOINLIKEUTXOPICKER_H
Definition: BitcoinLikeUtxoPicker.h:71
Definition: BitcoinLikeUtxoPicker.h:52
Definition: Deffered.hpp:49
Definition: BitcoinLikeTransactionBuilder.h:50
Definition: Currency.hpp:23
Definition: Account.cpp:8
Definition: DedicatedContext.hpp:39
Definition: BigInt.h:56
Definition: logger.hpp:44