forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTWStoredKey.h
315 lines (278 loc) · 14.8 KB
/
TWStoredKey.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
#pragma once
#include "TWBase.h"
#include "TWCoinType.h"
#include "TWData.h"
#include "TWDerivation.h"
#include "TWHDWallet.h"
#include "TWPrivateKey.h"
#include "TWStoredKeyEncryptionLevel.h"
#include "TWStoredKeyEncryption.h"
#include "TWString.h"
TW_EXTERN_C_BEGIN
/// Represents a key stored as an encrypted file.
TW_EXPORT_CLASS
struct TWStoredKey;
/// Loads a key from a file.
///
/// \param path filepath to the key as a non-null string
/// \note Returned object needs to be deleted with \TWStoredKeyDelete
/// \return Nullptr if the key can't be load, the stored key otherwise
TW_EXPORT_STATIC_METHOD
struct TWStoredKey* _Nullable TWStoredKeyLoad(TWString* _Nonnull path);
/// Imports a private key.
///
/// \param privateKey Non-null Block of data private key
/// \param name The name of the stored key to import as a non-null string
/// \param password Non-null block of data, password of the stored key
/// \param coin the coin type
/// \note Returned object needs to be deleted with \TWStoredKeyDelete
/// \return Nullptr if the key can't be imported, the stored key otherwise
TW_EXPORT_STATIC_METHOD
struct TWStoredKey* _Nullable TWStoredKeyImportPrivateKey(TWData* _Nonnull privateKey, TWString* _Nonnull name, TWData* _Nonnull password, enum TWCoinType coin);
/// Imports a private key.
///
/// \param privateKey Non-null Block of data private key
/// \param name The name of the stored key to import as a non-null string
/// \param password Non-null block of data, password of the stored key
/// \param coin the coin type
/// \param encryption cipher encryption mode
/// \note Returned object needs to be deleted with \TWStoredKeyDelete
/// \return Nullptr if the key can't be imported, the stored key otherwise
TW_EXPORT_STATIC_METHOD
struct TWStoredKey* _Nullable TWStoredKeyImportPrivateKeyWithEncryption(TWData* _Nonnull privateKey, TWString* _Nonnull name, TWData* _Nonnull password, enum TWCoinType coin, enum TWStoredKeyEncryption encryption);
/// Imports an HD wallet.
///
/// \param mnemonic Non-null bip39 mnemonic
/// \param name The name of the stored key to import as a non-null string
/// \param password Non-null block of data, password of the stored key
/// \param coin the coin type
/// \note Returned object needs to be deleted with \TWStoredKeyDelete
/// \return Nullptr if the key can't be imported, the stored key otherwise
TW_EXPORT_STATIC_METHOD
struct TWStoredKey* _Nullable TWStoredKeyImportHDWallet(TWString* _Nonnull mnemonic, TWString* _Nonnull name, TWData* _Nonnull password, enum TWCoinType coin);
/// Imports an HD wallet.
///
/// \param mnemonic Non-null bip39 mnemonic
/// \param name The name of the stored key to import as a non-null string
/// \param password Non-null block of data, password of the stored key
/// \param coin the coin type
/// \param encryption cipher encryption mode
/// \note Returned object needs to be deleted with \TWStoredKeyDelete
/// \return Nullptr if the key can't be imported, the stored key otherwise
TW_EXPORT_STATIC_METHOD
struct TWStoredKey* _Nullable TWStoredKeyImportHDWalletWithEncryption(TWString* _Nonnull mnemonic, TWString* _Nonnull name, TWData* _Nonnull password, enum TWCoinType coin, enum TWStoredKeyEncryption encryption);
/// Imports a key from JSON.
///
/// \param json Json stored key import format as a non-null block of data
/// \note Returned object needs to be deleted with \TWStoredKeyDelete
/// \return Nullptr if the key can't be imported, the stored key otherwise
TW_EXPORT_STATIC_METHOD
struct TWStoredKey* _Nullable TWStoredKeyImportJSON(TWData* _Nonnull json);
/// Creates a new key, with given encryption strength level. Returned object needs to be deleted.
///
/// \param name The name of the key to be stored
/// \param password Non-null block of data, password of the stored key
/// \param encryptionLevel The level of encryption, see \TWStoredKeyEncryptionLevel
/// \note Returned object needs to be deleted with \TWStoredKeyDelete
/// \return The stored key as a non-null pointer
TW_DEPRECATED_FOR("3.1.1", "TWStoredKeyCreateLevelAndEncryption")
TW_EXPORT_STATIC_METHOD
struct TWStoredKey* _Nonnull TWStoredKeyCreateLevel(TWString* _Nonnull name, TWData* _Nonnull password, enum TWStoredKeyEncryptionLevel encryptionLevel);
/// Creates a new key, with given encryption strength level. Returned object needs to be deleted.
///
/// \param name The name of the key to be stored
/// \param password Non-null block of data, password of the stored key
/// \param encryptionLevel The level of encryption, see \TWStoredKeyEncryptionLevel
/// \param encryption cipher encryption mode
/// \note Returned object needs to be deleted with \TWStoredKeyDelete
/// \return The stored key as a non-null pointer
TW_EXPORT_STATIC_METHOD
struct TWStoredKey* _Nonnull TWStoredKeyCreateLevelAndEncryption(TWString* _Nonnull name, TWData* _Nonnull password, enum TWStoredKeyEncryptionLevel encryptionLevel, enum TWStoredKeyEncryption encryption);
/// Creates a new key.
///
/// \deprecated use TWStoredKeyCreateLevel.
/// \param name The name of the key to be stored
/// \param password Non-null block of data, password of the stored key
/// \note Returned object needs to be deleted with \TWStoredKeyDelete
/// \return The stored key as a non-null pointer
TW_EXPORT_STATIC_METHOD struct TWStoredKey* _Nonnull TWStoredKeyCreate(TWString* _Nonnull name, TWData* _Nonnull password);
/// Creates a new key.
///
/// \deprecated use TWStoredKeyCreateLevel.
/// \param name The name of the key to be stored
/// \param password Non-null block of data, password of the stored key
/// \param encryption cipher encryption mode
/// \note Returned object needs to be deleted with \TWStoredKeyDelete
/// \return The stored key as a non-null pointer
TW_EXPORT_STATIC_METHOD struct TWStoredKey* _Nonnull TWStoredKeyCreateEncryption(TWString* _Nonnull name, TWData* _Nonnull password, enum TWStoredKeyEncryption encryption);
/// Delete a stored key
///
/// \param key The key to be deleted
TW_EXPORT_METHOD
void TWStoredKeyDelete(struct TWStoredKey* _Nonnull key);
/// Stored key unique identifier.
///
/// \param key Non-null pointer to a stored key
/// \note Returned object needs to be deleted with \TWStringDelete
/// \return The stored key unique identifier if it's found, null pointer otherwise.
TW_EXPORT_PROPERTY
TWString* _Nullable TWStoredKeyIdentifier(struct TWStoredKey* _Nonnull key);
/// Stored key namer.
///
/// \param key Non-null pointer to a stored key
/// \note Returned object needs to be deleted with \TWStringDelete
/// \return The stored key name as a non-null string pointer.
TW_EXPORT_PROPERTY
TWString* _Nonnull TWStoredKeyName(struct TWStoredKey* _Nonnull key);
/// Whether this key is a mnemonic phrase for a HD wallet.
///
/// \param key Non-null pointer to a stored key
/// \return true if the given stored key is a mnemonic, false otherwise
TW_EXPORT_PROPERTY
bool TWStoredKeyIsMnemonic(struct TWStoredKey* _Nonnull key);
/// The number of accounts.
///
/// \param key Non-null pointer to a stored key
/// \return the number of accounts associated to the given stored key
TW_EXPORT_PROPERTY
size_t TWStoredKeyAccountCount(struct TWStoredKey* _Nonnull key);
/// Returns the account at a given index.
///
/// \param key Non-null pointer to a stored key
/// \param index the account index to be retrieved
/// \note Returned object needs to be deleted with \TWAccountDelete
/// \return Null pointer if the associated account is not found, pointer to the account otherwise.
TW_EXPORT_METHOD
struct TWAccount* _Nullable TWStoredKeyAccount(struct TWStoredKey* _Nonnull key, size_t index);
/// Returns the account for a specific coin, creating it if necessary.
///
/// \param key Non-null pointer to a stored key
/// \param coin The coin type
/// \param wallet The associated HD wallet, can be null.
/// \note Returned object needs to be deleted with \TWAccountDelete
/// \return Null pointer if the associated account is not found/not created, pointer to the account otherwise.
TW_EXPORT_METHOD
struct TWAccount* _Nullable TWStoredKeyAccountForCoin(struct TWStoredKey* _Nonnull key, enum TWCoinType coin, struct TWHDWallet* _Nullable wallet);
/// Returns the account for a specific coin + derivation, creating it if necessary.
///
/// \param key Non-null pointer to a stored key
/// \param coin The coin type
/// \param derivation The derivation for the given coin
/// \param wallet the associated HD wallet, can be null.
/// \note Returned object needs to be deleted with \TWAccountDelete
/// \return Null pointer if the associated account is not found/not created, pointer to the account otherwise.
TW_EXPORT_METHOD
struct TWAccount* _Nullable TWStoredKeyAccountForCoinDerivation(struct TWStoredKey* _Nonnull key, enum TWCoinType coin, enum TWDerivation derivation, struct TWHDWallet* _Nullable wallet);
/// Adds a new account, using given derivation (usually TWDerivationDefault)
/// and derivation path (usually matches path from derivation, but custom possible).
///
/// \param key Non-null pointer to a stored key
/// \param address Non-null pointer to the address of the coin for this account
/// \param coin coin type
/// \param derivation derivation of the given coin type
/// \param derivationPath HD bip44 derivation path of the given coin
/// \param publicKey Non-null public key of the given coin/address
/// \param extendedPublicKey Non-null extended public key of the given coin/address
TW_EXPORT_METHOD
void TWStoredKeyAddAccountDerivation(struct TWStoredKey* _Nonnull key, TWString* _Nonnull address, enum TWCoinType coin, enum TWDerivation derivation, TWString* _Nonnull derivationPath, TWString* _Nonnull publicKey, TWString* _Nonnull extendedPublicKey);
/// Adds a new account, using given derivation path.
///
/// \deprecated Use TWStoredKeyAddAccountDerivation (with TWDerivationDefault) instead.
/// \param key Non-null pointer to a stored key
/// \param address Non-null pointer to the address of the coin for this account
/// \param coin coin type
/// \param derivationPath HD bip44 derivation path of the given coin
/// \param publicKey Non-null public key of the given coin/address
/// \param extendedPublicKey Non-null extended public key of the given coin/address
TW_EXPORT_METHOD
void TWStoredKeyAddAccount(struct TWStoredKey* _Nonnull key, TWString* _Nonnull address, enum TWCoinType coin, TWString* _Nonnull derivationPath, TWString* _Nonnull publicKey, TWString* _Nonnull extendedPublicKey);
/// Remove the account for a specific coin
///
/// \param key Non-null pointer to a stored key
/// \param coin Account coin type to be removed
TW_EXPORT_METHOD
void TWStoredKeyRemoveAccountForCoin(struct TWStoredKey* _Nonnull key, enum TWCoinType coin);
/// Remove the account for a specific coin with the given derivation.
///
/// \param key Non-null pointer to a stored key
/// \param coin Account coin type to be removed
/// \param derivation The derivation of the given coin type
TW_EXPORT_METHOD
void TWStoredKeyRemoveAccountForCoinDerivation(struct TWStoredKey* _Nonnull key, enum TWCoinType coin, enum TWDerivation derivation);
/// Remove the account for a specific coin with the given derivation path.
///
/// \param key Non-null pointer to a stored key
/// \param coin Account coin type to be removed
/// \param derivationPath The derivation path (bip44) of the given coin type
TW_EXPORT_METHOD
void TWStoredKeyRemoveAccountForCoinDerivationPath(struct TWStoredKey* _Nonnull key, enum TWCoinType coin, TWString* _Nonnull derivationPath);
/// Saves the key to a file.
///
/// \param key Non-null pointer to a stored key
/// \param path Non-null string filepath where the key will be saved
/// \return true if the key was successfully stored in the given filepath file, false otherwise
TW_EXPORT_METHOD
bool TWStoredKeyStore(struct TWStoredKey* _Nonnull key, TWString* _Nonnull path);
/// Decrypts the private key.
///
/// \param key Non-null pointer to a stored key
/// \param password Non-null block of data, password of the stored key
/// \return Decrypted private key as a block of data if success, null pointer otherwise
TW_EXPORT_METHOD
TWData* _Nullable TWStoredKeyDecryptPrivateKey(struct TWStoredKey* _Nonnull key, TWData* _Nonnull password);
/// Decrypts the mnemonic phrase.
///
/// \param key Non-null pointer to a stored key
/// \param password Non-null block of data, password of the stored key
/// \return Bip39 decrypted mnemonic if success, null pointer otherwise
TW_EXPORT_METHOD
TWString* _Nullable TWStoredKeyDecryptMnemonic(struct TWStoredKey* _Nonnull key, TWData* _Nonnull password);
/// Returns the private key for a specific coin. Returned object needs to be deleted.
///
/// \param key Non-null pointer to a stored key
/// \param coin Account coin type to be queried
/// \note Returned object needs to be deleted with \TWPrivateKeyDelete
/// \return Null pointer on failure, pointer to the private key otherwise
TW_EXPORT_METHOD
struct TWPrivateKey* _Nullable TWStoredKeyPrivateKey(struct TWStoredKey* _Nonnull key, enum TWCoinType coin, TWData* _Nonnull password);
/// Decrypts and returns the HD Wallet for mnemonic phrase keys. Returned object needs to be deleted.
///
/// \param key Non-null pointer to a stored key
/// \param password Non-null block of data, password of the stored key
/// \note Returned object needs to be deleted with \TWHDWalletDelete
/// \return Null pointer on failure, pointer to the HDWallet otherwise
TW_EXPORT_METHOD
struct TWHDWallet* _Nullable TWStoredKeyWallet(struct TWStoredKey* _Nonnull key, TWData* _Nonnull password);
/// Exports the key as JSON
///
/// \param key Non-null pointer to a stored key
/// \return Null pointer on failure, pointer to a block of data containing the json otherwise
TW_EXPORT_METHOD
TWData* _Nullable TWStoredKeyExportJSON(struct TWStoredKey* _Nonnull key);
/// Fills in empty and invalid addresses.
/// This method needs the encryption password to re-derive addresses from private keys.
///
/// \param key Non-null pointer to a stored key
/// \param password Non-null block of data, password of the stored key
/// \return `false` if the password is incorrect, true otherwise.
TW_EXPORT_METHOD
bool TWStoredKeyFixAddresses(struct TWStoredKey* _Nonnull key, TWData* _Nonnull password);
/// Re-derives address for the account(s) associated with the given coin.
/// This method can be used if address format has been changed.
/// In case of multiple accounts, all of them will be updated.
///
/// \param key Non-null pointer to a stored key
/// \param coin Account(s) coin type to be updated
/// \return `false` if there are no accounts associated with the given coin, true otherwise
TW_EXPORT_METHOD
bool TWStoredKeyUpdateAddress(struct TWStoredKey* _Nonnull key, enum TWCoinType coin);
/// Retrieve stored key encoding parameters, as JSON string.
///
/// \param key Non-null pointer to a stored key
/// \return Null pointer on failure, encoding parameter as a json string otherwise.
TW_EXPORT_PROPERTY
TWString* _Nullable TWStoredKeyEncryptionParameters(struct TWStoredKey* _Nonnull key);
TW_EXTERN_C_END