wallaby-cash 1.2.0
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.
- package/LICENSE +21 -0
- package/README.md +66 -0
- package/dist/base.d.ts +10 -0
- package/dist/helpers/logger.d.ts +2 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +2 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/lib/auth/index.d.ts +54 -0
- package/dist/lib/index.d.ts +3 -0
- package/dist/lib/system/index.d.ts +32 -0
- package/dist/lib/transactions/index.d.ts +80 -0
- package/dist/lib/utils/index.d.ts +21 -0
- package/dist/lib/utils/utils.d.ts +68 -0
- package/dist/lib/wallet/index.d.ts +99 -0
- package/dist/localStorageMock.d.ts +7 -0
- package/dist/routes/auth.d.ts +33 -0
- package/dist/routes/index.d.ts +3 -0
- package/dist/routes/system.d.ts +23 -0
- package/dist/routes/transactions.d.ts +28 -0
- package/dist/routes/wallet.d.ts +53 -0
- package/dist/tests/data/auth.d.ts +11 -0
- package/dist/tests/data/config.d.ts +6 -0
- package/dist/tests/data/index.d.ts +4 -0
- package/dist/tests/data/system.d.ts +47 -0
- package/dist/tests/data/user.d.ts +20 -0
- package/dist/types/auth.d.ts +48 -0
- package/dist/types/common.d.ts +206 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/merchants.d.ts +43 -0
- package/dist/types/transactions.d.ts +127 -0
- package/dist/types/wallet.d.ts +123 -0
- package/package.json +90 -0
- package/src/base.ts +95 -0
- package/src/helpers/logger.ts +11 -0
- package/src/index.ts +58 -0
- package/src/lib/auth/index.ts +225 -0
- package/src/lib/index.ts +3 -0
- package/src/lib/system/index.ts +104 -0
- package/src/lib/transactions/index.ts +259 -0
- package/src/lib/utils/index.ts +80 -0
- package/src/lib/utils/utils.ts +224 -0
- package/src/lib/wallet/index.ts +515 -0
- package/src/localStorageMock.js +23 -0
- package/src/routes/auth.ts +34 -0
- package/src/routes/index.ts +3 -0
- package/src/routes/system.ts +24 -0
- package/src/routes/transactions.ts +29 -0
- package/src/routes/wallet.ts +65 -0
- package/src/tests/__mocks__/axios.js +4 -0
- package/src/tests/__mocks__/localStorageMock.js +23 -0
- package/src/tests/auth.spec.ts +117 -0
- package/src/tests/data/auth.ts +49 -0
- package/src/tests/data/config.ts +8 -0
- package/src/tests/data/index.ts +4 -0
- package/src/tests/data/system.ts +54 -0
- package/src/tests/data/user.ts +25 -0
- package/src/tests/system.spec.ts +47 -0
- package/src/tests/utils.spec.ts +70 -0
- package/src/tests/wallet.spec.ts +44 -0
- package/src/types/auth.ts +57 -0
- package/src/types/common.ts +230 -0
- package/src/types/index.ts +5 -0
- package/src/types/merchants.ts +47 -0
- package/src/types/transactions.ts +139 -0
- package/src/types/wallet.ts +141 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { Base } from '../../base';
|
|
2
|
+
import { WallabyConfig, SeedPhraseType, BlockchainName, TokenStandard } from '../../types';
|
|
3
|
+
import logger from '../../helpers/logger';
|
|
4
|
+
import { Auth } from '../auth';
|
|
5
|
+
import { TransactionsRoutes } from '../../routes/transactions';
|
|
6
|
+
import { getEncryptedSeedPhrase } from '../utils/utils';
|
|
7
|
+
import {
|
|
8
|
+
CreateAllowanceRequest,
|
|
9
|
+
FeeEstimateRequest,
|
|
10
|
+
FeeEstimatesResponse,
|
|
11
|
+
Transaction,
|
|
12
|
+
TransactionStatusResponse,
|
|
13
|
+
TransferRequest,
|
|
14
|
+
TransferResponse,
|
|
15
|
+
} from '../../types';
|
|
16
|
+
|
|
17
|
+
export class Transactions extends Base {
|
|
18
|
+
private auth: Auth;
|
|
19
|
+
constructor(config: WallabyConfig) {
|
|
20
|
+
super(config);
|
|
21
|
+
this.auth = new Auth(config);
|
|
22
|
+
this.userId = config.userId;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
*
|
|
27
|
+
* @public
|
|
28
|
+
* @param {string} originAddress
|
|
29
|
+
* @param {string} recipientAddress
|
|
30
|
+
* @param {number} amount
|
|
31
|
+
* @param {string} assetSymbol
|
|
32
|
+
* @param {TransactionType} transactionType
|
|
33
|
+
* @param {TokenStandard} tokenStandard
|
|
34
|
+
* @returns {FeeEstimateRequest}
|
|
35
|
+
*/
|
|
36
|
+
async estimateFee({
|
|
37
|
+
originAddress,
|
|
38
|
+
recipientAddress,
|
|
39
|
+
amount,
|
|
40
|
+
assetSymbol,
|
|
41
|
+
transactionType,
|
|
42
|
+
tokenStandard,
|
|
43
|
+
}: FeeEstimateRequest): Promise<FeeEstimatesResponse | undefined> {
|
|
44
|
+
try {
|
|
45
|
+
const response = await this.request(TransactionsRoutes.estimateFee, {
|
|
46
|
+
method: TransactionsRoutes.estimateFee.method,
|
|
47
|
+
payload: { originAddress, recipientAddress, amount, assetSymbol, transactionType, tokenStandard },
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
if (!response || response.error) {
|
|
51
|
+
throw new Error(response.error);
|
|
52
|
+
}
|
|
53
|
+
return response;
|
|
54
|
+
} catch (error) {
|
|
55
|
+
logger('SDK::Transactions::estimateFee::error', error);
|
|
56
|
+
return Promise.reject(error);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
*
|
|
62
|
+
* @public
|
|
63
|
+
* @param {string} originAddress
|
|
64
|
+
* @param {string} recipientAddress
|
|
65
|
+
* @param {number} amount
|
|
66
|
+
* @param {string} assetId
|
|
67
|
+
* @param {string} reference
|
|
68
|
+
* @param {string} memo
|
|
69
|
+
* @param {string} hashKey
|
|
70
|
+
* @returns {TransferResponse} Transaction object which contains the transaction hash and status
|
|
71
|
+
*/
|
|
72
|
+
async transfer({
|
|
73
|
+
originAddress,
|
|
74
|
+
recipientAddress,
|
|
75
|
+
amount,
|
|
76
|
+
assetId,
|
|
77
|
+
reference,
|
|
78
|
+
memo,
|
|
79
|
+
hashKey,
|
|
80
|
+
}: TransferRequest): Promise<TransferResponse | undefined> {
|
|
81
|
+
try {
|
|
82
|
+
const signatureMessage = await this.auth.signatureMessage({ hashKey, userId: this.userId });
|
|
83
|
+
|
|
84
|
+
if (!signatureMessage || signatureMessage.error) {
|
|
85
|
+
throw new Error(signatureMessage?.error);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const encryptedSeed = getEncryptedSeedPhrase(this.userId, SeedPhraseType.Standard);
|
|
89
|
+
|
|
90
|
+
const { message, signature, clientAuthPubKey } = signatureMessage;
|
|
91
|
+
|
|
92
|
+
const response = await this.request(TransactionsRoutes.transfer, {
|
|
93
|
+
method: TransactionsRoutes.transfer.method,
|
|
94
|
+
payload: {
|
|
95
|
+
message,
|
|
96
|
+
signature,
|
|
97
|
+
clientAuthPubKey,
|
|
98
|
+
encryptedSeed,
|
|
99
|
+
originAddress,
|
|
100
|
+
recipientAddress,
|
|
101
|
+
assetId,
|
|
102
|
+
amount,
|
|
103
|
+
reference,
|
|
104
|
+
memo,
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
if (response.error) {
|
|
109
|
+
throw new Error(response.error);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return response;
|
|
113
|
+
} catch (error) {
|
|
114
|
+
logger('SDK::Transactions::transfer::error', error);
|
|
115
|
+
|
|
116
|
+
return Promise.reject(error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
*
|
|
122
|
+
* @public
|
|
123
|
+
* @param {string} transactionHash the transaction hash from the blockchain
|
|
124
|
+
* @param {string} reference a uuid unique to the transaction that links it with external systems
|
|
125
|
+
* @param {BlockchainName} blockchain blockchain name
|
|
126
|
+
* @returns {TransactionStatusResponse} Includes the status, the hash and other attributes.
|
|
127
|
+
*/
|
|
128
|
+
async getTransactionStatus({
|
|
129
|
+
transactionHash,
|
|
130
|
+
reference,
|
|
131
|
+
blockchain,
|
|
132
|
+
}: {
|
|
133
|
+
transactionHash: string;
|
|
134
|
+
reference: string;
|
|
135
|
+
blockchain: BlockchainName;
|
|
136
|
+
hashKey: string;
|
|
137
|
+
}): Promise<TransactionStatusResponse | undefined> {
|
|
138
|
+
try {
|
|
139
|
+
const response = await this.request(TransactionsRoutes.getTransaction, {
|
|
140
|
+
method: TransactionsRoutes.getTransaction.method,
|
|
141
|
+
payload: {
|
|
142
|
+
transactionHash,
|
|
143
|
+
transactionReference: reference,
|
|
144
|
+
chain: blockchain.toString().toUpperCase(),
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
if (response.error) {
|
|
149
|
+
throw new Error(response.error);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return response;
|
|
153
|
+
} catch (error) {
|
|
154
|
+
logger('SDK::Transactions::getTransactionStatus::error', error);
|
|
155
|
+
return Promise.reject(error);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
*
|
|
161
|
+
* @public
|
|
162
|
+
* @param {string} address
|
|
163
|
+
* @param {string} symbol
|
|
164
|
+
* @param {TokenStandard} tokenStandard an optional param used when dealing with non native assets
|
|
165
|
+
* @returns {Promise<{ address: string; transactions: Transaction[] }>}
|
|
166
|
+
*/
|
|
167
|
+
async getTransactions({
|
|
168
|
+
address,
|
|
169
|
+
symbol,
|
|
170
|
+
tokenStandard,
|
|
171
|
+
}: {
|
|
172
|
+
address: string;
|
|
173
|
+
symbol: string;
|
|
174
|
+
tokenStandard?: TokenStandard;
|
|
175
|
+
}): Promise<{ address: string; transactions: Transaction[]; error?: any } | undefined> {
|
|
176
|
+
try {
|
|
177
|
+
const response = await this.request(TransactionsRoutes.getTransactions, {
|
|
178
|
+
method: TransactionsRoutes.getTransactions.method,
|
|
179
|
+
payload: {
|
|
180
|
+
address,
|
|
181
|
+
tokenSymbol: symbol,
|
|
182
|
+
tokenType: tokenStandard,
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
if (response.error) {
|
|
187
|
+
throw new Error(response.error);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return response;
|
|
191
|
+
} catch (error) {
|
|
192
|
+
logger('SDK::Transactions::getTransactions::error', error);
|
|
193
|
+
return Promise.reject(error);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
*
|
|
199
|
+
* @public
|
|
200
|
+
* @param {string} hashKey
|
|
201
|
+
* @param {string} ownerAddress
|
|
202
|
+
* @param {string} spenderAddress
|
|
203
|
+
* @param {string} amount
|
|
204
|
+
* @param {string} assetId
|
|
205
|
+
* @param {string} contractAddress
|
|
206
|
+
* @param {string} reference
|
|
207
|
+
* @param {string} memo
|
|
208
|
+
* @returns {CreateAllowanceResponse}
|
|
209
|
+
*
|
|
210
|
+
*/
|
|
211
|
+
async createAllowance({
|
|
212
|
+
hashKey,
|
|
213
|
+
ownerAddress,
|
|
214
|
+
spenderAddress,
|
|
215
|
+
amount,
|
|
216
|
+
assetId,
|
|
217
|
+
contractAddress,
|
|
218
|
+
reference,
|
|
219
|
+
memo,
|
|
220
|
+
}: CreateAllowanceRequest): Promise<{ transaction: Transaction } | undefined> {
|
|
221
|
+
try {
|
|
222
|
+
const signatureMessage = await this.auth.signatureMessage({ hashKey, userId: this.userId });
|
|
223
|
+
|
|
224
|
+
if (!signatureMessage || signatureMessage.error) {
|
|
225
|
+
throw new Error(signatureMessage?.error);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const encryptedSeed = getEncryptedSeedPhrase(this.userId, SeedPhraseType.Standard);
|
|
229
|
+
|
|
230
|
+
const { message, signature, clientAuthPubKey } = signatureMessage;
|
|
231
|
+
|
|
232
|
+
const response = await this.request(TransactionsRoutes.createAllowance, {
|
|
233
|
+
method: TransactionsRoutes.createAllowance.method,
|
|
234
|
+
payload: {
|
|
235
|
+
message,
|
|
236
|
+
signature,
|
|
237
|
+
clientAuthPubKey,
|
|
238
|
+
encryptedSeed,
|
|
239
|
+
ownerAddress,
|
|
240
|
+
spenderAddress,
|
|
241
|
+
amount,
|
|
242
|
+
assetId,
|
|
243
|
+
contractAddress,
|
|
244
|
+
reference,
|
|
245
|
+
memo,
|
|
246
|
+
},
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
if (response.error) {
|
|
250
|
+
throw new Error(response.error);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return response;
|
|
254
|
+
} catch (error) {
|
|
255
|
+
logger('SDK::Transactions::createAllowance::error', error);
|
|
256
|
+
return Promise.reject(error);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { UtilsResponse, PasswordValidatorResponse, WallabyConfig, SeedPhraseType, RequestMothod } from '../../types';
|
|
2
|
+
import { Base } from '../../base';
|
|
3
|
+
import {
|
|
4
|
+
validatePassword,
|
|
5
|
+
setWallabyAuthPubKey,
|
|
6
|
+
getWallabyAuthPubKey,
|
|
7
|
+
setEncryptedPrivateKey,
|
|
8
|
+
getEncryptedPrivateKey,
|
|
9
|
+
getDecryptedPrivateKey,
|
|
10
|
+
setEncryptedSeedPhrase,
|
|
11
|
+
getEncryptedSeedPhrase,
|
|
12
|
+
getDecryptedSeedPhrase,
|
|
13
|
+
} from './utils';
|
|
14
|
+
import { Encrypted } from 'eth-crypto';
|
|
15
|
+
import logger from '../../helpers/logger';
|
|
16
|
+
|
|
17
|
+
export class Utils extends Base {
|
|
18
|
+
public setWallabyAuthPubKey: (userId: string, wallabyAuthPubKey: string) => void;
|
|
19
|
+
public getWallabyAuthPubKey: (userId: string) => UtilsResponse;
|
|
20
|
+
public setEncryptedPrivateKey: (userId: string, privateKey: string, hashKey: string) => boolean;
|
|
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;
|
|
32
|
+
constructor(config: WallabyConfig) {
|
|
33
|
+
super(config);
|
|
34
|
+
this.validatePassword = validatePassword;
|
|
35
|
+
this.setWallabyAuthPubKey = setWallabyAuthPubKey;
|
|
36
|
+
this.getWallabyAuthPubKey = getWallabyAuthPubKey;
|
|
37
|
+
this.setEncryptedPrivateKey = setEncryptedPrivateKey;
|
|
38
|
+
this.getEncryptedPrivateKey = getEncryptedPrivateKey;
|
|
39
|
+
this.getDecryptedPrivateKey = getDecryptedPrivateKey;
|
|
40
|
+
this.setEncryptedSeedPhrase = setEncryptedSeedPhrase;
|
|
41
|
+
this.getEncryptedSeedPhrase = getEncryptedSeedPhrase;
|
|
42
|
+
this.getDecryptedSeedPhrase = getDecryptedSeedPhrase;
|
|
43
|
+
this.fetchFromEndpoint = this.fetchFromEndpoint;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async fetchFromEndpoint({
|
|
47
|
+
endpoint,
|
|
48
|
+
method,
|
|
49
|
+
accessTokenRequired,
|
|
50
|
+
payload,
|
|
51
|
+
}: {
|
|
52
|
+
endpoint: string;
|
|
53
|
+
method: RequestMothod;
|
|
54
|
+
accessTokenRequired?: boolean;
|
|
55
|
+
payload?: any;
|
|
56
|
+
}): Promise<any> {
|
|
57
|
+
try {
|
|
58
|
+
const baseUrlRegex = new RegExp('https?://[^/]*.?wallaby.cash/api/v1', 'i');
|
|
59
|
+
const cleanEndpoint = endpoint.replace(baseUrlRegex, '');
|
|
60
|
+
|
|
61
|
+
const requestRoute = {
|
|
62
|
+
fullPath: cleanEndpoint,
|
|
63
|
+
method: method,
|
|
64
|
+
accessTokenRequired: accessTokenRequired ?? false,
|
|
65
|
+
};
|
|
66
|
+
const response = await this.request(requestRoute, {
|
|
67
|
+
method: method,
|
|
68
|
+
payload: payload,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
if (!response || response.error) {
|
|
72
|
+
throw new Error(response.error);
|
|
73
|
+
}
|
|
74
|
+
return response;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
logger('SDK::Utils::fetchFromEndpoint::error', error);
|
|
77
|
+
return Promise.reject(error);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { Encrypted, decryptWithPrivateKey, encryptWithPublicKey } from 'eth-crypto';
|
|
2
|
+
import { AES, enc } from 'crypto-js';
|
|
3
|
+
import { UtilsResponse, PasswordValidatorResponse, WallabyError, SeedPhraseType } from '../../types';
|
|
4
|
+
import logger from '../../helpers/logger';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* Stores Wallaby auth pubilc key to local storage
|
|
9
|
+
* @param userId
|
|
10
|
+
* @param wallabyAuthPubKey
|
|
11
|
+
*/
|
|
12
|
+
export const setWallabyAuthPubKey = (userId: string, wallabyAuthPubKey: string): boolean => {
|
|
13
|
+
try {
|
|
14
|
+
// For frameworks like NextJs, server render happens before the client render
|
|
15
|
+
// Therefore, localStorage will be undefined until the page has loaded on the client and the window object
|
|
16
|
+
// has been defined.
|
|
17
|
+
localStorage.setItem(`${process.env.NODE_ENV}-wallaby-auth-public-key-${userId}`, wallabyAuthPubKey);
|
|
18
|
+
// if (typeof window !== 'undefined') {
|
|
19
|
+
// }
|
|
20
|
+
return true;
|
|
21
|
+
} catch (error) {
|
|
22
|
+
logger('SDK::Utils::setWallabyAuthPubKey:error', error);
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
* @description Fetches Wallaby auth pubilc key from local storage
|
|
30
|
+
* @param userId
|
|
31
|
+
* @returns a string of Wallaby Auth Public key or undefined.
|
|
32
|
+
*/
|
|
33
|
+
export const getWallabyAuthPubKey = (userId: string): UtilsResponse => {
|
|
34
|
+
let wallabyAuthPubKey;
|
|
35
|
+
wallabyAuthPubKey = localStorage.getItem(`${process.env.NODE_ENV}-wallaby-auth-public-key-${userId}`) ?? undefined;
|
|
36
|
+
// if (typeof window !== 'undefined') {
|
|
37
|
+
// }
|
|
38
|
+
return wallabyAuthPubKey;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
* Encrypts and stores user private key
|
|
44
|
+
* @param userId ID of the user (should be a uuid)
|
|
45
|
+
* @param privateKey private key
|
|
46
|
+
* @param hashKey hashKey that is used to encrypt the private key
|
|
47
|
+
*/
|
|
48
|
+
export const setEncryptedPrivateKey = (userId: string, privateKey: string, hashKey: string): boolean => {
|
|
49
|
+
try {
|
|
50
|
+
const encryptedClientAuthPrivKey = AES.encrypt(privateKey, hashKey).toString();
|
|
51
|
+
localStorage.setItem(`${process.env.NODE_ENV}-client-auth-private-key-${userId}`, encryptedClientAuthPrivKey);
|
|
52
|
+
// if (typeof window !== 'undefined') {
|
|
53
|
+
// return true;
|
|
54
|
+
// }
|
|
55
|
+
return true;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
logger('SDK::Utils::setEncryptedPrivateKey', error);
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
*
|
|
64
|
+
* Fetches encrypted private key from local storage
|
|
65
|
+
* @param userId
|
|
66
|
+
*/
|
|
67
|
+
export const getEncryptedPrivateKey = (userId: string): UtilsResponse => {
|
|
68
|
+
let encryptedPrivateKey;
|
|
69
|
+
encryptedPrivateKey = localStorage.getItem(`${process.env.NODE_ENV}-client-auth-private-key-${userId}`);
|
|
70
|
+
// if (typeof window !== 'undefined') {
|
|
71
|
+
// logger('encryptedPrivateKey is found in local storage', encryptedPrivateKey)
|
|
72
|
+
// }
|
|
73
|
+
if (encryptedPrivateKey) {
|
|
74
|
+
return encryptedPrivateKey;
|
|
75
|
+
} else {
|
|
76
|
+
logger('encryptedPrivateKey is not found and returning undefined');
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
// return encryptedPrivateKey;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
*
|
|
84
|
+
* Decrypts user private key
|
|
85
|
+
* @param userId
|
|
86
|
+
* @param hashKey
|
|
87
|
+
* @returns
|
|
88
|
+
*/
|
|
89
|
+
export const getDecryptedPrivateKey = (userId: string, hashKey: string): UtilsResponse => {
|
|
90
|
+
const privateKey = getEncryptedPrivateKey(userId);
|
|
91
|
+
if (!privateKey) {
|
|
92
|
+
logger('SDK::Utils::getDecryptedPrivateKey::undefined?', !privateKey);
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const decryptedPrivateKey = AES.decrypt(privateKey, hashKey).toString(enc.Utf8);
|
|
97
|
+
if (decryptedPrivateKey !== '' && decryptedPrivateKey !== undefined) {
|
|
98
|
+
return decryptedPrivateKey;
|
|
99
|
+
} else {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
*
|
|
106
|
+
* @param userId
|
|
107
|
+
* @param encryptedSeedPhrase
|
|
108
|
+
*/
|
|
109
|
+
export const setEncryptedSeedPhrase = async (
|
|
110
|
+
userId: string,
|
|
111
|
+
clientAuthPubKey: string,
|
|
112
|
+
decryptedSeedPhrase: string,
|
|
113
|
+
seedPhraseType: SeedPhraseType,
|
|
114
|
+
): 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);
|
|
118
|
+
localStorage.setItem(
|
|
119
|
+
`${process.env.NODE_ENV}-encrypted-${seedPhraseType}-seedPhrase-${userId}`,
|
|
120
|
+
JSON.stringify(encryptedSeedPhrase),
|
|
121
|
+
);
|
|
122
|
+
// if (typeof window !== 'undefined') {
|
|
123
|
+
// }
|
|
124
|
+
return true;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
*
|
|
129
|
+
* @param {string} userId The user ID for whome the seedphrase belongs.
|
|
130
|
+
* @param {SeedPhraseType} seedPhraseType Standard or Mnemonic seedphrase
|
|
131
|
+
* @returns {UtilsResponse} It returns the encrypted seedphrase as a string or undefined
|
|
132
|
+
*/
|
|
133
|
+
export const getEncryptedSeedPhrase = (userId: string, seedPhraseType: SeedPhraseType): Encrypted | undefined => {
|
|
134
|
+
let encryptedSeedPhrase;
|
|
135
|
+
encryptedSeedPhrase = localStorage.getItem(`${process.env.NODE_ENV}-encrypted-${seedPhraseType}-seedPhrase-${userId}`);
|
|
136
|
+
// if (typeof window !== 'undefined') {
|
|
137
|
+
// }
|
|
138
|
+
|
|
139
|
+
if (!encryptedSeedPhrase) {
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
return JSON.parse(encryptedSeedPhrase);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
*
|
|
147
|
+
* @param {string} userId
|
|
148
|
+
* @param {string} hashKey
|
|
149
|
+
* @returns {UtilsResponse} It returns the decrypted seedphrase as a string or undefined
|
|
150
|
+
*/
|
|
151
|
+
export const getDecryptedSeedPhrase = async (
|
|
152
|
+
userId: string,
|
|
153
|
+
hashKey: string,
|
|
154
|
+
seedPhraseType: SeedPhraseType,
|
|
155
|
+
): Promise<UtilsResponse> => {
|
|
156
|
+
const encryptedSeedPhrase = getEncryptedSeedPhrase(userId, seedPhraseType);
|
|
157
|
+
const decryptedClientAuthPrivKey = getDecryptedPrivateKey(userId, hashKey);
|
|
158
|
+
|
|
159
|
+
if (!encryptedSeedPhrase || !decryptedClientAuthPrivKey) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
const decryptedSeedPhrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, encryptedSeedPhrase);
|
|
163
|
+
return decryptedSeedPhrase;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export const setAccessToken = (userId: string, accessToken: string): boolean => {
|
|
167
|
+
try {
|
|
168
|
+
localStorage.setItem(`${process.env.NODE_ENV}-access-token-${userId}`, accessToken);
|
|
169
|
+
// if (typeof window !== 'undefined') {
|
|
170
|
+
// }
|
|
171
|
+
|
|
172
|
+
return true;
|
|
173
|
+
} catch (error) {
|
|
174
|
+
logger('SDK::Utils::setAccessToken::error', error);
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export const getAccessToken = (userId: string): UtilsResponse | WallabyError => {
|
|
180
|
+
try {
|
|
181
|
+
let accessToken;
|
|
182
|
+
accessToken = localStorage.getItem(`${process.env.NODE_ENV}-access-token-${userId}`);
|
|
183
|
+
// if (typeof window !== 'undefined') {
|
|
184
|
+
// }
|
|
185
|
+
|
|
186
|
+
if (accessToken) {
|
|
187
|
+
return accessToken;
|
|
188
|
+
} else {
|
|
189
|
+
// Need to add a signin mechanism here to get the accessToken instead of returning undefined
|
|
190
|
+
return undefined;
|
|
191
|
+
}
|
|
192
|
+
} catch (error) {
|
|
193
|
+
logger('SDK::Utils::getAccessToken::error', error);
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
*
|
|
200
|
+
* @param userId
|
|
201
|
+
* @param hashKey
|
|
202
|
+
* @returns
|
|
203
|
+
*/
|
|
204
|
+
export const validatePassword = (userId: string, hashKey: string): PasswordValidatorResponse => {
|
|
205
|
+
const privateKey = getEncryptedPrivateKey(userId);
|
|
206
|
+
const encryptedSeedPhrase = getEncryptedSeedPhrase(userId, SeedPhraseType.Standard);
|
|
207
|
+
if (!privateKey || !encryptedSeedPhrase) {
|
|
208
|
+
return { valid: false, message: 'Could not locate client keys or seedPhrase' };
|
|
209
|
+
}
|
|
210
|
+
const decryptedPrivateKey = AES.decrypt(privateKey, hashKey).toString(enc.Utf8) ?? undefined;
|
|
211
|
+
if (decryptedPrivateKey) {
|
|
212
|
+
return { valid: true };
|
|
213
|
+
} else {
|
|
214
|
+
return { valid: false, message: 'hashKey is incorrect' };
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
export const clearLocalStorage = (userId: string) => {
|
|
219
|
+
localStorage.removeItem(`${process.env.NODE_ENV}-access-token-${userId}`);
|
|
220
|
+
localStorage.removeItem(`${process.env.NODE_ENV}-encrypted-${SeedPhraseType.Algorand}-seedPhrase-${userId}`);
|
|
221
|
+
localStorage.removeItem(`${process.env.NODE_ENV}-encrypted-${SeedPhraseType.Standard}-seedPhrase-${userId}`);
|
|
222
|
+
localStorage.removeItem(`${process.env.NODE_ENV}-client-auth-private-key-${userId}`);
|
|
223
|
+
localStorage.removeItem(`${process.env.NODE_ENV}-wallaby-auth-public-key-${userId}`);
|
|
224
|
+
};
|