32 #ifndef LEDGER_CORE_ABSTRACTEXTENDEDPUBLICKEY_H 33 #define LEDGER_CORE_ABSTRACTEXTENDEDPUBLICKEY_H 37 #include <math/Base58.hpp> 39 #include <crypto/SECP256k1Point.hpp> 40 #include <crypto/HashAlgorithm.h> 41 #include <crypto/DeterministicPublicKey.hpp> 42 #include <crypto/RIPEMD160.hpp> 44 #include <utils/Option.hpp> 45 #include <utils/DerivationPath.hpp> 47 #include <bytes/BytesReader.h> 49 #include <api/Currency.hpp> 50 #include <collections/DynamicObject.hpp> 53 template <
class NetworkParameters>
58 if (index >= childNums.size()) {
61 return _derive(index + 1, childNums, key.derive(childNums[index]));
64 std::string toBase58() {
65 auto config = std::make_shared<DynamicObject>();
66 config->putString(
"networkIdentifier", params().Identifier);
67 return Base58::encodeWithChecksum(getKey().toByteArray(params().XPUBVersion), config);
72 const NetworkParameters ¶ms,
73 const optional<std::vector<uint8_t>> &parentPublicKey,
74 const std::vector<uint8_t> &publicKey,
75 const std::vector<uint8_t> &chainCode,
76 const std::string &path) {
77 uint32_t parentFingerprint = 0;
79 if (parentPublicKey) {
82 auto hash = hashAlgorithm.bytesToBytesHash(ppp.toByteArray(
true));
83 hash = RIPEMD160::hash(hash);
84 parentFingerprint = ((hash[0] & 0xFFU) << 24) |
85 ((hash[1] & 0xFFU) << 16) |
86 ((hash[2] & 0xFFU) << 8) |
90 return DeterministicPublicKey(pk.toByteArray(
true), chainCode, p.getLastChildNum(), p.getDepth(), parentFingerprint, params.Identifier);
95 const NetworkParameters ¶ms,
96 const std::string &xpubBase58,
98 const std::string &networkBase58Dictionary =
"") {
100 auto config = std::make_shared<DynamicObject>();
101 config->putString(
"networkIdentifier", params.Identifier);
102 if (!networkBase58Dictionary.empty()) {
103 config->putString(
"base58Dictionary", networkBase58Dictionary);
105 auto decodeResult = Base58::checkAndDecode(xpubBase58, config);
106 if (decodeResult.isFailure())
107 throw decodeResult.getFailure();
111 auto version = reader.
read(params.XPUBVersion.size());
112 if (version != params.XPUBVersion) {
116 auto depth = reader.readNextByte();
118 auto fingerprint = reader.readNextBeUint();
120 auto childNum = reader.readNextBeUint();
122 auto chainCode = reader.read(32);
124 auto publicKey = reader.readUntilEnd();
128 std::vector<uint8_t> derivePublicKey(
const std::string &path) {
130 auto key = _derive(0, p.toVector(), getKey());
131 return key.getPublicKey();
134 std::vector<uint8_t> deriveHash160(
const std::string &path) {
136 auto key = _derive(0, p.toVector(), getKey());
137 return key.getPublicKeyHash160();
141 virtual const NetworkParameters ¶ms()
const = 0;
150 #endif //LEDGER_CORE_ABSTRACTEXTENDEDPUBLICKEY_H Definition: DeterministicPublicKey.hpp:38
std::vector< uint8_t > read(unsigned long length)
Definition: BytesReader.cpp:71
Definition: SECP256k1Point.hpp:40
Definition: AbstractExtendedPublicKey.h:54
Definition: DerivationPath.hpp:40
Definition: BytesReader.h:47
Definition: Currency.hpp:23
Definition: Account.cpp:8
Definition: HashAlgorithm.h:39
Definition: optional.hpp:177
Definition: Exception.hpp:45