@block-auth.io/core 0.5.1 → 0.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/dist/index.js CHANGED
@@ -1466,8 +1466,8 @@ async function emitOauthSignup(deps) {
1466
1466
 
1467
1467
  // src/auth/txns.ts
1468
1468
  function requestSignin(args) {
1469
- const { wsc, token, url, service, apiKey } = args;
1470
- wsc.emit("user:txns:create", { token, url, service, apiKey });
1469
+ const { wsc, token, url, service, apiKey, sessionId } = args;
1470
+ wsc.emit("user:txns:create", { token, url, service, apiKey, sessionId });
1471
1471
  }
1472
1472
  function cancelTxns(args) {
1473
1473
  args.wsc.emit("user:txns:update", { token: args.token, state: "cancelled" });
@@ -2283,7 +2283,8 @@ function createBlockAuthClient(config3) {
2283
2283
  token: userToken(),
2284
2284
  url: typeof window !== "undefined" ? window.location.href : "",
2285
2285
  service: $ui.get().serviceName ?? "blockauth",
2286
- apiKey: config3.apiKey
2286
+ apiKey: config3.apiKey,
2287
+ sessionId: browserBase.getDeviceSessionId()
2287
2288
  }),
2288
2289
  cancelTxns: () => cancelTxns({
2289
2290
  wsc: provider.wsc,
@@ -2339,7 +2340,8 @@ function createBlockAuthClient(config3) {
2339
2340
  }
2340
2341
 
2341
2342
  // src/runtime/authRuntime.ts
2342
- var SIGNIN_TIMEOUT_MS = 15e3;
2343
+ var SIGNIN_TIMEOUT_MS = 12e4;
2344
+ var USER_TXNS_POLL_MS = 1500;
2343
2345
  function createAuthRuntime(client, callbacks = {}) {
2344
2346
  const provider = client.provider;
2345
2347
  const hookState = {
@@ -2348,12 +2350,45 @@ function createAuthRuntime(client, callbacks = {}) {
2348
2350
  isSigning: false,
2349
2351
  isLoggingOut: false,
2350
2352
  lastCheckedDid: null,
2351
- signinAttemptId: 0
2353
+ signinAttemptId: 0,
2354
+ pendingSignin: null,
2355
+ pendingTxnId: null,
2356
+ settledAttemptId: 0
2352
2357
  };
2353
2358
  let getUserTxnsInterval = null;
2354
2359
  let unsignTimeout = null;
2355
2360
  let signinTimeout = null;
2356
2361
  const wsc = () => provider.wsc;
2362
+ const emitSdkSign = (token, address) => {
2363
+ wsc().emit("sdk:sign", { token, address });
2364
+ };
2365
+ const ownTxnItems = (items) => {
2366
+ const apiKey = client.apiKey;
2367
+ if (!apiKey) return items;
2368
+ return items.filter((item) => {
2369
+ let preParams = item?.pre_params;
2370
+ if (typeof preParams === "string") {
2371
+ try {
2372
+ preParams = JSON.parse(preParams);
2373
+ } catch {
2374
+ preParams = null;
2375
+ }
2376
+ }
2377
+ const appKey = preParams?.appKey;
2378
+ return !appKey || appKey === apiKey;
2379
+ });
2380
+ };
2381
+ const pollUserTxns = (token, shouldContinue, intervalMs) => {
2382
+ if (getUserTxnsInterval) clearInterval(getUserTxnsInterval);
2383
+ getUserTxnsInterval = setInterval(() => {
2384
+ if (shouldContinue()) {
2385
+ wsc().emit("user:txns", { token });
2386
+ } else if (getUserTxnsInterval) {
2387
+ clearInterval(getUserTxnsInterval);
2388
+ getUserTxnsInterval = null;
2389
+ }
2390
+ }, intervalMs);
2391
+ };
2357
2392
  const onSuccess = callbacks.onSuccess ?? (async (_address) => {
2358
2393
  });
2359
2394
  const onError = callbacks.onError ?? (async () => {
@@ -2365,9 +2400,15 @@ function createAuthRuntime(client, callbacks = {}) {
2365
2400
  await onSuccess(address);
2366
2401
  };
2367
2402
  const _onError = async () => {
2403
+ if (unsignTimeout) {
2404
+ clearTimeout(unsignTimeout);
2405
+ unsignTimeout = null;
2406
+ }
2368
2407
  hookState.isUnsignTimeoutSet = false;
2369
2408
  hookState.isSigned = false;
2370
2409
  hookState.isSigning = false;
2410
+ hookState.pendingSignin = null;
2411
+ hookState.pendingTxnId = null;
2371
2412
  if (getUserTxnsInterval) {
2372
2413
  clearInterval(getUserTxnsInterval);
2373
2414
  getUserTxnsInterval = null;
@@ -2377,6 +2418,7 @@ function createAuthRuntime(client, callbacks = {}) {
2377
2418
  setIsSigning(false);
2378
2419
  setIsSigningLoader(false);
2379
2420
  setIsAcceptingTxn(false);
2421
+ closeAllModals();
2380
2422
  resetUser();
2381
2423
  await onError();
2382
2424
  };
@@ -2431,6 +2473,8 @@ function createAuthRuntime(client, callbacks = {}) {
2431
2473
  }
2432
2474
  hookState.isSigning = false;
2433
2475
  hookState.isSigned = false;
2476
+ hookState.pendingSignin = null;
2477
+ hookState.pendingTxnId = null;
2434
2478
  setIsSigned(false);
2435
2479
  setIsSigning(false);
2436
2480
  setIsSigningLoader(false);
@@ -2484,6 +2528,8 @@ function createAuthRuntime(client, callbacks = {}) {
2484
2528
  hookState.isSigning = false;
2485
2529
  hookState.isSigned = false;
2486
2530
  hookState.isUnsignTimeoutSet = false;
2531
+ hookState.pendingSignin = null;
2532
+ hookState.pendingTxnId = null;
2487
2533
  }
2488
2534
  };
2489
2535
  const onSignup = (result) => {
@@ -2527,10 +2573,7 @@ function createAuthRuntime(client, callbacks = {}) {
2527
2573
  const _eDid = browserBase.getExternalService().did;
2528
2574
  const service = browserBase.getExternalService().service;
2529
2575
  const currentDid = service === "blockauth" ? _sDid : _eDid;
2530
- wsc().emit("sdk:sign", {
2531
- token: _accessToken,
2532
- address: currentDid
2533
- });
2576
+ emitSdkSign(_accessToken, currentDid);
2534
2577
  } else {
2535
2578
  if (hasPersistedSessionCandidate) {
2536
2579
  const _accessToken = wsc().accessToken;
@@ -2558,11 +2601,7 @@ function createAuthRuntime(client, callbacks = {}) {
2558
2601
  const signedExpiredAt = new Date(signedExpiredAtDateString);
2559
2602
  const timeDifference = signedExpiredAt.getTime() - currentDate.getTime();
2560
2603
  unsignTimeout = setTimeout(() => {
2561
- const _accessToken = wsc().accessToken;
2562
- wsc().emit("sdk:sign", {
2563
- token: _accessToken,
2564
- address: did
2565
- });
2604
+ emitSdkSign(wsc().accessToken, did);
2566
2605
  }, timeDifference);
2567
2606
  };
2568
2607
  const signinUnsigned = async () => {
@@ -2602,23 +2641,69 @@ function createAuthRuntime(client, callbacks = {}) {
2602
2641
  }
2603
2642
  void isUserSignedIn();
2604
2643
  };
2644
+ const captureOwnTxnId = (items) => {
2645
+ if (!hookState.pendingSignin || hookState.pendingTxnId != null) return;
2646
+ const initialItem = items.find((item) => item.state === "initial");
2647
+ if (initialItem && typeof initialItem.id !== "undefined") {
2648
+ hookState.pendingTxnId = initialItem.id;
2649
+ }
2650
+ };
2651
+ const settleSigningAttempt = (outcome) => {
2652
+ setWaitingForSuccessLogInSince(0);
2653
+ setIsSigning(false);
2654
+ hookState.isSigning = false;
2655
+ if (getUserTxnsInterval) {
2656
+ clearInterval(getUserTxnsInterval);
2657
+ getUserTxnsInterval = null;
2658
+ }
2659
+ if (outcome === "cancelled") {
2660
+ hookState.pendingSignin = null;
2661
+ hookState.pendingTxnId = null;
2662
+ hookState.settledAttemptId = hookState.signinAttemptId;
2663
+ if (!hookState.isSigned) closeAllModals();
2664
+ return;
2665
+ }
2666
+ if (hookState.settledAttemptId === hookState.signinAttemptId) {
2667
+ return;
2668
+ }
2669
+ hookState.settledAttemptId = hookState.signinAttemptId;
2670
+ const pendingSignin = hookState.pendingSignin;
2671
+ hookState.pendingSignin = null;
2672
+ const willVerifySign = outcome === "resolved" && !hookState.isSigned && !!pendingSignin;
2673
+ if (willVerifySign) {
2674
+ emitSdkSign(pendingSignin.accessToken, pendingSignin.did);
2675
+ } else if (!hookState.isSigned) {
2676
+ closeAllModals();
2677
+ }
2678
+ };
2605
2679
  const onUserTxns = async (data) => {
2606
2680
  const { success, message } = data;
2607
2681
  if (!success) return;
2608
- const hasInitialTxns = message.items && message.items.some((item) => item.state === "initial");
2682
+ const items = ownTxnItems(message.items ?? []);
2683
+ captureOwnTxnId(items);
2684
+ const hasInitialTxns = items.some((item) => item.state === "initial");
2609
2685
  if (!hasInitialTxns) {
2610
- setWaitingForSuccessLogInSince(0);
2611
- setIsSigning(false);
2612
- hookState.isSigning = false;
2613
- if (getUserTxnsInterval) {
2614
- clearInterval(getUserTxnsInterval);
2615
- getUserTxnsInterval = null;
2616
- }
2686
+ settleSigningAttempt("resolved");
2687
+ }
2688
+ setRequests(items);
2689
+ };
2690
+ const onUserTxnsUpdate = async (data) => {
2691
+ const { success, message } = data;
2692
+ if (!success) return;
2693
+ const items = ownTxnItems(message.items ?? []);
2694
+ captureOwnTxnId(items);
2695
+ const hasInitialTxns = items.some((item) => item.state === "initial");
2696
+ if (!hasInitialTxns) {
2697
+ const txnId = hookState.pendingTxnId;
2698
+ const wasCancelled = txnId != null && !items.some((item) => item.id === txnId);
2699
+ settleSigningAttempt(
2700
+ wasCancelled ? "cancelled" : items.length > 0 ? "resolved" : "none"
2701
+ );
2617
2702
  }
2618
- setRequests(message.items);
2703
+ setRequests(items);
2619
2704
  };
2620
2705
  const onUserTxnsCreate = async (data) => {
2621
- const { success } = data;
2706
+ const { success, message } = data;
2622
2707
  if (success) {
2623
2708
  setIsSigning(true);
2624
2709
  hookState.isSigning = true;
@@ -2633,13 +2718,22 @@ function createAuthRuntime(client, callbacks = {}) {
2633
2718
  const s = browserBase.getSession();
2634
2719
  const externalService = browserBase.getExternalService();
2635
2720
  let token = s.token;
2721
+ let did = s.did;
2636
2722
  if (externalService.service !== "blockauth") {
2637
2723
  token = externalService.token;
2724
+ did = externalService.did;
2638
2725
  }
2726
+ const accessToken = wsc()?.accessToken;
2727
+ hookState.pendingSignin = accessToken && did ? { accessToken, did, attemptId } : null;
2728
+ hookState.pendingTxnId = null;
2729
+ captureOwnTxnId(ownTxnItems(message?.items ?? []));
2639
2730
  wsc().emit("user:txns", { token });
2731
+ pollUserTxns(token, () => hookState.isSigning, USER_TXNS_POLL_MS);
2640
2732
  } else {
2641
2733
  setIsSigning(false);
2642
2734
  hookState.isSigning = false;
2735
+ hookState.pendingSignin = null;
2736
+ hookState.pendingTxnId = null;
2643
2737
  }
2644
2738
  };
2645
2739
  const onSdkSign = async (data) => {
@@ -2662,19 +2756,14 @@ function createAuthRuntime(client, callbacks = {}) {
2662
2756
  localStorage.removeItem("blockauth-logged-out");
2663
2757
  }
2664
2758
  if (getUserTxnsInterval === null) {
2665
- getUserTxnsInterval = setInterval(() => {
2666
- if (hookState.isSigning && hookState.isSigned) {
2667
- wsc().emit("user:txns", { token: _token });
2668
- } else if (getUserTxnsInterval) {
2669
- clearInterval(getUserTxnsInterval);
2670
- getUserTxnsInterval = null;
2671
- }
2672
- }, 500);
2759
+ pollUserTxns(
2760
+ _token,
2761
+ () => hookState.isSigning && hookState.isSigned,
2762
+ 500
2763
+ );
2673
2764
  }
2674
2765
  setUnsignTimeout(message.signedExpiredAt, _did);
2675
- if (externalService.connected) {
2676
- closeAllModals();
2677
- }
2766
+ closeAllModals();
2678
2767
  await fetchUserProfile(provider, _did);
2679
2768
  setWaitingForSuccessLogInSince(0);
2680
2769
  await _onSuccess(_did);
@@ -2738,6 +2827,8 @@ function createAuthRuntime(client, callbacks = {}) {
2738
2827
  setIsSigning(false);
2739
2828
  setIsSigningLoader(false);
2740
2829
  hookState.isSigning = false;
2830
+ hookState.pendingSignin = null;
2831
+ hookState.pendingTxnId = null;
2741
2832
  closeAllModals();
2742
2833
  if (typeof window !== "undefined") {
2743
2834
  const isLandingPage = window.location.pathname === "/" || window.location.pathname === "/signin";
@@ -2852,7 +2943,7 @@ function createAuthRuntime(client, callbacks = {}) {
2852
2943
  wsc().on("provider:signup", onProviderSignup);
2853
2944
  wsc().on("provider:signin", onProviderSignin);
2854
2945
  wsc().on("user:txns", onUserTxns);
2855
- wsc().on("user:txns:update", onUserTxns);
2946
+ wsc().on("user:txns:update", onUserTxnsUpdate);
2856
2947
  wsc().on("user:txns:create", onUserTxnsCreate);
2857
2948
  wsc().on("sdk:sign", onSdkSign);
2858
2949
  wsc().on("signin:corrupted", onSigninCorrupted);
@@ -2889,6 +2980,8 @@ function createAuthRuntime(client, callbacks = {}) {
2889
2980
  signinTimeout = null;
2890
2981
  }
2891
2982
  hookState.isSigning = false;
2983
+ hookState.pendingSignin = null;
2984
+ hookState.pendingTxnId = null;
2892
2985
  if (getUserTxnsInterval) {
2893
2986
  clearInterval(getUserTxnsInterval);
2894
2987
  getUserTxnsInterval = null;
@@ -2899,7 +2992,7 @@ function createAuthRuntime(client, callbacks = {}) {
2899
2992
  w.off("provider:signup", onProviderSignup);
2900
2993
  w.off("provider:signin", onProviderSignin);
2901
2994
  w.off("user:txns", onUserTxns);
2902
- w.off("user:txns:update", onUserTxns);
2995
+ w.off("user:txns:update", onUserTxnsUpdate);
2903
2996
  w.off("user:txns:create", onUserTxnsCreate);
2904
2997
  w.off("sdk:sign", onSdkSign);
2905
2998
  w.off("signin:corrupted", onSigninCorrupted);