@dexterai/connect 0.29.0 → 0.31.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/README.md +134 -0
- package/dist/{chunk-XKOVUW5P.js → chunk-4FTJB7FT.js} +591 -636
- package/dist/chunk-WHGR5FUY.js +316 -0
- package/dist/chunk-Z6YHGWG3.js +618 -0
- package/dist/connectionContract-BzV_pjOI.d.ts +239 -0
- package/dist/enroll-BmD1y8KH.d.ts +276 -0
- package/dist/hosted.d.ts +79 -0
- package/dist/hosted.js +101 -0
- package/dist/index.d.ts +15 -3
- package/dist/index.js +20 -182
- package/dist/react.d.ts +76 -4
- package/dist/react.js +596 -50
- package/package.json +7 -2
- package/dist/connectionContract-QGRidIHM.d.ts +0 -470
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConnectError,
|
|
3
|
+
activateRecoveredVault,
|
|
4
|
+
classifyWebAuthnRejection,
|
|
5
|
+
clearPendingCreation,
|
|
6
|
+
immediateAuthentication,
|
|
7
|
+
immediateGetSupported,
|
|
8
|
+
normalizeMissingVaultRecoveryOptions,
|
|
9
|
+
openCeremonyPopup,
|
|
10
|
+
parseMissingVaultRecoveryChallenge,
|
|
11
|
+
parseMissingVaultRecoveryResult,
|
|
12
|
+
parseWalletIdentityProof,
|
|
13
|
+
primeImmediateSupport,
|
|
14
|
+
readErrorCode,
|
|
15
|
+
resolveDexterApiBase,
|
|
16
|
+
resolveDexterConnectHost,
|
|
17
|
+
resolveDexterRpId,
|
|
18
|
+
resolveWalletStoreMode,
|
|
19
|
+
shouldUsePopup,
|
|
20
|
+
validateMissingVaultRecoveryAssertion
|
|
21
|
+
} from "./chunk-4FTJB7FT.js";
|
|
22
|
+
|
|
23
|
+
// src/missingVaultRecovery.ts
|
|
24
|
+
import {
|
|
25
|
+
startAuthentication
|
|
26
|
+
} from "@simplewebauthn/browser";
|
|
27
|
+
var RECOVERY_PATH = "/api/passkey-vault/recover-missing";
|
|
28
|
+
var INDEXTER_RECOVERY_ORIGIN = "https://indexter.cash";
|
|
29
|
+
async function recoverMissingVaultForAccount(config) {
|
|
30
|
+
requireIndexterRecoveryOpener();
|
|
31
|
+
const apiBase = resolveDexterApiBase(config.apiBase);
|
|
32
|
+
const connectHost = resolveDexterConnectHost(config.connectHost);
|
|
33
|
+
const accountAccessToken = requireAccountAccessToken(config.accountAccessToken);
|
|
34
|
+
let expectedRequest;
|
|
35
|
+
const popupResult = await openCeremonyPopup(
|
|
36
|
+
"recover-missing-vault",
|
|
37
|
+
{
|
|
38
|
+
connectHost,
|
|
39
|
+
missingVaultRecoveryRequest: async () => {
|
|
40
|
+
expectedRequest = await fetchRecoveryChallenge(apiBase, accountAccessToken);
|
|
41
|
+
return expectedRequest;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
|
+
if (!expectedRequest) {
|
|
46
|
+
throw new ConnectError("missing_vault_recovery_challenge_missing");
|
|
47
|
+
}
|
|
48
|
+
const credential = await validateMissingVaultRecoveryAssertion(
|
|
49
|
+
popupResult,
|
|
50
|
+
expectedRequest.options.challenge
|
|
51
|
+
);
|
|
52
|
+
return completeRecovery(
|
|
53
|
+
apiBase,
|
|
54
|
+
accountAccessToken,
|
|
55
|
+
credential,
|
|
56
|
+
expectedRequest.account
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
function requireIndexterRecoveryOpener() {
|
|
60
|
+
if (typeof window === "undefined") {
|
|
61
|
+
throw new ConnectError(
|
|
62
|
+
"not_browser",
|
|
63
|
+
"missing Vault recovery requires the Indexter browser session"
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
if (window.location.origin !== INDEXTER_RECOVERY_ORIGIN) {
|
|
67
|
+
throw new ConnectError(
|
|
68
|
+
"untrusted_missing_vault_recovery_opener",
|
|
69
|
+
"missing Vault recovery is restricted to Indexter"
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function runHostedMissingVaultRecoveryProof(options) {
|
|
74
|
+
const normalized = normalizeMissingVaultRecoveryOptions(options);
|
|
75
|
+
let credential;
|
|
76
|
+
try {
|
|
77
|
+
credential = await startAuthentication({ optionsJSON: normalized });
|
|
78
|
+
} catch (err) {
|
|
79
|
+
throw new ConnectError(
|
|
80
|
+
"webauthn_failed",
|
|
81
|
+
err instanceof Error ? err.message : String(err)
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
return validateMissingVaultRecoveryAssertion(credential, normalized.challenge);
|
|
85
|
+
}
|
|
86
|
+
async function fetchRecoveryChallenge(apiBase, accountAccessToken) {
|
|
87
|
+
const res = await fetch(`${apiBase}${RECOVERY_PATH}/challenge`, {
|
|
88
|
+
method: "POST",
|
|
89
|
+
headers: {
|
|
90
|
+
authorization: `Bearer ${accountAccessToken}`,
|
|
91
|
+
"content-type": "application/json"
|
|
92
|
+
},
|
|
93
|
+
body: "{}"
|
|
94
|
+
});
|
|
95
|
+
if (!res.ok) {
|
|
96
|
+
throw new ConnectError(
|
|
97
|
+
await readErrorCode(res),
|
|
98
|
+
`missing Vault recovery challenge ${res.status}`
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return parseMissingVaultRecoveryChallenge(await res.json());
|
|
102
|
+
}
|
|
103
|
+
async function completeRecovery(apiBase, accountAccessToken, credential, expectedAccount) {
|
|
104
|
+
const res = await fetch(`${apiBase}${RECOVERY_PATH}/complete`, {
|
|
105
|
+
method: "POST",
|
|
106
|
+
headers: {
|
|
107
|
+
authorization: `Bearer ${accountAccessToken}`,
|
|
108
|
+
"content-type": "application/json"
|
|
109
|
+
},
|
|
110
|
+
body: JSON.stringify({ credential })
|
|
111
|
+
});
|
|
112
|
+
if (!res.ok) {
|
|
113
|
+
throw new ConnectError(
|
|
114
|
+
await readErrorCode(res),
|
|
115
|
+
`missing Vault recovery completion ${res.status}`
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
return parseMissingVaultRecoveryResult(
|
|
119
|
+
await res.json(),
|
|
120
|
+
credential,
|
|
121
|
+
expectedAccount
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
function requireAccountAccessToken(value) {
|
|
125
|
+
if (typeof value !== "string" || value.length < 16 || value.length > 16384 || /\s/.test(value)) {
|
|
126
|
+
throw new ConnectError("missing_account_session");
|
|
127
|
+
}
|
|
128
|
+
return value;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/recover.ts
|
|
132
|
+
import { startAuthentication as startAuthentication2, browserSupportsWebAuthn } from "@simplewebauthn/browser";
|
|
133
|
+
var ANON_SIGN_BASE = "/api/passkey-anon/sign";
|
|
134
|
+
primeImmediateSupport();
|
|
135
|
+
async function recoverWallet(config = {}) {
|
|
136
|
+
let apiBase;
|
|
137
|
+
let walletStore;
|
|
138
|
+
try {
|
|
139
|
+
apiBase = resolveDexterApiBase(config.apiBase);
|
|
140
|
+
} catch (err) {
|
|
141
|
+
return netError("untrusted_api_base", err);
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
walletStore = resolveWalletStoreMode(config.walletStore);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
return {
|
|
147
|
+
ok: false,
|
|
148
|
+
reason: "error",
|
|
149
|
+
error: err instanceof ConnectError ? err : new ConnectError("invalid_wallet_store_mode")
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
if (shouldUsePopup(config.transport)) {
|
|
153
|
+
let outcome;
|
|
154
|
+
try {
|
|
155
|
+
outcome = await openCeremonyPopup("recover", {
|
|
156
|
+
connectHost: config.connectHost,
|
|
157
|
+
preferImmediate: config.preferImmediate,
|
|
158
|
+
...walletStore === "provisional" ? { walletStore } : {}
|
|
159
|
+
});
|
|
160
|
+
} catch (err) {
|
|
161
|
+
const ce = err instanceof ConnectError ? err : new ConnectError("popup_failed", err instanceof Error ? err.message : String(err));
|
|
162
|
+
if (ce.code === "popup_closed") return { ok: false, reason: "cancelled" };
|
|
163
|
+
return { ok: false, reason: "error", error: ce };
|
|
164
|
+
}
|
|
165
|
+
if (outcome.ok) {
|
|
166
|
+
try {
|
|
167
|
+
outcome = {
|
|
168
|
+
...outcome,
|
|
169
|
+
walletIdentityProof: parseWalletIdentityProof(
|
|
170
|
+
outcome.walletIdentityProof
|
|
171
|
+
)
|
|
172
|
+
};
|
|
173
|
+
} catch (err) {
|
|
174
|
+
return netError("wallet_identity_proof_malformed", err);
|
|
175
|
+
}
|
|
176
|
+
if (walletStore === "commit") {
|
|
177
|
+
activateRecoveredVault(
|
|
178
|
+
outcome.userHandle,
|
|
179
|
+
outcome.credentialId,
|
|
180
|
+
outcome.vault,
|
|
181
|
+
outcome.walletIdentityProof
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return outcome;
|
|
186
|
+
}
|
|
187
|
+
if (typeof navigator === "undefined" || !browserSupportsWebAuthn()) {
|
|
188
|
+
return {
|
|
189
|
+
ok: false,
|
|
190
|
+
reason: "error",
|
|
191
|
+
error: new ConnectError("webauthn_unsupported", "WebAuthn unavailable in this environment")
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
const onPhase = config.onPhase;
|
|
195
|
+
onPhase?.("challenge");
|
|
196
|
+
let options;
|
|
197
|
+
try {
|
|
198
|
+
const res = await fetch(`${apiBase}${ANON_SIGN_BASE}/recover-challenge`, {
|
|
199
|
+
method: "POST",
|
|
200
|
+
headers: { "content-type": "application/json" },
|
|
201
|
+
body: "{}"
|
|
202
|
+
});
|
|
203
|
+
if (!res.ok) return { ok: false, reason: "error", error: new ConnectError(await readErrorCode(res)) };
|
|
204
|
+
const serverOptions = (await res.json()).options;
|
|
205
|
+
if (!serverOptions?.challenge) {
|
|
206
|
+
return {
|
|
207
|
+
ok: false,
|
|
208
|
+
reason: "error",
|
|
209
|
+
error: new ConnectError("recover_challenge_malformed", "no challenge in response")
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
options = { ...serverOptions, rpId: resolveDexterRpId(serverOptions.rpId) };
|
|
213
|
+
} catch (err) {
|
|
214
|
+
if (err instanceof ConnectError && err.code === "untrusted_rp_id") {
|
|
215
|
+
return { ok: false, reason: "error", error: err };
|
|
216
|
+
}
|
|
217
|
+
return netError("recover_challenge_failed", err);
|
|
218
|
+
}
|
|
219
|
+
onPhase?.("passkey");
|
|
220
|
+
const useImmediate = Boolean(config.preferImmediate) && await immediateGetSupported();
|
|
221
|
+
let credential;
|
|
222
|
+
try {
|
|
223
|
+
credential = useImmediate ? await immediateAuthentication(options) : await startAuthentication2({ optionsJSON: options });
|
|
224
|
+
} catch (err) {
|
|
225
|
+
if (classifyWebAuthnRejection(err)) {
|
|
226
|
+
return { ok: false, reason: useImmediate ? "no_credential" : "cancelled" };
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
ok: false,
|
|
230
|
+
reason: "error",
|
|
231
|
+
error: new ConnectError("webauthn_failed", err instanceof Error ? err.message : String(err))
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
onPhase?.("verifying");
|
|
235
|
+
let userHandle;
|
|
236
|
+
let credentialId;
|
|
237
|
+
let walletIdentityProof;
|
|
238
|
+
try {
|
|
239
|
+
const res = await fetch(`${apiBase}${ANON_SIGN_BASE}/recover-verify`, {
|
|
240
|
+
method: "POST",
|
|
241
|
+
headers: { "content-type": "application/json" },
|
|
242
|
+
body: JSON.stringify({ credential })
|
|
243
|
+
});
|
|
244
|
+
if (res.status === 404) return { ok: false, reason: "no_credential" };
|
|
245
|
+
if (!res.ok) return { ok: false, reason: "error", error: new ConnectError(await readErrorCode(res)) };
|
|
246
|
+
const data = await res.json();
|
|
247
|
+
if (data.verified !== true || data.credentialId !== credential.id || typeof data.userHandle !== "string" || !data.userHandle) {
|
|
248
|
+
throw new ConnectError("recover_verify_malformed");
|
|
249
|
+
}
|
|
250
|
+
userHandle = data.userHandle;
|
|
251
|
+
credentialId = data.credentialId;
|
|
252
|
+
walletIdentityProof = parseWalletIdentityProof(data.walletIdentityProof);
|
|
253
|
+
} catch (err) {
|
|
254
|
+
return netError("recover_verify_failed", err);
|
|
255
|
+
}
|
|
256
|
+
let vault;
|
|
257
|
+
try {
|
|
258
|
+
const res = await fetch(
|
|
259
|
+
`${apiBase}/api/passkey-vault-anon/status?user_handle=${encodeURIComponent(userHandle)}`
|
|
260
|
+
);
|
|
261
|
+
if (!res.ok) return { ok: false, reason: "error", error: new ConnectError(await readErrorCode(res)) };
|
|
262
|
+
const data = await res.json();
|
|
263
|
+
if (!data.hasVault || !data.vault) {
|
|
264
|
+
const initialized = await fetch(`${apiBase}/api/passkey-vault-anon/initialize`, {
|
|
265
|
+
method: "POST",
|
|
266
|
+
headers: {
|
|
267
|
+
"content-type": "application/json",
|
|
268
|
+
authorization: `Bearer ${walletIdentityProof.token}`
|
|
269
|
+
},
|
|
270
|
+
body: JSON.stringify({ userHandle, credentialId })
|
|
271
|
+
});
|
|
272
|
+
if (!initialized.ok) return { ok: false, reason: "error", error: new ConnectError(await readErrorCode(initialized)) };
|
|
273
|
+
const created = await initialized.json();
|
|
274
|
+
if (!created.vaultPda || !created.swigStateAddress) throw new ConnectError("initialize_malformed");
|
|
275
|
+
data.vault = {
|
|
276
|
+
vaultPda: created.vaultPda,
|
|
277
|
+
swigAddress: created.swigStateAddress,
|
|
278
|
+
receiveAddress: created.receiveAddress ?? null,
|
|
279
|
+
walletLabel: created.walletLabel ?? null,
|
|
280
|
+
isActivated: false
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
vault = {
|
|
284
|
+
vaultPda: data.vault.vaultPda,
|
|
285
|
+
swigAddress: data.vault.swigAddress,
|
|
286
|
+
receiveAddress: data.vault.receiveAddress ?? null,
|
|
287
|
+
isActivated: Boolean(data.vault.isActivated),
|
|
288
|
+
walletLabel: data.vault.walletLabel ?? null
|
|
289
|
+
};
|
|
290
|
+
} catch (err) {
|
|
291
|
+
return netError("vault_status_failed", err);
|
|
292
|
+
}
|
|
293
|
+
if (walletStore === "commit") {
|
|
294
|
+
activateRecoveredVault(
|
|
295
|
+
userHandle,
|
|
296
|
+
credentialId,
|
|
297
|
+
vault,
|
|
298
|
+
walletIdentityProof
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
clearPendingCreation(credentialId);
|
|
302
|
+
return { ok: true, userHandle, credentialId, vault, walletIdentityProof };
|
|
303
|
+
}
|
|
304
|
+
function netError(code, err) {
|
|
305
|
+
return {
|
|
306
|
+
ok: false,
|
|
307
|
+
reason: "error",
|
|
308
|
+
error: new ConnectError(code, err instanceof Error ? err.message : String(err))
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export {
|
|
313
|
+
recoverMissingVaultForAccount,
|
|
314
|
+
runHostedMissingVaultRecoveryProof,
|
|
315
|
+
recoverWallet
|
|
316
|
+
};
|