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,515 @@
|
|
|
1
|
+
import { Base } from '../../base';
|
|
2
|
+
import {
|
|
3
|
+
QueryUserRequest,
|
|
4
|
+
CreateWalletRequest,
|
|
5
|
+
CreateWalletResponse,
|
|
6
|
+
RecoverWalletRequest,
|
|
7
|
+
RecoverWalletResponse,
|
|
8
|
+
TokenBalanceRequest,
|
|
9
|
+
TokenBalancResponse,
|
|
10
|
+
WallabyError,
|
|
11
|
+
QueryWalletRequest,
|
|
12
|
+
UserAssetsResponse,
|
|
13
|
+
UserWalletsResponse,
|
|
14
|
+
WalletAssetsResponse,
|
|
15
|
+
WallabyConfig,
|
|
16
|
+
SignUpResponse,
|
|
17
|
+
SignInResponse,
|
|
18
|
+
SeedPhraseType,
|
|
19
|
+
ConvertMnemonicSeedPhraseRequest,
|
|
20
|
+
ConvertMnemonicSeedPhraseResponse,
|
|
21
|
+
CreateMultisigAssetRequest,
|
|
22
|
+
CreateMultisigAssetResponse,
|
|
23
|
+
ClearWalletInput,
|
|
24
|
+
} from '../../types';
|
|
25
|
+
import { WalletRoutes } from '../../routes';
|
|
26
|
+
import * as Utils from '../utils/utils';
|
|
27
|
+
import { Auth } from '../auth';
|
|
28
|
+
import logger from '../../helpers/logger';
|
|
29
|
+
import { createIdentity, decryptWithPrivateKey, encryptWithPublicKey, hash, sign } from 'eth-crypto';
|
|
30
|
+
import { v4 as uuid } from 'uuid';
|
|
31
|
+
|
|
32
|
+
export class Wallet extends Base {
|
|
33
|
+
private auth: Auth;
|
|
34
|
+
constructor(config: WallabyConfig) {
|
|
35
|
+
super(config);
|
|
36
|
+
this.auth = new Auth(config);
|
|
37
|
+
this.userId = config.userId;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
* @description Create a newly fresh wallet, returns asset list and encrypted seedPhrase
|
|
44
|
+
* @param {string} address Wallet address
|
|
45
|
+
* @param {string} symbol Token symbol
|
|
46
|
+
* @param {TokenStandard} tokenStandard Token standard. Leave as null if the token is native to the blockchain. i.e ALGO BTC or ETH
|
|
47
|
+
* @returns {CreateWalletResponse} The wallet has been created successfully
|
|
48
|
+
*/
|
|
49
|
+
async getTokenBalance({ address, symbol, tokenStandard }: TokenBalanceRequest): Promise<TokenBalancResponse | undefined> {
|
|
50
|
+
try {
|
|
51
|
+
const response = await this.request(WalletRoutes.getTokenBalance, {
|
|
52
|
+
method: WalletRoutes.getTokenBalance.method,
|
|
53
|
+
payload: { address, symbol, tokenStandard },
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (!response || response.error) {
|
|
57
|
+
throw new Error(response.error);
|
|
58
|
+
}
|
|
59
|
+
return response;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
logger('SDK::Wallet::getTokenBalance::error', error);
|
|
62
|
+
return Promise.reject(error);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
*
|
|
68
|
+
* @public
|
|
69
|
+
* @description Create a newly fresh wallet, returns asset list and encrypted seedPhrase
|
|
70
|
+
* @param {string} firstName The user's first name
|
|
71
|
+
* @param {string} lastName The user's last name
|
|
72
|
+
* @param {email} email The user's email
|
|
73
|
+
* @param {string} hashKey The user's chosen password
|
|
74
|
+
* @returns {CreateWalletResponse} The wallet has been created successfully
|
|
75
|
+
*/
|
|
76
|
+
async create({
|
|
77
|
+
firstName,
|
|
78
|
+
lastName,
|
|
79
|
+
email,
|
|
80
|
+
hashKey,
|
|
81
|
+
}: Pick<CreateWalletRequest, 'firstName' | 'lastName' | 'email' | 'hashKey'>): Promise<CreateWalletResponse | WallabyError> {
|
|
82
|
+
// try to fetch user data from localStorage.
|
|
83
|
+
// If wallabyAuthPubKey exists then call Auth.signIn else call Auth.signUp
|
|
84
|
+
try {
|
|
85
|
+
const wallabyAuthPubKey = Utils.getWallabyAuthPubKey(this.userId);
|
|
86
|
+
|
|
87
|
+
logger('SDK::Wallet::create::wallabyAuthPubKey', wallabyAuthPubKey);
|
|
88
|
+
|
|
89
|
+
let authentication: SignInResponse | SignUpResponse | undefined;
|
|
90
|
+
if (wallabyAuthPubKey) {
|
|
91
|
+
authentication = await this.auth.signIn({ hashKey });
|
|
92
|
+
} else {
|
|
93
|
+
authentication = await this.auth.signUp({ hashKey, firstName, lastName, email });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!authentication) {
|
|
97
|
+
throw new Error();
|
|
98
|
+
}
|
|
99
|
+
const signatureMessage = await this.auth.signatureMessage({ hashKey, userId: this.userId });
|
|
100
|
+
if (!signatureMessage || signatureMessage.error) {
|
|
101
|
+
throw new Error(signatureMessage?.error);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const { message, signature, clientAuthPubKey, decryptedClientAuthPrivKey } = signatureMessage;
|
|
105
|
+
|
|
106
|
+
const response: Omit<CreateWalletResponse, 'decryptedSeedPhrase'> = await this.request(WalletRoutes.create, {
|
|
107
|
+
method: WalletRoutes.create.method,
|
|
108
|
+
payload: {
|
|
109
|
+
message,
|
|
110
|
+
signature,
|
|
111
|
+
clientAuthPubKey,
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
if (response.error) {
|
|
116
|
+
throw new Error(response.error);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let decryptedSeedPhrase: string | undefined;
|
|
120
|
+
let decryptedMnemonicSeedphrase: string | undefined;
|
|
121
|
+
|
|
122
|
+
// Decrypt the seedPhrase and retrun as part of response in case it needs to be displayed to the user.
|
|
123
|
+
decryptedSeedPhrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, response.encryptedSeed);
|
|
124
|
+
|
|
125
|
+
// Encrypt again and store seedPhrase in localstorage.
|
|
126
|
+
await Utils.setEncryptedSeedPhrase(this.userId, clientAuthPubKey, decryptedSeedPhrase, SeedPhraseType.Standard);
|
|
127
|
+
|
|
128
|
+
// Request must send seedPhrase encrypted with WallabyAuthPubKey
|
|
129
|
+
if (!wallabyAuthPubKey) {
|
|
130
|
+
throw new Error('wallabyAuthPubKey is not found');
|
|
131
|
+
}
|
|
132
|
+
const encryptedSeedphraseWithWallabyKey = await encryptWithPublicKey(wallabyAuthPubKey, decryptedSeedPhrase);
|
|
133
|
+
|
|
134
|
+
const mnemonicSeedPhrase = await this.convertMnemonicSeedphrase({
|
|
135
|
+
hashKey,
|
|
136
|
+
// This is the standard seedPhrase from the wallet creation response but encrypted using Wallaby's auth public key
|
|
137
|
+
encryptedSeedPhrase: encryptedSeedphraseWithWallabyKey,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
if (!mnemonicSeedPhrase || mnemonicSeedPhrase.error) {
|
|
141
|
+
throw new Error(mnemonicSeedPhrase?.error);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const { convertedEncryptedSeed } = mnemonicSeedPhrase;
|
|
145
|
+
// Decrypt the seedPhrase and retrun as part of response in case it needs to be displayed to the user.
|
|
146
|
+
|
|
147
|
+
decryptedMnemonicSeedphrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, convertedEncryptedSeed);
|
|
148
|
+
|
|
149
|
+
await Utils.setEncryptedSeedPhrase(this.userId, clientAuthPubKey, decryptedMnemonicSeedphrase, SeedPhraseType.Algorand);
|
|
150
|
+
|
|
151
|
+
return { ...response, decryptedSeedPhrase, decryptedMnemonicSeedphrase };
|
|
152
|
+
} catch (error) {
|
|
153
|
+
logger('SDK::Wallet::create::error', error);
|
|
154
|
+
return Promise.reject(error);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
*
|
|
160
|
+
* @public
|
|
161
|
+
* @description Create a newly fresh wallet, returns asset list and encrypted seedPhrase
|
|
162
|
+
* @param {string} firstName The user's first name
|
|
163
|
+
* @param {string} lastName The user's last name
|
|
164
|
+
* @param {email} email The user's email
|
|
165
|
+
* @param {string} hashKey The user's chosen password
|
|
166
|
+
* @returns {CreateWalletResponse} The wallet has been created successfully
|
|
167
|
+
*/
|
|
168
|
+
async createWithNewAccount({
|
|
169
|
+
firstName,
|
|
170
|
+
lastName,
|
|
171
|
+
email,
|
|
172
|
+
hashKey,
|
|
173
|
+
}: Pick<CreateWalletRequest, 'firstName' | 'lastName' | 'email' | 'hashKey'>): Promise<CreateWalletResponse | WallabyError> {
|
|
174
|
+
// try to fetch user data from localStorage.
|
|
175
|
+
// If wallabyAuthPubKey exists then call Auth.signIn else call Auth.signUp
|
|
176
|
+
try {
|
|
177
|
+
const wallabyAuthPubKey = Utils.getWallabyAuthPubKey(this.userId);
|
|
178
|
+
|
|
179
|
+
logger('SDK::Wallet::create::wallabyAuthPubKey', wallabyAuthPubKey);
|
|
180
|
+
|
|
181
|
+
const authentication: SignUpResponse | undefined = await this.auth.signUp({ hashKey, firstName, lastName, email });
|
|
182
|
+
|
|
183
|
+
if (!authentication) {
|
|
184
|
+
throw new Error();
|
|
185
|
+
}
|
|
186
|
+
const signatureMessage = await this.auth.signatureMessage({ hashKey, userId: this.userId });
|
|
187
|
+
if (!signatureMessage || signatureMessage.error) {
|
|
188
|
+
throw new Error(signatureMessage?.error);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const { message, signature, clientAuthPubKey, decryptedClientAuthPrivKey } = signatureMessage;
|
|
192
|
+
|
|
193
|
+
const response: Omit<CreateWalletResponse, 'decryptedSeedPhrase'> = await this.request(WalletRoutes.create, {
|
|
194
|
+
method: WalletRoutes.create.method,
|
|
195
|
+
payload: {
|
|
196
|
+
message,
|
|
197
|
+
signature,
|
|
198
|
+
clientAuthPubKey,
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
if (response.error) {
|
|
203
|
+
throw new Error(response.error);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
let decryptedSeedPhrase: string | undefined;
|
|
207
|
+
let decryptedMnemonicSeedphrase: string | undefined;
|
|
208
|
+
|
|
209
|
+
// Decrypt the seedPhrase and retrun as part of response in case it needs to be displayed to the user.
|
|
210
|
+
decryptedSeedPhrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, response.encryptedSeed);
|
|
211
|
+
|
|
212
|
+
// Encrypt again and store seedPhrase in localstorage.
|
|
213
|
+
await Utils.setEncryptedSeedPhrase(this.userId, clientAuthPubKey, decryptedSeedPhrase, SeedPhraseType.Standard);
|
|
214
|
+
|
|
215
|
+
// Request must send seedPhrase encrypted with WallabyAuthPubKey
|
|
216
|
+
if (!wallabyAuthPubKey) {
|
|
217
|
+
throw new Error('wallabyAuthPubKey is not found');
|
|
218
|
+
}
|
|
219
|
+
const encryptedSeedphraseWithWallabyKey = await encryptWithPublicKey(wallabyAuthPubKey, decryptedSeedPhrase);
|
|
220
|
+
|
|
221
|
+
const mnemonicSeedPhrase = await this.convertMnemonicSeedphrase({
|
|
222
|
+
hashKey,
|
|
223
|
+
// This is the standard seedPhrase from the wallet creation response but encrypted using Wallaby's auth public key
|
|
224
|
+
encryptedSeedPhrase: encryptedSeedphraseWithWallabyKey,
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
if (!mnemonicSeedPhrase || mnemonicSeedPhrase.error) {
|
|
228
|
+
throw new Error(mnemonicSeedPhrase?.error);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const { convertedEncryptedSeed } = mnemonicSeedPhrase;
|
|
232
|
+
// Decrypt the seedPhrase and retrun as part of response in case it needs to be displayed to the user.
|
|
233
|
+
|
|
234
|
+
decryptedMnemonicSeedphrase = await decryptWithPrivateKey(decryptedClientAuthPrivKey, convertedEncryptedSeed);
|
|
235
|
+
|
|
236
|
+
await Utils.setEncryptedSeedPhrase(this.userId, clientAuthPubKey, decryptedMnemonicSeedphrase, SeedPhraseType.Algorand);
|
|
237
|
+
|
|
238
|
+
return { ...response, decryptedSeedPhrase, decryptedMnemonicSeedphrase };
|
|
239
|
+
} catch (error) {
|
|
240
|
+
logger('SDK::Wallet::create::error', error);
|
|
241
|
+
return Promise.reject(error);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
*
|
|
247
|
+
* @public
|
|
248
|
+
* @description Removes all Wallaby data from local storage.
|
|
249
|
+
* @param {string} userId The user's ID that is associated with their stored keys
|
|
250
|
+
* @returns {Boolean}
|
|
251
|
+
*/
|
|
252
|
+
clearWalletData({ userId }: ClearWalletInput): Boolean {
|
|
253
|
+
try {
|
|
254
|
+
Utils.clearLocalStorage(userId);
|
|
255
|
+
return true;
|
|
256
|
+
} catch (error) {
|
|
257
|
+
logger('SDK::Wallet::clearWalletData::error', error);
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
*
|
|
264
|
+
* @public
|
|
265
|
+
* @description Convert BIP39 Standard seed phrase to Algorand fixed 25 words mnemonic phrase
|
|
266
|
+
* @param {string} hashKey
|
|
267
|
+
* @param {Encrypted} encryptedSeedPhrase It needs to be encypted using wallabyAuthPubKey
|
|
268
|
+
* @returns {ConvertMnemonicSeedPhraseResponse} The mnemonic seed phrase has been converted successfully.
|
|
269
|
+
*/
|
|
270
|
+
async convertMnemonicSeedphrase({
|
|
271
|
+
hashKey,
|
|
272
|
+
encryptedSeedPhrase,
|
|
273
|
+
}: Pick<ConvertMnemonicSeedPhraseRequest, 'hashKey' | 'encryptedSeedPhrase'>): Promise<
|
|
274
|
+
ConvertMnemonicSeedPhraseResponse | undefined
|
|
275
|
+
> {
|
|
276
|
+
try {
|
|
277
|
+
const reference = uuid();
|
|
278
|
+
const signatureMessage = await this.auth.signatureMessage({ hashKey, userId: this.userId });
|
|
279
|
+
if (!signatureMessage || signatureMessage.error) {
|
|
280
|
+
throw new Error(signatureMessage?.error);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const { message, signature, clientAuthPubKey } = signatureMessage;
|
|
284
|
+
|
|
285
|
+
const response: ConvertMnemonicSeedPhraseResponse = await this.request(WalletRoutes.convertMnemonicSeedphrase, {
|
|
286
|
+
method: WalletRoutes.convertMnemonicSeedphrase.method,
|
|
287
|
+
payload: {
|
|
288
|
+
reference,
|
|
289
|
+
signature,
|
|
290
|
+
message,
|
|
291
|
+
clientAuthPubKey,
|
|
292
|
+
encryptedSeed: encryptedSeedPhrase,
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
if (response.error) {
|
|
297
|
+
throw new Error(response.error);
|
|
298
|
+
}
|
|
299
|
+
return response;
|
|
300
|
+
} catch (error) {
|
|
301
|
+
logger('SDK::Wallet::convertMnemonicSeedphrase::error', error);
|
|
302
|
+
return Promise.reject(error);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
*
|
|
308
|
+
* @public
|
|
309
|
+
* @description Recover the lost wallet and its assets.
|
|
310
|
+
* @param {string} newHashKey User need to set a new password while recovering the wallet
|
|
311
|
+
* @param {string} seedPhrase It needs to be encypted using wallabyAuthPubKey
|
|
312
|
+
* @returns {RecoverWalletResponse} The wallet has been recovered successfully.
|
|
313
|
+
*/
|
|
314
|
+
async recover({
|
|
315
|
+
newHashKey,
|
|
316
|
+
seedPhrase,
|
|
317
|
+
}: Omit<RecoverWalletRequest, 'clientAuthPubKey'>): Promise<RecoverWalletResponse | undefined> {
|
|
318
|
+
try {
|
|
319
|
+
// Create new key pair to use with the new (recovered) wallet
|
|
320
|
+
const { publicKey, privateKey } = createIdentity();
|
|
321
|
+
const initRecovery = await this.initRecoveryFlow({ clientAuthPubKey: publicKey });
|
|
322
|
+
if (!initRecovery || initRecovery.error) {
|
|
323
|
+
throw new Error(initRecovery?.error);
|
|
324
|
+
}
|
|
325
|
+
const { signatureMessage, wallabyAuthPubKey } = initRecovery;
|
|
326
|
+
|
|
327
|
+
const hashedMessage = hash.keccak256(signatureMessage);
|
|
328
|
+
const signature = sign(privateKey, hashedMessage);
|
|
329
|
+
|
|
330
|
+
const encryptedSeedphraseWithWallabyKey = await encryptWithPublicKey(wallabyAuthPubKey, seedPhrase);
|
|
331
|
+
const response = await this.request(WalletRoutes.recover, {
|
|
332
|
+
method: WalletRoutes.recover.method,
|
|
333
|
+
payload: {
|
|
334
|
+
clientAuthPubKey: publicKey,
|
|
335
|
+
signature,
|
|
336
|
+
message: signatureMessage,
|
|
337
|
+
externalUserId: this.userId,
|
|
338
|
+
encryptedSeed: encryptedSeedphraseWithWallabyKey,
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
if (!response || response?.error) {
|
|
343
|
+
throw new Error(response.error);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Store the new keys and seedphrase in the new browswer
|
|
347
|
+
Utils.setWallabyAuthPubKey(this.userId, wallabyAuthPubKey);
|
|
348
|
+
Utils.setEncryptedPrivateKey(this.userId, privateKey, newHashKey);
|
|
349
|
+
await Utils.setEncryptedSeedPhrase(this.userId, publicKey, seedPhrase, SeedPhraseType.Standard);
|
|
350
|
+
|
|
351
|
+
// Get Algorand mnemonic seedphrase
|
|
352
|
+
const mnemonicSeedPhrase = await this.convertMnemonicSeedphrase({
|
|
353
|
+
hashKey: newHashKey,
|
|
354
|
+
// This is the standard seedPhrase from the wallet creation response but encrypted using Wallaby's auth public key
|
|
355
|
+
encryptedSeedPhrase: encryptedSeedphraseWithWallabyKey,
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
if (!mnemonicSeedPhrase || mnemonicSeedPhrase.error) {
|
|
359
|
+
throw new Error(mnemonicSeedPhrase?.error);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Decrypt the seedPhrase and retrun as part of response in case it needs to be displayed to the user.
|
|
363
|
+
const { convertedEncryptedSeed } = mnemonicSeedPhrase;
|
|
364
|
+
const decryptedMnemonicSeedphrase = await decryptWithPrivateKey(privateKey, convertedEncryptedSeed);
|
|
365
|
+
await Utils.setEncryptedSeedPhrase(this.userId, publicKey, decryptedMnemonicSeedphrase, SeedPhraseType.Algorand);
|
|
366
|
+
|
|
367
|
+
return { ...response, seedPhrase, decryptedMnemonicSeedphrase };
|
|
368
|
+
} catch (error) {
|
|
369
|
+
logger('SDK::Wallet::recover::error', error);
|
|
370
|
+
return Promise.reject(error);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
*
|
|
376
|
+
* @description Get all user assets across all wallets by specifying user id
|
|
377
|
+
* @param userId Wallaby's user ID
|
|
378
|
+
* @returns {UserWalletsResponse} Returns an array of user assets by specifying user id
|
|
379
|
+
*/
|
|
380
|
+
async getUserAssets({ userId }: QueryUserRequest): Promise<UserAssetsResponse | undefined> {
|
|
381
|
+
try {
|
|
382
|
+
const response = await this.request(WalletRoutes.getUserAssets, {
|
|
383
|
+
method: WalletRoutes.getUserAssets.method,
|
|
384
|
+
payload: {
|
|
385
|
+
userId,
|
|
386
|
+
},
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
if (!response || response.error) {
|
|
390
|
+
throw new Error(response.error);
|
|
391
|
+
}
|
|
392
|
+
return response;
|
|
393
|
+
} catch (error) {
|
|
394
|
+
logger('SDK::Wallet::getUserAssets::error', error);
|
|
395
|
+
return Promise.reject(error);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
*
|
|
401
|
+
* @description Get all wallets belonging to a user.
|
|
402
|
+
* @param userId Wallaby's user ID
|
|
403
|
+
* @returns {UserWalletsResponse} Returns an array of user wallets by specifying user ID
|
|
404
|
+
*/
|
|
405
|
+
async getUserWallets({ userId }: QueryUserRequest): Promise<UserWalletsResponse | undefined> {
|
|
406
|
+
try {
|
|
407
|
+
const response = await this.request(WalletRoutes.getUserWallets, {
|
|
408
|
+
method: WalletRoutes.getUserWallets.method,
|
|
409
|
+
payload: {
|
|
410
|
+
userId,
|
|
411
|
+
},
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
if (!response || response.error) {
|
|
415
|
+
throw new Error(response.error);
|
|
416
|
+
}
|
|
417
|
+
return response;
|
|
418
|
+
} catch (error) {
|
|
419
|
+
logger('SDK::Wallet::getUserWallets::error', error);
|
|
420
|
+
return Promise.reject(error);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
*
|
|
426
|
+
* @description Get all assets that exist in a certain wallet, by specifying a wallet ID
|
|
427
|
+
* @param walletId Wallaby's wallet ID
|
|
428
|
+
* @returns {WalletAssetsResponse} Returns all assets belonging in a certain wallet ID
|
|
429
|
+
*/
|
|
430
|
+
async getWalletAssets({ walletId }: QueryWalletRequest): Promise<WalletAssetsResponse | undefined> {
|
|
431
|
+
try {
|
|
432
|
+
const response = await this.request(WalletRoutes.getWalletAssets, {
|
|
433
|
+
method: WalletRoutes.getWalletAssets.method,
|
|
434
|
+
payload: {
|
|
435
|
+
walletId,
|
|
436
|
+
},
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
if (!response || response.error) {
|
|
440
|
+
throw new Error(response.error);
|
|
441
|
+
}
|
|
442
|
+
return response;
|
|
443
|
+
} catch (error) {
|
|
444
|
+
logger('SDK::Wallet::getWalletAssets::error', error);
|
|
445
|
+
return Promise.reject(error);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
*
|
|
451
|
+
* @public
|
|
452
|
+
* @description Initiate creation for a new multisig asset, by specifying participants and the threshold
|
|
453
|
+
* @param {string} assetSymbol Asset symbol
|
|
454
|
+
* @param {string} tokenType Optional value only used when dealing with non native assets (aka tokens), If native asset, keep the field empty
|
|
455
|
+
* @param {number} threshold The threshold is the number of signatures required to sign a transaction
|
|
456
|
+
* @param {string} creatorAddress The address of the account that is initiating the multisig asset creation
|
|
457
|
+
* @param {Array} participants The array containing the addresses of the users requested to participate in the multisig
|
|
458
|
+
* @param {number} version Version of the multi-sig
|
|
459
|
+
* @returns {CreateMultisigAssetResponse} Multisig asset was created successfully
|
|
460
|
+
*/
|
|
461
|
+
async createMultisigAsset({
|
|
462
|
+
assetSymbol,
|
|
463
|
+
tokenType,
|
|
464
|
+
threshold,
|
|
465
|
+
creatorAddress,
|
|
466
|
+
participants,
|
|
467
|
+
version,
|
|
468
|
+
}: CreateMultisigAssetRequest): Promise<CreateMultisigAssetResponse> {
|
|
469
|
+
try {
|
|
470
|
+
const response = await this.request(WalletRoutes.createMultisigAsset, {
|
|
471
|
+
method: WalletRoutes.createMultisigAsset.method,
|
|
472
|
+
payload: {
|
|
473
|
+
assetSymbol,
|
|
474
|
+
tokenType: tokenType ?? null,
|
|
475
|
+
threshold,
|
|
476
|
+
creatorAddress,
|
|
477
|
+
participants,
|
|
478
|
+
version,
|
|
479
|
+
},
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
if (!response || response.error) {
|
|
483
|
+
throw new Error();
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
return response;
|
|
487
|
+
} catch (error) {
|
|
488
|
+
logger('SDK::Wallet::createMultisigAsset::error', error);
|
|
489
|
+
return Promise.reject(error);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
private async initRecoveryFlow({
|
|
494
|
+
clientAuthPubKey,
|
|
495
|
+
}: Pick<RecoverWalletRequest, 'clientAuthPubKey'>): Promise<RecoverWalletResponse | undefined> {
|
|
496
|
+
try {
|
|
497
|
+
const initRecovery = await this.request(WalletRoutes.initRecoveryFlow, {
|
|
498
|
+
method: WalletRoutes.initRecoveryFlow.method,
|
|
499
|
+
payload: {
|
|
500
|
+
externalUserId: this.userId,
|
|
501
|
+
clientAuthPubKey,
|
|
502
|
+
},
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
if (!initRecovery || initRecovery.error) {
|
|
506
|
+
throw new Error(initRecovery.error);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
return { signatureMessage: initRecovery.signatureMessage, wallabyAuthPubKey: initRecovery.wallabyAuthPubKey };
|
|
510
|
+
} catch (error) {
|
|
511
|
+
logger('SDK::Wallet::initRecoveryFlow::error', error);
|
|
512
|
+
return Promise.reject(error);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
class LocalStorageMock {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.store = {};
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
clear() {
|
|
7
|
+
this.store = {};
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
getItem(key) {
|
|
11
|
+
return this.store[key] || null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
setItem(key, value) {
|
|
15
|
+
this.store[key] = String(value);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
removeItem(key) {
|
|
19
|
+
delete this.store[key];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
global.localStorage = new LocalStorageMock();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { RequestMothod } from '../types';
|
|
2
|
+
|
|
3
|
+
export const AuthRoutes = {
|
|
4
|
+
signatureMessage: {
|
|
5
|
+
fullPath: '/auth/signature-message',
|
|
6
|
+
method: RequestMothod.Post,
|
|
7
|
+
accessTokenRequired: false,
|
|
8
|
+
},
|
|
9
|
+
signIn: {
|
|
10
|
+
fullPath: '/auth/sign-in',
|
|
11
|
+
method: RequestMothod.Post,
|
|
12
|
+
accessTokenRequired: true,
|
|
13
|
+
},
|
|
14
|
+
signUp: {
|
|
15
|
+
fullPath: '/auth/sign-up',
|
|
16
|
+
method: RequestMothod.Post,
|
|
17
|
+
accessTokenRequired: false,
|
|
18
|
+
},
|
|
19
|
+
refreshAcessToken: {
|
|
20
|
+
fullPath: '/auth/refresh-access-token',
|
|
21
|
+
method: RequestMothod.Post,
|
|
22
|
+
accessTokenRequired: true,
|
|
23
|
+
},
|
|
24
|
+
clientAuthPubKey: {
|
|
25
|
+
fullPath: 'auth/client-auth-pubkey',
|
|
26
|
+
method: RequestMothod.Put,
|
|
27
|
+
accessTokenRequired: true,
|
|
28
|
+
},
|
|
29
|
+
wallabyAuthPublicKey: {
|
|
30
|
+
fullPath: '/auth/wallaby-auth-pubkey',
|
|
31
|
+
method: RequestMothod.Get,
|
|
32
|
+
accessTokenRequired: true,
|
|
33
|
+
},
|
|
34
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { QueryUserRequest, RequestMothod } from '../types';
|
|
2
|
+
|
|
3
|
+
export const SystemRoutes = {
|
|
4
|
+
getSupportedBlockchains: {
|
|
5
|
+
fullPath: '/chains',
|
|
6
|
+
method: RequestMothod.Get,
|
|
7
|
+
accessTokenRequired: false,
|
|
8
|
+
},
|
|
9
|
+
getSupportedTokens: {
|
|
10
|
+
fullPath: '/assets/configurations',
|
|
11
|
+
method: RequestMothod.Get,
|
|
12
|
+
accessTokenRequired: false,
|
|
13
|
+
},
|
|
14
|
+
getUserByExternalUserId: {
|
|
15
|
+
fullPath: ({ externalUserId }: QueryUserRequest) => `/users/${externalUserId}/externalUserId`,
|
|
16
|
+
method: RequestMothod.Get,
|
|
17
|
+
accessTokenRequired: false,
|
|
18
|
+
},
|
|
19
|
+
getUserData: {
|
|
20
|
+
fullPath: ({ userId }: QueryUserRequest) => `/users/${userId}`,
|
|
21
|
+
method: RequestMothod.Get,
|
|
22
|
+
accessTokenRequired: false,
|
|
23
|
+
},
|
|
24
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RequestMothod } from '../types';
|
|
2
|
+
|
|
3
|
+
export const TransactionsRoutes = {
|
|
4
|
+
transfer: {
|
|
5
|
+
fullPath: '/transactions/transfer',
|
|
6
|
+
method: RequestMothod.Post,
|
|
7
|
+
accessTokenRequired: true,
|
|
8
|
+
},
|
|
9
|
+
createAllowance: {
|
|
10
|
+
fullPath: '/transactions/create-allowance',
|
|
11
|
+
method: RequestMothod.Post,
|
|
12
|
+
accessTokenRequired: true,
|
|
13
|
+
},
|
|
14
|
+
estimateFee: {
|
|
15
|
+
fullPath: '/transactions/estimate-fee',
|
|
16
|
+
method: RequestMothod.Get,
|
|
17
|
+
accessTokenRequired: true,
|
|
18
|
+
},
|
|
19
|
+
getTransaction: {
|
|
20
|
+
fullPath: '/transactions/status',
|
|
21
|
+
method: RequestMothod.Get,
|
|
22
|
+
accessTokenRequired: true,
|
|
23
|
+
},
|
|
24
|
+
getTransactions: {
|
|
25
|
+
fullPath: '/query/transactions',
|
|
26
|
+
method: RequestMothod.Get,
|
|
27
|
+
accessTokenRequired: true,
|
|
28
|
+
},
|
|
29
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { TokenBalanceRequest, QueryUserRequest, QueryWalletRequest, RequestMothod } from '../types';
|
|
2
|
+
|
|
3
|
+
export const WalletRoutes = {
|
|
4
|
+
create: {
|
|
5
|
+
fullPath: '/wallets/create',
|
|
6
|
+
method: RequestMothod.Post,
|
|
7
|
+
accessTokenRequired: true,
|
|
8
|
+
},
|
|
9
|
+
initRecoveryFlow: {
|
|
10
|
+
fullPath: 'wallets/recover/init',
|
|
11
|
+
method: RequestMothod.Post,
|
|
12
|
+
accessTokenRequired: false,
|
|
13
|
+
},
|
|
14
|
+
recover: {
|
|
15
|
+
fullPath: '/wallets/recover',
|
|
16
|
+
method: RequestMothod.Post,
|
|
17
|
+
accessTokenRequired: false,
|
|
18
|
+
},
|
|
19
|
+
getTokenBalance: {
|
|
20
|
+
fullPath: ({ address, symbol, tokenStandard }: TokenBalanceRequest) => {
|
|
21
|
+
return `/query/assets/balance?address=${address}&assetSymbol=${symbol}${
|
|
22
|
+
tokenStandard ? `&tokenType=${tokenStandard}` : ''
|
|
23
|
+
}`;
|
|
24
|
+
},
|
|
25
|
+
method: RequestMothod.Get,
|
|
26
|
+
accessTokenRequired: false,
|
|
27
|
+
},
|
|
28
|
+
// TODO Move this to transactions module
|
|
29
|
+
estimateFee: {
|
|
30
|
+
fullPath: '/transactions/estimate-fee',
|
|
31
|
+
method: RequestMothod.Get,
|
|
32
|
+
accessTokenRequired: true,
|
|
33
|
+
},
|
|
34
|
+
getWalletAssets: {
|
|
35
|
+
fullPath: ({ walletId }: QueryWalletRequest) => {
|
|
36
|
+
return `/wallets/${walletId}/assets`;
|
|
37
|
+
},
|
|
38
|
+
method: RequestMothod.Get,
|
|
39
|
+
accessTokenRequired: true,
|
|
40
|
+
},
|
|
41
|
+
getUserAssets: {
|
|
42
|
+
fullPath: ({ userId }: QueryUserRequest) => {
|
|
43
|
+
return `/users/${userId}/assets`;
|
|
44
|
+
},
|
|
45
|
+
method: RequestMothod.Get,
|
|
46
|
+
accessTokenRequired: true,
|
|
47
|
+
},
|
|
48
|
+
getUserWallets: {
|
|
49
|
+
fullPath: ({ userId }: QueryUserRequest) => {
|
|
50
|
+
return `/users/${userId}/wallets`;
|
|
51
|
+
},
|
|
52
|
+
method: RequestMothod.Get,
|
|
53
|
+
accessTokenRequired: true,
|
|
54
|
+
},
|
|
55
|
+
convertMnemonicSeedphrase: {
|
|
56
|
+
fullPath: '/wallets/convert-mnemonic-seed',
|
|
57
|
+
method: RequestMothod.Post,
|
|
58
|
+
accessTokenRequired: false,
|
|
59
|
+
},
|
|
60
|
+
createMultisigAsset: {
|
|
61
|
+
fullPath: '/wallets/create-multisig-asset',
|
|
62
|
+
method: RequestMothod.Post,
|
|
63
|
+
accessTokenRequired: true,
|
|
64
|
+
},
|
|
65
|
+
};
|