1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#pragma once
8

9
#include "Data.h"
10
#include "DerivationPath.h"
11
#include "Hash.h"
12
#include "PrivateKey.h"
13
#include "PublicKey.h"
14

15
#include <TrustWalletCore/TWCoinType.h>
16
#include <TrustWalletCore/TWCurve.h>
17
#include <TrustWalletCore/TWHDVersion.h>
18
#include <TrustWalletCore/TWPurpose.h>
19

20
#include <array>
21
#include <optional>
22
#include <string>
23

24
namespace TW {
25

26
class HDWallet {
27
  public:
28
    static constexpr size_t seedSize = 64;
29
    static constexpr size_t maxMnemomincSize = 240;
30
    static constexpr size_t maxExtendedKeySize = 128;
31

32
  public:
33
    /// Wallet seed.
34
    std::array<byte, seedSize> seed;
35

36
    /// Mnemonic word list.
37
    std::string mnemonic;
38

39
    /// Mnemonic passphrase.
40
    std::string passphrase;
41

42
    /// Entropy bytes (11 bits from each word)
43
    TW::Data entropy;
44

45
  public:
46
    /// Determines if a mnemonic phrase is valid.
47
    static bool isValid(const std::string& mnemonic);
48

49
    /// Initializes a new random HDWallet with the provided strength in bits.
50
    HDWallet(int strength, const std::string& passphrase);
51

52
    /// Initializes an HDWallet from a mnemonic seed.
53
    HDWallet(const std::string& mnemonic, const std::string& passphrase);
54

55
    /// Initializes an HDWallet from a seed.
56
    HDWallet(const Data& data, const std::string& passphrase);
57

58
    HDWallet(const HDWallet& other) = default;
59
    HDWallet(HDWallet&& other) = default;
60
    HDWallet& operator=(const HDWallet& other) = default;
61
    HDWallet& operator=(HDWallet&& other) = default;
62

63
    virtual ~HDWallet();
64

65
    void updateEntropy();
66

67
    /// Returns master key.
68
    PrivateKey getMasterKey(TWCurve curve) const;
69

70
    /// Returns the master private key extension (32 byte).
71
    PrivateKey getMasterKeyExtension(TWCurve curve) const;
72

73
    /// Returns the private key at the given derivation path.
74
    PrivateKey getKey(const TWCoinType coin, const DerivationPath& derivationPath) const;
75

76
    /// Derives the address for a coin.
77
    std::string deriveAddress(TWCoinType coin) const;
78

79
    /// Returns the extended private key.
80
    std::string getExtendedPrivateKey(TWPurpose purpose, TWCoinType coin, TWHDVersion version) const;
81

82
    /// Returns the exteded public key.
83
    std::string getExtendedPublicKey(TWPurpose purpose, TWCoinType coin, TWHDVersion version) const;
84

85
    /// Computes the public key from an exteded public key representation.
86
    static std::optional<PublicKey> getPublicKeyFromExtended(const std::string& extended, TWCoinType coin, const DerivationPath& path);
87

88
    /// Computes the private key from an exteded private key representation.
89
    static std::optional<PrivateKey> getPrivateKeyFromExtended(const std::string& extended, TWCoinType coin, const DerivationPath& path);
90

91
  public:
92
    // Private key type (later could be moved out of HDWallet)
93
    enum PrivateKeyType {
94
      PrivateKeyTypeDefault32 = 0, // 32-byte private key
95
      PrivateKeyTypeExtended96 = 1, // 3*32-byte extended private key
96
    };
97
    
98
    // obtain privateKeyType used by the coin/curve
99
    static PrivateKeyType getPrivateKeyType(TWCurve curve);
100
};
101

102
} // namespace TW
103

104
/// Wrapper for C interface.
105 1
struct TWHDWallet {
106
    TW::HDWallet impl;
107
};

Read our documentation on viewing source code .

Loading