wallaby-cash 1.3.2 → 1.3.4

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.
Files changed (44) hide show
  1. package/dist/base.d.ts +20 -1
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.esm.js +1 -1
  4. package/dist/index.esm.js.map +1 -1
  5. package/dist/index.js +1 -1
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.umd.js +1 -1
  8. package/dist/index.umd.js.map +1 -1
  9. package/dist/lib/auth/index.d.ts +16 -10
  10. package/dist/lib/transactions/index.d.ts +5 -5
  11. package/dist/lib/utils/index.d.ts +29 -9
  12. package/dist/lib/utils/utils.d.ts +22 -13
  13. package/dist/lib/wallet/index.d.ts +34 -11
  14. package/dist/routes/auth.d.ts +11 -6
  15. package/dist/routes/merchants.d.ts +7 -7
  16. package/dist/routes/system.d.ts +4 -4
  17. package/dist/routes/transactions.d.ts +13 -6
  18. package/dist/routes/wallet.d.ts +15 -10
  19. package/dist/tests/data/user.d.ts +3 -2
  20. package/dist/types/auth.d.ts +5 -10
  21. package/dist/types/common.d.ts +5 -1
  22. package/dist/types/transactions.d.ts +2 -2
  23. package/dist/types/wallet.d.ts +13 -6
  24. package/package.json +2 -2
  25. package/src/base.ts +40 -8
  26. package/src/index.ts +2 -2
  27. package/src/lib/auth/index.ts +49 -24
  28. package/src/lib/transactions/index.ts +7 -7
  29. package/src/lib/utils/index.ts +77 -19
  30. package/src/lib/utils/utils.ts +96 -36
  31. package/src/lib/wallet/index.ts +209 -35
  32. package/src/routes/auth.ts +11 -6
  33. package/src/routes/merchants.ts +7 -7
  34. package/src/routes/system.ts +4 -4
  35. package/src/routes/transactions.ts +23 -6
  36. package/src/routes/wallet.ts +15 -10
  37. package/src/tests/auth.spec.ts +26 -24
  38. package/src/tests/data/user.ts +3 -2
  39. package/src/tests/utils.spec.ts +9 -13
  40. package/src/tests/wallet.spec.ts +12 -7
  41. package/src/types/auth.ts +5 -11
  42. package/src/types/common.ts +6 -1
  43. package/src/types/transactions.ts +2 -2
  44. package/src/types/wallet.ts +14 -6
