@swype-org/react-sdk 0.1.9 → 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.d.cts CHANGED
@@ -387,8 +387,9 @@ declare function useTransferSigning(pollIntervalMs?: number, options?: UseTransf
387
387
 
388
388
  declare function fetchProviders(apiBaseUrl: string, token: string): Promise<Provider[]>;
389
389
  declare function fetchChains(apiBaseUrl: string, token: string): Promise<Chain[]>;
390
- declare function fetchAccounts(apiBaseUrl: string, token: string): Promise<Account[]>;
390
+ declare function fetchAccounts(apiBaseUrl: string, token: string, credentialId: string): Promise<Account[]>;
391
391
  interface CreateTransferParams {
392
+ credentialId: string;
392
393
  sourceType: SourceType;
393
394
  sourceId: string;
394
395
  destination: Destination;
package/dist/index.d.ts CHANGED
@@ -387,8 +387,9 @@ declare function useTransferSigning(pollIntervalMs?: number, options?: UseTransf
387
387
 
388
388
  declare function fetchProviders(apiBaseUrl: string, token: string): Promise<Provider[]>;
389
389
  declare function fetchChains(apiBaseUrl: string, token: string): Promise<Chain[]>;
390
- declare function fetchAccounts(apiBaseUrl: string, token: string): Promise<Account[]>;
390
+ declare function fetchAccounts(apiBaseUrl: string, token: string, credentialId: string): Promise<Account[]>;
391
391
  interface CreateTransferParams {
392
+ credentialId: string;
392
393
  sourceType: SourceType;
393
394
  sourceId: string;
394
395
  destination: Destination;
package/dist/index.js CHANGED
@@ -175,8 +175,9 @@ async function fetchChains(apiBaseUrl, token) {
175
175
  const data = await res.json();
176
176
  return data.items;
177
177
  }
178
- async function fetchAccounts(apiBaseUrl, token) {
179
- const res = await fetch(`${apiBaseUrl}/v1/accounts`, {
178
+ async function fetchAccounts(apiBaseUrl, token, credentialId) {
179
+ const params = new URLSearchParams({ credentialId });
180
+ const res = await fetch(`${apiBaseUrl}/v1/accounts?${params.toString()}`, {
180
181
  headers: { Authorization: `Bearer ${token}` }
181
182
  });
182
183
  if (!res.ok) await throwApiError(res);
@@ -186,6 +187,7 @@ async function fetchAccounts(apiBaseUrl, token) {
186
187
  async function createTransfer(apiBaseUrl, token, params) {
187
188
  const body = {
188
189
  id: crypto.randomUUID(),
190
+ credentialId: params.credentialId,
189
191
  sources: [{ [params.sourceType]: params.sourceId }],
190
192
  destinations: [
191
193
  {
@@ -1931,6 +1933,7 @@ function AdvancedSettings({
1931
1933
  )
1932
1934
  ] });
1933
1935
  }
1936
+ var ACTIVE_CREDENTIAL_STORAGE_KEY = "swype_active_credential_id";
1934
1937
  function isMobile() {
1935
1938
  if (typeof navigator === "undefined") return false;
1936
1939
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
@@ -2034,6 +2037,10 @@ function SwypePayment({
2034
2037
  const [transfer, setTransfer] = useState(null);
2035
2038
  const [creatingTransfer, setCreatingTransfer] = useState(false);
2036
2039
  const [registeringPasskey, setRegisteringPasskey] = useState(false);
2040
+ const [activeCredentialId, setActiveCredentialId] = useState(() => {
2041
+ if (typeof window === "undefined") return null;
2042
+ return window.localStorage.getItem(ACTIVE_CREDENTIAL_STORAGE_KEY);
2043
+ });
2037
2044
  const [mobileFlow, setMobileFlow] = useState(false);
2038
2045
  const pollingTransferIdRef = useRef(null);
2039
2046
  const [selectSourceChainName, setSelectSourceChainName] = useState("");
@@ -2058,7 +2065,7 @@ function SwypePayment({
2058
2065
  if (!token || cancelled) return;
2059
2066
  const { config } = await fetchUserConfig(apiBaseUrl, token);
2060
2067
  if (cancelled) return;
2061
- if (!config.passkey) {
2068
+ if (!config.passkey || !activeCredentialId) {
2062
2069
  setStep("register-passkey");
2063
2070
  } else if (depositAmount != null && depositAmount > 0) {
2064
2071
  setStep("ready");
@@ -2079,7 +2086,7 @@ function SwypePayment({
2079
2086
  return () => {
2080
2087
  cancelled = true;
2081
2088
  };
2082
- }, [ready, authenticated, step, depositAmount, apiBaseUrl, getAccessToken]);
2089
+ }, [ready, authenticated, step, depositAmount, apiBaseUrl, getAccessToken, activeCredentialId]);
2083
2090
  const loadingDataRef = useRef(false);
2084
2091
  useEffect(() => {
2085
2092
  if (!authenticated) return;
@@ -2090,11 +2097,15 @@ function SwypePayment({
2090
2097
  setLoadingData(true);
2091
2098
  setError(null);
2092
2099
  try {
2100
+ if (!activeCredentialId) {
2101
+ setStep("register-passkey");
2102
+ return;
2103
+ }
2093
2104
  const token = await getAccessToken();
2094
2105
  if (!token) throw new Error("Not authenticated");
2095
2106
  const [prov, accts, chn] = await Promise.all([
2096
2107
  fetchProviders(apiBaseUrl, token),
2097
- fetchAccounts(apiBaseUrl, token),
2108
+ fetchAccounts(apiBaseUrl, token, activeCredentialId),
2098
2109
  fetchChains(apiBaseUrl, token)
2099
2110
  ]);
2100
2111
  if (cancelled) return;
@@ -2126,7 +2137,7 @@ function SwypePayment({
2126
2137
  cancelled = true;
2127
2138
  loadingDataRef.current = false;
2128
2139
  };
2129
- }, [authenticated, accounts.length, apiBaseUrl, getAccessToken]);
2140
+ }, [authenticated, accounts.length, apiBaseUrl, getAccessToken, activeCredentialId]);
2130
2141
  useEffect(() => {
2131
2142
  if (!polling.transfer) return;
2132
2143
  if (polling.transfer.status === "COMPLETED") {
@@ -2200,6 +2211,11 @@ function SwypePayment({
2200
2211
  setError("No account or provider selected.");
2201
2212
  return;
2202
2213
  }
2214
+ if (!activeCredentialId) {
2215
+ setError("Create a passkey on this device before continuing.");
2216
+ setStep("register-passkey");
2217
+ return;
2218
+ }
2203
2219
  setStep("processing");
2204
2220
  setError(null);
2205
2221
  setCreatingTransfer(true);
@@ -2235,6 +2251,7 @@ function SwypePayment({
2235
2251
  }
2236
2252
  }
2237
2253
  const t = await createTransfer(apiBaseUrl, token, {
2254
+ credentialId: activeCredentialId,
2238
2255
  sourceType: effectiveSourceType,
2239
2256
  sourceId: effectiveSourceId,
2240
2257
  destination,
@@ -2268,6 +2285,7 @@ function SwypePayment({
2268
2285
  amount,
2269
2286
  sourceId,
2270
2287
  sourceType,
2288
+ activeCredentialId,
2271
2289
  destination,
2272
2290
  apiBaseUrl,
2273
2291
  getAccessToken,
@@ -2465,6 +2483,8 @@ function SwypePayment({
2465
2483
  if (!token) throw new Error("Not authenticated");
2466
2484
  const { credentialId, publicKey } = await createPasskeyCredential("Swype User");
2467
2485
  await registerPasskey(apiBaseUrl, token, credentialId, publicKey);
2486
+ setActiveCredentialId(credentialId);
2487
+ window.localStorage.setItem(ACTIVE_CREDENTIAL_STORAGE_KEY, credentialId);
2468
2488
  if (depositAmount != null && depositAmount > 0) {
2469
2489
  setStep("ready");
2470
2490
  } else {