-
Notifications
You must be signed in to change notification settings - Fork 4
/
CredentialsContext.tsx
157 lines (136 loc) · 4.08 KB
/
CredentialsContext.tsx
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
import ExpoConstants from "expo-constants";
import * as SecureStore from "expo-secure-store";
import * as React from "react";
export const KEY_LIST = [
"TURNKEY_API_PUBLIC_KEY",
"TURNKEY_API_PRIVATE_KEY",
"TURNKEY_BASE_URL",
"TURNKEY_ORGANIZATION_ID",
"TURNKEY_PRIVATE_KEY_ID",
"INFURA_API_KEY",
"ETHERSCAN_API_KEY",
] as const;
type TCredentials = {
TURNKEY_API_PUBLIC_KEY: string | null;
TURNKEY_API_PRIVATE_KEY: string | null;
TURNKEY_BASE_URL: string | null;
TURNKEY_ORGANIZATION_ID: string | null;
TURNKEY_PRIVATE_KEY_ID: string | null;
INFURA_API_KEY: string | null;
ETHERSCAN_API_KEY: string | null;
};
export const initialCredentialsState = {
TURNKEY_API_PUBLIC_KEY: null,
TURNKEY_API_PRIVATE_KEY: null,
TURNKEY_BASE_URL: "https://api.turnkey.com",
TURNKEY_ORGANIZATION_ID: null,
TURNKEY_PRIVATE_KEY_ID: null,
INFURA_API_KEY: null,
ETHERSCAN_API_KEY: null,
};
type TCredentialsContextValue = {
credentials: TCredentials;
updateCredential: (
key: (typeof KEY_LIST)[number],
value: string
) => Promise<void>;
hasAllCredentials: boolean;
resetAll: () => Promise<void>;
} | null;
const CredentialsContext = React.createContext<TCredentialsContextValue>(null);
export function CredentialsContextProvider(props: {
children: React.ReactNode;
}) {
const [credentials, setCredentials] = React.useState<TCredentials>(
initialCredentialsState
);
const hasInitialized = React.useRef<boolean>(false);
React.useEffect(() => {
if (hasInitialized.current) {
return;
}
hasInitialized.current = true;
Promise.all(
KEY_LIST.map(async (key) => {
return {
key,
storedValue: await SecureStore.getItemAsync(key),
};
})
).then((mapList) => {
const result: TCredentials = { ...initialCredentialsState };
for (const { key, storedValue } of mapList) {
result[key] = storedValue;
// Only use prefilled variables in dev builds
if (__DEV__) {
if (storedValue == null) {
const maybePrefilledValue: string | undefined =
ExpoConstants.manifest?.extra?.[key];
if (
maybePrefilledValue &&
// Not a placeholder
!maybePrefilledValue.startsWith("<")
) {
result[key] = maybePrefilledValue ?? null;
}
}
}
// Sane defaults
if (key === "TURNKEY_BASE_URL" && result[key] == null) {
result[key] = initialCredentialsState[key];
}
}
setCredentials(result);
});
}, []);
const contextValue = React.useMemo(() => {
return {
credentials,
updateCredential: async (
key: (typeof KEY_LIST)[number],
value: string | null
) => {
if (value == null) {
await SecureStore.deleteItemAsync(key);
} else {
await SecureStore.setItemAsync(key, value);
}
setCredentials((currentState) => ({
...currentState,
[key]: value,
}));
},
hasAllCredentials: Object.keys(credentials).every((key) =>
// @ts-expect-error -- `Object.keys(...)` doesn't refine
Boolean(credentials[key])
),
resetAll: async () => {
setCredentials(initialCredentialsState);
await Promise.all(
KEY_LIST.map(async (key) => {
const maybeValue = initialCredentialsState[key];
if (maybeValue != null) {
await SecureStore.setItemAsync(key, maybeValue);
} else {
await SecureStore.deleteItemAsync(key);
}
})
);
},
};
}, [credentials]);
return (
<CredentialsContext.Provider value={contextValue}>
{props.children}
</CredentialsContext.Provider>
);
}
export function useCredentialsContext(): NonNullable<TCredentialsContextValue> {
const value = React.useContext(CredentialsContext);
if (value == null) {
throw new Error(
`Context wasn't initialized. Did you forget to put a \`<CredentialsContextProvider>\` ancestor?`
);
}
return value;
}