@dynamic-labs/sdk-react-core 2.1.0-alpha.10 → 2.1.0-alpha.11
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/CHANGELOG.md +13 -0
- package/package.cjs +2 -2
- package/package.js +2 -2
- package/package.json +13 -13
- package/src/index.cjs +2 -0
- package/src/index.d.ts +1 -1
- package/src/index.js +1 -0
- package/src/lib/data/api/chains/balances.cjs +38 -0
- package/src/lib/data/api/chains/balances.d.ts +8 -0
- package/src/lib/data/api/chains/balances.js +34 -0
- package/src/lib/data/api/chains/index.d.ts +1 -0
- package/src/lib/shared/types/general.d.ts +1 -0
- package/src/lib/utils/constants/errors.cjs +2 -0
- package/src/lib/utils/constants/errors.d.ts +1 -0
- package/src/lib/utils/constants/errors.js +2 -1
- package/src/lib/utils/hooks/index.d.ts +1 -0
- package/src/lib/utils/hooks/useEmbeddedWallet/useEmbeddedWallet.cjs +34 -3
- package/src/lib/utils/hooks/useEmbeddedWallet/useEmbeddedWallet.d.ts +18 -9
- package/src/lib/utils/hooks/useEmbeddedWallet/useEmbeddedWallet.js +34 -3
- package/src/lib/utils/hooks/useEmbeddedWallet/useMPCEmbeddedWallet/useMPCEmbeddedWallet.cjs +5 -1
- package/src/lib/utils/hooks/useEmbeddedWallet/useMPCEmbeddedWallet/useMPCEmbeddedWallet.d.ts +1 -0
- package/src/lib/utils/hooks/useEmbeddedWallet/useMPCEmbeddedWallet/useMPCEmbeddedWallet.js +5 -1
- package/src/lib/utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.cjs +4 -0
- package/src/lib/utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.d.ts +2 -0
- package/src/lib/utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/constants.js +3 -1
- package/src/lib/utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/useSecureEnclaveEmbeddedWallet.cjs +68 -28
- package/src/lib/utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/useSecureEnclaveEmbeddedWallet.d.ts +11 -6
- package/src/lib/utils/hooks/useEmbeddedWallet/useSecureEnclaveEmbeddedWallet/useSecureEnclaveEmbeddedWallet.js +70 -30
- package/src/lib/utils/hooks/useTokenBalances/index.d.ts +1 -0
- package/src/lib/utils/hooks/useTokenBalances/useTokenBalances.cjs +141 -0
- package/src/lib/utils/hooks/useTokenBalances/useTokenBalances.d.ts +12 -0
- package/src/lib/utils/hooks/useTokenBalances/useTokenBalances.js +137 -0
- package/src/lib/views/EmbeddedReveal/EmbeddedRevealView/EmbeddedRevealView.cjs +1 -1
- package/src/lib/views/EmbeddedReveal/EmbeddedRevealView/EmbeddedRevealView.js +1 -1
- package/src/lib/views/EmbeddedReveal/utils/turnkeyExport/turnkeyExport.cjs +5 -4
- package/src/lib/views/EmbeddedReveal/utils/turnkeyExport/turnkeyExport.js +5 -4
|
@@ -47,11 +47,12 @@ require('../../../../context/VerificationContext/VerificationContext.cjs');
|
|
|
47
47
|
require('react-dom');
|
|
48
48
|
var useElementById = require('../../useElementById/useElementById.cjs');
|
|
49
49
|
var WalletContext = require('../../../../context/WalletContext/WalletContext.cjs');
|
|
50
|
+
var useIsTurnkeyWallet = require('../../useIsTurnkeyWallet/useIsTurnkeyWallet.cjs');
|
|
50
51
|
var validateUserIsLoggedIn = require('../../../validations/validateUserIsLoggedIn/validateUserIsLoggedIn.cjs');
|
|
51
52
|
var validateTurnkeyProviderEnabled = require('../../../validations/validateTurnkeyProviderEnabled/validateTurnkeyProviderEnabled.cjs');
|
|
52
53
|
var errors = require('../../../constants/errors.cjs');
|
|
53
|
-
var useIsTurnkeyWallet = require('../../useIsTurnkeyWallet/useIsTurnkeyWallet.cjs');
|
|
54
54
|
var passkeyRecovery = require('../../../../views/Passkey/utils/passkeyRecovery/passkeyRecovery.cjs');
|
|
55
|
+
var turnkeyExport = require('../../../../views/EmbeddedReveal/utils/turnkeyExport/turnkeyExport.cjs');
|
|
55
56
|
var useTurnkey = require('./useTurnkey/useTurnkey.cjs');
|
|
56
57
|
var constants = require('./constants.cjs');
|
|
57
58
|
require('../../../../context/ThemeContext/ThemeContext.cjs');
|
|
@@ -105,20 +106,21 @@ var useInternalDynamicContext = require('../../../../context/DynamicContext/useD
|
|
|
105
106
|
* getPasskeys, - gets all passkeys for the user
|
|
106
107
|
* isLoadingEmbeddedWallet, - loading state of the embedded wallet
|
|
107
108
|
* isSessionActive, - checks if the embedded wallet session is active
|
|
109
|
+
* revealEmbeddedWalletKey - export the recovery phrase or private key of the embedded wallet
|
|
108
110
|
* sendOneTimeCode, - sends a one-time code to the user so they can create a session or add a new passkey
|
|
109
111
|
* userHasEmbeddedWallet, - checks if the user has an embedded wallet
|
|
110
112
|
* }
|
|
111
113
|
*/
|
|
112
114
|
const useSecureEnclaveEmbeddedWallet = () => {
|
|
113
115
|
var _a, _b, _c, _d;
|
|
114
|
-
const { projectSettings, onboardingOnlyJwt, primaryWallet, user, environmentId, walletConnectorOptions, setUser, } = useInternalDynamicContext.useInternalDynamicContext();
|
|
116
|
+
const { projectSettings, authToken, onboardingOnlyJwt, primaryWallet, user, environmentId, walletConnectorOptions, setUser, } = useInternalDynamicContext.useInternalDynamicContext();
|
|
115
117
|
const { createTurnkeyWallet } = useTurnkey.useTurnkey();
|
|
116
118
|
const { hasRecoveryEmail } = useIsTurnkeyWallet.useIsTurnkeyWallet();
|
|
117
119
|
const { createRootElement } = useElementById.useElementById();
|
|
118
120
|
const { isLoadingEmbeddedWallet, setIsLoadingEmbeddedWallet } = WalletContext.useWalletContext();
|
|
119
121
|
const wallet = getEOAWallet.getEOAWallet(primaryWallet, (user === null || user === void 0 ? void 0 : user.verifiedCredentials) || []);
|
|
120
122
|
/**
|
|
121
|
-
* Checks if the user has
|
|
123
|
+
* Checks if the user has a secure enclave embedded wallet or not
|
|
122
124
|
* @returns boolean
|
|
123
125
|
*/
|
|
124
126
|
const userHasEmbeddedWallet = React.useCallback(() => hasEmbeddedWallet.hasEmbeddedWallet(getAuthToken.getAuthToken() || onboardingOnlyJwt, 'turnkey'), [onboardingOnlyJwt]);
|
|
@@ -159,9 +161,9 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
159
161
|
* @param oneTimeCode - optional one-time code parameter. If not informed it tries to restore a session
|
|
160
162
|
* @returns Promise<'session_created' | 'session_restored'> - returns a string indicating the status of the session
|
|
161
163
|
*/
|
|
162
|
-
const createOrRestoreSession = React.useCallback((
|
|
163
|
-
var _h, _j, _k, _l, _m, _o
|
|
164
|
-
const jwt = (
|
|
164
|
+
const createOrRestoreSession = React.useCallback((options) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
165
|
+
var _g, _h, _j, _k, _l, _m, _o;
|
|
166
|
+
const jwt = (_g = getAuthToken.getAuthToken()) !== null && _g !== void 0 ? _g : onboardingOnlyJwt;
|
|
165
167
|
const { decodedJwt } = validateUserIsLoggedIn.validateUserIsLoggedIn(jwt);
|
|
166
168
|
const turnkeyRecoveryHandler = getEmbeddedWalletAuthHandler.getEmbeddedWalletAuthHandler(wallet === null || wallet === void 0 ? void 0 : wallet.connector);
|
|
167
169
|
if (turnkeyRecoveryHandler.isSessionActive()) {
|
|
@@ -169,13 +171,13 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
169
171
|
}
|
|
170
172
|
const turnkeyAuthIframeContainerRef = createRootElement(constants.iframeContainerId);
|
|
171
173
|
// try to restore session if possible
|
|
172
|
-
if (passkeyRecovery.canRestoreEmbeddedWalletSession((
|
|
174
|
+
if (passkeyRecovery.canRestoreEmbeddedWalletSession((_h = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _h === void 0 ? void 0 : _h.sessionKeyDuration)) {
|
|
173
175
|
try {
|
|
174
176
|
const sessionRestored = yield passkeyRecovery.restoreEmbeddedWalletSession({
|
|
175
177
|
authToken: jwt,
|
|
176
178
|
iframeContainer: turnkeyAuthIframeContainerRef.current,
|
|
177
179
|
iframeElementId: constants.iframeElementId,
|
|
178
|
-
sessionExpiration: getEmbeddedWalletSessionExpiration.getEmbeddedWalletSessionExpiration((
|
|
180
|
+
sessionExpiration: getEmbeddedWalletSessionExpiration.getEmbeddedWalletSessionExpiration((_k = (_j = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk) === null || _j === void 0 ? void 0 : _j.embeddedWallets) === null || _k === void 0 ? void 0 : _k.sessionKeyDuration),
|
|
179
181
|
wallet: primaryWallet,
|
|
180
182
|
});
|
|
181
183
|
if (sessionRestored)
|
|
@@ -185,7 +187,7 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
185
187
|
logger.logger.error('Failed to restore embedded wallet', err);
|
|
186
188
|
}
|
|
187
189
|
}
|
|
188
|
-
if (!oneTimeCode) {
|
|
190
|
+
if (!(options === null || options === void 0 ? void 0 : options.oneTimeCode)) {
|
|
189
191
|
throw new utils.DynamicError('One-time code is required to create a session.', errors.INVALID_REQUEST);
|
|
190
192
|
}
|
|
191
193
|
// tries to create a new session with the informed one-time code
|
|
@@ -193,11 +195,11 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
193
195
|
if (!userHasEmbeddedWallet() || !hasRecoveryEmail) {
|
|
194
196
|
throw new utils.DynamicError('User does not have a secure enclave wallet or a verified email', errors.INVALID_WALLET_DATA);
|
|
195
197
|
}
|
|
196
|
-
const organizationId = (
|
|
197
|
-
yield turnkeyRecoveryHandler.verifyRecoveryCode(oneTimeCode, organizationId);
|
|
198
|
+
const organizationId = (_o = (_m = (_l = decodedJwt.verifiedCredentials) === null || _l === void 0 ? void 0 : _l.find(({ walletName }) => walletName === null || walletName === void 0 ? void 0 : walletName.startsWith('turnkey'))) === null || _m === void 0 ? void 0 : _m.walletProperties) === null || _o === void 0 ? void 0 : _o.turnkeySubOrganizationId;
|
|
199
|
+
yield turnkeyRecoveryHandler.verifyRecoveryCode(options.oneTimeCode, organizationId);
|
|
198
200
|
const sessionSettings = {
|
|
199
201
|
createdAt: new Date().getTime(),
|
|
200
|
-
emailCode: oneTimeCode,
|
|
202
|
+
emailCode: options.oneTimeCode,
|
|
201
203
|
userId: turnkeyRecoveryHandler.recoveryUserId,
|
|
202
204
|
};
|
|
203
205
|
localStorage.LocalStorage.setToLS(localStorage$1.EMBEDDED_WALLET_SESSION_SETTINGS, sessionSettings);
|
|
@@ -213,15 +215,14 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
213
215
|
]);
|
|
214
216
|
/**
|
|
215
217
|
* Creates a new passkey both for an existing wallet or a wallet to be created
|
|
216
|
-
* @param
|
|
217
|
-
*
|
|
218
|
+
* @param options - optional parameter to inform a one-time code to create a session,
|
|
218
219
|
* @returns Promise<WebAuthnAttestation>
|
|
219
220
|
*/
|
|
220
|
-
const createPasskey = React.useCallback((
|
|
221
|
-
var
|
|
221
|
+
const createPasskey = React.useCallback((options) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
222
|
+
var _p, _q;
|
|
222
223
|
const { decodedJwt, jwt } = validateUserIsLoggedIn.validateUserIsLoggedIn(getAuthToken.getAuthToken() || onboardingOnlyJwt);
|
|
223
224
|
validateTurnkeyProviderEnabled.validateTurnkeyProviderEnabled(projectSettings);
|
|
224
|
-
const turnkeyWallet = findTurnkeyWallet.findTurnkeyWalletByPrimaryChain(walletConnectorOptions, (
|
|
225
|
+
const turnkeyWallet = findTurnkeyWallet.findTurnkeyWalletByPrimaryChain(walletConnectorOptions, (_p = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _p === void 0 ? void 0 : _p.chainConfigurations, decodedJwt.verifiedCredentials);
|
|
225
226
|
if (!(turnkeyWallet === null || turnkeyWallet === void 0 ? void 0 : turnkeyWallet.walletConnector)) {
|
|
226
227
|
throw new utils.DynamicError('Wallet connector not found', errors.INVALID_WALLET_DATA);
|
|
227
228
|
}
|
|
@@ -236,13 +237,13 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
236
237
|
if (!('id' in turnkeyWallet) || !hasEmailVC) {
|
|
237
238
|
return walletConnector.getWebAuthnAttestation();
|
|
238
239
|
}
|
|
239
|
-
const organizationId = (
|
|
240
|
+
const organizationId = (_q = turnkeyWallet === null || turnkeyWallet === void 0 ? void 0 : turnkeyWallet.walletProperties) === null || _q === void 0 ? void 0 : _q.turnkeySubOrganizationId;
|
|
240
241
|
if (!organizationId) {
|
|
241
242
|
throw new utils.DynamicError('No sub organization id found for the wallet', errors.INVALID_WALLET_DATA);
|
|
242
243
|
}
|
|
243
244
|
const turnkAuthenticatorHandler = getEmbeddedWalletAuthHandler.getEmbeddedWalletAuthHandler(walletConnector);
|
|
244
245
|
// add new passkey to the wallet through an active session
|
|
245
|
-
yield createOrRestoreSession(
|
|
246
|
+
yield createOrRestoreSession(options);
|
|
246
247
|
const { attestation, challenge, displayName } = yield walletConnector.getWebAuthnAttestation();
|
|
247
248
|
yield turnkAuthenticatorHandler.addPasskeyAuthenticator({
|
|
248
249
|
attestation,
|
|
@@ -265,9 +266,7 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
265
266
|
onboardingOnlyJwt,
|
|
266
267
|
projectSettings,
|
|
267
268
|
setUser,
|
|
268
|
-
user
|
|
269
|
-
user === null || user === void 0 ? void 0 : user.phoneNumber,
|
|
270
|
-
user === null || user === void 0 ? void 0 : user.verifiedCredentials,
|
|
269
|
+
user,
|
|
271
270
|
walletConnectorOptions,
|
|
272
271
|
]);
|
|
273
272
|
/**
|
|
@@ -275,8 +274,8 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
275
274
|
* @returns Promise<Passkey[]>
|
|
276
275
|
*/
|
|
277
276
|
const getPasskeys = React.useCallback(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
278
|
-
var
|
|
279
|
-
const { jwt } = validateUserIsLoggedIn.validateUserIsLoggedIn((
|
|
277
|
+
var _r;
|
|
278
|
+
const { jwt } = validateUserIsLoggedIn.validateUserIsLoggedIn((_r = getAuthToken.getAuthToken()) !== null && _r !== void 0 ? _r : onboardingOnlyJwt);
|
|
280
279
|
const data = yield embeddedWallets.getUserPasskeys({
|
|
281
280
|
environmentId: environmentId,
|
|
282
281
|
userJwt: jwt,
|
|
@@ -290,15 +289,55 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
290
289
|
const isSessionActive = (_d = ((wallet === null || wallet === void 0 ? void 0 : wallet.connector) &&
|
|
291
290
|
'getAuthenticatorHandler' in wallet.connector &&
|
|
292
291
|
((_c = getEmbeddedWalletAuthHandler.getEmbeddedWalletAuthHandler(wallet.connector)) === null || _c === void 0 ? void 0 : _c.isSessionActive()))) !== null && _d !== void 0 ? _d : false;
|
|
292
|
+
/**
|
|
293
|
+
* export the recovery phrase or private key of the embedded wallet
|
|
294
|
+
* @param options - optional parameter object containing the type of key to reveal 'recoveryPhrase' | 'privateKey'
|
|
295
|
+
* and the html container id to render the iframe into
|
|
296
|
+
* @returns Promise<string> - returns the embedded wallet recovery phrase or private key
|
|
297
|
+
*/
|
|
298
|
+
const revealEmbeddedWalletKey = (_s) => _tslib.__awaiter(void 0, [_s], void 0, function* ({ type, htmlContainerId, }) {
|
|
299
|
+
var _t, _u;
|
|
300
|
+
if (!user) {
|
|
301
|
+
throw new utils.DynamicError('User is not logged in', errors.USER_NOT_LOGGED_IN);
|
|
302
|
+
}
|
|
303
|
+
yield turnkeyExport.cleanupExport({
|
|
304
|
+
wallet,
|
|
305
|
+
});
|
|
306
|
+
const turnkeyWallet = findTurnkeyWallet.findTurnkeyWalletByPrimaryChain(walletConnectorOptions, (_t = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _t === void 0 ? void 0 : _t.chainConfigurations, user === null || user === void 0 ? void 0 : user.verifiedCredentials);
|
|
307
|
+
const turnkeyHDWalletId = (_u = turnkeyWallet === null || turnkeyWallet === void 0 ? void 0 : turnkeyWallet.walletProperties) === null || _u === void 0 ? void 0 : _u.turnkeyHDWalletId;
|
|
308
|
+
if (type === 'recoveryPhrase' && !turnkeyHDWalletId) {
|
|
309
|
+
type = 'privateKey';
|
|
310
|
+
}
|
|
311
|
+
const iframeContainerElement = createRootElement(constants.revealIframeElementId, htmlContainerId || constants.revealIframeContainerId);
|
|
312
|
+
// hide the iframe container until the export is done
|
|
313
|
+
// since it will show unrelated turnkey content
|
|
314
|
+
iframeContainerElement.current.style.display = 'none';
|
|
315
|
+
yield turnkeyExport.initExport({
|
|
316
|
+
iframeContainer: iframeContainerElement.current,
|
|
317
|
+
iframeElementId: constants.revealIframeElementId,
|
|
318
|
+
wallet,
|
|
319
|
+
});
|
|
320
|
+
yield turnkeyExport.exportCredential({
|
|
321
|
+
address: type === 'privateKey' ? wallet === null || wallet === void 0 ? void 0 : wallet.address : undefined,
|
|
322
|
+
authToken,
|
|
323
|
+
environmentId,
|
|
324
|
+
userEmail: user === null || user === void 0 ? void 0 : user.email,
|
|
325
|
+
wallet,
|
|
326
|
+
});
|
|
327
|
+
// show the iframe container after the export is done
|
|
328
|
+
// to show the exported content only
|
|
329
|
+
iframeContainerElement.current.style.display = 'block';
|
|
330
|
+
return true;
|
|
331
|
+
});
|
|
293
332
|
/**
|
|
294
333
|
* Sends a one-time code to the user so they can create a session or add a new passkey
|
|
295
334
|
* @returns Promise<'code_sent'>
|
|
296
335
|
*/
|
|
297
336
|
const sendOneTimeCode = React.useCallback(() => _tslib.__awaiter(void 0, void 0, void 0, function* () {
|
|
298
|
-
var
|
|
299
|
-
const { jwt, decodedJwt } = validateUserIsLoggedIn.validateUserIsLoggedIn((
|
|
337
|
+
var _v, _w, _x;
|
|
338
|
+
const { jwt, decodedJwt } = validateUserIsLoggedIn.validateUserIsLoggedIn((_v = getAuthToken.getAuthToken()) !== null && _v !== void 0 ? _v : onboardingOnlyJwt);
|
|
300
339
|
validateTurnkeyProviderEnabled.validateTurnkeyProviderEnabled(projectSettings);
|
|
301
|
-
const targetTurnkeyWallet = findTurnkeyWallet.findTurnkeyWalletByPrimaryChain(walletConnectorOptions, (
|
|
340
|
+
const targetTurnkeyWallet = findTurnkeyWallet.findTurnkeyWalletByPrimaryChain(walletConnectorOptions, (_w = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _w === void 0 ? void 0 : _w.chainConfigurations, decodedJwt.verifiedCredentials);
|
|
302
341
|
if (!targetTurnkeyWallet || !('id' in targetTurnkeyWallet)) {
|
|
303
342
|
throw new utils.DynamicError('User does not have a valid secure enclave wallet', errors.INVALID_WALLET_DATA);
|
|
304
343
|
}
|
|
@@ -309,7 +348,7 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
309
348
|
throw new utils.DynamicError('Session is active. No need to create a new one', errors.ACTIVE_SESSION);
|
|
310
349
|
}
|
|
311
350
|
const turnkeyAuthIframeContainerRef = createRootElement(constants.iframeContainerId);
|
|
312
|
-
const publicKey = yield turnkeyAuthenticatorHandler.initRecovery('email', turnkeyAuthIframeContainerRef.current, constants.iframeElementId, getEmbeddedWalletSessionExpiration.getEmbeddedWalletSessionExpiration((
|
|
351
|
+
const publicKey = yield turnkeyAuthenticatorHandler.initRecovery('email', turnkeyAuthIframeContainerRef.current, constants.iframeElementId, getEmbeddedWalletSessionExpiration.getEmbeddedWalletSessionExpiration((_x = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _x === void 0 ? void 0 : _x.sessionKeyDuration));
|
|
313
352
|
if (!publicKey) {
|
|
314
353
|
throw new utils.DynamicError(errors.ERROR_SENDING_CODE);
|
|
315
354
|
}
|
|
@@ -336,6 +375,7 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
336
375
|
getPasskeys,
|
|
337
376
|
isLoadingEmbeddedWallet,
|
|
338
377
|
isSessionActive,
|
|
378
|
+
revealEmbeddedWalletKey,
|
|
339
379
|
sendOneTimeCode,
|
|
340
380
|
userHasEmbeddedWallet,
|
|
341
381
|
};
|
|
@@ -10,6 +10,7 @@ import { Wallet } from '../../../../shared';
|
|
|
10
10
|
* getPasskeys, - gets all passkeys for the user
|
|
11
11
|
* isLoadingEmbeddedWallet, - loading state of the embedded wallet
|
|
12
12
|
* isSessionActive, - checks if the embedded wallet session is active
|
|
13
|
+
* revealEmbeddedWalletKey - export the recovery phrase or private key of the embedded wallet
|
|
13
14
|
* sendOneTimeCode, - sends a one-time code to the user so they can create a session or add a new passkey
|
|
14
15
|
* userHasEmbeddedWallet, - checks if the user has an embedded wallet
|
|
15
16
|
* }
|
|
@@ -18,15 +19,19 @@ export declare const useSecureEnclaveEmbeddedWallet: () => {
|
|
|
18
19
|
readonly createEmbeddedWallet: (chains?: ChainEnum[], options?: {
|
|
19
20
|
webAuthnAttestation: WebAuthnAttestation;
|
|
20
21
|
}) => Promise<Wallet | undefined>;
|
|
21
|
-
readonly createOrRestoreSession: (
|
|
22
|
-
oneTimeCode
|
|
23
|
-
}) => Promise<string>;
|
|
24
|
-
readonly createPasskey: (
|
|
25
|
-
oneTimeCode
|
|
26
|
-
}) => Promise<WebAuthnAttestation>;
|
|
22
|
+
readonly createOrRestoreSession: (options: {
|
|
23
|
+
oneTimeCode: string;
|
|
24
|
+
} | undefined) => Promise<string>;
|
|
25
|
+
readonly createPasskey: (options: {
|
|
26
|
+
oneTimeCode: string;
|
|
27
|
+
} | undefined) => Promise<WebAuthnAttestation>;
|
|
27
28
|
readonly getPasskeys: () => Promise<import("@dynamic-labs/sdk-api-core").UserPasskey[]>;
|
|
28
29
|
readonly isLoadingEmbeddedWallet: boolean;
|
|
29
30
|
readonly isSessionActive: boolean;
|
|
31
|
+
readonly revealEmbeddedWalletKey: ({ type, htmlContainerId, }: {
|
|
32
|
+
type: 'recoveryPhrase' | 'privateKey';
|
|
33
|
+
htmlContainerId?: string;
|
|
34
|
+
}) => Promise<boolean>;
|
|
30
35
|
readonly sendOneTimeCode: () => Promise<string>;
|
|
31
36
|
readonly userHasEmbeddedWallet: () => boolean;
|
|
32
37
|
};
|
|
@@ -43,13 +43,14 @@ import '../../../../context/VerificationContext/VerificationContext.js';
|
|
|
43
43
|
import 'react-dom';
|
|
44
44
|
import { useElementById } from '../../useElementById/useElementById.js';
|
|
45
45
|
import { useWalletContext } from '../../../../context/WalletContext/WalletContext.js';
|
|
46
|
+
import { useIsTurnkeyWallet } from '../../useIsTurnkeyWallet/useIsTurnkeyWallet.js';
|
|
46
47
|
import { validateUserIsLoggedIn } from '../../../validations/validateUserIsLoggedIn/validateUserIsLoggedIn.js';
|
|
47
48
|
import { validateTurnkeyProviderEnabled } from '../../../validations/validateTurnkeyProviderEnabled/validateTurnkeyProviderEnabled.js';
|
|
48
|
-
import { ACTIVE_SESSION, SESSION_RESTORED, INVALID_REQUEST, INVALID_WALLET_DATA, SESSION_CREATED, ERROR_SENDING_CODE, CODE_SENT } from '../../../constants/errors.js';
|
|
49
|
-
import { useIsTurnkeyWallet } from '../../useIsTurnkeyWallet/useIsTurnkeyWallet.js';
|
|
49
|
+
import { ACTIVE_SESSION, SESSION_RESTORED, INVALID_REQUEST, INVALID_WALLET_DATA, SESSION_CREATED, USER_NOT_LOGGED_IN, ERROR_SENDING_CODE, CODE_SENT } from '../../../constants/errors.js';
|
|
50
50
|
import { canRestoreEmbeddedWalletSession, restoreEmbeddedWalletSession } from '../../../../views/Passkey/utils/passkeyRecovery/passkeyRecovery.js';
|
|
51
|
+
import { cleanupExport, initExport, exportCredential } from '../../../../views/EmbeddedReveal/utils/turnkeyExport/turnkeyExport.js';
|
|
51
52
|
import { useTurnkey } from './useTurnkey/useTurnkey.js';
|
|
52
|
-
import { iframeContainerId, iframeElementId } from './constants.js';
|
|
53
|
+
import { iframeContainerId, iframeElementId, revealIframeElementId, revealIframeContainerId } from './constants.js';
|
|
53
54
|
import '../../../../context/ThemeContext/ThemeContext.js';
|
|
54
55
|
import '@dynamic-labs/types';
|
|
55
56
|
import 'yup';
|
|
@@ -101,20 +102,21 @@ import { useInternalDynamicContext } from '../../../../context/DynamicContext/us
|
|
|
101
102
|
* getPasskeys, - gets all passkeys for the user
|
|
102
103
|
* isLoadingEmbeddedWallet, - loading state of the embedded wallet
|
|
103
104
|
* isSessionActive, - checks if the embedded wallet session is active
|
|
105
|
+
* revealEmbeddedWalletKey - export the recovery phrase or private key of the embedded wallet
|
|
104
106
|
* sendOneTimeCode, - sends a one-time code to the user so they can create a session or add a new passkey
|
|
105
107
|
* userHasEmbeddedWallet, - checks if the user has an embedded wallet
|
|
106
108
|
* }
|
|
107
109
|
*/
|
|
108
110
|
const useSecureEnclaveEmbeddedWallet = () => {
|
|
109
111
|
var _a, _b, _c, _d;
|
|
110
|
-
const { projectSettings, onboardingOnlyJwt, primaryWallet, user, environmentId, walletConnectorOptions, setUser, } = useInternalDynamicContext();
|
|
112
|
+
const { projectSettings, authToken, onboardingOnlyJwt, primaryWallet, user, environmentId, walletConnectorOptions, setUser, } = useInternalDynamicContext();
|
|
111
113
|
const { createTurnkeyWallet } = useTurnkey();
|
|
112
114
|
const { hasRecoveryEmail } = useIsTurnkeyWallet();
|
|
113
115
|
const { createRootElement } = useElementById();
|
|
114
116
|
const { isLoadingEmbeddedWallet, setIsLoadingEmbeddedWallet } = useWalletContext();
|
|
115
117
|
const wallet = getEOAWallet(primaryWallet, (user === null || user === void 0 ? void 0 : user.verifiedCredentials) || []);
|
|
116
118
|
/**
|
|
117
|
-
* Checks if the user has
|
|
119
|
+
* Checks if the user has a secure enclave embedded wallet or not
|
|
118
120
|
* @returns boolean
|
|
119
121
|
*/
|
|
120
122
|
const userHasEmbeddedWallet = useCallback(() => hasEmbeddedWallet(getAuthToken() || onboardingOnlyJwt, 'turnkey'), [onboardingOnlyJwt]);
|
|
@@ -155,9 +157,9 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
155
157
|
* @param oneTimeCode - optional one-time code parameter. If not informed it tries to restore a session
|
|
156
158
|
* @returns Promise<'session_created' | 'session_restored'> - returns a string indicating the status of the session
|
|
157
159
|
*/
|
|
158
|
-
const createOrRestoreSession = useCallback((
|
|
159
|
-
var _h, _j, _k, _l, _m, _o
|
|
160
|
-
const jwt = (
|
|
160
|
+
const createOrRestoreSession = useCallback((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
161
|
+
var _g, _h, _j, _k, _l, _m, _o;
|
|
162
|
+
const jwt = (_g = getAuthToken()) !== null && _g !== void 0 ? _g : onboardingOnlyJwt;
|
|
161
163
|
const { decodedJwt } = validateUserIsLoggedIn(jwt);
|
|
162
164
|
const turnkeyRecoveryHandler = getEmbeddedWalletAuthHandler(wallet === null || wallet === void 0 ? void 0 : wallet.connector);
|
|
163
165
|
if (turnkeyRecoveryHandler.isSessionActive()) {
|
|
@@ -165,13 +167,13 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
165
167
|
}
|
|
166
168
|
const turnkeyAuthIframeContainerRef = createRootElement(iframeContainerId);
|
|
167
169
|
// try to restore session if possible
|
|
168
|
-
if (canRestoreEmbeddedWalletSession((
|
|
170
|
+
if (canRestoreEmbeddedWalletSession((_h = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _h === void 0 ? void 0 : _h.sessionKeyDuration)) {
|
|
169
171
|
try {
|
|
170
172
|
const sessionRestored = yield restoreEmbeddedWalletSession({
|
|
171
173
|
authToken: jwt,
|
|
172
174
|
iframeContainer: turnkeyAuthIframeContainerRef.current,
|
|
173
175
|
iframeElementId,
|
|
174
|
-
sessionExpiration: getEmbeddedWalletSessionExpiration((
|
|
176
|
+
sessionExpiration: getEmbeddedWalletSessionExpiration((_k = (_j = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk) === null || _j === void 0 ? void 0 : _j.embeddedWallets) === null || _k === void 0 ? void 0 : _k.sessionKeyDuration),
|
|
175
177
|
wallet: primaryWallet,
|
|
176
178
|
});
|
|
177
179
|
if (sessionRestored)
|
|
@@ -181,7 +183,7 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
181
183
|
logger.error('Failed to restore embedded wallet', err);
|
|
182
184
|
}
|
|
183
185
|
}
|
|
184
|
-
if (!oneTimeCode) {
|
|
186
|
+
if (!(options === null || options === void 0 ? void 0 : options.oneTimeCode)) {
|
|
185
187
|
throw new DynamicError('One-time code is required to create a session.', INVALID_REQUEST);
|
|
186
188
|
}
|
|
187
189
|
// tries to create a new session with the informed one-time code
|
|
@@ -189,11 +191,11 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
189
191
|
if (!userHasEmbeddedWallet() || !hasRecoveryEmail) {
|
|
190
192
|
throw new DynamicError('User does not have a secure enclave wallet or a verified email', INVALID_WALLET_DATA);
|
|
191
193
|
}
|
|
192
|
-
const organizationId = (
|
|
193
|
-
yield turnkeyRecoveryHandler.verifyRecoveryCode(oneTimeCode, organizationId);
|
|
194
|
+
const organizationId = (_o = (_m = (_l = decodedJwt.verifiedCredentials) === null || _l === void 0 ? void 0 : _l.find(({ walletName }) => walletName === null || walletName === void 0 ? void 0 : walletName.startsWith('turnkey'))) === null || _m === void 0 ? void 0 : _m.walletProperties) === null || _o === void 0 ? void 0 : _o.turnkeySubOrganizationId;
|
|
195
|
+
yield turnkeyRecoveryHandler.verifyRecoveryCode(options.oneTimeCode, organizationId);
|
|
194
196
|
const sessionSettings = {
|
|
195
197
|
createdAt: new Date().getTime(),
|
|
196
|
-
emailCode: oneTimeCode,
|
|
198
|
+
emailCode: options.oneTimeCode,
|
|
197
199
|
userId: turnkeyRecoveryHandler.recoveryUserId,
|
|
198
200
|
};
|
|
199
201
|
LocalStorage.setToLS(EMBEDDED_WALLET_SESSION_SETTINGS, sessionSettings);
|
|
@@ -209,15 +211,14 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
209
211
|
]);
|
|
210
212
|
/**
|
|
211
213
|
* Creates a new passkey both for an existing wallet or a wallet to be created
|
|
212
|
-
* @param
|
|
213
|
-
*
|
|
214
|
+
* @param options - optional parameter to inform a one-time code to create a session,
|
|
214
215
|
* @returns Promise<WebAuthnAttestation>
|
|
215
216
|
*/
|
|
216
|
-
const createPasskey = useCallback((
|
|
217
|
-
var
|
|
217
|
+
const createPasskey = useCallback((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
218
|
+
var _p, _q;
|
|
218
219
|
const { decodedJwt, jwt } = validateUserIsLoggedIn(getAuthToken() || onboardingOnlyJwt);
|
|
219
220
|
validateTurnkeyProviderEnabled(projectSettings);
|
|
220
|
-
const turnkeyWallet = findTurnkeyWalletByPrimaryChain(walletConnectorOptions, (
|
|
221
|
+
const turnkeyWallet = findTurnkeyWalletByPrimaryChain(walletConnectorOptions, (_p = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _p === void 0 ? void 0 : _p.chainConfigurations, decodedJwt.verifiedCredentials);
|
|
221
222
|
if (!(turnkeyWallet === null || turnkeyWallet === void 0 ? void 0 : turnkeyWallet.walletConnector)) {
|
|
222
223
|
throw new DynamicError('Wallet connector not found', INVALID_WALLET_DATA);
|
|
223
224
|
}
|
|
@@ -232,13 +233,13 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
232
233
|
if (!('id' in turnkeyWallet) || !hasEmailVC) {
|
|
233
234
|
return walletConnector.getWebAuthnAttestation();
|
|
234
235
|
}
|
|
235
|
-
const organizationId = (
|
|
236
|
+
const organizationId = (_q = turnkeyWallet === null || turnkeyWallet === void 0 ? void 0 : turnkeyWallet.walletProperties) === null || _q === void 0 ? void 0 : _q.turnkeySubOrganizationId;
|
|
236
237
|
if (!organizationId) {
|
|
237
238
|
throw new DynamicError('No sub organization id found for the wallet', INVALID_WALLET_DATA);
|
|
238
239
|
}
|
|
239
240
|
const turnkAuthenticatorHandler = getEmbeddedWalletAuthHandler(walletConnector);
|
|
240
241
|
// add new passkey to the wallet through an active session
|
|
241
|
-
yield createOrRestoreSession(
|
|
242
|
+
yield createOrRestoreSession(options);
|
|
242
243
|
const { attestation, challenge, displayName } = yield walletConnector.getWebAuthnAttestation();
|
|
243
244
|
yield turnkAuthenticatorHandler.addPasskeyAuthenticator({
|
|
244
245
|
attestation,
|
|
@@ -261,9 +262,7 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
261
262
|
onboardingOnlyJwt,
|
|
262
263
|
projectSettings,
|
|
263
264
|
setUser,
|
|
264
|
-
user
|
|
265
|
-
user === null || user === void 0 ? void 0 : user.phoneNumber,
|
|
266
|
-
user === null || user === void 0 ? void 0 : user.verifiedCredentials,
|
|
265
|
+
user,
|
|
267
266
|
walletConnectorOptions,
|
|
268
267
|
]);
|
|
269
268
|
/**
|
|
@@ -271,8 +270,8 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
271
270
|
* @returns Promise<Passkey[]>
|
|
272
271
|
*/
|
|
273
272
|
const getPasskeys = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
274
|
-
var
|
|
275
|
-
const { jwt } = validateUserIsLoggedIn((
|
|
273
|
+
var _r;
|
|
274
|
+
const { jwt } = validateUserIsLoggedIn((_r = getAuthToken()) !== null && _r !== void 0 ? _r : onboardingOnlyJwt);
|
|
276
275
|
const data = yield getUserPasskeys({
|
|
277
276
|
environmentId: environmentId,
|
|
278
277
|
userJwt: jwt,
|
|
@@ -286,15 +285,55 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
286
285
|
const isSessionActive = (_d = ((wallet === null || wallet === void 0 ? void 0 : wallet.connector) &&
|
|
287
286
|
'getAuthenticatorHandler' in wallet.connector &&
|
|
288
287
|
((_c = getEmbeddedWalletAuthHandler(wallet.connector)) === null || _c === void 0 ? void 0 : _c.isSessionActive()))) !== null && _d !== void 0 ? _d : false;
|
|
288
|
+
/**
|
|
289
|
+
* export the recovery phrase or private key of the embedded wallet
|
|
290
|
+
* @param options - optional parameter object containing the type of key to reveal 'recoveryPhrase' | 'privateKey'
|
|
291
|
+
* and the html container id to render the iframe into
|
|
292
|
+
* @returns Promise<string> - returns the embedded wallet recovery phrase or private key
|
|
293
|
+
*/
|
|
294
|
+
const revealEmbeddedWalletKey = (_s) => __awaiter(void 0, [_s], void 0, function* ({ type, htmlContainerId, }) {
|
|
295
|
+
var _t, _u;
|
|
296
|
+
if (!user) {
|
|
297
|
+
throw new DynamicError('User is not logged in', USER_NOT_LOGGED_IN);
|
|
298
|
+
}
|
|
299
|
+
yield cleanupExport({
|
|
300
|
+
wallet,
|
|
301
|
+
});
|
|
302
|
+
const turnkeyWallet = findTurnkeyWalletByPrimaryChain(walletConnectorOptions, (_t = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _t === void 0 ? void 0 : _t.chainConfigurations, user === null || user === void 0 ? void 0 : user.verifiedCredentials);
|
|
303
|
+
const turnkeyHDWalletId = (_u = turnkeyWallet === null || turnkeyWallet === void 0 ? void 0 : turnkeyWallet.walletProperties) === null || _u === void 0 ? void 0 : _u.turnkeyHDWalletId;
|
|
304
|
+
if (type === 'recoveryPhrase' && !turnkeyHDWalletId) {
|
|
305
|
+
type = 'privateKey';
|
|
306
|
+
}
|
|
307
|
+
const iframeContainerElement = createRootElement(revealIframeElementId, htmlContainerId || revealIframeContainerId);
|
|
308
|
+
// hide the iframe container until the export is done
|
|
309
|
+
// since it will show unrelated turnkey content
|
|
310
|
+
iframeContainerElement.current.style.display = 'none';
|
|
311
|
+
yield initExport({
|
|
312
|
+
iframeContainer: iframeContainerElement.current,
|
|
313
|
+
iframeElementId: revealIframeElementId,
|
|
314
|
+
wallet,
|
|
315
|
+
});
|
|
316
|
+
yield exportCredential({
|
|
317
|
+
address: type === 'privateKey' ? wallet === null || wallet === void 0 ? void 0 : wallet.address : undefined,
|
|
318
|
+
authToken,
|
|
319
|
+
environmentId,
|
|
320
|
+
userEmail: user === null || user === void 0 ? void 0 : user.email,
|
|
321
|
+
wallet,
|
|
322
|
+
});
|
|
323
|
+
// show the iframe container after the export is done
|
|
324
|
+
// to show the exported content only
|
|
325
|
+
iframeContainerElement.current.style.display = 'block';
|
|
326
|
+
return true;
|
|
327
|
+
});
|
|
289
328
|
/**
|
|
290
329
|
* Sends a one-time code to the user so they can create a session or add a new passkey
|
|
291
330
|
* @returns Promise<'code_sent'>
|
|
292
331
|
*/
|
|
293
332
|
const sendOneTimeCode = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
294
|
-
var
|
|
295
|
-
const { jwt, decodedJwt } = validateUserIsLoggedIn((
|
|
333
|
+
var _v, _w, _x;
|
|
334
|
+
const { jwt, decodedJwt } = validateUserIsLoggedIn((_v = getAuthToken()) !== null && _v !== void 0 ? _v : onboardingOnlyJwt);
|
|
296
335
|
validateTurnkeyProviderEnabled(projectSettings);
|
|
297
|
-
const targetTurnkeyWallet = findTurnkeyWalletByPrimaryChain(walletConnectorOptions, (
|
|
336
|
+
const targetTurnkeyWallet = findTurnkeyWalletByPrimaryChain(walletConnectorOptions, (_w = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _w === void 0 ? void 0 : _w.chainConfigurations, decodedJwt.verifiedCredentials);
|
|
298
337
|
if (!targetTurnkeyWallet || !('id' in targetTurnkeyWallet)) {
|
|
299
338
|
throw new DynamicError('User does not have a valid secure enclave wallet', INVALID_WALLET_DATA);
|
|
300
339
|
}
|
|
@@ -305,7 +344,7 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
305
344
|
throw new DynamicError('Session is active. No need to create a new one', ACTIVE_SESSION);
|
|
306
345
|
}
|
|
307
346
|
const turnkeyAuthIframeContainerRef = createRootElement(iframeContainerId);
|
|
308
|
-
const publicKey = yield turnkeyAuthenticatorHandler.initRecovery('email', turnkeyAuthIframeContainerRef.current, iframeElementId, getEmbeddedWalletSessionExpiration((
|
|
347
|
+
const publicKey = yield turnkeyAuthenticatorHandler.initRecovery('email', turnkeyAuthIframeContainerRef.current, iframeElementId, getEmbeddedWalletSessionExpiration((_x = projectSettings === null || projectSettings === void 0 ? void 0 : projectSettings.sdk.embeddedWallets) === null || _x === void 0 ? void 0 : _x.sessionKeyDuration));
|
|
309
348
|
if (!publicKey) {
|
|
310
349
|
throw new DynamicError(ERROR_SENDING_CODE);
|
|
311
350
|
}
|
|
@@ -332,6 +371,7 @@ const useSecureEnclaveEmbeddedWallet = () => {
|
|
|
332
371
|
getPasskeys,
|
|
333
372
|
isLoadingEmbeddedWallet,
|
|
334
373
|
isSessionActive,
|
|
374
|
+
revealEmbeddedWalletKey,
|
|
335
375
|
sendOneTimeCode,
|
|
336
376
|
userHasEmbeddedWallet,
|
|
337
377
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useTokenBalances } from './useTokenBalances';
|