wallaby-cash 1.3.2 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,43 +6,96 @@ import {
6
6
  getWallabyAuthPubKey,
7
7
  setEncryptedPrivateKey,
8
8
  getEncryptedPrivateKey,
9
- getDecryptedPrivateKey,
10
- setEncryptedSeedPhrase,
11
9
  getEncryptedSeedPhrase,
12
10
  getDecryptedSeedPhrase,
13
11
  } from './utils';
14
- import { Encrypted } from 'eth-crypto';
12
+ import { encryptWithPublicKey } from 'eth-crypto';
15
13
  import logger from '../../helpers/logger';
14
+ import { Auth } from '../auth';
15
+ import { AES, enc } from 'crypto-js';
16
16
 
17
17
  export class Utils extends Base {
18
18
  public setWallabyAuthPubKey: (userId: string, wallabyAuthPubKey: string) => void;
19
19
  public getWallabyAuthPubKey: (userId: string) => UtilsResponse;
20
- public setEncryptedPrivateKey: (userId: string, privateKey: string, hashKey: string) => boolean;
20
+ public setEncryptedPrivateKey: (userId: string, privateKey: string, password: string) => boolean;
21
21
  public getEncryptedPrivateKey: (userId: string) => UtilsResponse;
22
- public getDecryptedPrivateKey: (userId: string, hashKey: string) => string | undefined;
23
- public setEncryptedSeedPhrase: (
22
+ public getEncryptedSeedPhrase: (userId: string, seedPhraseType: SeedPhraseType) => string | undefined;
23
+ public getDecryptedSeedPhrase: (
24
24
  userId: string,
25
- clientAuthPubKey: string,
26
- decryptedSeedPhrase: string,
25
+ password: string,
26
+ salt: string,
27
27
  seedPhraseType: SeedPhraseType,
28
- ) => Promise<boolean>;
29
- public getEncryptedSeedPhrase: (userId: string, seedPhraseType: SeedPhraseType) => Encrypted | undefined;
30
- public getDecryptedSeedPhrase: (userId: string, hashKey: string, seedphraseType: SeedPhraseType) => Promise<UtilsResponse>;
31
- public validatePassword: (userId: string, hashKey: string) => PasswordValidatorResponse;
28
+ ) => Promise<UtilsResponse>;
29
+ public validatePassword: (userId: string, password: string) => PasswordValidatorResponse;
30
+
31
+ private auth: Auth;
32
32
  constructor(config: WallabyConfig) {
33
33
  super(config);
34
+ this.auth = new Auth(config);
34
35
  this.validatePassword = validatePassword;
35
36
  this.setWallabyAuthPubKey = setWallabyAuthPubKey;
36
37
  this.getWallabyAuthPubKey = getWallabyAuthPubKey;
37
38
  this.setEncryptedPrivateKey = setEncryptedPrivateKey;
38
39
  this.getEncryptedPrivateKey = getEncryptedPrivateKey;
39
- this.getDecryptedPrivateKey = getDecryptedPrivateKey;
40
- this.setEncryptedSeedPhrase = setEncryptedSeedPhrase;
41
40
  this.getEncryptedSeedPhrase = getEncryptedSeedPhrase;
42
41
  this.getDecryptedSeedPhrase = getDecryptedSeedPhrase;
43
42
  this.fetchFromEndpoint = this.fetchFromEndpoint;
43
+ this.setEncryptedSeedPhrase = this.setEncryptedSeedPhrase;
44
+ this.getDecryptedPrivateKey = this.getDecryptedPrivateKey;
44
45
  }
45
46
 
47
+ /**
48
+ *
49
+ * Decrypts user private key
50
+ * @param userId
51
+ * @param password
52
+ * @returns
53
+ */
54
+ getDecryptedPrivateKey = (userId: string, password: string): UtilsResponse => {
55
+ const privateKey = getEncryptedPrivateKey(userId);
56
+ if (!privateKey) {
57
+ logger('SDK::Utils::getDecryptedPrivateKey::undefined?', !privateKey);
58
+ return undefined;
59
+ }
60
+
61
+ const decryptedPrivateKey = AES.decrypt(privateKey, password).toString(enc.Utf8);
62
+ if (decryptedPrivateKey !== '' && decryptedPrivateKey !== undefined) {
63
+ return decryptedPrivateKey;
64
+ } else {
65
+ return undefined;
66
+ }
67
+ };
68
+
69
+ /**
70
+ *
71
+ * @param {string} userId
72
+ * @param {string} decryptedSeedPhrase
73
+ * @param {SeedPhraseType} seedphraseType
74
+ * @param {string} password
75
+ * @param {string} salt
76
+ */
77
+ setEncryptedSeedPhrase = async (
78
+ userId: string,
79
+ clientAuthPubKey: string,
80
+ decryptedSeedPhrase: string,
81
+ seedPhraseType: SeedPhraseType,
82
+ password: string,
83
+ salt: string,
84
+ ): Promise<boolean> => {
85
+ // First layer of encryption uses the clientAuthPubKey.
86
+ const encryptedOnceSeedPhrase = await encryptWithPublicKey(clientAuthPubKey, decryptedSeedPhrase);
87
+
88
+ // The second layer uses a combination of the user's password/password and the salt.
89
+ const saltyHash = `${salt}${password}`;
90
+ const doubleEncryptedSeedPhrase = await AES.encrypt(JSON.stringify(encryptedOnceSeedPhrase), saltyHash);
91
+
92
+ localStorage.setItem(
93
+ `${process.env.NODE_ENV}-encrypted-${seedPhraseType}-seedPhrase-${userId}`,
94
+ doubleEncryptedSeedPhrase.toString(),
95
+ );
96
+ return true;
97
+ };
98
+
46
99
  async fetchFromEndpoint({
47
100
  endpoint,
48
101
  method,
@@ -43,11 +43,11 @@ export const getWallabyAuthPubKey = (userId: string): UtilsResponse => {
43
43
  * Encrypts and stores user private key
44
44
  * @param userId ID of the user (should be a uuid)
45
45
  * @param privateKey private key
46
- * @param hashKey hashKey that is used to encrypt the private key
46
+ * @param password password that is used to encrypt the private key
47
47
  */
48
- export const setEncryptedPrivateKey = (userId: string, privateKey: string, hashKey: string): boolean => {
48
+ export const setEncryptedPrivateKey = (userId: string, privateKey: string, password: string): boolean => {
49
49
  try {
50
- const encryptedClientAuthPrivKey = AES.encrypt(privateKey, hashKey).toString();
50
+ const encryptedClientAuthPrivKey = AES.encrypt(privateKey, password).toString();
51
51
  localStorage.setItem(`${process.env.NODE_ENV}-client-auth-private-key-${userId}`, encryptedClientAuthPrivKey);
52
52
  // if (typeof window !== 'undefined') {
53
53
  // return true;
@@ -83,17 +83,17 @@ export const getEncryptedPrivateKey = (userId: string): UtilsResponse => {
83
83
  *
84
84
  * Decrypts user private key
85
85
  * @param userId
86
- * @param hashKey
86
+ * @param password
87
87
  * @returns
88
88
  */
89
- export const getDecryptedPrivateKey = (userId: string, hashKey: string): UtilsResponse => {
89
+ export const getDecryptedPrivateKey = (userId: string, password: string): UtilsResponse => {
90
90
  const privateKey = getEncryptedPrivateKey(userId);
91
91
  if (!privateKey) {
92
92
  logger('SDK::Utils::getDecryptedPrivateKey::undefined?', !privateKey);
93
93
  return undefined;
94
94
  }
95
95
 
96
- const decryptedPrivateKey = AES.decrypt(privateKey, hashKey).toString(enc.Utf8);
96
+ const decryptedPrivateKey = AES.decrypt(privateKey, password).toString(enc.Utf8);
97
97
  if (decryptedPrivateKey !== '' && decryptedPrivateKey !== undefined) {
98
98
  return decryptedPrivateKey;
99
99
  } else {
@@ -103,24 +103,31 @@ export const getDecryptedPrivateKey = (userId: string, hashKey: string): UtilsRe
103
103
 
104
104
  /**
105
105
  *
106
- * @param userId
107
- * @param encryptedSeedPhrase
106
+ * @param {string} userId
107
+ * @param {string} decryptedSeedPhrase
108
+ * @param {SeedPhraseType} seedphraseType
109
+ * @param {string} password
110
+ * @param {string} salt
108
111
  */
109
112
  export const setEncryptedSeedPhrase = async (
110
113
  userId: string,
111
114
  clientAuthPubKey: string,
112
115
  decryptedSeedPhrase: string,
113
116
  seedPhraseType: SeedPhraseType,
117
+ password: string,
118
+ salt: string,
114
119
  ): Promise<boolean> => {
115
- logger('SDK::Utils::setEncryptedSeedPhrase::decryptedSeedPhrase', decryptedSeedPhrase);
116
- const encryptedSeedPhrase = await encryptWithPublicKey(clientAuthPubKey, JSON.stringify(decryptedSeedPhrase));
117
- logger('SDK::Utils::setEncryptedSeedPhrase::encryptedSeedPhrase', encryptedSeedPhrase);
120
+ // First layer of encryption uses the clientAuthPubKey.
121
+ const encryptedOnceSeedPhrase = await encryptWithPublicKey(clientAuthPubKey, decryptedSeedPhrase);
122
+
123
+ // The second layer uses a combination of the user's password/password and the salt.
124
+ const saltyHash = `${salt}${password}`;
125
+ const doubleEncryptedSeedPhrase = await AES.encrypt(JSON.stringify(encryptedOnceSeedPhrase), saltyHash);
126
+
118
127
  localStorage.setItem(
119
128
  `${process.env.NODE_ENV}-encrypted-${seedPhraseType}-seedPhrase-${userId}`,
120
- JSON.stringify(encryptedSeedPhrase),
129
+ doubleEncryptedSeedPhrase.toString(),
121
130
  );
122
- // if (typeof window !== 'undefined') {
123
- // }
124
131
  return true;
125
132
  };
126
133
 
@@ -130,7 +137,7 @@ export const setEncryptedSeedPhrase = async (
130
137
  * @param {SeedPhraseType} seedPhraseType Standard or Mnemonic seedphrase
131
138
  * @returns {UtilsResponse} It returns the encrypted seedphrase as a string or undefined
132
139
  */
133
- export const getEncryptedSeedPhrase = (userId: string, seedPhraseType: SeedPhraseType): Encrypted | undefined => {
140
+ export const getEncryptedSeedPhrase = (userId: string, seedPhraseType: SeedPhraseType): string | undefined => {
134
141
  let encryptedSeedPhrase;
135
142
  encryptedSeedPhrase = localStorage.getItem(`${process.env.NODE_ENV}-encrypted-${seedPhraseType}-seedPhrase-${userId}`);
136
143
  // if (typeof window !== 'undefined') {
@@ -139,35 +146,43 @@ export const getEncryptedSeedPhrase = (userId: string, seedPhraseType: SeedPhras
139
146
  if (!encryptedSeedPhrase) {
140
147
  return undefined;
141
148
  }
142
- return JSON.parse(encryptedSeedPhrase);
149
+ return encryptedSeedPhrase;
143
150
  };
144
151
 
145
152
  /**
146
153
  *
147
154
  * @param {string} userId
148
- * @param {string} hashKey
155
+ * @param {string} password
156
+ * @param {string} salt
157
+ * @param {SeedPhraseType} seedPhraseType
149
158
  * @returns {UtilsResponse} It returns the decrypted seedphrase as a string or undefined
150
159
  */
151
160
  export const getDecryptedSeedPhrase = async (
152
161
  userId: string,
153
- hashKey: string,
162
+ password: string,
163
+ salt: string,
154
164
  seedPhraseType: SeedPhraseType,
155
165
  ): Promise<UtilsResponse> => {
156
166
  const encryptedSeedPhrase = getEncryptedSeedPhrase(userId, seedPhraseType);
157
- const decryptedClientAuthPrivKey = getDecryptedPrivateKey(userId, hashKey);
167
+ const decryptedClientAuthPrivKey = getDecryptedPrivateKey(userId, password);
158
168
 
159
169
  if (!encryptedSeedPhrase || !decryptedClientAuthPrivKey) {
160
170
  return undefined;
161
171
  }
162
- const decryptedSeedPhrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, encryptedSeedPhrase);
172
+
173
+ const saltyHash = `${salt}${password}`;
174
+ // First we need to decrypt the first layer with user salt and with their password/password
175
+ const decryptedOnceSeedPhrase = AES.decrypt(encryptedSeedPhrase, saltyHash);
176
+ // Then we need to make sure the first layer of decryption is parsed properly.
177
+ const parsedSeed: Encrypted = JSON.parse(decryptedOnceSeedPhrase.toString(enc.Utf8));
178
+ // Now we decrypt the seedphrase with the private key and return the decrypted string
179
+ const decryptedSeedPhrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, parsedSeed);
163
180
  return decryptedSeedPhrase;
164
181
  };
165
182
 
166
183
  export const setAccessToken = (userId: string, accessToken: string): boolean => {
167
184
  try {
168
185
  localStorage.setItem(`${process.env.NODE_ENV}-access-token-${userId}`, accessToken);
169
- // if (typeof window !== 'undefined') {
170
- // }
171
186
 
172
187
  return true;
173
188
  } catch (error) {
@@ -198,20 +213,20 @@ export const getAccessToken = (userId: string): UtilsResponse | WallabyError =>
198
213
  /**
199
214
  *
200
215
  * @param userId
201
- * @param hashKey
216
+ * @param password
202
217
  * @returns
203
218
  */
204
- export const validatePassword = (userId: string, hashKey: string): PasswordValidatorResponse => {
219
+ export const validatePassword = (userId: string, password: string): PasswordValidatorResponse => {
205
220
  const privateKey = getEncryptedPrivateKey(userId);
206
221
  const encryptedSeedPhrase = getEncryptedSeedPhrase(userId, SeedPhraseType.Standard);
207
222
  if (!privateKey || !encryptedSeedPhrase) {
208
223
  return { valid: false, message: 'Could not locate client keys or seedPhrase' };
209
224
  }
210
- const decryptedPrivateKey = AES.decrypt(privateKey, hashKey).toString(enc.Utf8) ?? undefined;
225
+ const decryptedPrivateKey = AES.decrypt(privateKey, password).toString(enc.Utf8) ?? undefined;
211
226
  if (decryptedPrivateKey) {
212
227
  return { valid: true };
213
228
  } else {
214
- return { valid: false, message: 'hashKey is incorrect' };
229
+ return { valid: false, message: 'password is incorrect' };
215
230
  }
216
231
  };
217
232
 
@@ -1,5 +1,6 @@
1
1
  import { Base } from '../../base';
2
2
  import {
3
+ AuthResponse,
3
4
  QueryUserRequest,
4
5
  CreateWalletRequest,
5
6
  CreateWalletResponse,
@@ -13,14 +14,14 @@ import {
13
14
  UserWalletsResponse,
14
15
  WalletAssetsResponse,
15
16
  WallabyConfig,
16
- SignUpResponse,
17
- SignInResponse,
18
17
  SeedPhraseType,
19
18
  ConvertMnemonicSeedPhraseRequest,
20
19
  ConvertMnemonicSeedPhraseResponse,
21
20
  CreateMultisigAssetRequest,
22
21
  CreateMultisigAssetResponse,
23
22
  ClearWalletInput,
23
+ ImportWalletRequest,
24
+ ImportWalletResponse,
24
25
  } from '../../types';
25
26
  import { WalletRoutes } from '../../routes';
26
27
  import * as Utils from '../utils/utils';
@@ -70,15 +71,15 @@ export class Wallet extends Base {
70
71
  * @param {string} firstName The user's first name
71
72
  * @param {string} lastName The user's last name
72
73
  * @param {email} email The user's email
73
- * @param {string} hashKey The user's chosen password
74
+ * @param {string} password The user's chosen password
74
75
  * @returns {CreateWalletResponse} The wallet has been created successfully
75
76
  */
76
77
  async create({
77
78
  firstName,
78
79
  lastName,
79
80
  email,
80
- hashKey,
81
- }: Pick<CreateWalletRequest, 'firstName' | 'lastName' | 'email' | 'hashKey'>): Promise<CreateWalletResponse | WallabyError> {
81
+ password,
82
+ }: Pick<CreateWalletRequest, 'firstName' | 'lastName' | 'email' | 'password'>): Promise<CreateWalletResponse | WallabyError> {
82
83
  // try to fetch user data from localStorage.
83
84
  // If wallabyAuthPubKey exists then call Auth.signIn else call Auth.signUp
84
85
  try {
@@ -86,17 +87,17 @@ export class Wallet extends Base {
86
87
 
87
88
  logger('SDK::Wallet::create::wallabyAuthPubKey', wallabyAuthPubKey);
88
89
 
89
- let authentication: SignInResponse | SignUpResponse | undefined;
90
+ let authentication: AuthResponse | undefined;
90
91
  if (wallabyAuthPubKey) {
91
- authentication = await this.auth.signIn({ hashKey });
92
+ authentication = await this.auth.signIn({ password });
92
93
  } else {
93
- authentication = await this.auth.signUp({ hashKey, firstName, lastName, email });
94
+ authentication = await this.auth.signUp({ password, firstName, lastName, email });
94
95
  }
95
96
 
96
97
  if (!authentication) {
97
98
  throw new Error();
98
99
  }
99
- const signatureMessage = await this.auth.signatureMessage({ hashKey, userId: this.userId });
100
+ const signatureMessage = await this.auth.signatureMessage({ password, userId: this.userId });
100
101
  if (!signatureMessage || signatureMessage.error) {
101
102
  throw new Error(signatureMessage?.error);
102
103
  }
@@ -122,8 +123,23 @@ export class Wallet extends Base {
122
123
  // Decrypt the seedPhrase and retrun as part of response in case it needs to be displayed to the user.
123
124
  decryptedSeedPhrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, response.encryptedSeed);
124
125
 
126
+ // We need to get the user's salt in order for us to double encrypt the seedphrase before storing
127
+ // in local storage.
128
+ const salt = await this.auth.getUserSalt();
129
+
130
+ if (!salt) {
131
+ throw new Error('User salt could not be retrieved, Wallet creation could not be finished');
132
+ }
133
+
125
134
  // Encrypt again and store seedPhrase in localstorage.
126
- await Utils.setEncryptedSeedPhrase(this.userId, clientAuthPubKey, decryptedSeedPhrase, SeedPhraseType.Standard);
135
+ await Utils.setEncryptedSeedPhrase(
136
+ this.userId,
137
+ clientAuthPubKey,
138
+ decryptedSeedPhrase,
139
+ SeedPhraseType.Standard,
140
+ password,
141
+ salt,
142
+ );
127
143
 
128
144
  // Request must send seedPhrase encrypted with WallabyAuthPubKey
129
145
  if (!wallabyAuthPubKey) {
@@ -132,7 +148,7 @@ export class Wallet extends Base {
132
148
  const encryptedSeedphraseWithWallabyKey = await encryptWithPublicKey(wallabyAuthPubKey, decryptedSeedPhrase);
133
149
 
134
150
  const mnemonicSeedPhrase = await this.convertMnemonicSeedphrase({
135
- hashKey,
151
+ password,
136
152
  // This is the standard seedPhrase from the wallet creation response but encrypted using Wallaby's auth public key
137
153
  encryptedSeedPhrase: encryptedSeedphraseWithWallabyKey,
138
154
  });
@@ -146,7 +162,14 @@ export class Wallet extends Base {
146
162
 
147
163
  decryptedMnemonicSeedphrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, convertedEncryptedSeed);
148
164
 
149
- await Utils.setEncryptedSeedPhrase(this.userId, clientAuthPubKey, decryptedMnemonicSeedphrase, SeedPhraseType.Algorand);
165
+ await Utils.setEncryptedSeedPhrase(
166
+ this.userId,
167
+ clientAuthPubKey,
168
+ decryptedMnemonicSeedphrase,
169
+ SeedPhraseType.Algorand,
170
+ password,
171
+ salt,
172
+ );
150
173
 
151
174
  return { ...response, decryptedSeedPhrase, decryptedMnemonicSeedphrase };
152
175
  } catch (error) {
@@ -162,15 +185,15 @@ export class Wallet extends Base {
162
185
  * @param {string} firstName The user's first name
163
186
  * @param {string} lastName The user's last name
164
187
  * @param {email} email The user's email
165
- * @param {string} hashKey The user's chosen password
188
+ * @param {string} password The user's chosen password
166
189
  * @returns {CreateWalletResponse} The wallet has been created successfully
167
190
  */
168
191
  async createWithNewAccount({
169
192
  firstName,
170
193
  lastName,
171
194
  email,
172
- hashKey,
173
- }: Pick<CreateWalletRequest, 'firstName' | 'lastName' | 'email' | 'hashKey'>): Promise<CreateWalletResponse | WallabyError> {
195
+ password,
196
+ }: Pick<CreateWalletRequest, 'firstName' | 'lastName' | 'email' | 'password'>): Promise<CreateWalletResponse | WallabyError> {
174
197
  // try to fetch user data from localStorage.
175
198
  // If wallabyAuthPubKey exists then call Auth.signIn else call Auth.signUp
176
199
  try {
@@ -178,12 +201,14 @@ export class Wallet extends Base {
178
201
 
179
202
  logger('SDK::Wallet::create::wallabyAuthPubKey', wallabyAuthPubKey);
180
203
 
181
- const authentication: SignUpResponse | undefined = await this.auth.signUp({ hashKey, firstName, lastName, email });
204
+ const authentication: AuthResponse | undefined = await this.auth.signUp({ password, firstName, lastName, email });
182
205
 
183
206
  if (!authentication) {
184
207
  throw new Error();
185
208
  }
186
- const signatureMessage = await this.auth.signatureMessage({ hashKey, userId: this.userId });
209
+
210
+ const signatureMessage = await this.auth.signatureMessage({ password, userId: this.userId });
211
+
187
212
  if (!signatureMessage || signatureMessage.error) {
188
213
  throw new Error(signatureMessage?.error);
189
214
  }
@@ -209,8 +234,23 @@ export class Wallet extends Base {
209
234
  // Decrypt the seedPhrase and retrun as part of response in case it needs to be displayed to the user.
210
235
  decryptedSeedPhrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, response.encryptedSeed);
211
236
 
237
+ // We need to get the user's salt in order for us to double encrypt the seedphrase before storing
238
+ // in local storage.
239
+ const salt = await this.auth.getUserSalt();
240
+
241
+ if (!salt) {
242
+ throw new Error('User salt could not be retrieved, Wallet creation could not be finished');
243
+ }
244
+
212
245
  // Encrypt again and store seedPhrase in localstorage.
213
- await Utils.setEncryptedSeedPhrase(this.userId, clientAuthPubKey, decryptedSeedPhrase, SeedPhraseType.Standard);
246
+ await Utils.setEncryptedSeedPhrase(
247
+ this.userId,
248
+ clientAuthPubKey,
249
+ decryptedSeedPhrase,
250
+ SeedPhraseType.Standard,
251
+ password,
252
+ salt,
253
+ );
214
254
 
215
255
  // Request must send seedPhrase encrypted with WallabyAuthPubKey
216
256
  if (!wallabyAuthPubKey) {
@@ -219,7 +259,7 @@ export class Wallet extends Base {
219
259
  const encryptedSeedphraseWithWallabyKey = await encryptWithPublicKey(wallabyAuthPubKey, decryptedSeedPhrase);
220
260
 
221
261
  const mnemonicSeedPhrase = await this.convertMnemonicSeedphrase({
222
- hashKey,
262
+ password,
223
263
  // This is the standard seedPhrase from the wallet creation response but encrypted using Wallaby's auth public key
224
264
  encryptedSeedPhrase: encryptedSeedphraseWithWallabyKey,
225
265
  });
@@ -233,7 +273,14 @@ export class Wallet extends Base {
233
273
 
234
274
  decryptedMnemonicSeedphrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, convertedEncryptedSeed);
235
275
 
236
- await Utils.setEncryptedSeedPhrase(this.userId, clientAuthPubKey, decryptedMnemonicSeedphrase, SeedPhraseType.Algorand);
276
+ await Utils.setEncryptedSeedPhrase(
277
+ this.userId,
278
+ clientAuthPubKey,
279
+ decryptedMnemonicSeedphrase,
280
+ SeedPhraseType.Algorand,
281
+ password,
282
+ salt,
283
+ );
237
284
 
238
285
  return { ...response, decryptedSeedPhrase, decryptedMnemonicSeedphrase };
239
286
  } catch (error) {
@@ -242,6 +289,75 @@ export class Wallet extends Base {
242
289
  }
243
290
  }
244
291
 
292
+ /**
293
+ *
294
+ * @public
295
+ * @description Import an existing wallet, returns asset list and encrypted seedPhrase
296
+ * @param {string} password The user's chosen password
297
+ * @param {string} seedPhrase The user's seed phrase
298
+ * @returns {ImportWalletResponse} The wallet has been imported successfully
299
+ * The return includes the wallet, wallet assets, and the encrypted seedphrase
300
+ */
301
+ async import({ password, seedPhrase }: ImportWalletRequest): Promise<ImportWalletResponse> {
302
+ // try to fetch user data from localStorage.
303
+ try {
304
+ const wallabyAuthPubKey = Utils.getWallabyAuthPubKey(this.userId);
305
+
306
+ logger('SDK::Wallet::create::wallabyAuthPubKey', wallabyAuthPubKey);
307
+
308
+ let authentication: AuthResponse | undefined;
309
+ if (wallabyAuthPubKey) {
310
+ authentication = await this.auth.signIn({ password });
311
+ } else {
312
+ authentication = await this.auth.signUp({ password, firstName: '', lastName: '', email: '' });
313
+ }
314
+
315
+ if (!authentication) {
316
+ throw new Error('User is not authorized to perform this action');
317
+ }
318
+
319
+ const signatureMessage = await this.auth.signatureMessage({ password, userId: this.userId });
320
+
321
+ if (!signatureMessage || signatureMessage.error) {
322
+ throw new Error(signatureMessage?.error);
323
+ }
324
+
325
+ const { message, signature, clientAuthPubKey, decryptedClientAuthPrivKey } = signatureMessage;
326
+
327
+ const encryptedSeedphraseWithWallabyKey = await encryptWithPublicKey(authentication.user.wallabyAuthPubKey, seedPhrase);
328
+
329
+ const response: ImportWalletResponse = await this.request(WalletRoutes.import, {
330
+ method: WalletRoutes.import.method,
331
+ payload: {
332
+ message,
333
+ signature,
334
+ clientAuthPubKey,
335
+ encryptedSeedphraseWithWallabyKey,
336
+ },
337
+ });
338
+
339
+ if (response.error) {
340
+ throw new Error(response.error);
341
+ }
342
+
343
+ // We need to get the user's salt in order for us to double encrypt the seedphrase before storing
344
+ // in local storage.
345
+ const salt = await this.auth.getUserSalt();
346
+
347
+ if (!salt) {
348
+ throw new Error('User salt could not be retrieved, Wallet creation could not be finished');
349
+ }
350
+
351
+ // Encrypt again and store seedPhrase in localstorage.
352
+ // TODO Add storeSeedPhrase function to replace the below code
353
+ await Utils.setEncryptedSeedPhrase(this.userId, clientAuthPubKey, seedPhrase, SeedPhraseType.Standard, password, salt);
354
+ return response;
355
+ } catch (error) {
356
+ logger('SDK::Wallet::create::error', error);
357
+ return Promise.reject(error);
358
+ }
359
+ }
360
+
245
361
  /**
246
362
  *
247
363
  * @public
@@ -263,19 +379,19 @@ export class Wallet extends Base {
263
379
  *
264
380
  * @public
265
381
  * @description Convert BIP39 Standard seed phrase to Algorand fixed 25 words mnemonic phrase
266
- * @param {string} hashKey
382
+ * @param {string} password
267
383
  * @param {Encrypted} encryptedSeedPhrase It needs to be encypted using wallabyAuthPubKey
268
384
  * @returns {ConvertMnemonicSeedPhraseResponse} The mnemonic seed phrase has been converted successfully.
269
385
  */
270
386
  async convertMnemonicSeedphrase({
271
- hashKey,
387
+ password,
272
388
  encryptedSeedPhrase,
273
- }: Pick<ConvertMnemonicSeedPhraseRequest, 'hashKey' | 'encryptedSeedPhrase'>): Promise<
389
+ }: Pick<ConvertMnemonicSeedPhraseRequest, 'password' | 'encryptedSeedPhrase'>): Promise<
274
390
  ConvertMnemonicSeedPhraseResponse | undefined
275
391
  > {
276
392
  try {
277
393
  const reference = uuid();
278
- const signatureMessage = await this.auth.signatureMessage({ hashKey, userId: this.userId });
394
+ const signatureMessage = await this.auth.signatureMessage({ password, userId: this.userId });
279
395
  if (!signatureMessage || signatureMessage.error) {
280
396
  throw new Error(signatureMessage?.error);
281
397
  }
@@ -307,12 +423,12 @@ export class Wallet extends Base {
307
423
  *
308
424
  * @public
309
425
  * @description Recover the lost wallet and its assets.
310
- * @param {string} newHashKey User need to set a new password while recovering the wallet
426
+ * @param {string} newPassword User need to set a new password while recovering the wallet
311
427
  * @param {string} seedPhrase It needs to be encypted using wallabyAuthPubKey
312
428
  * @returns {RecoverWalletResponse} The wallet has been recovered successfully.
313
429
  */
314
430
  async recover({
315
- newHashKey,
431
+ newPassword,
316
432
  seedPhrase,
317
433
  }: Omit<RecoverWalletRequest, 'clientAuthPubKey'>): Promise<RecoverWalletResponse | undefined> {
318
434
  try {
@@ -345,12 +461,21 @@ export class Wallet extends Base {
345
461
 
346
462
  // Store the new keys and seedphrase in the new browswer
347
463
  Utils.setWallabyAuthPubKey(this.userId, wallabyAuthPubKey);
348
- Utils.setEncryptedPrivateKey(this.userId, privateKey, newHashKey);
349
- await Utils.setEncryptedSeedPhrase(this.userId, publicKey, seedPhrase, SeedPhraseType.Standard);
464
+ Utils.setEncryptedPrivateKey(this.userId, privateKey, newPassword);
465
+
466
+ // We need to get the user's salt in order for us to double encrypt the seedphrase before storing
467
+ // in local storage.
468
+ const salt = await this.auth.getUserSalt();
469
+
470
+ if (!salt) {
471
+ throw new Error('User salt could not be retrieved, Wallet recovery could not be finished');
472
+ }
473
+
474
+ await Utils.setEncryptedSeedPhrase(this.userId, publicKey, seedPhrase, SeedPhraseType.Standard, newPassword, salt);
350
475
 
351
476
  // Get Algorand mnemonic seedphrase
352
477
  const mnemonicSeedPhrase = await this.convertMnemonicSeedphrase({
353
- hashKey: newHashKey,
478
+ password: newPassword,
354
479
  // This is the standard seedPhrase from the wallet creation response but encrypted using Wallaby's auth public key
355
480
  encryptedSeedPhrase: encryptedSeedphraseWithWallabyKey,
356
481
  });
@@ -362,7 +487,14 @@ export class Wallet extends Base {
362
487
  // Decrypt the seedPhrase and retrun as part of response in case it needs to be displayed to the user.
363
488
  const { convertedEncryptedSeed } = mnemonicSeedPhrase;
364
489
  const decryptedMnemonicSeedphrase = await decryptWithPrivateKey(privateKey, convertedEncryptedSeed);
365
- await Utils.setEncryptedSeedPhrase(this.userId, publicKey, decryptedMnemonicSeedphrase, SeedPhraseType.Algorand);
490
+ await Utils.setEncryptedSeedPhrase(
491
+ this.userId,
492
+ publicKey,
493
+ decryptedMnemonicSeedphrase,
494
+ SeedPhraseType.Algorand,
495
+ newPassword,
496
+ salt,
497
+ );
366
498
 
367
499
  return { ...response, seedPhrase, decryptedMnemonicSeedphrase };
368
500
  } catch (error) {
@@ -31,4 +31,9 @@ export const AuthRoutes = {
31
31
  method: RequestMothod.Get,
32
32
  accessTokenRequired: true,
33
33
  },
34
+ getUserSalt: {
35
+ fullPath: '/users/salt',
36
+ method: RequestMothod.Get,
37
+ accessTokenRequired: true,
38
+ },
34
39
  };
@@ -12,7 +12,24 @@ export const TransactionsRoutes = {
12
12
  accessTokenRequired: true,
13
13
  },
14
14
  estimateFee: {
15
- fullPath: '/transactions/estimate-fee',
15
+ fullPath: ({
16
+ assetSymbol,
17
+ originAddress,
18
+ recipientAddress,
19
+ amount,
20
+ transactionType,
21
+ tokenType,
22
+ }: {
23
+ assetSymbol: string;
24
+ originAddress: string;
25
+ recipientAddress: string;
26
+ amount: number;
27
+ transactionType: string;
28
+ tokenType?: string;
29
+ }) =>
30
+ `/transactions/estimate-fee?assetSymbol=${assetSymbol}&originAddress=${originAddress}&recipientAddress=${recipientAddress}&amount=${amount}&transactionType=${transactionType}${
31
+ tokenType ? `&tokenType=${tokenType}` : ''
32
+ }`,
16
33
  method: RequestMothod.Get,
17
34
  accessTokenRequired: true,
18
35
  },
@@ -6,6 +6,11 @@ export const WalletRoutes = {
6
6
  method: RequestMothod.Post,
7
7
  accessTokenRequired: true,
8
8
  },
9
+ import: {
10
+ fullPath: '/wallets/import-seed-phrase',
11
+ method: RequestMothod.Post,
12
+ accessTokenRequired: true,
13
+ },
9
14
  initRecoveryFlow: {
10
15
  fullPath: 'wallets/recover/init',
11
16
  method: RequestMothod.Post,