ledger-core
BitcoinLikeTransactionApi.h
1 /*
2  *
3  * BitcoinLikeTransactionApi
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 12/07/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 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 #ifndef LEDGER_CORE_BITCOINLIKETRANSACTIONAPI_H
32 #define LEDGER_CORE_BITCOINLIKETRANSACTIONAPI_H
33 
34 #include <api/BitcoinLikeTransaction.hpp>
35 #include <wallet/common/api_impl/OperationApi.h>
36 #include <wallet/bitcoin/explorers/BitcoinLikeBlockchainExplorer.hpp>
37 #include "BitcoinLikeInputApi.h"
38 #include "BitcoinLikeOutputApi.h"
39 #include <api/BitcoinLikeBlock.hpp>
40 #include "BitcoinLikeBlockApi.h"
41 #include <api/EstimatedSize.hpp>
42 #include <wallet/bitcoin/api_impl/BitcoinLikeWritableInputApi.h>
43 #include <api/KeychainEngines.hpp>
44 #include <api/BitcoinLikeSignature.hpp>
45 #include <api/BitcoinLikeSignatureState.hpp>
46 
47 namespace ledger {
48  namespace core {
49 
51  uint32_t sequence;
52  std::string address;
53  std::string previousTxHash;
54  int32_t outputIndex;
55  std::vector<std::vector<uint8_t>> pubKeys;
57 
58  BitcoinLikePreparedInput() = default;
59 
60  BitcoinLikePreparedInput(uint32_t sequence_,
61  const std::string &address_,
62  const std::string &previousTxHash_,
63  int32_t outputIndex_,
64  std::vector<std::vector<uint8_t>> pubKeys_,
65  BitcoinLikeBlockchainExplorerOutput output_) : sequence(sequence_),
66  address(address_),
67  previousTxHash(previousTxHash_),
68  outputIndex(outputIndex_),
69  pubKeys(pubKeys_) {
70  output.value = output_.value;
71  output.index = output_.index;
72  output.address = output_.address;
73  output.script = output_.script;
74  output.transactionHash = output_.transactionHash;
75  output.time = output_.time;
76 
77  }
78  };
79 
80  class BytesWriter;
81 
83  public:
84  explicit BitcoinLikeTransactionApi(const api::Currency &currency,
85  const std::string &keychainEngine = api::KeychainEngines::BIP32_P2PKH,
86  uint64_t currentBlockHeight = 0);
87 
88  explicit BitcoinLikeTransactionApi(const std::shared_ptr<OperationApi> &operation);
89 
90  std::vector<std::shared_ptr<api::BitcoinLikeInput>> getInputs() override;
91 
92  std::vector<std::shared_ptr<api::BitcoinLikeOutput>> getOutputs() override;
93 
94  std::shared_ptr<api::BitcoinLikeBlock> getBlock() override;
95 
96  int64_t getLockTime() override;
97 
98  std::shared_ptr<api::Amount> getFees() override;
99 
100  std::string getHash() override;
101 
102  std::chrono::system_clock::time_point getTime() override;
103 
104  optional<int32_t> getTimestamp() override;
105 
106  std::vector<uint8_t> serialize() override;
107 
108  optional<std::vector<uint8_t>> getWitness() override;
109 
110  api::EstimatedSize getEstimatedSize() override;
111 
112  std::vector<uint8_t> serializeOutputs() override;
113 
114  int32_t getVersion() override;
115 
116  api::BitcoinLikeSignatureState setSignatures(const std::vector<api::BitcoinLikeSignature> & signatures, bool override = false) override;
117 
118  api::BitcoinLikeSignatureState setDERSignatures(const std::vector<std::vector<uint8_t>> & signatures, bool override = false) override;
119 
120  BitcoinLikeTransactionApi &addInput(const std::shared_ptr<BitcoinLikeWritableInputApi> &input);
121 
122  BitcoinLikeTransactionApi &addOutput(const std::shared_ptr<api::BitcoinLikeOutput> &output);
123 
124  BitcoinLikeTransactionApi &setLockTime(uint32_t lockTime);
125 
126  BitcoinLikeTransactionApi &setVersion(uint32_t version);
127 
128  BitcoinLikeTransactionApi &setTimestamp(uint32_t timestamp);
129 
130  BitcoinLikeTransactionApi &setHash(const std::string &hash);
131 
132 
133  static std::shared_ptr<api::BitcoinLikeTransaction> parseRawTransaction(const api::Currency &currency,
134  const std::vector<uint8_t> &rawTransaction,
135  std::experimental::optional<int32_t> currentBlockHeight,
136  bool isSigned);
137 
138  static std::shared_ptr<api::BitcoinLikeTransaction> parseRawSignedTransaction(const api::Currency &currency,
139  const std::vector<uint8_t> &rawTransaction,
140  std::experimental::optional<int32_t> currentBlockHeight);
141 
142  static api::EstimatedSize estimateSize(std::size_t inputCount,
143  std::size_t outputCount,
144  const api::Currency &currency,
145  const std::string &keychainEngine);
146 
147  private:
148  inline bool isWriteable() const;
149 
150  inline bool isReadOnly() const;
151 
152  inline void serializeProlog(BytesWriter &out);
153 
154  inline void serializeInputs(BytesWriter &out);
155 
156  inline void serializeOutputs(BytesWriter &out);
157 
158  inline void serializeEpilogue(BytesWriter &out);
159 
160  private:
161  int32_t _version;
162  std::vector<std::shared_ptr<api::BitcoinLikeInput>> _inputs;
163  std::vector<std::shared_ptr<api::BitcoinLikeOutput>> _outputs;
164  int32_t _lockTime;
165  std::shared_ptr<api::Amount> _fees;
166  std::chrono::system_clock::time_point _time;
167  std::shared_ptr<BitcoinLikeBlockApi> _block;
168  std::string _hash;
169  api::Currency _currency;
171  Option<uint32_t> _timestamp;
172  bool _writable;
173  std::string _keychainEngine;
174  uint64_t _currentBlockHeight;
175  };
176  }
177 }
178 
179 
180 #endif //LEDGER_CORE_BITCOINLIKETRANSACTIONAPI_H
Definition: EstimatedSize.hpp:18
Definition: BitcoinLikeBlockchainExplorer.hpp:65
Definition: BitcoinLikeTransactionApi.h:50
Definition: BitcoinLikeTransaction.hpp:32
Definition: BytesWriter.h:44
Definition: Currency.hpp:23
Definition: Account.cpp:8
Definition: BitcoinLikeTransactionApi.h:82
Definition: optional.hpp:177
Definition: BitcoinLikeNetworkParameters.hpp:17