@swype-org/react-sdk 0.1.8 → 0.1.10

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.cjs CHANGED
@@ -178,8 +178,9 @@ async function fetchChains(apiBaseUrl, token) {
178
178
  const data = await res.json();
179
179
  return data.items;
180
180
  }
181
- async function fetchAccounts(apiBaseUrl, token) {
182
- const res = await fetch(`${apiBaseUrl}/v1/accounts`, {
181
+ async function fetchAccounts(apiBaseUrl, token, credentialId) {
182
+ const params = new URLSearchParams({ credentialId });
183
+ const res = await fetch(`${apiBaseUrl}/v1/accounts?${params.toString()}`, {
183
184
  headers: { Authorization: `Bearer ${token}` }
184
185
  });
185
186
  if (!res.ok) await throwApiError(res);
@@ -189,6 +190,7 @@ async function fetchAccounts(apiBaseUrl, token) {
189
190
  async function createTransfer(apiBaseUrl, token, params) {
190
191
  const body = {
191
192
  id: crypto.randomUUID(),
193
+ credentialId: params.credentialId,
192
194
  sources: [{ [params.sourceType]: params.sourceId }],
193
195
  destinations: [
194
196
  {
@@ -1934,6 +1936,7 @@ function AdvancedSettings({
1934
1936
  )
1935
1937
  ] });
1936
1938
  }
1939
+ var ACTIVE_CREDENTIAL_STORAGE_KEY = "swype_active_credential_id";
1937
1940
  function isMobile() {
1938
1941
  if (typeof navigator === "undefined") return false;
1939
1942
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
@@ -2037,6 +2040,10 @@ function SwypePayment({
2037
2040
  const [transfer, setTransfer] = react.useState(null);
2038
2041
  const [creatingTransfer, setCreatingTransfer] = react.useState(false);
2039
2042
  const [registeringPasskey, setRegisteringPasskey] = react.useState(false);
2043
+ const [activeCredentialId, setActiveCredentialId] = react.useState(() => {
2044
+ if (typeof window === "undefined") return null;
2045
+ return window.localStorage.getItem(ACTIVE_CREDENTIAL_STORAGE_KEY);
2046
+ });
2040
2047
  const [mobileFlow, setMobileFlow] = react.useState(false);
2041
2048
  const pollingTransferIdRef = react.useRef(null);
2042
2049
  const [selectSourceChainName, setSelectSourceChainName] = react.useState("");
@@ -2061,7 +2068,7 @@ function SwypePayment({
2061
2068
  if (!token || cancelled) return;
2062
2069
  const { config } = await fetchUserConfig(apiBaseUrl, token);
2063
2070
  if (cancelled) return;
2064
- if (!config.passkey) {
2071
+ if (!config.passkey || !activeCredentialId) {
2065
2072
  setStep("register-passkey");
2066
2073
  } else if (depositAmount != null && depositAmount > 0) {
2067
2074
  setStep("ready");
@@ -2082,7 +2089,7 @@ function SwypePayment({
2082
2089
  return () => {
2083
2090
  cancelled = true;
2084
2091
  };
2085
- }, [ready, authenticated, step, depositAmount, apiBaseUrl, getAccessToken]);
2092
+ }, [ready, authenticated, step, depositAmount, apiBaseUrl, getAccessToken, activeCredentialId]);
2086
2093
  const loadingDataRef = react.useRef(false);
2087
2094
  react.useEffect(() => {
2088
2095
  if (!authenticated) return;
@@ -2093,11 +2100,15 @@ function SwypePayment({
2093
2100
  setLoadingData(true);
2094
2101
  setError(null);
2095
2102
  try {
2103
+ if (!activeCredentialId) {
2104
+ setStep("register-passkey");
2105
+ return;
2106
+ }
2096
2107
  const token = await getAccessToken();
2097
2108
  if (!token) throw new Error("Not authenticated");
2098
2109
  const [prov, accts, chn] = await Promise.all([
2099
2110
  fetchProviders(apiBaseUrl, token),
2100
- fetchAccounts(apiBaseUrl, token),
2111
+ fetchAccounts(apiBaseUrl, token, activeCredentialId),
2101
2112
  fetchChains(apiBaseUrl, token)
2102
2113
  ]);
2103
2114
  if (cancelled) return;
@@ -2129,7 +2140,7 @@ function SwypePayment({
2129
2140
  cancelled = true;
2130
2141
  loadingDataRef.current = false;
2131
2142
  };
2132
- }, [authenticated, accounts.length, apiBaseUrl, getAccessToken]);
2143
+ }, [authenticated, accounts.length, apiBaseUrl, getAccessToken, activeCredentialId]);
2133
2144
  react.useEffect(() => {
2134
2145
  if (!polling.transfer) return;
2135
2146
  if (polling.transfer.status === "COMPLETED") {
@@ -2203,6 +2214,11 @@ function SwypePayment({
2203
2214
  setError("No account or provider selected.");
2204
2215
  return;
2205
2216
  }
2217
+ if (!activeCredentialId) {
2218
+ setError("Create a passkey on this device before continuing.");
2219
+ setStep("register-passkey");
2220
+ return;
2221
+ }
2206
2222
  setStep("processing");
2207
2223
  setError(null);
2208
2224
  setCreatingTransfer(true);
@@ -2220,7 +2236,25 @@ function SwypePayment({
2220
2236
  effectiveSourceId = activeWallet.id;
2221
2237
  }
2222
2238
  }
2239
+ const isActiveWallet = effectiveSourceType === "walletId" && accounts.some(
2240
+ (a) => a.wallets.some((w) => w.id === effectiveSourceId && w.status === "ACTIVE")
2241
+ );
2242
+ if (!isActiveWallet) {
2243
+ let found = false;
2244
+ for (const acct of accounts) {
2245
+ for (const wallet of acct.wallets) {
2246
+ if (wallet.status === "ACTIVE" && wallet.sources.some((s) => s.balance.available.amount >= parsedAmount)) {
2247
+ effectiveSourceType = "walletId";
2248
+ effectiveSourceId = wallet.id;
2249
+ found = true;
2250
+ break;
2251
+ }
2252
+ }
2253
+ if (found) break;
2254
+ }
2255
+ }
2223
2256
  const t = await createTransfer(apiBaseUrl, token, {
2257
+ credentialId: activeCredentialId,
2224
2258
  sourceType: effectiveSourceType,
2225
2259
  sourceId: effectiveSourceId,
2226
2260
  destination,
@@ -2254,6 +2288,7 @@ function SwypePayment({
2254
2288
  amount,
2255
2289
  sourceId,
2256
2290
  sourceType,
2291
+ activeCredentialId,
2257
2292
  destination,
2258
2293
  apiBaseUrl,
2259
2294
  getAccessToken,
@@ -2451,6 +2486,8 @@ function SwypePayment({
2451
2486
  if (!token) throw new Error("Not authenticated");
2452
2487
  const { credentialId, publicKey } = await createPasskeyCredential("Swype User");
2453
2488
  await registerPasskey(apiBaseUrl, token, credentialId, publicKey);
2489
+ setActiveCredentialId(credentialId);
2490
+ window.localStorage.setItem(ACTIVE_CREDENTIAL_STORAGE_KEY, credentialId);
2454
2491
  if (depositAmount != null && depositAmount > 0) {
2455
2492
  setStep("ready");
2456
2493
  } else {