ledger-core
BitcoinLikeScript.h
1 /*
2  *
3  * BitcoinLikeScript.h
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 03/04/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_BITCOINLIKESCRIPT_H
33 #define LEDGER_CORE_BITCOINLIKESCRIPT_H
34 
35 #include <utils/Try.hpp>
36 #include <utils/Either.hpp>
37 #include "operators.h"
38 #include <list>
39 #include <api/BitcoinLikeNetworkParameters.hpp>
40 #include <bitcoin/BitcoinLikeAddress.hpp>
41 
42 namespace ledger {
43  namespace core {
44 
45  using BitcoinLikeScriptOpCode = btccore::opcodetype;
46 
48  public:
49  explicit BitcoinLikeScriptChunk(BitcoinLikeScriptOpCode op);
50 
51  explicit BitcoinLikeScriptChunk(const std::vector<uint8_t> &bytes);
52 
53  const std::vector<uint8_t> &getBytes() const;
54 
55  bool isBytes() const;
56 
57  BitcoinLikeScriptOpCode getOpCode() const;
58 
59  bool isEqualTo(btccore::opcodetype code) const;
60 
61  bool sizeEqualsTo(std::size_t size) const;
62 
63  bool isOpCode() const;
64 
65  private:
66  Either<std::vector<uint8_t>, BitcoinLikeScriptOpCode> _value;
67  };
68 
70  bool isSigned;
71  std::string keychainEngine;
72 
73  BitcoinLikeScriptConfiguration(bool isSigned_,
74  const std::string &keychainEngine_) : isSigned(isSigned_),
75  keychainEngine(keychainEngine_) {
76  if (isSigned_ && keychainEngine_.empty()) {
77  throw make_exception(api::ErrorCode::INVALID_ARGUMENT,
78  "Keychain engine required when constructing signed scripts");
79  }
80  }
81 
83  this->isSigned = copy.isSigned;
84  this->keychainEngine = copy.keychainEngine;
85  }
86 
88  this->isSigned = copy.isSigned;
89  this->keychainEngine = copy.keychainEngine;
90  return *this;
91  }
92  };
93 
95  public:
96  BitcoinLikeScript() : _configuration(BitcoinLikeScriptConfiguration(false, "")) {};
97 
98  BitcoinLikeScript(const BitcoinLikeScriptConfiguration &configuration) : _configuration(configuration) {};
99 
100  BitcoinLikeScript &operator<<(btccore::opcodetype op_code);
101 
102  BitcoinLikeScript &operator<<(const std::vector<uint8_t> &bytes);
103 
104  const BitcoinLikeScriptChunk &operator[](int index) const;
105 
106  std::size_t size() const;
107 
108  std::string toString() const;
109 
110  std::vector<uint8_t> serialize() const;
111 
112  const std::list<BitcoinLikeScriptChunk> &toList() const;
113 
114  bool isP2PKH() const;
115 
116  bool isP2SH() const;
117 
118  bool isP2WPKH() const;
119 
120  bool isP2WSH() const;
121 
122  Option<BitcoinLikeAddress> parseAddress(const api::Currency &currency) const;
123 
124  static Try<BitcoinLikeScript> parse(const std::vector<uint8_t> &script,
126  false, ""));
127 
128  static BitcoinLikeScript fromAddress(const std::string &address, const api::Currency &currency);
129 
130  private:
131  std::list<BitcoinLikeScriptChunk> _chunks;
132  BitcoinLikeScriptConfiguration _configuration;
133  };
134  }
135 }
136 
137 
138 #endif //LEDGER_CORE_BITCOINLIKESCRIPT_H
Definition: Try.hpp:49
Definition: Either.hpp:43
Definition: BitcoinLikeScript.h:47
Definition: Option.hpp:49
Definition: BitcoinLikeScript.h:69
Definition: Currency.hpp:23
Definition: BitcoinLikeScript.h:94
Definition: Account.cpp:8