@invonetwork/web-sdk 3.5.0 → 3.6.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/CHANGELOG.md +113 -14
- package/README.md +263 -21
- package/dist/index.cjs +672 -19
- package/dist/index.d.cts +135 -3
- package/dist/index.d.ts +135 -3
- package/dist/index.js +672 -19
- package/dist/server.d.cts +2 -2
- package/dist/server.d.ts +2 -2
- package/dist/{types-Dxo3IsYq.d.cts → types-ZNoG09S5.d.cts} +1 -1
- package/dist/{types-Dxo3IsYq.d.ts → types-ZNoG09S5.d.ts} +1 -1
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -92,6 +92,599 @@ function assertionToJSON(cred) {
|
|
|
92
92
|
};
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
+
// src/shared/hosted-approval.ts
|
|
96
|
+
var HOSTED_FLOWS = [
|
|
97
|
+
"transfer",
|
|
98
|
+
"send",
|
|
99
|
+
"send_receipt",
|
|
100
|
+
"transfer_receipt"
|
|
101
|
+
];
|
|
102
|
+
var HOSTED_MESSAGE_TYPE = "invo.approval";
|
|
103
|
+
var MAX_EXPIRES_IN_S = 24 * 24 * 60 * 60;
|
|
104
|
+
var CONFIRM_QUIET_CODES = /* @__PURE__ */ new Set([
|
|
105
|
+
"DEVICE_APPROVAL_ENROLLMENT_ALREADY_DECIDED",
|
|
106
|
+
"DEVICE_APPROVAL_NO_ENROLLMENT_PENDING"
|
|
107
|
+
]);
|
|
108
|
+
var MAX_TRANSIENT_POLL_FAILURES = 5;
|
|
109
|
+
function derivePlatformOrigin(verificationUriComplete, explicit) {
|
|
110
|
+
let u;
|
|
111
|
+
try {
|
|
112
|
+
u = new URL(verificationUriComplete);
|
|
113
|
+
} catch {
|
|
114
|
+
throw new InvoError({
|
|
115
|
+
message: "begin returned no absolute verification_uri_complete.",
|
|
116
|
+
code: "UNEXPECTED_RESPONSE",
|
|
117
|
+
status: 0
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const isLocal = u.hostname === "localhost" || u.hostname === "127.0.0.1" || u.hostname === "[::1]";
|
|
121
|
+
if (u.protocol !== "https:" && !(u.protocol === "http:" && isLocal)) {
|
|
122
|
+
throw new InvoError({
|
|
123
|
+
message: "The hosted approval page must be https (plaintext would let any page forge the wake-up).",
|
|
124
|
+
code: "INVALID_INPUT",
|
|
125
|
+
status: 0
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (explicit !== void 0 && u.origin !== explicit) {
|
|
129
|
+
throw new InvoError({
|
|
130
|
+
message: `platformOrigin (${explicit}) does not match the hosted page's origin (${u.origin}).`,
|
|
131
|
+
code: "INVALID_INPUT",
|
|
132
|
+
status: 0
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return u.origin;
|
|
136
|
+
}
|
|
137
|
+
function normalizeEnrollment(input) {
|
|
138
|
+
if (!input || typeof input !== "object") return null;
|
|
139
|
+
const state = typeof input.state === "string" ? input.state : "";
|
|
140
|
+
if (!state) return null;
|
|
141
|
+
const label = input.device_label ?? input.deviceLabel;
|
|
142
|
+
const code = input.match_code ?? input.matchCode;
|
|
143
|
+
const at = input.requested_at ?? input.requestedAt;
|
|
144
|
+
return {
|
|
145
|
+
state,
|
|
146
|
+
deviceLabel: typeof label === "string" && label ? label : "your phone",
|
|
147
|
+
matchCode: typeof code === "string" ? code : "",
|
|
148
|
+
requestedAt: typeof at === "string" ? at : void 0,
|
|
149
|
+
raw: input
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
var BRAND = "#4f46e5";
|
|
153
|
+
var FONT = "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif";
|
|
154
|
+
var MONO = "ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace";
|
|
155
|
+
var PROMPT_IDS = {
|
|
156
|
+
root: "invo-hosted-enrollment",
|
|
157
|
+
title: "invo-hosted-enrollment-title",
|
|
158
|
+
code: "invo-hosted-enrollment-code",
|
|
159
|
+
yes: "invo-hosted-enrollment-yes",
|
|
160
|
+
no: "invo-hosted-enrollment-no",
|
|
161
|
+
stop: "invo-hosted-enrollment-stop",
|
|
162
|
+
error: "invo-hosted-enrollment-error"
|
|
163
|
+
};
|
|
164
|
+
var HostedEnrollmentPrompt = class {
|
|
165
|
+
constructor() {
|
|
166
|
+
this.handlers = { respond: async () => {
|
|
167
|
+
}, renderPrompt: true };
|
|
168
|
+
this.enrollment = null;
|
|
169
|
+
/** The last match code the player answered, so a stale poll can't re-ask it. */
|
|
170
|
+
this.decided = null;
|
|
171
|
+
this.root = null;
|
|
172
|
+
this.previousFocus = null;
|
|
173
|
+
this.keyHandler = null;
|
|
174
|
+
this.busy = false;
|
|
175
|
+
}
|
|
176
|
+
get current() {
|
|
177
|
+
return this.enrollment;
|
|
178
|
+
}
|
|
179
|
+
configure(handlers) {
|
|
180
|
+
this.handlers = handlers;
|
|
181
|
+
this.decided = null;
|
|
182
|
+
if (this.enrollment) this.render();
|
|
183
|
+
}
|
|
184
|
+
update(input) {
|
|
185
|
+
let next = normalizeEnrollment(input);
|
|
186
|
+
if (next && next.state === "awaiting_screen" && this.decided && this.decided.code === next.matchCode) {
|
|
187
|
+
next = this.decided.decision === "approve" ? { ...next, state: "confirmed" } : null;
|
|
188
|
+
}
|
|
189
|
+
const changed = this.enrollment === null !== (next === null) || this.enrollment && next && (this.enrollment.state !== next.state || this.enrollment.matchCode !== next.matchCode || this.enrollment.deviceLabel !== next.deviceLabel);
|
|
190
|
+
this.enrollment = next;
|
|
191
|
+
if (changed) this.handlers.onEnrollment?.(next, this.handlers.respond);
|
|
192
|
+
this.render();
|
|
193
|
+
}
|
|
194
|
+
clear() {
|
|
195
|
+
this.update(null);
|
|
196
|
+
}
|
|
197
|
+
// --- rendering ---
|
|
198
|
+
render() {
|
|
199
|
+
if (typeof document === "undefined") return;
|
|
200
|
+
const e = this.enrollment;
|
|
201
|
+
const visible = this.handlers.renderPrompt && e && (e.state === "awaiting_screen" || e.state === "confirmed");
|
|
202
|
+
if (!visible || !e) {
|
|
203
|
+
this.unmount();
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
this.mount();
|
|
207
|
+
const root = this.root;
|
|
208
|
+
root.innerHTML = "";
|
|
209
|
+
root.appendChild(e.state === "awaiting_screen" ? this.buildPrompt(e) : this.buildFinishing(e));
|
|
210
|
+
this.focusInitial(e.state);
|
|
211
|
+
}
|
|
212
|
+
mount() {
|
|
213
|
+
if (this.root) return;
|
|
214
|
+
this.previousFocus = document.activeElement;
|
|
215
|
+
const overlay = document.createElement("div");
|
|
216
|
+
overlay.id = PROMPT_IDS.root;
|
|
217
|
+
overlay.setAttribute("role", "dialog");
|
|
218
|
+
overlay.setAttribute("aria-modal", "true");
|
|
219
|
+
overlay.setAttribute("aria-labelledby", PROMPT_IDS.title);
|
|
220
|
+
overlay.tabIndex = -1;
|
|
221
|
+
overlay.style.cssText = [
|
|
222
|
+
"position:fixed",
|
|
223
|
+
"inset:0",
|
|
224
|
+
"z-index:2147483000",
|
|
225
|
+
"display:flex",
|
|
226
|
+
"align-items:center",
|
|
227
|
+
"justify-content:center",
|
|
228
|
+
"padding:16px",
|
|
229
|
+
"background:rgba(17,24,39,.55)",
|
|
230
|
+
`font-family:${FONT}`,
|
|
231
|
+
"color:#111827",
|
|
232
|
+
"-webkit-font-smoothing:antialiased",
|
|
233
|
+
"outline:none"
|
|
234
|
+
].join(";");
|
|
235
|
+
this.keyHandler = (ev) => this.onKeyDown(ev);
|
|
236
|
+
overlay.addEventListener("keydown", this.keyHandler);
|
|
237
|
+
document.body.appendChild(overlay);
|
|
238
|
+
this.root = overlay;
|
|
239
|
+
}
|
|
240
|
+
unmount() {
|
|
241
|
+
if (!this.root) return;
|
|
242
|
+
if (this.keyHandler) this.root.removeEventListener("keydown", this.keyHandler);
|
|
243
|
+
this.root.remove();
|
|
244
|
+
this.root = null;
|
|
245
|
+
this.keyHandler = null;
|
|
246
|
+
this.busy = false;
|
|
247
|
+
const prev = this.previousFocus;
|
|
248
|
+
this.previousFocus = null;
|
|
249
|
+
if (prev && typeof prev.focus === "function") {
|
|
250
|
+
try {
|
|
251
|
+
prev.focus();
|
|
252
|
+
} catch {
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
card() {
|
|
257
|
+
const card = document.createElement("div");
|
|
258
|
+
card.style.cssText = [
|
|
259
|
+
"background:#ffffff",
|
|
260
|
+
"border-radius:16px",
|
|
261
|
+
"box-shadow:0 20px 60px rgba(17,24,39,.25)",
|
|
262
|
+
"max-width:420px",
|
|
263
|
+
"width:100%",
|
|
264
|
+
"padding:28px 28px 24px",
|
|
265
|
+
"box-sizing:border-box",
|
|
266
|
+
"text-align:center"
|
|
267
|
+
].join(";");
|
|
268
|
+
const mark = document.createElement("div");
|
|
269
|
+
mark.textContent = "INVO";
|
|
270
|
+
mark.setAttribute("aria-hidden", "true");
|
|
271
|
+
mark.style.cssText = `font-weight:800;letter-spacing:.18em;font-size:13px;color:${BRAND};margin-bottom:14px`;
|
|
272
|
+
card.appendChild(mark);
|
|
273
|
+
return card;
|
|
274
|
+
}
|
|
275
|
+
button(id, label, primary) {
|
|
276
|
+
const b = document.createElement("button");
|
|
277
|
+
b.type = "button";
|
|
278
|
+
b.id = id;
|
|
279
|
+
b.textContent = label;
|
|
280
|
+
b.style.cssText = [
|
|
281
|
+
"display:block",
|
|
282
|
+
"width:100%",
|
|
283
|
+
"box-sizing:border-box",
|
|
284
|
+
"border-radius:10px",
|
|
285
|
+
"padding:12px 16px",
|
|
286
|
+
"font:inherit",
|
|
287
|
+
"font-size:15px",
|
|
288
|
+
"font-weight:600",
|
|
289
|
+
"cursor:pointer",
|
|
290
|
+
"margin-top:10px",
|
|
291
|
+
primary ? `background:${BRAND};color:#ffffff;border:1px solid ${BRAND}` : "background:#ffffff;color:#374151;border:1px solid #d1d5db"
|
|
292
|
+
].join(";");
|
|
293
|
+
return b;
|
|
294
|
+
}
|
|
295
|
+
buildPrompt(e) {
|
|
296
|
+
const card = this.card();
|
|
297
|
+
const title = document.createElement("h2");
|
|
298
|
+
title.id = PROMPT_IDS.title;
|
|
299
|
+
title.textContent = `Set up INVO on ${e.deviceLabel}?`;
|
|
300
|
+
title.style.cssText = "margin:0 0 14px;font-size:20px;font-weight:700;line-height:1.3";
|
|
301
|
+
card.appendChild(title);
|
|
302
|
+
const codeLabel = document.createElement("div");
|
|
303
|
+
codeLabel.textContent = "Code";
|
|
304
|
+
codeLabel.style.cssText = "font-size:12px;text-transform:uppercase;letter-spacing:.12em;color:#6b7280";
|
|
305
|
+
card.appendChild(codeLabel);
|
|
306
|
+
const code = document.createElement("div");
|
|
307
|
+
code.id = PROMPT_IDS.code;
|
|
308
|
+
code.textContent = e.matchCode;
|
|
309
|
+
code.setAttribute("aria-label", `Code ${e.matchCode.split("").join(" ")}`);
|
|
310
|
+
code.style.cssText = [
|
|
311
|
+
`font-family:${MONO}`,
|
|
312
|
+
"font-size:40px",
|
|
313
|
+
"font-weight:700",
|
|
314
|
+
"letter-spacing:.3em",
|
|
315
|
+
"text-indent:.3em",
|
|
316
|
+
"line-height:1.2",
|
|
317
|
+
"margin:6px 0 16px",
|
|
318
|
+
"color:#111827",
|
|
319
|
+
"background:#f3f4f6",
|
|
320
|
+
"border-radius:12px",
|
|
321
|
+
"padding:14px 8px"
|
|
322
|
+
].join(";");
|
|
323
|
+
card.appendChild(code);
|
|
324
|
+
const body = document.createElement("p");
|
|
325
|
+
body.textContent = "Say Yes only if the phone you just scanned/opened shows this code.";
|
|
326
|
+
body.style.cssText = "margin:0 0 6px;font-size:15px;line-height:1.5;color:#374151";
|
|
327
|
+
card.appendChild(body);
|
|
328
|
+
const err = document.createElement("div");
|
|
329
|
+
err.id = PROMPT_IDS.error;
|
|
330
|
+
err.setAttribute("role", "alert");
|
|
331
|
+
err.style.cssText = "min-height:18px;font-size:13px;color:#b91c1c;margin-top:4px";
|
|
332
|
+
card.appendChild(err);
|
|
333
|
+
const yes = this.button(PROMPT_IDS.yes, "Yes, it shows this code", true);
|
|
334
|
+
yes.addEventListener("click", () => void this.decide("approve"));
|
|
335
|
+
card.appendChild(yes);
|
|
336
|
+
const no = this.button(PROMPT_IDS.no, "No, that is not my phone", false);
|
|
337
|
+
no.addEventListener("click", () => void this.decide("deny"));
|
|
338
|
+
card.appendChild(no);
|
|
339
|
+
return card;
|
|
340
|
+
}
|
|
341
|
+
buildFinishing(e) {
|
|
342
|
+
const card = this.card();
|
|
343
|
+
card.style.padding = "18px 22px";
|
|
344
|
+
const row = document.createElement("div");
|
|
345
|
+
row.style.cssText = "display:flex;align-items:center;justify-content:space-between;gap:12px;flex-wrap:wrap";
|
|
346
|
+
const text = document.createElement("div");
|
|
347
|
+
text.id = PROMPT_IDS.title;
|
|
348
|
+
text.textContent = `Finishing on ${e.deviceLabel}\u2026`;
|
|
349
|
+
text.tabIndex = -1;
|
|
350
|
+
text.setAttribute("aria-live", "polite");
|
|
351
|
+
text.style.cssText = "font-size:15px;color:#374151;outline:none";
|
|
352
|
+
row.appendChild(text);
|
|
353
|
+
const stop = document.createElement("button");
|
|
354
|
+
stop.type = "button";
|
|
355
|
+
stop.id = PROMPT_IDS.stop;
|
|
356
|
+
stop.textContent = "Stop it, wrong code";
|
|
357
|
+
stop.style.cssText = `background:none;border:0;padding:6px 0;font:inherit;font-size:14px;font-weight:600;color:${BRAND};cursor:pointer;text-decoration:underline`;
|
|
358
|
+
stop.addEventListener("click", () => void this.decide("deny"));
|
|
359
|
+
row.appendChild(stop);
|
|
360
|
+
card.appendChild(row);
|
|
361
|
+
return card;
|
|
362
|
+
}
|
|
363
|
+
async decide(decision) {
|
|
364
|
+
if (this.busy || !this.enrollment) return;
|
|
365
|
+
this.busy = true;
|
|
366
|
+
this.root?.focus();
|
|
367
|
+
this.setButtonsDisabled(true);
|
|
368
|
+
try {
|
|
369
|
+
await this.handlers.respond(decision);
|
|
370
|
+
const now = this.enrollment;
|
|
371
|
+
if (!now) return;
|
|
372
|
+
this.decided = { code: now.matchCode, decision };
|
|
373
|
+
if (decision === "deny") {
|
|
374
|
+
this.update(null);
|
|
375
|
+
} else if (now.state === "awaiting_screen") {
|
|
376
|
+
this.update({ ...now.raw, state: "confirmed" });
|
|
377
|
+
}
|
|
378
|
+
} catch (err) {
|
|
379
|
+
const box = this.root?.querySelector(`#${PROMPT_IDS.error}`);
|
|
380
|
+
if (box) {
|
|
381
|
+
box.textContent = err instanceof Error && err.message ? `Couldn't send your answer: ${err.message}` : "Couldn't send your answer. Try again.";
|
|
382
|
+
}
|
|
383
|
+
} finally {
|
|
384
|
+
this.busy = false;
|
|
385
|
+
this.setButtonsDisabled(false);
|
|
386
|
+
if (this.root && this.enrollment?.state === "awaiting_screen") this.focusInitial("awaiting_screen");
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
setButtonsDisabled(disabled) {
|
|
390
|
+
this.root?.querySelectorAll("button").forEach((b) => {
|
|
391
|
+
b.disabled = disabled;
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
focusables() {
|
|
395
|
+
if (!this.root) return [];
|
|
396
|
+
return Array.from(this.root.querySelectorAll("button:not([disabled])"));
|
|
397
|
+
}
|
|
398
|
+
/** Prompt: first button. Finishing row: the text, NOT "Stop it" — a stray Enter must not deny. */
|
|
399
|
+
focusInitial(state) {
|
|
400
|
+
const target = state === "confirmed" ? this.root?.querySelector(`#${PROMPT_IDS.title}`) : this.focusables()[0];
|
|
401
|
+
try {
|
|
402
|
+
(target ?? this.root)?.focus();
|
|
403
|
+
} catch {
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
onKeyDown(ev) {
|
|
407
|
+
if (ev.key === "Escape") {
|
|
408
|
+
ev.preventDefault();
|
|
409
|
+
void this.decide("deny");
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
if (ev.key !== "Tab") return;
|
|
413
|
+
ev.preventDefault();
|
|
414
|
+
const items = this.focusables();
|
|
415
|
+
if (items.length === 0) {
|
|
416
|
+
this.root?.focus();
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
const active = document.activeElement;
|
|
420
|
+
const idx = items.indexOf(active);
|
|
421
|
+
let nextIdx;
|
|
422
|
+
if (idx === -1) nextIdx = ev.shiftKey ? items.length - 1 : 0;
|
|
423
|
+
else nextIdx = (idx + (ev.shiftKey ? -1 : 1) + items.length) % items.length;
|
|
424
|
+
items[nextIdx].focus();
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
var COOP_HINT = "The popup was severed from this window as soon as INVO's page loaded. Your page most likely sends `Cross-Origin-Opener-Policy: same-origin`; use `same-origin-allow-popups` so the hosted page can reach window.opener. The grant is still live: the device_approval.approved webhook will tell your server.";
|
|
428
|
+
var BLOCKED_HINT = "The browser blocked the popup. Nothing was started \u2014 call approveHosted() from a click handler.";
|
|
429
|
+
function pollToken(err) {
|
|
430
|
+
if (!(err instanceof InvoError) || err.status === 0 || err.status === 429 || err.status >= 500) return null;
|
|
431
|
+
const body = err.body && typeof err.body === "object" ? err.body : {};
|
|
432
|
+
const token = typeof body["error"] === "string" ? body["error"] : "";
|
|
433
|
+
return { token, body };
|
|
434
|
+
}
|
|
435
|
+
async function runHostedApproval(opts, api, deps = {}) {
|
|
436
|
+
if (typeof window === "undefined") {
|
|
437
|
+
throw new InvoError({
|
|
438
|
+
message: "approveHosted requires a browser window.",
|
|
439
|
+
code: "DOM_UNAVAILABLE",
|
|
440
|
+
status: 0
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
if (!opts.transactionId || !String(opts.transactionId).trim()) {
|
|
444
|
+
throw new Error("approveHosted requires a `transactionId`.");
|
|
445
|
+
}
|
|
446
|
+
if (!HOSTED_FLOWS.includes(opts.flow)) {
|
|
447
|
+
throw new Error(`approveHosted: unknown flow "${String(opts.flow)}" (expected ${HOSTED_FLOWS.join(" | ")}).`);
|
|
448
|
+
}
|
|
449
|
+
if (opts.renderPrompt !== false && typeof document === "undefined") {
|
|
450
|
+
throw new InvoError({
|
|
451
|
+
message: "approveHosted needs a DOM to render the match-code prompt (or pass renderPrompt:false).",
|
|
452
|
+
code: "DOM_UNAVAILABLE",
|
|
453
|
+
status: 0
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
const signal = opts.signal;
|
|
457
|
+
if (signal?.aborted) return { status: "closed", popupBlocked: false };
|
|
458
|
+
const open = deps.open ?? ((u, n, f) => window.open(u, n, f));
|
|
459
|
+
const navigate = deps.navigate ?? ((u) => window.location.assign(u));
|
|
460
|
+
const closedPollMs = deps.closedPollMs ?? 500;
|
|
461
|
+
const coopGraceMs = deps.coopGraceMs ?? 1500;
|
|
462
|
+
const createPrompt = deps.createPrompt ?? (() => new HostedEnrollmentPrompt());
|
|
463
|
+
const width = opts.popup?.width ?? 480;
|
|
464
|
+
const height = opts.popup?.height ?? 720;
|
|
465
|
+
const sw = window.screen?.width ?? width;
|
|
466
|
+
const sh = window.screen?.height ?? height;
|
|
467
|
+
const left = Math.max(0, Math.round((sw - width) / 2));
|
|
468
|
+
const top = Math.max(0, Math.round((sh - height) / 2));
|
|
469
|
+
const features = `popup=yes,width=${width},height=${height},left=${left},top=${top},resizable=yes,scrollbars=yes`;
|
|
470
|
+
const name = `invo-approval-${encodeURIComponent(String(opts.transactionId)).replace(/[^A-Za-z0-9-]/g, "")}`;
|
|
471
|
+
let popup;
|
|
472
|
+
try {
|
|
473
|
+
popup = open("about:blank", name, features);
|
|
474
|
+
} catch {
|
|
475
|
+
popup = null;
|
|
476
|
+
}
|
|
477
|
+
const mode = opts.popupBlocked ?? "resolve";
|
|
478
|
+
if (!popup && mode === "resolve") {
|
|
479
|
+
return { status: "blocked", popupBlocked: true, message: BLOCKED_HINT };
|
|
480
|
+
}
|
|
481
|
+
let begin;
|
|
482
|
+
try {
|
|
483
|
+
begin = await api.begin();
|
|
484
|
+
} catch (err) {
|
|
485
|
+
try {
|
|
486
|
+
popup?.close();
|
|
487
|
+
} catch {
|
|
488
|
+
}
|
|
489
|
+
if (signal?.aborted || err instanceof InvoError && err.code === "ABORTED") {
|
|
490
|
+
return { status: "closed", popupBlocked: false };
|
|
491
|
+
}
|
|
492
|
+
throw err;
|
|
493
|
+
}
|
|
494
|
+
const deviceCode = typeof begin["device_code"] === "string" ? begin["device_code"] : "";
|
|
495
|
+
const url = typeof begin["verification_uri_complete"] === "string" ? begin["verification_uri_complete"] : "";
|
|
496
|
+
if (!deviceCode || !url) {
|
|
497
|
+
try {
|
|
498
|
+
popup?.close();
|
|
499
|
+
} catch {
|
|
500
|
+
}
|
|
501
|
+
const { device_code: _dc, ...safeBody } = begin;
|
|
502
|
+
throw new InvoError({
|
|
503
|
+
message: "begin returned no device_code / verification_uri_complete.",
|
|
504
|
+
code: "UNEXPECTED_RESPONSE",
|
|
505
|
+
status: 0,
|
|
506
|
+
body: safeBody
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
let origin;
|
|
510
|
+
try {
|
|
511
|
+
origin = derivePlatformOrigin(url, opts.platformOrigin);
|
|
512
|
+
} catch (err) {
|
|
513
|
+
try {
|
|
514
|
+
popup?.close();
|
|
515
|
+
} catch {
|
|
516
|
+
}
|
|
517
|
+
throw err;
|
|
518
|
+
}
|
|
519
|
+
const expiresIn = Math.min(
|
|
520
|
+
MAX_EXPIRES_IN_S,
|
|
521
|
+
Math.max(1, Number(begin["expires_in"]) || 600)
|
|
522
|
+
);
|
|
523
|
+
let interval = Math.max(1, Number(begin["interval"]) || 5);
|
|
524
|
+
if (!popup) {
|
|
525
|
+
try {
|
|
526
|
+
navigate(url);
|
|
527
|
+
return { status: "redirected", popupBlocked: true };
|
|
528
|
+
} catch (err) {
|
|
529
|
+
return {
|
|
530
|
+
status: "blocked",
|
|
531
|
+
popupBlocked: true,
|
|
532
|
+
message: `The browser blocked the popup and the redirect fallback failed${err instanceof Error && err.message ? `: ${err.message}` : "."}`
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
const win = popup;
|
|
537
|
+
try {
|
|
538
|
+
const loc = win.location;
|
|
539
|
+
if (loc && typeof loc.replace === "function") loc.replace(url);
|
|
540
|
+
else if (loc) loc.href = url;
|
|
541
|
+
} catch {
|
|
542
|
+
}
|
|
543
|
+
const navigatedAt = Date.now();
|
|
544
|
+
try {
|
|
545
|
+
win.focus?.();
|
|
546
|
+
} catch {
|
|
547
|
+
}
|
|
548
|
+
return new Promise((resolve, reject) => {
|
|
549
|
+
let settled = false;
|
|
550
|
+
let prompt;
|
|
551
|
+
let wake = null;
|
|
552
|
+
let expiryTimer;
|
|
553
|
+
let closedTimer;
|
|
554
|
+
const cleanup = (closePopup) => {
|
|
555
|
+
settled = true;
|
|
556
|
+
window.removeEventListener("message", onMessage);
|
|
557
|
+
signal?.removeEventListener("abort", onAbort);
|
|
558
|
+
if (expiryTimer !== void 0) clearTimeout(expiryTimer);
|
|
559
|
+
if (closedTimer !== void 0) clearInterval(closedTimer);
|
|
560
|
+
prompt?.clear();
|
|
561
|
+
if (closePopup && !win.closed) {
|
|
562
|
+
try {
|
|
563
|
+
win.close();
|
|
564
|
+
} catch {
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
wake?.();
|
|
568
|
+
};
|
|
569
|
+
const finish = (result, closePopup) => {
|
|
570
|
+
if (settled) return;
|
|
571
|
+
cleanup(closePopup);
|
|
572
|
+
resolve(result);
|
|
573
|
+
};
|
|
574
|
+
const fail = (err) => {
|
|
575
|
+
if (settled) return;
|
|
576
|
+
cleanup(false);
|
|
577
|
+
reject(err);
|
|
578
|
+
};
|
|
579
|
+
const respond = async (decision) => {
|
|
580
|
+
if (settled) return;
|
|
581
|
+
try {
|
|
582
|
+
await api.confirmEnrollment(deviceCode, decision);
|
|
583
|
+
} catch (err) {
|
|
584
|
+
if (err instanceof InvoError && err.rawCode && CONFIRM_QUIET_CODES.has(err.rawCode)) {
|
|
585
|
+
prompt?.update(null);
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
throw err;
|
|
589
|
+
}
|
|
590
|
+
wake?.();
|
|
591
|
+
};
|
|
592
|
+
const ensurePrompt = () => {
|
|
593
|
+
if (!prompt) {
|
|
594
|
+
prompt = createPrompt();
|
|
595
|
+
prompt.configure({
|
|
596
|
+
respond,
|
|
597
|
+
onEnrollment: opts.onEnrollment,
|
|
598
|
+
renderPrompt: opts.renderPrompt !== false
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
return prompt;
|
|
602
|
+
};
|
|
603
|
+
const showEnrollment = (input) => {
|
|
604
|
+
const e = input && typeof input === "object" ? input : null;
|
|
605
|
+
if (e) ensurePrompt().update(e);
|
|
606
|
+
else prompt?.update(null);
|
|
607
|
+
};
|
|
608
|
+
const onMessage = (event) => {
|
|
609
|
+
if (event.origin !== origin) return;
|
|
610
|
+
if (event.source && event.source !== win) return;
|
|
611
|
+
const data = event.data;
|
|
612
|
+
if (!data || typeof data !== "object" || data.type !== HOSTED_MESSAGE_TYPE) return;
|
|
613
|
+
wake?.();
|
|
614
|
+
};
|
|
615
|
+
const onAbort = () => finish({ status: "closed", popupBlocked: false }, true);
|
|
616
|
+
const pause = (ms) => new Promise((r) => {
|
|
617
|
+
const t = setTimeout(() => {
|
|
618
|
+
wake = null;
|
|
619
|
+
r();
|
|
620
|
+
}, ms);
|
|
621
|
+
wake = () => {
|
|
622
|
+
clearTimeout(t);
|
|
623
|
+
wake = null;
|
|
624
|
+
r();
|
|
625
|
+
};
|
|
626
|
+
});
|
|
627
|
+
window.addEventListener("message", onMessage);
|
|
628
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
629
|
+
expiryTimer = setTimeout(() => finish({ status: "expired", popupBlocked: false }, false), expiresIn * 1e3);
|
|
630
|
+
closedTimer = setInterval(() => {
|
|
631
|
+
if (!win.closed) return;
|
|
632
|
+
if (Date.now() - navigatedAt < coopGraceMs) {
|
|
633
|
+
finish({ status: "blocked", popupBlocked: true, message: COOP_HINT }, false);
|
|
634
|
+
} else {
|
|
635
|
+
finish({ status: "closed", popupBlocked: false }, false);
|
|
636
|
+
}
|
|
637
|
+
}, closedPollMs);
|
|
638
|
+
void (async () => {
|
|
639
|
+
let transientFailures = 0;
|
|
640
|
+
while (!settled) {
|
|
641
|
+
await pause(interval * 1e3);
|
|
642
|
+
if (settled) return;
|
|
643
|
+
let raw;
|
|
644
|
+
try {
|
|
645
|
+
raw = await api.poll(deviceCode);
|
|
646
|
+
} catch (err) {
|
|
647
|
+
if (settled) return;
|
|
648
|
+
const p = pollToken(err);
|
|
649
|
+
if (!p) {
|
|
650
|
+
if (++transientFailures >= MAX_TRANSIENT_POLL_FAILURES) return fail(err);
|
|
651
|
+
const ra = err instanceof InvoError && err.status === 429 ? err.retryAfter : void 0;
|
|
652
|
+
if (typeof ra === "number" && ra > interval) interval = Math.min(ra, MAX_EXPIRES_IN_S);
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
transientFailures = 0;
|
|
656
|
+
const bodyInterval = Number(p.body["interval"]);
|
|
657
|
+
switch (p.token) {
|
|
658
|
+
case "authorization_pending":
|
|
659
|
+
if (bodyInterval > 0) interval = bodyInterval;
|
|
660
|
+
showEnrollment(p.body["enrollment"]);
|
|
661
|
+
continue;
|
|
662
|
+
case "slow_down":
|
|
663
|
+
interval = Math.max(interval + 5, bodyInterval > 0 ? bodyInterval : 0);
|
|
664
|
+
continue;
|
|
665
|
+
case "expired_token":
|
|
666
|
+
return finish({ status: "expired", popupBlocked: false }, false);
|
|
667
|
+
case "access_denied":
|
|
668
|
+
return finish({ status: "denied", popupBlocked: false }, false);
|
|
669
|
+
default:
|
|
670
|
+
return fail(err);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
if (settled) return;
|
|
674
|
+
if (raw["status"] !== "approved") continue;
|
|
675
|
+
try {
|
|
676
|
+
const approval = await api.approve(deviceCode);
|
|
677
|
+
if (settled) return;
|
|
678
|
+
finish({ status: "approved", popupBlocked: false, approval }, true);
|
|
679
|
+
} catch (err) {
|
|
680
|
+
fail(err);
|
|
681
|
+
}
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
})();
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
|
|
95
688
|
// src/index.ts
|
|
96
689
|
function toPendingCollectItem(row) {
|
|
97
690
|
return {
|
|
@@ -120,6 +713,29 @@ function toBalanceRow(row) {
|
|
|
120
713
|
raw: row
|
|
121
714
|
};
|
|
122
715
|
}
|
|
716
|
+
function toApproveResult(raw, transactionId) {
|
|
717
|
+
const holdReason = raw["error_code"] ?? raw["code"];
|
|
718
|
+
return {
|
|
719
|
+
status: String(raw["status"] ?? ""),
|
|
720
|
+
next: String(raw["next"] ?? ""),
|
|
721
|
+
transactionId: String(raw["transaction_id"] ?? transactionId),
|
|
722
|
+
claimCode: raw["claim_code"],
|
|
723
|
+
claimCodeExpiresAt: raw["claim_code_expires_at"],
|
|
724
|
+
holdReason: typeof holdReason === "string" ? holdReason : void 0,
|
|
725
|
+
risk: raw["risk"],
|
|
726
|
+
guardianApproval: raw["guardian_approval"] ?? void 0,
|
|
727
|
+
pollEndpoint: raw["poll_endpoint"],
|
|
728
|
+
raw
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
function toConfirmReceiptResult(raw) {
|
|
732
|
+
const holdReason = raw["error_code"] ?? raw["code"];
|
|
733
|
+
return {
|
|
734
|
+
status: String(raw["status"] ?? ""),
|
|
735
|
+
holdReason: typeof holdReason === "string" ? holdReason : void 0,
|
|
736
|
+
raw
|
|
737
|
+
};
|
|
738
|
+
}
|
|
123
739
|
var PlatformCommerceClient = class {
|
|
124
740
|
/**
|
|
125
741
|
* Embed INVO's hosted checkout in an iframe and listen for completion.
|
|
@@ -258,6 +874,60 @@ var InvoClient = class {
|
|
|
258
874
|
return { status: String(raw["status"] ?? ""), device: raw["device"] ?? null, raw };
|
|
259
875
|
});
|
|
260
876
|
}
|
|
877
|
+
async approveHosted(opts) {
|
|
878
|
+
const txn = String(opts.transactionId ?? "");
|
|
879
|
+
const id = encodeURIComponent(txn);
|
|
880
|
+
const signal = opts.signal;
|
|
881
|
+
const api = {
|
|
882
|
+
begin: () => this.withTokenRetry(
|
|
883
|
+
() => this.post(
|
|
884
|
+
"/api/sdk/approvals/device/begin",
|
|
885
|
+
{ transaction_id: txn, flow: opts.flow, channel: "popup" },
|
|
886
|
+
signal
|
|
887
|
+
)
|
|
888
|
+
),
|
|
889
|
+
poll: (deviceCode) => this.withTokenRetry(
|
|
890
|
+
() => this.post(
|
|
891
|
+
"/api/sdk/approvals/device/poll",
|
|
892
|
+
{ device_code: deviceCode },
|
|
893
|
+
signal
|
|
894
|
+
)
|
|
895
|
+
),
|
|
896
|
+
confirmEnrollment: (deviceCode, decision) => this.withTokenRetry(
|
|
897
|
+
() => this.post(
|
|
898
|
+
"/api/sdk/approvals/device/confirm-enrollment",
|
|
899
|
+
{ device_code: deviceCode, decision },
|
|
900
|
+
signal
|
|
901
|
+
)
|
|
902
|
+
),
|
|
903
|
+
approve: (deviceCode) => this.withTokenRetry(async () => {
|
|
904
|
+
const body = { device_code: deviceCode };
|
|
905
|
+
switch (opts.flow) {
|
|
906
|
+
case "transfer":
|
|
907
|
+
return toApproveResult(
|
|
908
|
+
await this.post(`/api/sdk/transfers/${id}/approve`, body, signal),
|
|
909
|
+
txn
|
|
910
|
+
);
|
|
911
|
+
case "send":
|
|
912
|
+
return toApproveResult(
|
|
913
|
+
await this.post(`/api/sdk/send/${id}/approve`, body, signal),
|
|
914
|
+
txn
|
|
915
|
+
);
|
|
916
|
+
case "send_receipt":
|
|
917
|
+
return toConfirmReceiptResult(
|
|
918
|
+
await this.post(`/api/sdk/send/${id}/confirm-receipt`, body, signal)
|
|
919
|
+
);
|
|
920
|
+
case "transfer_receipt":
|
|
921
|
+
return toConfirmReceiptResult(
|
|
922
|
+
await this.post(`/api/sdk/transfers/${id}/confirm-receipt`, body, signal)
|
|
923
|
+
);
|
|
924
|
+
default:
|
|
925
|
+
throw new Error(`approveHosted: unknown flow "${String(opts.flow)}".`);
|
|
926
|
+
}
|
|
927
|
+
})
|
|
928
|
+
};
|
|
929
|
+
return runHostedApproval(opts, api);
|
|
930
|
+
}
|
|
261
931
|
/** Approve a SEND with the player's passkey. */
|
|
262
932
|
async approveSend(transactionId, opts) {
|
|
263
933
|
return this.approve("send", transactionId, opts);
|
|
@@ -525,19 +1195,7 @@ var InvoClient = class {
|
|
|
525
1195
|
{ webauthn_assertion: assertion },
|
|
526
1196
|
signal
|
|
527
1197
|
);
|
|
528
|
-
|
|
529
|
-
return {
|
|
530
|
-
status: String(raw["status"] ?? ""),
|
|
531
|
-
next: String(raw["next"] ?? ""),
|
|
532
|
-
transactionId: String(raw["transaction_id"] ?? transactionId),
|
|
533
|
-
claimCode: raw["claim_code"],
|
|
534
|
-
claimCodeExpiresAt: raw["claim_code_expires_at"],
|
|
535
|
-
holdReason: typeof holdReason === "string" ? holdReason : void 0,
|
|
536
|
-
risk: raw["risk"],
|
|
537
|
-
guardianApproval: raw["guardian_approval"] ?? void 0,
|
|
538
|
-
pollEndpoint: raw["poll_endpoint"],
|
|
539
|
-
raw
|
|
540
|
-
};
|
|
1198
|
+
return toApproveResult(raw, transactionId);
|
|
541
1199
|
});
|
|
542
1200
|
}
|
|
543
1201
|
async confirmReceipt(flow, transactionId, opts) {
|
|
@@ -554,12 +1212,7 @@ var InvoClient = class {
|
|
|
554
1212
|
{ webauthn_assertion: assertion },
|
|
555
1213
|
signal
|
|
556
1214
|
);
|
|
557
|
-
|
|
558
|
-
return {
|
|
559
|
-
status: String(raw["status"] ?? ""),
|
|
560
|
-
holdReason: typeof holdReason === "string" ? holdReason : void 0,
|
|
561
|
-
raw
|
|
562
|
-
};
|
|
1215
|
+
return toConfirmReceiptResult(raw);
|
|
563
1216
|
});
|
|
564
1217
|
}
|
|
565
1218
|
};
|