@volr/react 0.1.83 → 0.1.84

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}\`)`}`);
@@ -18614,6 +18629,34 @@ function useVolr() {
18614
18629
  error
18615
18630
  };
18616
18631
  }
18632
+ init_sha3();
18633
+ init_utils2();
18634
+ function toChecksumAddress(address) {
18635
+ const addr = address.toLowerCase().replace("0x", "");
18636
+ const hash3 = bytesToHex2(keccak_256(new TextEncoder().encode(addr)));
18637
+ let checksummed = "0x";
18638
+ for (let i = 0; i < addr.length; i++) {
18639
+ if (parseInt(hash3[i], 16) >= 8) {
18640
+ checksummed += addr[i].toUpperCase();
18641
+ } else {
18642
+ checksummed += addr[i];
18643
+ }
18644
+ }
18645
+ return checksummed;
18646
+ }
18647
+ function detectWalletConnector() {
18648
+ if (typeof window === "undefined" || !window.ethereum) {
18649
+ return void 0;
18650
+ }
18651
+ const provider = window.ethereum;
18652
+ if (provider.info?.rdns) return provider.info.rdns;
18653
+ if (provider.isMetaMask) return "io.metamask";
18654
+ if (provider.isCoinbaseWallet) return "com.coinbase.wallet";
18655
+ if (provider.isRabby) return "io.rabby";
18656
+ if (provider.isZerion) return "io.zerion";
18657
+ if (provider.isBraveWallet) return "com.brave.wallet";
18658
+ return "unknown";
18659
+ }
18617
18660
  function useVolrLogin() {
18618
18661
  const { config, setUser } = useVolrContext();
18619
18662
  const { setAccessToken, setRefreshToken, client } = useInternalAuth();
@@ -18743,15 +18786,84 @@ function useVolrLogin() {
18743
18786
  },
18744
18787
  [client, setAccessToken, setRefreshToken, setUser, toVolrUser]
18745
18788
  );
18789
+ const signWithWallet = react.useCallback(
18790
+ async (walletAddress) => {
18791
+ if (typeof window === "undefined" || !window.ethereum) {
18792
+ throw new Error("No Ethereum wallet found. Please install MetaMask or another wallet.");
18793
+ }
18794
+ const ethereum = window.ethereum;
18795
+ const chainIdHex = await ethereum.request({ method: "eth_chainId" });
18796
+ const chainId = parseInt(chainIdHex, 16);
18797
+ const nonce = await requestSiweNonce();
18798
+ const domain = window.location.host;
18799
+ const origin = window.location.origin;
18800
+ const statement = "Sign in with Ethereum to continue";
18801
+ const issuedAt = (/* @__PURE__ */ new Date()).toISOString();
18802
+ const checksumAddress2 = toChecksumAddress(walletAddress);
18803
+ const message = `${domain} wants you to sign in with your Ethereum account:
18804
+ ${checksumAddress2}
18805
+
18806
+ ${statement}
18807
+
18808
+ URI: ${origin}
18809
+ Version: 1
18810
+ Chain ID: ${chainId}
18811
+ Nonce: ${nonce}
18812
+ Issued At: ${issuedAt}`;
18813
+ const signature = await ethereum.request({
18814
+ method: "personal_sign",
18815
+ params: [message, walletAddress]
18816
+ });
18817
+ const walletConnector = detectWalletConnector();
18818
+ return verifySiweSignature(message, signature, { walletConnector, chainId });
18819
+ },
18820
+ [requestSiweNonce, verifySiweSignature]
18821
+ );
18746
18822
  const handlePasskeyComplete = react.useCallback(async () => {
18747
18823
  }, []);
18824
+ const createSiweSession = react.useCallback(async () => {
18825
+ const response = await client.post("/auth/siwe/session", {});
18826
+ return response;
18827
+ }, [client]);
18828
+ const checkSiweSession = react.useCallback(
18829
+ async (sessionId) => {
18830
+ const response = await client.get(`/auth/siwe/session/${sessionId}`);
18831
+ if (response.status === "completed" && response.tokens && response.user) {
18832
+ setAccessToken(response.tokens.accessToken);
18833
+ setRefreshToken(response.tokens.refreshToken);
18834
+ setUser(toVolrUser(response.user));
18835
+ }
18836
+ return response;
18837
+ },
18838
+ [client, setAccessToken, setRefreshToken, setUser, toVolrUser]
18839
+ );
18840
+ const getSiweSignUrl = react.useCallback(
18841
+ (sessionId) => {
18842
+ const accentColor = config.accentColor || "#303030";
18843
+ const params = new URLSearchParams({
18844
+ session: sessionId,
18845
+ accentColor
18846
+ });
18847
+ if (apiBaseUrl.includes("dev-api") || apiBaseUrl.includes("localhost")) {
18848
+ params.set("env", "dev");
18849
+ }
18850
+ const lang = typeof navigator !== "undefined" ? navigator.language?.startsWith("ko") ? "ko" : "en" : "en";
18851
+ params.set("lang", lang);
18852
+ return `https://sign.volr.io?${params.toString()}`;
18853
+ },
18854
+ [apiBaseUrl, config]
18855
+ );
18748
18856
  return {
18749
18857
  requestEmailCode,
18750
18858
  verifyEmailCode,
18751
18859
  handleSocialLogin,
18752
18860
  requestSiweNonce,
18753
18861
  verifySiweSignature,
18754
- handlePasskeyComplete
18862
+ signWithWallet,
18863
+ handlePasskeyComplete,
18864
+ createSiweSession,
18865
+ checkSiweSession,
18866
+ getSiweSignUrl
18755
18867
  };
18756
18868
  }
18757
18869
  function useVolrAuthCallback(options = {}) {