forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHDWallet.h
149 lines (111 loc) · 6.13 KB
/
HDWallet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
#pragma once
#include "Data.h"
#include "DerivationPath.h"
#include "Hash.h"
#include "PrivateKey.h"
#include "PublicKey.h"
#include <TrustWalletCore/TWCoinType.h>
#include <TrustWalletCore/TWCurve.h>
#include <TrustWalletCore/TWHDVersion.h>
#include <TrustWalletCore/TWPurpose.h>
#include <TrustWalletCore/TWDerivation.h>
#include <array>
#include <optional>
#include <string>
namespace TW {
template<size_t seedSize = 64>
class HDWallet {
public:
static constexpr size_t mSeedSize = seedSize;
static constexpr size_t maxMnemomincSize = 240;
static constexpr size_t maxExtendedKeySize = 128;
private:
/// Wallet seed, derived one-way from the mnemonic and passphrase
std::array<byte, seedSize> seed;
/// Mnemonic word list (aka. recovery phrase).
std::string mnemonic;
/// Passphrase for mnemonic encryption.
std::string passphrase;
/// Entropy is the binary 1-to-1 representation of the mnemonic (11 bits from each word)
TW::Data entropy;
public:
const std::array<byte, seedSize>& getSeed() const { return seed; }
const std::string& getMnemonic() const { return mnemonic; }
const std::string& getPassphrase() const { return passphrase; }
const TW::Data& getEntropy() const { return entropy; }
public:
/// Initializes an HDWallet from given seed.
HDWallet(const Data& seed);
/// Initializes a new random HDWallet with the provided strength in bits.
/// Throws on invalid strength.
HDWallet(int strength, const std::string& passphrase);
/// Initializes an HDWallet from a BIP39 mnemonic and a passphrase, check English dict by default.
/// Throws on invalid mnemonic.
HDWallet(const std::string& mnemonic, const std::string& passphrase, const bool check = true);
/// Initializes an HDWallet from an entropy.
/// Throws on invalid data.
HDWallet(const Data& entropy, const std::string& passphrase);
HDWallet(const HDWallet& other) = default;
HDWallet(HDWallet&& other) = default;
HDWallet& operator=(const HDWallet& other) = default;
HDWallet& operator=(HDWallet&& other) = default;
virtual ~HDWallet();
/// Returns master key.
PrivateKey getMasterKey(TWCurve curve) const;
/// Returns the master private key extension (32 byte).
PrivateKey getMasterKeyExtension(TWCurve curve) const;
/// Returns the private key with the given derivation.
PrivateKey getKey(const TWCoinType coin, TWDerivation derivation) const;
/// Returns the private key at the given derivation path.
PrivateKey getKey(const TWCoinType coin, const DerivationPath& derivationPath) const;
/// Returns the private key at the given derivation path and curve.
PrivateKey getKeyByCurve(TWCurve curve, const DerivationPath& derivationPath) const;
/// Derives the address for a coin (default derivation).
std::string deriveAddress(TWCoinType coin) const;
/// Derives the address for a coin with given derivation.
std::string deriveAddress(TWCoinType coin, TWDerivation derivation) const;
/// Returns the extended private key for default 0 account with the given derivation.
std::string getExtendedPrivateKeyDerivation(TWPurpose purpose, TWCoinType coin, TWDerivation derivation, TWHDVersion version) const {
return getExtendedPrivateKeyAccount(purpose, coin, derivation, version, 0);
}
/// Returns the extended public key for default 0 account with the given derivation.
std::string getExtendedPublicKeyDerivation(TWPurpose purpose, TWCoinType coin, TWDerivation derivation, TWHDVersion version) const {
return getExtendedPublicKeyAccount(purpose, coin, derivation, version, 0);
}
/// Returns the extended private key for default 0 account; derivation path used is "m/purpose'/coin'/0'".
std::string getExtendedPrivateKey(TWPurpose purpose, TWCoinType coin, TWHDVersion version) const {
return getExtendedPrivateKeyAccount(purpose, coin, TWDerivationDefault, version, 0);
}
/// Returns the extended public key for default 0 account; derivation path used is "m/purpose'/coin'/0'".
std::string getExtendedPublicKey(TWPurpose purpose, TWCoinType coin, TWHDVersion version) const {
return getExtendedPublicKeyAccount(purpose, coin, TWDerivationDefault, version, 0);
}
/// Returns the extended private key for a custom account; derivation path used is "m/purpose'/coin'/account'".
std::string getExtendedPrivateKeyAccount(TWPurpose purpose, TWCoinType coin, TWDerivation derivation, TWHDVersion version, uint32_t account) const;
/// Returns the extended public key for a custom account; derivation path used is "m/purpose'/coin'/account'".
std::string getExtendedPublicKeyAccount(TWPurpose purpose, TWCoinType coin, TWDerivation derivation, TWHDVersion version, uint32_t account) const;
/// Returns the BIP32 Root Key (private)
std::string getRootKey(TWCoinType coin, TWHDVersion version) const;
/// Computes the public key from an extended public key representation.
static std::optional<PublicKey> getPublicKeyFromExtended(const std::string& extended, TWCoinType coin, const DerivationPath& path);
/// Computes the private key from an extended private key representation.
static std::optional<PrivateKey> getPrivateKeyFromExtended(const std::string& extended, TWCoinType coin, const DerivationPath& path);
/// Derive the given seed for the given coin, with the given Derivation path
/// \param coin Coin to be used in order to retrieve the curve type
/// \param seed Custom seed to be used for the derivation, can be a mnemonic seed as well as an ethereum signature seed
/// \param path The derivation path to use
/// \return The computed private key
static PrivateKey bip32DeriveRawSeed(TWCoinType coin, const Data& seed, const DerivationPath& path);
private:
void updateSeedAndEntropy(bool check = true);
// For Cardano, derive 2nd staking derivation path from the primary one
static DerivationPath cardanoStakingDerivationPath(const DerivationPath& path);
};
} // namespace TW
/// Wrapper for C interface.
struct TWHDWallet {
TW::HDWallet<> impl;
};