@@ -6,52 +6,110 @@ 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: (
24
- userId: string,
25
- clientAuthPubKey: string,
26
- decryptedSeedPhrase: string,
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;
22
+ public getEncryptedSeedPhrase: (userId: string, seedPhraseType: SeedPhraseType) => string | undefined;
23
+ public getDecryptedSeedPhrase: ({
24
+ userId,
25
+ password,
26
+ salt,
27
+ seedPhraseType,
28
+ }: {
29
+ userId: string;
30
+ password: string;
31
+ salt: string;
32
+ seedPhraseType: SeedPhraseType;
33
+ }) => Promise<UtilsResponse>;
34
+ public validatePassword: (userId: string, password: string) => PasswordValidatorResponse;
35
+
36
+ private auth: Auth;
32
37
  constructor(config: WallabyConfig) {
33
38
  super(config);
39
+ this.auth = new Auth(config);
34
40
  this.validatePassword = validatePassword;
35
41
  this.setWallabyAuthPubKey = setWallabyAuthPubKey;
36
42
  this.getWallabyAuthPubKey = getWallabyAuthPubKey;
37
43
  this.setEncryptedPrivateKey = setEncryptedPrivateKey;
38
44
  this.getEncryptedPrivateKey = getEncryptedPrivateKey;
39
- this.getDecryptedPrivateKey = getDecryptedPrivateKey;
40
- this.setEncryptedSeedPhrase = setEncryptedSeedPhrase;
41
45
  this.getEncryptedSeedPhrase = getEncryptedSeedPhrase;
42
46
  this.getDecryptedSeedPhrase = getDecryptedSeedPhrase;
43
47
  this.fetchFromEndpoint = this.fetchFromEndpoint;
48
+ this.setEncryptedSeedPhrase = this.setEncryptedSeedPhrase;
49
+ this.getDecryptedPrivateKey = this.getDecryptedPrivateKey;
44
50
  }
45
51
 
52
+ /**
53
+ *
54
+ * Decrypts user private key
55
+ * @param userId
56
+ * @param password
57
+ * @returns
58
+ */
59
+ getDecryptedPrivateKey = (userId: string, password: string): UtilsResponse => {
60
+ const privateKey = getEncryptedPrivateKey(userId);
61
+ if (!privateKey) {
62
+ logger('SDK::Utils::getDecryptedPrivateKey::undefined?', !privateKey);
63
+ return undefined;
64
+ }
65
+
66
+ const decryptedPrivateKey = AES.decrypt(privateKey, password).toString(enc.Utf8);
67
+ if (decryptedPrivateKey !== '' && decryptedPrivateKey !== undefined) {
68
+ return decryptedPrivateKey;
69
+ } else {
70
+ return undefined;
71
+ }
72
+ };
73
+
74
+ /**
75
+ *
76
+ * @param {string} userId
77
+ * @param {string} decryptedSeedPhrase
78
+ * @param {SeedPhraseType} seedphraseType
79
+ * @param {string} password
80
+ * @param {string} salt
81
+ */
82
+ setEncryptedSeedPhrase = async (
83
+ userId: string,
84
+ clientAuthPubKey: string,
85
+ decryptedSeedPhrase: string,
86
+ seedPhraseType: SeedPhraseType,
87
+ password: string,
88
+ salt: string,
89
+ ): Promise<boolean> => {
90
+ // First layer of encryption uses the clientAuthPubKey.
91
+ const encryptedOnceSeedPhrase = await encryptWithPublicKey(clientAuthPubKey, decryptedSeedPhrase);
92
+
93
+ // The second layer uses a combination of the user's password/password and the salt.
94
+ const saltyHash = `${salt}${password}`;
95
+ const doubleEncryptedSeedPhrase = await AES.encrypt(JSON.stringify(encryptedOnceSeedPhrase), saltyHash);
96
+
97
+ localStorage.setItem(
98
+ `${process.env.NODE_ENV}-encrypted-${seedPhraseType}-seedPhrase-${userId}`,
99
+ doubleEncryptedSeedPhrase.toString(),
100
+ );
101
+ return true;
102
+ };
103
+
46
104
  async fetchFromEndpoint({
47
105
  endpoint,
48
106
  method,
49
- accessTokenRequired,
107
+ requiresAccessToken,
50
108
  payload,
51
109
  }: {
52
110
  endpoint: string;
53
111
  method: RequestMothod;
54
- accessTokenRequired?: boolean;
112
+ requiresAccessToken?: boolean;
55
113
  payload?: any;
56
114
  }): Promise<any> {
57
115
  try {
@@ -61,7 +119,7 @@ export class Utils extends Base {
61
119
  const requestRoute = {
62
120
  fullPath: cleanEndpoint,
63
121
  method: method,
64
- accessTokenRequired: accessTokenRequired ?? false,
122
+ requiresAccessToken: requiresAccessToken ?? false,
65
123
  };
66
124
  const response = await this.request(requestRoute, {
67
125
  method: method,
@@ -35,7 +35,17 @@ export const getWallabyAuthPubKey = (userId: string): UtilsResponse => {
35
35
  wallabyAuthPubKey = localStorage.getItem(`${process.env.NODE_ENV}-wallaby-auth-public-key-${userId}`) ?? undefined;
36
36
  // if (typeof window !== 'undefined') {
37
37
  // }
38
- return wallabyAuthPubKey;
38
+ if (!wallabyAuthPubKey) {
39
+ // try to get the wallabyAuthPubKey from the legacy key
40
+ wallabyAuthPubKey = localStorage.getItem(`wallabyAuthPubKey_${userId}`);
41
+ }
42
+ if (wallabyAuthPubKey) {
43
+ // updating the key to the new format
44
+ localStorage.setItem(`${process.env.NODE_ENV}-wallaby-auth-public-key-${userId}`, wallabyAuthPubKey);
45
+ return wallabyAuthPubKey;
46
+ } else {
47
+ return undefined;
48
+ }
39
49
  };
40
50
 
41
51
  /**
@@ -43,11 +53,11 @@ export const getWallabyAuthPubKey = (userId: string): UtilsResponse => {
43
53
  * Encrypts and stores user private key
44
54
  * @param userId ID of the user (should be a uuid)
45
55
  * @param privateKey private key
46
- * @param hashKey hashKey that is used to encrypt the private key
56
+ * @param password password that is used to encrypt the private key
47
57
  */
48
- export const setEncryptedPrivateKey = (userId: string, privateKey: string, hashKey: string): boolean => {
58
+ export const setEncryptedPrivateKey = (userId: string, privateKey: string, password: string): boolean => {
49
59
  try {
50
- const encryptedClientAuthPrivKey = AES.encrypt(privateKey, hashKey).toString();
60
+ const encryptedClientAuthPrivKey = AES.encrypt(privateKey, password).toString();
51
61
  localStorage.setItem(`${process.env.NODE_ENV}-client-auth-private-key-${userId}`, encryptedClientAuthPrivKey);
52
62
  // if (typeof window !== 'undefined') {
53
63
  // return true;
@@ -70,10 +80,16 @@ export const getEncryptedPrivateKey = (userId: string): UtilsResponse => {
70
80
  // if (typeof window !== 'undefined') {
71
81
  // logger('encryptedPrivateKey is found in local storage', encryptedPrivateKey)
72
82
  // }
83
+ if (!encryptedPrivateKey) {
84
+ // try to get the encryptedPrivateKey from the legacy key
85
+ encryptedPrivateKey = localStorage.getItem(`encryptedPrivateKey_${userId}`);
86
+ }
87
+
73
88
  if (encryptedPrivateKey) {
89
+ localStorage.setItem(`${process.env.NODE_ENV}-client-auth-private-key-${userId}`, encryptedPrivateKey);
74
90
  return encryptedPrivateKey;
75
91
  } else {
76
- logger('encryptedPrivateKey is not found and returning undefined');
92
+ logger('SDK::Utils::getEncryptedPrivateKey: private key not found');
77
93
  return undefined;
78
94
  }
79
95
  // return encryptedPrivateKey;
@@ -83,17 +99,17 @@ export const getEncryptedPrivateKey = (userId: string): UtilsResponse => {
83
99
  *
84
100
  * Decrypts user private key
85
101
  * @param userId
86
- * @param hashKey
102
+ * @param password
87
103
  * @returns
88
104
  */
89
- export const getDecryptedPrivateKey = (userId: string, hashKey: string): UtilsResponse => {
105
+ export const getDecryptedPrivateKey = (userId: string, password: string): UtilsResponse => {
90
106
  const privateKey = getEncryptedPrivateKey(userId);
91
107
  if (!privateKey) {
92
108
  logger('SDK::Utils::getDecryptedPrivateKey::undefined?', !privateKey);
93
109
  return undefined;
94
110
  }
95
111
 
96
- const decryptedPrivateKey = AES.decrypt(privateKey, hashKey).toString(enc.Utf8);
112
+ const decryptedPrivateKey = AES.decrypt(privateKey, password).toString(enc.Utf8);
97
113
  if (decryptedPrivateKey !== '' && decryptedPrivateKey !== undefined) {
98
114
  return decryptedPrivateKey;
99
115
  } else {
@@ -103,24 +119,31 @@ export const getDecryptedPrivateKey = (userId: string, hashKey: string): UtilsRe
103
119
 
104
120
  /**
105
121
  *
106
- * @param userId
107
- * @param encryptedSeedPhrase
122
+ * @param {string} userId
123
+ * @param {string} decryptedSeedPhrase
124
+ * @param {SeedPhraseType} seedphraseType
125
+ * @param {string} password
126
+ * @param {string} salt
108
127
  */
109
128
  export const setEncryptedSeedPhrase = async (
110
129
  userId: string,
111
130
  clientAuthPubKey: string,
112
131
  decryptedSeedPhrase: string,
113
132
  seedPhraseType: SeedPhraseType,
133
+ password: string,
134
+ salt: string,
114
135
  ): 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);
136
+ // First layer of encryption uses the clientAuthPubKey.
137
+ const encryptedOnceSeedPhrase = await encryptWithPublicKey(clientAuthPubKey, decryptedSeedPhrase);
138
+
139
+ // The second layer uses a combination of the user's password/password and the salt.
140
+ const saltyHash = `${salt}${password}`;
141
+ const doubleEncryptedSeedPhrase = await AES.encrypt(JSON.stringify(encryptedOnceSeedPhrase), saltyHash);
142
+
118
143
  localStorage.setItem(
119
144
  `${process.env.NODE_ENV}-encrypted-${seedPhraseType}-seedPhrase-${userId}`,
120
- JSON.stringify(encryptedSeedPhrase),
145
+ doubleEncryptedSeedPhrase.toString(),
121
146
  );
122
- // if (typeof window !== 'undefined') {
123
- // }
124
147
  return true;
125
148
  };
126
149
 
@@ -130,44 +153,74 @@ export const setEncryptedSeedPhrase = async (
130
153
  * @param {SeedPhraseType} seedPhraseType Standard or Mnemonic seedphrase
131
154
  * @returns {UtilsResponse} It returns the encrypted seedphrase as a string or undefined
132
155
  */
133
- export const getEncryptedSeedPhrase = (userId: string, seedPhraseType: SeedPhraseType): Encrypted | undefined => {
156
+ export const getEncryptedSeedPhrase = (userId: string, seedPhraseType: SeedPhraseType): string | undefined => {
134
157
  let encryptedSeedPhrase;
135
158
  encryptedSeedPhrase = localStorage.getItem(`${process.env.NODE_ENV}-encrypted-${seedPhraseType}-seedPhrase-${userId}`);
136
159
  // if (typeof window !== 'undefined') {
137
160
  // }
138
161
 
139
162
  if (!encryptedSeedPhrase) {
140
- return undefined;
163
+ // try to get the encryptedSeedPhrase from the legacy key
164
+ switch (seedPhraseType) {
165
+ case SeedPhraseType.Algorand:
166
+ encryptedSeedPhrase = localStorage.getItem(`encryptedAlgorandSeed_${userId}`);
167
+ break;
168
+ case SeedPhraseType.Standard:
169
+ encryptedSeedPhrase = localStorage.getItem(`encryptedSeed_${userId}`);
170
+ break;
171
+ default:
172
+ break;
173
+ }
174
+ }
175
+
176
+ if (encryptedSeedPhrase) {
177
+ // updating the key to the new format
178
+ localStorage.setItem(`${process.env.NODE_ENV}-encrypted-${seedPhraseType}-seedPhrase-${userId}`, encryptedSeedPhrase);
179
+ return encryptedSeedPhrase;
141
180
  }
142
- return JSON.parse(encryptedSeedPhrase);
181
+
182
+ return undefined;
143
183
  };
144
184
 
145
185
  /**
146
186
  *
147
187
  * @param {string} userId
148
- * @param {string} hashKey
188
+ * @param {string} password
189
+ * @param {string} salt
190
+ * @param {SeedPhraseType} seedPhraseType
149
191
  * @returns {UtilsResponse} It returns the decrypted seedphrase as a string or undefined
150
192
  */
151
- export const getDecryptedSeedPhrase = async (
152
- userId: string,
153
- hashKey: string,
154
- seedPhraseType: SeedPhraseType,
155
- ): Promise<UtilsResponse> => {
193
+ export const getDecryptedSeedPhrase = async ({
194
+ userId,
195
+ password,
196
+ salt,
197
+ seedPhraseType,
198
+ }: {
199
+ userId: string;
200
+ password: string;
201
+ salt: string;
202
+ seedPhraseType: SeedPhraseType;
203
+ }): Promise<UtilsResponse> => {
156
204
  const encryptedSeedPhrase = getEncryptedSeedPhrase(userId, seedPhraseType);
157
- const decryptedClientAuthPrivKey = getDecryptedPrivateKey(userId, hashKey);
205
+ const decryptedClientAuthPrivKey = getDecryptedPrivateKey(userId, password);
158
206
 
159
207
  if (!encryptedSeedPhrase || !decryptedClientAuthPrivKey) {
160
208
  return undefined;
161
209
  }
162
- const decryptedSeedPhrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, encryptedSeedPhrase);
210
+
211
+ const saltyHash = `${salt}${password}`;
212
+ // First we need to decrypt the first layer with user salt and with their password/password
213
+ const decryptedOnceSeedPhrase = AES.decrypt(encryptedSeedPhrase, saltyHash);
214
+ // Then we need to make sure the first layer of decryption is parsed properly.
215
+ const parsedSeed: Encrypted = JSON.parse(decryptedOnceSeedPhrase.toString(enc.Utf8));
216
+ // Now we decrypt the seedphrase with the private key and return the decrypted string
217
+ const decryptedSeedPhrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, parsedSeed);
163
218
  return decryptedSeedPhrase;
164
219
  };
165
220
 
166
221
  export const setAccessToken = (userId: string, accessToken: string): boolean => {
167
222
  try {
168
223
  localStorage.setItem(`${process.env.NODE_ENV}-access-token-${userId}`, accessToken);
169
- // if (typeof window !== 'undefined') {
170
- // }
171
224
 
172
225
  return true;
173
226
  } catch (error) {
@@ -180,13 +233,15 @@ export const getAccessToken = (userId: string): UtilsResponse | WallabyError =>
180
233
  try {
181
234
  let accessToken;
182
235
  accessToken = localStorage.getItem(`${process.env.NODE_ENV}-access-token-${userId}`);
183
- // if (typeof window !== 'undefined') {
184
- // }
236
+
237
+ if (!accessToken) {
238
+ // try to get the accessToken from the legacy key
239
+ accessToken = localStorage.getItem(`accessToken_${userId}`);
240
+ }
185
241
 
186
242
  if (accessToken) {
187
243
  return accessToken;
188
244
  } else {
189
- // Need to add a signin mechanism here to get the accessToken instead of returning undefined
190
245
  return undefined;
191
246
  }
192
247
  } catch (error) {
@@ -198,20 +253,20 @@ export const getAccessToken = (userId: string): UtilsResponse | WallabyError =>
198
253
  /**
199
254
  *
200
255
  * @param userId
201
- * @param hashKey
256
+ * @param password
202
257
  * @returns
203
258
  */
204
- export const validatePassword = (userId: string, hashKey: string): PasswordValidatorResponse => {
259
+ export const validatePassword = (userId: string, password: string): PasswordValidatorResponse => {
205
260
  const privateKey = getEncryptedPrivateKey(userId);
206
261
  const encryptedSeedPhrase = getEncryptedSeedPhrase(userId, SeedPhraseType.Standard);
207
262
  if (!privateKey || !encryptedSeedPhrase) {
208
263
  return { valid: false, message: 'Could not locate client keys or seedPhrase' };
209
264
  }
210
- const decryptedPrivateKey = AES.decrypt(privateKey, hashKey).toString(enc.Utf8) ?? undefined;
265
+ const decryptedPrivateKey = AES.decrypt(privateKey, password).toString(enc.Utf8) ?? undefined;
211
266
  if (decryptedPrivateKey) {
212
267
  return { valid: true };
213
268
  } else {
214
- return { valid: false, message: 'hashKey is incorrect' };
269
+ return { valid: false, message: 'password is incorrect' };
215
270
  }
216
271
  };
217
272
 
@@ -221,4 +276,9 @@ export const clearLocalStorage = (userId: string) => {
221
276
  localStorage.removeItem(`${process.env.NODE_ENV}-encrypted-${SeedPhraseType.Standard}-seedPhrase-${userId}`);
222
277
  localStorage.removeItem(`${process.env.NODE_ENV}-client-auth-private-key-${userId}`);
223
278
  localStorage.removeItem(`${process.env.NODE_ENV}-wallaby-auth-public-key-${userId}`);
279
+
280
+ localStorage.removeItem(`encryptedPrivateKey_${userId}`);
281
+ localStorage.removeItem(`wallabyAuthPubKey_${userId}`);
282
+ localStorage.removeItem(`encryptedSeed_${userId}`);
283
+ localStorage.removeItem(`encryptedAlgorandSeed_${userId}`);
224
284
  };