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 "Account.h"
10
#include "EncryptionParameters.h"
11
#include "../Data.h"
12
#include "../HDWallet.h"
13

14
#include <TrustWalletCore/TWCoinType.h>
15
#include <nlohmann/json.hpp>
16

17
#include <optional>
18
#include <string>
19

20
namespace TW::Keystore {
21

22
/// An stored key can be either a private key or a mnemonic phrase for a HD
23
/// wallet.
24
enum class StoredKeyType { privateKey, mnemonicPhrase };
25

26
/// Represents a key stored as an encrypted file.
27 1
class StoredKey {
28
public:
29
    /// Type of key stored.
30
    StoredKeyType type;
31

32
    /// Unique identifier.
33
    std::optional<std::string> id;
34

35
    /// Name.
36
    std::string name;
37

38
    /// Encrypted payload.
39
    EncryptionParameters payload;
40

41
    /// Active accounts.
42
    std::vector<Account> accounts;
43

44
    /// Create a new StoredKey, with the given name, mnemonic and password.
45
    /// @throws std::invalid_argument if mnemonic is invalid
46
    static StoredKey createWithMnemonic(const std::string& name, const Data& password, const std::string& mnemonic);
47

48
    /// Create a new StoredKey, with the given name, mnemonic and password.
49
    /// @throws std::invalid_argument if mnemonic is invalid
50
    static StoredKey createWithMnemonicRandom(const std::string& name, const Data& password);
51

52
    /// Create a new StoredKey, with the given name, mnemonic and password, and also add the default address for the given coin..
53
    /// @throws std::invalid_argument if mnemonic is invalid
54
    static StoredKey createWithMnemonicAddDefaultAddress(const std::string& name, const Data& password, const std::string& mnemonic, TWCoinType coin);
55

56
    /// Create a new StoredKey, with the given name and private key.
57
    /// @throws std::invalid_argument if privateKeyData is not a vald private key
58
    static StoredKey createWithPrivateKey(const std::string& name, const Data& password, const Data& privateKeyData);
59

60
    /// Create a new StoredKey, with the given name and private key, and also add the default address for the given coin..
61
    /// @throws std::invalid_argument if privateKeyData is not a vald private key
62
    static StoredKey createWithPrivateKeyAddDefaultAddress(const std::string& name, const Data& password, TWCoinType coin, const Data& privateKeyData);
63

64
    /// Create a StoredKey from a JSON object.
65
    static StoredKey createWithJson(const nlohmann::json& json);
66

67
    /// Returns the HDWallet for this key.
68
    ///
69
    /// @throws std::invalid_argument if this key is of a type other than `mnemonicPhrase`.
70
    const HDWallet wallet(const Data& password) const;
71

72
    /// Returns the account for a specific coin, creating it if necessary and
73
    /// the provided wallet is not `nullptr`.
74
    std::optional<const Account> account(TWCoinType coin, const HDWallet* wallet);
75

76
    /// Returns the account for a specific coin if it exists.
77
    std::optional<const Account> account(TWCoinType coin) const;
78
    
79
    /// Add an account
80
    void addAccount(const std::string& address, TWCoinType coin, const DerivationPath& derivationPath, const std::string& extetndedPublicKey);
81

82
    /// Remove the account for a specific coin
83
    void removeAccount(TWCoinType coin);
84
    
85
    /// Returns the private key for a specific coin, creating an account if necessary.
86
    ///
87
    /// @throws std::invalid_argument if this key is of a type other than
88
    /// `mnemonicPhrase` and a coin other than the default is requested.
89
    const PrivateKey privateKey(TWCoinType coin, const Data& password);
90

91
    /// Loads and decrypts a stored key from a file.
92
    ///
93
    /// @param path file path to load from.
94
    /// @returns descrypted key.
95
    /// @throws DecryptionError
96
    static StoredKey load(const std::string& path);
97

98
    /// Stores the key into an encrypted file.
99
    ///
100
    /// @param path file path to store in.
101
    void store(const std::string& path);
102

103
    /// Initializes `StoredKey` with a JSON object.
104
    void loadJson(const nlohmann::json& json);
105

106
    /// Saves `this` as a JSON object.
107
    nlohmann::json json() const;
108

109
    /// Fills in all empty and invalid addresses.
110
    ///
111
    /// Use to fix legacy wallets with invalid address data. This method needs
112
    /// the encryption password to re-derive addresses from private keys.
113
    void fixAddresses(const Data& password);
114

115
private:
116
    /// Default constructor, private
117 1
    StoredKey() : type(StoredKeyType::mnemonicPhrase) {}
118

119
    /// Initializes a `StoredKey` with a type, an encryption password, and unencrypted data.
120
    /// This contstructor will encrypt the provided data with default encryption
121
    /// parameters.
122
    StoredKey(StoredKeyType type, std::string name, const Data& password, const Data& data);
123
};
124

125
} // namespace TW::Keystore
126

127
/// Wrapper for C interface.
128 1
struct TWStoredKey {
129
    TW::Keystore::StoredKey impl;
130
};

Read our documentation on viewing source code .

Loading