forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDerivationPath.h
162 lines (134 loc) · 3.97 KB
/
DerivationPath.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
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
#pragma once
#include <TrustWalletCore/TWCoinType.h>
#include <TrustWalletCore/TWPurpose.h>
#include <cstdint>
#include <initializer_list>
#include <string>
#include <vector>
namespace TW {
struct DerivationPathIndex {
uint32_t value = 0;
bool hardened = true;
DerivationPathIndex() = default;
DerivationPathIndex(uint32_t value, bool hardened = true)
: value(value), hardened(hardened) {}
/// The derivation index.
uint32_t derivationIndex() const {
if (hardened) {
return value | 0x80000000;
} else {
return value;
}
}
std::string string() const {
if (hardened) {
return std::to_string(value) + "'";
} else {
return std::to_string(value);
}
}
};
/// A BIP32 HD wallet derivation path.
struct DerivationPath {
std::vector<DerivationPathIndex> indices;
TWPurpose purpose() const {
if (indices.size() == 0) {
return TWPurposeBIP44;
}
return static_cast<TWPurpose>(indices[0].value);
}
void setPurpose(TWPurpose v) {
if (indices.size() == 0) {
return;
}
indices[0] = DerivationPathIndex(v, /* hardened: */ true);
}
uint32_t coin() const {
if (indices.size() <= 1) {
return TWCoinTypeBitcoin;
}
return indices[1].value;
}
void setCoin(uint32_t v) {
if (indices.size() <= 1) {
return;
}
indices[1] = DerivationPathIndex(v, /* hardened: */ true);
}
uint32_t account() const {
if (indices.size() <= 2) {
return 0;
}
return indices[2].value;
}
void setAccount(uint32_t v) {
if (indices.size() <= 2) {
return;
}
indices[2] = DerivationPathIndex(v, /* hardened: */ true);
}
uint32_t change() const {
if (indices.size() <= 3) {
return 0;
}
return indices[3].value;
}
void setChange(uint32_t v) {
if (indices.size() <= 3) {
return;
}
indices[3] = DerivationPathIndex(v, /* hardened: */ false);
}
uint32_t address() const {
if (indices.size() <= 4) {
return 0;
}
return indices[4].value;
}
void setAddress(uint32_t v) {
if (indices.size() <= 4) {
return;
}
indices[4] = DerivationPathIndex(v, /* hardened: */ false);
}
DerivationPath() = default;
explicit DerivationPath(std::initializer_list<DerivationPathIndex> l)
: indices(l) {}
explicit DerivationPath(std::vector<DerivationPathIndex> indices)
: indices(std::move(indices)) {}
/// Creates a `DerivationPath` by BIP44 components.
DerivationPath(TWPurpose purpose, uint32_t coin, uint32_t account, uint32_t change,
uint32_t address)
: indices(std::vector<DerivationPathIndex>(5)) {
setPurpose(purpose);
setCoin(coin);
setAccount(account);
setChange(change);
setAddress(address);
}
/// Creates a derivation path with a string description like `m/10/0/2'/3`
///
/// \throws std::invalid_argument if the string is not a valid derivation
/// path.
explicit DerivationPath(const std::string& string);
/// String representation.
std::string string() const noexcept;
};
inline bool operator==(const DerivationPathIndex& lhs, const DerivationPathIndex& rhs) {
return lhs.value == rhs.value && lhs.hardened == rhs.hardened;
}
inline bool operator==(const DerivationPath& lhs, const DerivationPath& rhs) {
return std::equal(lhs.indices.begin(), lhs.indices.end(), rhs.indices.begin(),
rhs.indices.end());
}
} // namespace TW
/// Wrapper for C interface.
struct TWDerivationPath {
TW::DerivationPath impl;
};
struct TWDerivationPathIndex {
TW::DerivationPathIndex impl;
};