forked from gari-network/gari-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetWalletDetails.js
53 lines (48 loc) · 1.88 KB
/
getWalletDetails.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const { createWallet, getWalletDetails, } = require('./api.js')
const { sdkValidate } = require('./sdkInitialize.js')
const initialize = require('./helper.js')
/**
*
* @param {string} gariClientId - gariClientId of the app
* @param {string} token - jwt token for user information
* @returns
*/
async function createWalletOrGetWallet(token) {
try {
const validate = sdkValidate()
if (!validate) {
throw new Error(`sdk not initialized`)
}
// fetch user : if exist then return publickey and its gariBalance
// const { data: response } = await getWalletDetails(token);
const getWalletResponse = await getWalletDetails(token).catch((error) => {
console.log("error while getting wallet details ", error);
if(error.response.status != 404)
{
throw Error(error);
}
return undefined;
});
if (getWalletResponse && getWalletResponse.data) {
// user wallet details found
if(getWalletResponse.data.data.publicKey)
{
const publicKey = getWalletResponse.data.data.publicKey
const balance = getWalletResponse.data.data.balance
return { publicKey, balance }
}
}
// userwallet details not found
// web3auth generates pub/priv key pair for that userId
const newUserWeb3Login = await initialize(token);
const { publicKey } = newUserWeb3Login;
// create user wallet without its tokenAssociatedAccount // token here refers to gari token
const { data } = await createWallet(publicKey, token)
const balance = data.data.balance;
return { publicKey, balance };
} catch (error) {
console.log('error', error)
throw Error(error)
}
}
module.exports = createWalletOrGetWallet