@volr/react 0.1.83 → 0.1.85

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
@@ -1723,6 +1723,16 @@ function byteSwap32(arr) {
1723
1723
  }
1724
1724
  return arr;
1725
1725
  }
1726
+ function bytesToHex2(bytes) {
1727
+ abytes(bytes);
1728
+ if (hasHexBuiltin)
1729
+ return bytes.toHex();
1730
+ let hex = "";
1731
+ for (let i = 0; i < bytes.length; i++) {
1732
+ hex += hexes2[bytes[i]];
1733
+ }
1734
+ return hex;
1735
+ }
1726
1736
  function utf8ToBytes(str) {
1727
1737
  if (typeof str !== "string")
1728
1738
  throw new Error("string expected");
@@ -1766,12 +1776,17 @@ function randomBytes(bytesLength = 32) {
1766
1776
  }
1767
1777
  throw new Error("crypto.getRandomValues must be defined");
1768
1778
  }
1769
- var isLE, swap32IfBE, Hash;
1779
+ var isLE, swap32IfBE, hasHexBuiltin, hexes2, Hash;
1770
1780
  var init_utils2 = __esm({
1771
1781
  "../node_modules/@noble/hashes/esm/utils.js"() {
1772
1782
  init_cryptoNode();
1773
1783
  isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
1774
1784
  swap32IfBE = isLE ? (u) => u : byteSwap32;
1785
+ hasHexBuiltin = /* @__PURE__ */ (() => (
1786
+ // @ts-ignore
1787
+ typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
1788
+ ))();
1789
+ hexes2 = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
1775
1790
  Hash = class {
1776
1791
  };
1777
1792
  }
@@ -4601,13 +4616,13 @@ function hexToNumber2(hex) {
4601
4616
  throw new Error("hex string expected, got " + typeof hex);
4602
4617
  return hex === "" ? _0n2 : BigInt("0x" + hex);
4603
4618
  }
4604
- function bytesToHex2(bytes) {
4619
+ function bytesToHex3(bytes) {
4605
4620
  abytes2(bytes);
4606
- if (hasHexBuiltin)
4621
+ if (hasHexBuiltin2)
4607
4622
  return bytes.toHex();
4608
4623
  let hex = "";
4609
4624
  for (let i = 0; i < bytes.length; i++) {
4610
- hex += hexes2[bytes[i]];
4625
+ hex += hexes3[bytes[i]];
4611
4626
  }
4612
4627
  return hex;
4613
4628
  }
@@ -4623,7 +4638,7 @@ function asciiToBase16(ch) {
4623
4638
  function hexToBytes2(hex) {
4624
4639
  if (typeof hex !== "string")
4625
4640
  throw new Error("hex string expected, got " + typeof hex);
4626
- if (hasHexBuiltin)
4641
+ if (hasHexBuiltin2)
4627
4642
  return Uint8Array.fromHex(hex);
4628
4643
  const hl = hex.length;
4629
4644
  const al = hl / 2;
@@ -4642,11 +4657,11 @@ function hexToBytes2(hex) {
4642
4657
  return array;
4643
4658
  }
4644
4659
  function bytesToNumberBE(bytes) {
4645
- return hexToNumber2(bytesToHex2(bytes));
4660
+ return hexToNumber2(bytesToHex3(bytes));
4646
4661
  }
4647
4662
  function bytesToNumberLE(bytes) {
4648
4663
  abytes2(bytes);
4649
- return hexToNumber2(bytesToHex2(Uint8Array.from(bytes).reverse()));
4664
+ return hexToNumber2(bytesToHex3(Uint8Array.from(bytes).reverse()));
4650
4665
  }
4651
4666
  function numberToBytesBE(n, len) {
4652
4667
  return hexToBytes2(n.toString(16).padStart(len * 2, "0"));
@@ -4782,14 +4797,14 @@ function memoized(fn) {
4782
4797
  return computed;
4783
4798
  };
4784
4799
  }
4785
- var _0n2, _1n2, hasHexBuiltin, hexes2, asciis, isPosBig, bitMask, u8n, u8fr, validatorFns;
4800
+ var _0n2, _1n2, hasHexBuiltin2, hexes3, asciis, isPosBig, bitMask, u8n, u8fr, validatorFns;
4786
4801
  var init_utils4 = __esm({
4787
4802
  "../node_modules/@noble/curves/esm/abstract/utils.js"() {
4788
4803
  _0n2 = /* @__PURE__ */ BigInt(0);
4789
4804
  _1n2 = /* @__PURE__ */ BigInt(1);
4790
- hasHexBuiltin = // @ts-ignore
4805
+ hasHexBuiltin2 = // @ts-ignore
4791
4806
  typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function";
4792
- hexes2 = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
4807
+ hexes3 = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
4793
4808
  asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
4794
4809
  isPosBig = (n) => typeof n === "bigint" && _0n2 <= n;
4795
4810
  bitMask = (n) => (_1n2 << BigInt(n)) - _1n2;
@@ -5363,7 +5378,7 @@ function validatePointOpts(curve) {
5363
5378
  return Object.freeze({ ...opts });
5364
5379
  }
5365
5380
  function numToSizedHex(num2, size5) {
5366
- return bytesToHex2(numberToBytesBE(num2, size5));
5381
+ return bytesToHex3(numberToBytesBE(num2, size5));
5367
5382
  }
5368
5383
  function weierstrassPoints(opts) {
5369
5384
  const CURVE = validatePointOpts(opts);
@@ -5403,7 +5418,7 @@ function weierstrassPoints(opts) {
5403
5418
  const { allowedPrivateKeyLengths: lengths, nByteLength, wrapPrivateKey, n: N } = CURVE;
5404
5419
  if (lengths && typeof key !== "bigint") {
5405
5420
  if (isBytes2(key))
5406
- key = bytesToHex2(key);
5421
+ key = bytesToHex3(key);
5407
5422
  if (typeof key !== "string" || !lengths.includes(key.length))
5408
5423
  throw new Error("invalid private key");
5409
5424
  key = key.padStart(nByteLength * 2, "0");
@@ -5753,7 +5768,7 @@ function weierstrassPoints(opts) {
5753
5768
  }
5754
5769
  toHex(isCompressed = true) {
5755
5770
  abool("isCompressed", isCompressed);
5756
- return bytesToHex2(this.toRawBytes(isCompressed));
5771
+ return bytesToHex3(this.toRawBytes(isCompressed));
5757
5772
  }
5758
5773
  }
5759
5774
  Point2.BASE = new Point2(CURVE.Gx, CURVE.Gy, Fp.ONE);
@@ -7668,7 +7683,7 @@ function fromBoolean(value, options = {}) {
7668
7683
  function fromBytes(value, options = {}) {
7669
7684
  let string = "";
7670
7685
  for (let i = 0; i < value.length; i++)
7671
- string += hexes3[value[i]];
7686
+ string += hexes4[value[i]];
7672
7687
  const hex = `0x${string}`;
7673
7688
  if (typeof options.size === "number") {
7674
7689
  assertSize3(hex, options.size);
@@ -7757,14 +7772,14 @@ function validate2(value, options = {}) {
7757
7772
  return false;
7758
7773
  }
7759
7774
  }
7760
- var encoder4, hexes3, IntegerOutOfRangeError2, InvalidHexTypeError, InvalidHexValueError, SizeOverflowError3, SliceOffsetOutOfBoundsError3, SizeExceedsPaddingSizeError3;
7775
+ var encoder4, hexes4, IntegerOutOfRangeError2, InvalidHexTypeError, InvalidHexValueError, SizeOverflowError3, SliceOffsetOutOfBoundsError3, SizeExceedsPaddingSizeError3;
7761
7776
  var init_Hex = __esm({
7762
7777
  "../node_modules/ox/_esm/core/Hex.js"() {
7763
7778
  init_Errors();
7764
7779
  init_hex();
7765
7780
  init_Json();
7766
7781
  encoder4 = /* @__PURE__ */ new TextEncoder();
7767
- hexes3 = /* @__PURE__ */ Array.from({ length: 256 }, (_v, i) => i.toString(16).padStart(2, "0"));
7782
+ hexes4 = /* @__PURE__ */ Array.from({ length: 256 }, (_v, i) => i.toString(16).padStart(2, "0"));
7768
7783
  IntegerOutOfRangeError2 = class extends BaseError3 {
7769
7784
  constructor({ max, min, signed, size: size5, value }) {
7770
7785
  super(`Number \`${value}\` is not in safe${size5 ? ` ${size5 * 8}-bit` : ""}${signed ? " signed" : " unsigned"} integer range ${max ? `(\`${min}\` to \`${max}\`)` : `(above \`${min}\`)`}`);
@@ -8969,6 +8984,10 @@ var init_call = __esm({
8969
8984
  });
8970
8985
  var VolrContext = react.createContext(null);
8971
8986
  var InternalAuthContext = react.createContext(null);
8987
+ var ERROR_CODES = {
8988
+ AUTH_PROJECT_NOT_FOUND: "AUTH_PROJECT_NOT_FOUND",
8989
+ AUTH_PROJECT_REQUIRED: "AUTH_PROJECT_REQUIRED",
8990
+ ORIGIN_NOT_ALLOWED: "ORIGIN_NOT_ALLOWED"};
8972
8991
  function mapBackendError(error) {
8973
8992
  return new sdkCore.VolrError(error.code, error.message);
8974
8993
  }
@@ -9045,6 +9064,32 @@ var STORAGE_CHANNELS = {
9045
9064
  };
9046
9065
 
9047
9066
  // src/headless/client.ts
9067
+ var CONFIG_ERROR_CODES = [
9068
+ ERROR_CODES.AUTH_PROJECT_NOT_FOUND,
9069
+ ERROR_CODES.AUTH_PROJECT_REQUIRED,
9070
+ ERROR_CODES.ORIGIN_NOT_ALLOWED
9071
+ ];
9072
+ function logConfigurationError(errorCode, errorMessage) {
9073
+ const isDevelopment = typeof process !== "undefined" && process.env?.NODE_ENV === "development";
9074
+ const errorMessages = {
9075
+ [ERROR_CODES.AUTH_PROJECT_NOT_FOUND]: `[Volr SDK] \u274C Invalid projectApiKey: Project not found.
9076
+ Please verify your VolrProvider config.projectApiKey is correct.
9077
+ You can find your API key in the Volr Dashboard > Project Settings.`,
9078
+ [ERROR_CODES.AUTH_PROJECT_REQUIRED]: `[Volr SDK] \u274C Missing projectApiKey.
9079
+ Please provide projectApiKey in your VolrProvider config.
9080
+ Example: <VolrProvider config={{ projectApiKey: 'your-api-key', ... }}>`,
9081
+ [ERROR_CODES.ORIGIN_NOT_ALLOWED]: `[Volr SDK] \u274C Origin not allowed for this project.
9082
+ The current origin is not in your project's allowed origins list.
9083
+ Add this origin in Volr Dashboard > Project Settings > Security.`
9084
+ };
9085
+ const devMessage = errorMessages[errorCode] || `[Volr SDK] \u274C Configuration error: ${errorCode}
9086
+ ${errorMessage}`;
9087
+ if (isDevelopment) {
9088
+ console.error(devMessage);
9089
+ } else {
9090
+ console.error(`[Volr SDK] Configuration error: ${errorCode}. Check console in development mode for details.`);
9091
+ }
9092
+ }
9048
9093
  var APIClient = class {
9049
9094
  constructor(config) {
9050
9095
  this.refreshPromise = null;
@@ -9091,6 +9136,14 @@ var APIClient = class {
9091
9136
  async (error) => {
9092
9137
  const originalRequest = error.config;
9093
9138
  const requestUrl = originalRequest?.url || "";
9139
+ const responseData = error.response?.data;
9140
+ if (responseData && !responseData.ok && responseData.error) {
9141
+ const errorCode = responseData.error.code;
9142
+ const errorMessage = responseData.error.message;
9143
+ if (CONFIG_ERROR_CODES.includes(errorCode)) {
9144
+ logConfigurationError(errorCode, errorMessage);
9145
+ }
9146
+ }
9094
9147
  const publicAuthEndpoints = [
9095
9148
  "/auth/refresh",
9096
9149
  "/auth/siwe/nonce",
@@ -18614,6 +18667,34 @@ function useVolr() {
18614
18667
  error
18615
18668
  };
18616
18669
  }
18670
+ init_sha3();
18671
+ init_utils2();
18672
+ function toChecksumAddress(address) {
18673
+ const addr = address.toLowerCase().replace("0x", "");
18674
+ const hash3 = bytesToHex2(keccak_256(new TextEncoder().encode(addr)));
18675
+ let checksummed = "0x";
18676
+ for (let i = 0; i < addr.length; i++) {
18677
+ if (parseInt(hash3[i], 16) >= 8) {
18678
+ checksummed += addr[i].toUpperCase();
18679
+ } else {
18680
+ checksummed += addr[i];
18681
+ }
18682
+ }
18683
+ return checksummed;
18684
+ }
18685
+ function detectWalletConnector() {
18686
+ if (typeof window === "undefined" || !window.ethereum) {
18687
+ return void 0;
18688
+ }
18689
+ const provider = window.ethereum;
18690
+ if (provider.info?.rdns) return provider.info.rdns;
18691
+ if (provider.isMetaMask) return "io.metamask";
18692
+ if (provider.isCoinbaseWallet) return "com.coinbase.wallet";
18693
+ if (provider.isRabby) return "io.rabby";
18694
+ if (provider.isZerion) return "io.zerion";
18695
+ if (provider.isBraveWallet) return "com.brave.wallet";
18696
+ return "unknown";
18697
+ }
18617
18698
  function useVolrLogin() {
18618
18699
  const { config, setUser } = useVolrContext();
18619
18700
  const { setAccessToken, setRefreshToken, client } = useInternalAuth();
@@ -18743,15 +18824,84 @@ function useVolrLogin() {
18743
18824
  },
18744
18825
  [client, setAccessToken, setRefreshToken, setUser, toVolrUser]
18745
18826
  );
18827
+ const signWithWallet = react.useCallback(
18828
+ async (walletAddress) => {
18829
+ if (typeof window === "undefined" || !window.ethereum) {
18830
+ throw new Error("No Ethereum wallet found. Please install MetaMask or another wallet.");
18831
+ }
18832
+ const ethereum = window.ethereum;
18833
+ const chainIdHex = await ethereum.request({ method: "eth_chainId" });
18834
+ const chainId = parseInt(chainIdHex, 16);
18835
+ const nonce = await requestSiweNonce();
18836
+ const domain = window.location.host;
18837
+ const origin = window.location.origin;
18838
+ const statement = "Sign in with Ethereum to continue";
18839
+ const issuedAt = (/* @__PURE__ */ new Date()).toISOString();
18840
+ const checksumAddress2 = toChecksumAddress(walletAddress);
18841
+ const message = `${domain} wants you to sign in with your Ethereum account:
18842
+ ${checksumAddress2}
18843
+
18844
+ ${statement}
18845
+
18846
+ URI: ${origin}
18847
+ Version: 1
18848
+ Chain ID: ${chainId}
18849
+ Nonce: ${nonce}
18850
+ Issued At: ${issuedAt}`;
18851
+ const signature = await ethereum.request({
18852
+ method: "personal_sign",
18853
+ params: [message, walletAddress]
18854
+ });
18855
+ const walletConnector = detectWalletConnector();
18856
+ return verifySiweSignature(message, signature, { walletConnector, chainId });
18857
+ },
18858
+ [requestSiweNonce, verifySiweSignature]
18859
+ );
18746
18860
  const handlePasskeyComplete = react.useCallback(async () => {
18747
18861
  }, []);
18862
+ const createSiweSession = react.useCallback(async () => {
18863
+ const response = await client.post("/auth/siwe/session", {});
18864
+ return response;
18865
+ }, [client]);
18866
+ const checkSiweSession = react.useCallback(
18867
+ async (sessionId) => {
18868
+ const response = await client.get(`/auth/siwe/session/${sessionId}`);
18869
+ if (response.status === "completed" && response.tokens && response.user) {
18870
+ setAccessToken(response.tokens.accessToken);
18871
+ setRefreshToken(response.tokens.refreshToken);
18872
+ setUser(toVolrUser(response.user));
18873
+ }
18874
+ return response;
18875
+ },
18876
+ [client, setAccessToken, setRefreshToken, setUser, toVolrUser]
18877
+ );
18878
+ const getSiweSignUrl = react.useCallback(
18879
+ (sessionId) => {
18880
+ const accentColor = config.accentColor || "#303030";
18881
+ const params = new URLSearchParams({
18882
+ session: sessionId,
18883
+ accentColor
18884
+ });
18885
+ if (apiBaseUrl.includes("dev-api") || apiBaseUrl.includes("localhost")) {
18886
+ params.set("env", "dev");
18887
+ }
18888
+ const lang = typeof navigator !== "undefined" ? navigator.language?.startsWith("ko") ? "ko" : "en" : "en";
18889
+ params.set("lang", lang);
18890
+ return `https://sign.volr.io?${params.toString()}`;
18891
+ },
18892
+ [apiBaseUrl, config]
18893
+ );
18748
18894
  return {
18749
18895
  requestEmailCode,
18750
18896
  verifyEmailCode,
18751
18897
  handleSocialLogin,
18752
18898
  requestSiweNonce,
18753
18899
  verifySiweSignature,
18754
- handlePasskeyComplete
18900
+ signWithWallet,
18901
+ handlePasskeyComplete,
18902
+ createSiweSession,
18903
+ checkSiweSession,
18904
+ getSiweSignUrl
18755
18905
  };
18756
18906
  }
18757
18907
  function useVolrAuthCallback(options = {}) {