-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
storage.js
171 lines (147 loc) · 4.97 KB
/
storage.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Note: If a piece of data wasn't found, returning null instead of default state of the
// property to avoid coupling with redux reducers
import * as SecureStore from "expo-secure-store";
import { AsyncStorage } from "react-native";
import { createFromPrivateKey } from "tasit-account/dist/testHelpers/helpers";
import { logWarn } from "@helpers";
// Storage keys
const EPHEMERAL_ACCOUNT_PRIV_KEY = "EPHEMERAL_ACCOUNT_PRIV_KEY";
const MY_ASSETS_LIST = "MY_ASSETS_LIST";
const EPHEMERAL_ACCOUNT_CREATION_STATUS = "EPHEMERAL_ACCOUNT_CREATION_STATUS";
const EPHEMERAL_ACCOUNT_CREATION_ACTIONS = "EPHEMERAL_ACCOUNT_CREATION_ACTIONS";
const IS_FIRST_APP_USE = "IS_FIRST_APP_USE";
const USER_ACTIONS = "USER_ACTIONS";
const _clearData = async (key): Promise<any> => {
try {
await AsyncStorage.removeItem(key);
} catch (error) {
logWarn(`Unable to delete data (key = ${key}) from storage.`);
}
try {
await SecureStore.deleteItemAsync(key);
} catch (error) {
logWarn(`Unable to delete data key = ${key} from secure storage.`);
}
};
export const clearAllStorage = async () => {
await _clearData(IS_FIRST_APP_USE);
await _clearData(EPHEMERAL_ACCOUNT_PRIV_KEY);
await _clearData(USER_ACTIONS);
await _clearData(EPHEMERAL_ACCOUNT_CREATION_ACTIONS);
await _clearData(EPHEMERAL_ACCOUNT_CREATION_STATUS);
await _clearData(MY_ASSETS_LIST);
};
const _toString = obj => {
if (obj === null) return null;
try {
return JSON.stringify(obj);
} catch {
throw Error(`Unable to parse object to a JSON string.`);
}
};
const _fromString = string => {
try {
return JSON.parse(string);
} catch {
throw Error(`Unable to parse JSON string to an object.`);
}
};
// Note: the value should be a string
const _storeData = async (key, value, securely) => {
try {
if (securely) {
// More about options:
// https://docs.expo.io/versions/latest/sdk/securestore/#securestoresetitemasynckey-value-options
const options = { keychainAccessible: SecureStore.WHEN_UNLOCKED };
await SecureStore.setItemAsync(key, value, options);
} else {
await AsyncStorage.setItem(key, value);
}
} catch (error) {
throw Error(`Unable to ${securely ? "securely" : ""} store data.`);
}
};
export const storeUserActions = async userActions => {
const strUserActions = _toString(userActions);
await _storeData(USER_ACTIONS, strUserActions, false);
};
const _retrieveData = async key => {
try {
let value = await AsyncStorage.getItem(key);
if (value === null) value = await SecureStore.getItemAsync(key);
return value;
} catch (error) {
throw Error(`Unable to retrieve data from storage.`);
}
};
export const retrieveUserActions = async () => {
const strUserActions = await _retrieveData(USER_ACTIONS);
const userActions = _fromString(strUserActions);
return userActions;
};
export const storeIsFirstUse = async isFirstUse => {
const strIsFirstUse = _toString(isFirstUse);
await _storeData(IS_FIRST_APP_USE, strIsFirstUse, false);
};
export const retrieveIsFirstUse = async () => {
const strIsFirstUse = await _retrieveData(IS_FIRST_APP_USE);
const isFirstUse = _fromString(strIsFirstUse);
if (isFirstUse === null) return true;
return isFirstUse;
};
export const storeAccountCreationActions = async creationActions => {
const strCreationActions = _toString(creationActions);
await _storeData(
EPHEMERAL_ACCOUNT_CREATION_ACTIONS,
strCreationActions,
false
);
};
export const retrieveAccountCreationActions = async () => {
const strCreationActions = await _retrieveData(
EPHEMERAL_ACCOUNT_CREATION_ACTIONS
);
const creationActions = _fromString(strCreationActions);
return creationActions;
};
export const storeAccountCreationStatus = async status => {
await _storeData(EPHEMERAL_ACCOUNT_CREATION_STATUS, status, false);
};
export const retrieveAccountCreationStatus = async () => {
const status = await _retrieveData(EPHEMERAL_ACCOUNT_CREATION_STATUS);
return status;
};
export const storeAccount = async account => {
const privateKey = account !== null ? account.privateKey : null;
await _storeData(EPHEMERAL_ACCOUNT_PRIV_KEY, privateKey, true);
};
export const retrieveAccount = async () => {
let account = null;
const privateKey = await _retrieveData(EPHEMERAL_ACCOUNT_PRIV_KEY);
if (privateKey != null) account = createFromPrivateKey(privateKey);
return account;
};
export const storeMyAssets = async myAssets => {
const strMyAssets = _toString(myAssets);
await _storeData(MY_ASSETS_LIST, strMyAssets, false);
};
export const retrieveMyAssets = async () => {
const strMyAssets = await _retrieveData(MY_ASSETS_LIST);
const myAssets = _fromString(strMyAssets);
return myAssets;
};
export default {
storeAccount,
retrieveAccount,
storeMyAssets,
retrieveMyAssets,
storeAccountCreationStatus,
retrieveAccountCreationStatus,
storeAccountCreationActions,
retrieveAccountCreationActions,
storeIsFirstUse,
retrieveIsFirstUse,
retrieveUserActions,
storeUserActions,
clearAllStorage,
};