ledger-core
BitcoinLikeAddress.hpp
1 /*
2  *
3  * BitcoinLikeAddress
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 12/12/2016.
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_BITCOINLIKEADDRESS_HPP
32 #define LEDGER_CORE_BITCOINLIKEADDRESS_HPP
33 
34 #include "../api/BitcoinLikeAddress.hpp"
35 #include "../api/BitcoinLikeNetworkParameters.hpp"
36 #include "../utils/optional.hpp"
37 #include <wallet/common/AbstractAddress.h>
38 #include <api/BitcoinLikeExtendedPublicKey.hpp>
39 #include <collections/DynamicObject.hpp>
40 namespace ledger {
41  namespace core {
43  public:
44  BitcoinLikeAddress(const api::Currency& currency,
45  const std::vector<uint8_t>& hash160,
46  const std::string &keychainEngine,
47  const Option<std::string>& derivationPath = Option<std::string>());
48  virtual std::vector<uint8_t> getVersion() override;
49  std::vector<uint8_t> getVersionFromKeychainEngine(const std::string &keychainEngine,
50  const api::BitcoinLikeNetworkParameters &params) const;
51  virtual std::vector<uint8_t> getHash160() override;
53  virtual std::string toBase58() override;
54  virtual std::string toBech32() override;
55  std::string toBase58() const;
56  std::string toBech32() const;
57  virtual bool isP2SH() override;
58  virtual bool isP2PKH() override;
59  virtual bool isP2WSH() override;
60  virtual bool isP2WPKH() override;
61  virtual optional<std::string> getDerivationPath() override;
62 
63  std::string toString() override;
64  std::string getStringAddress() const;
65 
66  static std::shared_ptr<AbstractAddress> parse(const std::string& address, const api::Currency& currency,
67  const Option<std::string>& derivationPath = Option<std::string>());
68  static std::shared_ptr<BitcoinLikeAddress> fromBase58(const std::string& address,
69  const api::Currency& currency,
70  const Option<std::string>& derivationPath = Option<std::string>());
71 
72  static std::shared_ptr<BitcoinLikeAddress> fromBech32(const std::string& address,
73  const api::Currency& currency,
74  const Option<std::string>& derivationPath = Option<std::string>());
75 
76  static std::string fromPublicKey(const std::shared_ptr<api::BitcoinLikeExtendedPublicKey> &pubKey,
77  const api::Currency &currency,
78  const std::string &derivationPath,
79  const std::string &keychainEngine);
80 
81  static std::vector<uint8_t> fromPublicKeyToHash160(const std::vector<uint8_t> &pubKey,
82  const std::vector<uint8_t> &pubKeyHash160,
83  const api::Currency &currency,
84  const std::string &keychainEngine);
85 
86  static std::vector<uint8_t> fromPublicKeyToHash160(const std::vector<uint8_t> &pubKey,
87  const api::Currency &currency,
88  const std::string &keychainEngine);
89 
90 
91  private:
92  const std::vector<uint8_t> _hash160;
94  const Option<std::string> _derivationPath;
95  const std::string _keychainEngine;
96  };
97  }
98 }
99 
100 
101 #endif //LEDGER_CORE_BITCOINLIKEADDRESS_HPP
virtual std::string toBech32() override
Definition: BitcoinLikeAddress.cpp:105
virtual optional< std::string > getDerivationPath() override
Definition: BitcoinLikeAddress.cpp:125
virtual bool isP2SH() override
Definition: BitcoinLikeAddress.cpp:109
virtual std::string toBase58() override
Definition: BitcoinLikeAddress.cpp:90
virtual std::vector< uint8_t > getVersion() override
Definition: BitcoinLikeAddress.cpp:61
virtual bool isP2PKH() override
Definition: BitcoinLikeAddress.cpp:113
Definition: AbstractAddress.h:42
virtual bool isP2WSH() override
Definition: BitcoinLikeAddress.cpp:117
virtual std::vector< uint8_t > getHash160() override
Definition: BitcoinLikeAddress.cpp:82
virtual api::BitcoinLikeNetworkParameters getNetworkParameters() override
Definition: BitcoinLikeAddress.cpp:86
Definition: Currency.hpp:23
Definition: Account.cpp:8
Definition: BitcoinLikeAddress.hpp:42
std::string toString() override
Definition: BitcoinLikeAddress.cpp:154
virtual bool isP2WPKH() override
Definition: BitcoinLikeAddress.cpp:121
Definition: BitcoinLikeAddress.hpp:23
Definition: optional.hpp:177
Definition: BitcoinLikeNetworkParameters.hpp:17