ledger-core
BitcoinLikeTransactionBuilder.h
1 /*
2  *
3  * BitcoinLikeTransactionBuilder.h
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 27/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_BITCOINLIKETRANSACTIONBUILDER_H
33 #define LEDGER_CORE_BITCOINLIKETRANSACTIONBUILDER_H
34 
35 #include <api/BitcoinLikeTransactionBuilder.hpp>
36 #include <api/BitcoinLikePickingStrategy.hpp>
37 #include <api/BitcoinLikeNetworkParameters.hpp>
38 #include <api/BitcoinLikeTransaction.hpp>
39 #include <api/Amount.hpp>
40 #include <functional>
41 #include <list>
42 #include <async/Future.hpp>
43 #include <math/BigInt.h>
44 #include <spdlog/logger.h>
45 #include <api/Currency.hpp>
46 
47 namespace ledger {
48  namespace core {
49 
51  BitcoinLikeTransactionBuildRequest(const std::shared_ptr<BigInt>& minChange);
52  std::list<std::tuple<std::string, int32_t, uint32_t>> inputs;
53  std::list<std::tuple<std::shared_ptr<BigInt>, std::shared_ptr<api::BitcoinLikeScript>>> outputs;
54  std::list<std::string> changePaths;
55  std::list<std::tuple<std::string, int32_t>> excludedUtxo;
56  int32_t changeCount;
57  std::shared_ptr<BigInt> feePerByte;
59  std::shared_ptr<BigInt> maxChange;
60  std::shared_ptr<BigInt> minChange;
61  bool wipe;
62  };
63 
64  using BitcoinLikeTransactionBuildFunction = std::function<Future<std::shared_ptr<api::BitcoinLikeTransaction>> (const BitcoinLikeTransactionBuildRequest&)>;
65 
66  class BitcoinLikeTransactionBuilder : public api::BitcoinLikeTransactionBuilder, public std::enable_shared_from_this<BitcoinLikeTransactionBuilder> {
67  public:
69  const std::shared_ptr<api::ExecutionContext>& context,
70  const api::Currency& params,
71  const std::shared_ptr<spdlog::logger>& logger,
72  const BitcoinLikeTransactionBuildFunction& buildFunction);
74  std::shared_ptr<api::BitcoinLikeTransactionBuilder>
75  addInput(const std::string &transactionHash, int32_t index, int32_t sequence) override;
76 
77  std::shared_ptr<api::BitcoinLikeTransactionBuilder> addOutput(const std::shared_ptr<api::Amount> &amount,
78  const std::shared_ptr<api::BitcoinLikeScript> &script) override;
79 
80  std::shared_ptr<api::BitcoinLikeTransactionBuilder> addChangePath(const std::string &path) override;
81 
82  std::shared_ptr<api::BitcoinLikeTransactionBuilder>
83  excludeUtxo(const std::string &transactionHash, int32_t outputIndex) override;
84 
85  std::shared_ptr<api::BitcoinLikeTransactionBuilder> setNumberOfChangeAddresses(int32_t count) override;
86 
87  std::shared_ptr<api::BitcoinLikeTransactionBuilder>
88  pickInputs(api::BitcoinLikePickingStrategy strategy, int32_t sequence) override;
89 
90  std::shared_ptr<api::BitcoinLikeTransactionBuilder>
91  sendToAddress(const std::shared_ptr<api::Amount> &amount, const std::string &address) override;
92 
93  std::shared_ptr<api::BitcoinLikeTransactionBuilder>
94  wipeToAddress(const std::string &address) override;
95 
96  std::shared_ptr<api::BitcoinLikeTransactionBuilder>
97  setFeesPerByte(const std::shared_ptr<api::Amount> &fees) override;
98 
99  std::shared_ptr<api::BitcoinLikeTransactionBuilder>
100  setMaxAmountOnChange(const std::shared_ptr<api::Amount> &amount) override;
101 
102  std::shared_ptr<api::BitcoinLikeTransactionBuilder>
103  setMinAmountOnChange(const std::shared_ptr<api::Amount> &amount) override;
104 
105  std::shared_ptr<api::BitcoinLikeTransactionBuilder> clone() override;
106 
107  void reset() override;
108 
109  void build(const std::shared_ptr<api::BitcoinLikeTransactionCallback> &callback) override;
111  private:
112  api::Currency _currency;
113  std::shared_ptr<api::BitcoinLikeScript> createSendScript(const std::string &address);
114  BitcoinLikeTransactionBuildFunction _build;
116  std::shared_ptr<api::ExecutionContext> _context;
117  std::shared_ptr<spdlog::logger> _logger;
118 
119  };
120  }
121 }
122 
123 #endif //LEDGER_CORE_BITCOINLIKETRANSACTIONBUILDER_H
Definition: BitcoinLikeTransactionBuilder.h:66
Definition: Option.hpp:49
Definition: Deffered.hpp:49
Definition: BitcoinLikeTransactionBuilder.h:50
Definition: BitcoinLikeTransactionBuilder.hpp:29
Definition: Currency.hpp:23
Definition: Account.cpp:8
Definition: logger.hpp:44