6da5090202
This allows people to validate and convert a deriviation path from a string to a vector of numbers.
83 lines
2.7 KiB
C++
83 lines
2.7 KiB
C++
/*
|
|
* This file is part of the Flowee project
|
|
* Copyright (C) 2021 Tom Zander <tom@flowee.org>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#ifndef HDMASTERKEY_H
|
|
#define HDMASTERKEY_H
|
|
|
|
#include "primitives/key.h"
|
|
#include <string>
|
|
|
|
/**
|
|
* Hierarchically Deterministic wallet Master key.
|
|
* This class stores a HD wallet masterkey and allow you to derive any child key
|
|
* from it.
|
|
* This implements BIP32 and combined with the fromMnemonic() method this supports BIP39 based wallets.
|
|
*/
|
|
class HDMasterKey
|
|
{
|
|
public:
|
|
/// Adding this value to a path-element makes it 'hardened'
|
|
static const uint32_t Hardened = 0x80000000;
|
|
|
|
HDMasterKey(const HDMasterKey &other) = default;
|
|
|
|
/**
|
|
* Convert a string based derivation path into a vector of fields.
|
|
* This throws an exception when the string is not a fully valid derivation path.
|
|
*/
|
|
static std::vector<uint32_t> deriveFromString(const std::string &path);
|
|
|
|
/**
|
|
* Create a masterkey from a known-good mnemonic.
|
|
* @see Mnemonic::validateMnemonic() to make sure the string is good.
|
|
* @param phrase a UTF8-encoded multi-word mnemonic phrase
|
|
*/
|
|
static HDMasterKey fromMnemonic(const std::string &phrase, const std::string &password = std::string());
|
|
static HDMasterKey fromSeed(const std::vector<uint8_t> &seed);
|
|
|
|
/**
|
|
* Used in the string output methods.
|
|
*/
|
|
enum Chain {
|
|
Testnet,
|
|
MainChain
|
|
};
|
|
/// returns true if the key is valid.
|
|
bool isValid() const;
|
|
|
|
std::string toString(Chain chain = MainChain) const; ///< output xpub pubkey key as text
|
|
std::string privToString(Chain chain = MainChain) const; ///< output xprv private key as text
|
|
|
|
CKey derive(const std::vector<uint32_t> &path) const;
|
|
CKey derive(const std::string &path) const;
|
|
|
|
HDMasterKey &operator=(const HDMasterKey &other) = default;
|
|
friend bool operator==(const HDMasterKey &a, const HDMasterKey &b) {
|
|
return memcmp(&a.m_fingerprint[0], &b.m_fingerprint[0], 4) == 0 && a.m_chaincode == b.m_chaincode && a.m_key == b.m_key;
|
|
}
|
|
|
|
protected:
|
|
HDMasterKey();
|
|
|
|
private:
|
|
uint8_t m_fingerprint[4];
|
|
ChainCode m_chaincode;
|
|
CKey m_key;
|
|
};
|
|
|
|
#endif
|