forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDerivationPath.h
More file actions
102 lines (76 loc) · 3.21 KB
/
Copy pathDerivationPath.h
File metadata and controls
102 lines (76 loc) · 3.21 KB
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
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#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 { return static_cast<TWPurpose>(indices[0].value); }
void setPurpose(TWPurpose v) { indices[0] = DerivationPathIndex(v, /* hardened: */ true); }
TWCoinType coin() const { return static_cast<TWCoinType>(indices[1].value); }
void setCoin(TWCoinType v) { indices[1] = DerivationPathIndex(v, /* hardened: */ true); }
uint32_t account() const { return indices[2].value; }
void setAccount(uint32_t v) { indices[2] = DerivationPathIndex(v, /* hardened: */ true); }
uint32_t change() const { return indices[3].value; }
void setChange(uint32_t v) { indices[3] = DerivationPathIndex(v, /* hardened: */ false); }
uint32_t address() const { return indices[4].value; }
void setAddress(uint32_t v) { 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, TWCoinType 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