import { j as y, g as Qe, w as Ze, x as Xe, y as et } from "./en_US-M4RYPLCZ-Dn9HWCHX.js";
import { b as tt } from "./sha3-DjQmYPLK.js";
import { c as U } from "./clsx.m-DCYaa4RU.js";
import { E as X, _ as w, d as le, y as nt } from "./hooks.module-DtITtF4G.js";
class A {
  constructor(e, t) {
    this.scope = e, this.module = t;
  }
  storeObject(e, t) {
    this.setItem(e, JSON.stringify(t));
  }
  loadObject(e) {
    const t = this.getItem(e);
    return t ? JSON.parse(t) : void 0;
  }
  setItem(e, t) {
    localStorage.setItem(this.scopedKey(e), t);
  }
  getItem(e) {
    return localStorage.getItem(this.scopedKey(e));
  }
  removeItem(e) {
    localStorage.removeItem(this.scopedKey(e));
  }
  clear() {
    const e = this.scopedKey(""), t = [];
    for (let s = 0; s < localStorage.length; s++) {
      const r = localStorage.key(s);
      typeof r == "string" && r.startsWith(e) && t.push(r);
    }
    t.forEach((s) => localStorage.removeItem(s));
  }
  scopedKey(e) {
    return `-${this.scope}${this.module ? `:${this.module}` : ""}:${e}`;
  }
  static clearAll() {
    new A("CBWSDK").clear(), new A("walletlink").clear();
  }
}
const v = {
  rpc: {
    invalidInput: -32e3,
    resourceNotFound: -32001,
    resourceUnavailable: -32002,
    transactionRejected: -32003,
    methodNotSupported: -32004,
    limitExceeded: -32005,
    parse: -32700,
    invalidRequest: -32600,
    methodNotFound: -32601,
    invalidParams: -32602,
    internal: -32603
  },
  provider: {
    userRejectedRequest: 4001,
    unauthorized: 4100,
    unsupportedMethod: 4200,
    disconnected: 4900,
    chainDisconnected: 4901,
    unsupportedChain: 4902
  }
}, ee = {
  "-32700": {
    standard: "JSON RPC 2.0",
    message: "Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text."
  },
  "-32600": {
    standard: "JSON RPC 2.0",
    message: "The JSON sent is not a valid Request object."
  },
  "-32601": {
    standard: "JSON RPC 2.0",
    message: "The method does not exist / is not available."
  },
  "-32602": {
    standard: "JSON RPC 2.0",
    message: "Invalid method parameter(s)."
  },
  "-32603": {
    standard: "JSON RPC 2.0",
    message: "Internal JSON-RPC error."
  },
  "-32000": {
    standard: "EIP-1474",
    message: "Invalid input."
  },
  "-32001": {
    standard: "EIP-1474",
    message: "Resource not found."
  },
  "-32002": {
    standard: "EIP-1474",
    message: "Resource unavailable."
  },
  "-32003": {
    standard: "EIP-1474",
    message: "Transaction rejected."
  },
  "-32004": {
    standard: "EIP-1474",
    message: "Method not supported."
  },
  "-32005": {
    standard: "EIP-1474",
    message: "Request limit exceeded."
  },
  4001: {
    standard: "EIP-1193",
    message: "User rejected the request."
  },
  4100: {
    standard: "EIP-1193",
    message: "The requested account and/or method has not been authorized by the user."
  },
  4200: {
    standard: "EIP-1193",
    message: "The requested method is not supported by this Ethereum provider."
  },
  4900: {
    standard: "EIP-1193",
    message: "The provider is disconnected from all chains."
  },
  4901: {
    standard: "EIP-1193",
    message: "The provider is disconnected from the specified chain."
  },
  4902: {
    standard: "EIP-3085",
    message: "Unrecognized chain ID."
  }
}, Re = "Unspecified error message.", st = "Unspecified server error.";
function ie(n, e = Re) {
  if (n && Number.isInteger(n)) {
    const t = n.toString();
    if (te(ee, t))
      return ee[t].message;
    if (Pe(n))
      return st;
  }
  return e;
}
function it(n) {
  if (!Number.isInteger(n))
    return !1;
  const e = n.toString();
  return !!(ee[e] || Pe(n));
}
function rt(n, { shouldIncludeStack: e = !1 } = {}) {
  const t = {};
  if (n && typeof n == "object" && !Array.isArray(n) && te(n, "code") && it(n.code)) {
    const s = n;
    t.code = s.code, s.message && typeof s.message == "string" ? (t.message = s.message, te(s, "data") && (t.data = s.data)) : (t.message = ie(t.code), t.data = { originalError: ue(n) });
  } else
    t.code = v.rpc.internal, t.message = he(n, "message") ? n.message : Re, t.data = { originalError: ue(n) };
  return e && (t.stack = he(n, "stack") ? n.stack : void 0), t;
}
function Pe(n) {
  return n >= -32099 && n <= -32e3;
}
function ue(n) {
  return n && typeof n == "object" && !Array.isArray(n) ? Object.assign({}, n) : n;
}
function te(n, e) {
  return Object.prototype.hasOwnProperty.call(n, e);
}
function he(n, e) {
  return typeof n == "object" && n !== null && e in n && typeof n[e] == "string";
}
const m = {
  rpc: {
    parse: (n) => C(v.rpc.parse, n),
    invalidRequest: (n) => C(v.rpc.invalidRequest, n),
    invalidParams: (n) => C(v.rpc.invalidParams, n),
    methodNotFound: (n) => C(v.rpc.methodNotFound, n),
    internal: (n) => C(v.rpc.internal, n),
    server: (n) => {
      if (!n || typeof n != "object" || Array.isArray(n))
        throw new Error("Ethereum RPC Server errors must provide single object argument.");
      const { code: e } = n;
      if (!Number.isInteger(e) || e > -32005 || e < -32099)
        throw new Error('"code" must be an integer such that: -32099 <= code <= -32005');
      return C(e, n);
    },
    invalidInput: (n) => C(v.rpc.invalidInput, n),
    resourceNotFound: (n) => C(v.rpc.resourceNotFound, n),
    resourceUnavailable: (n) => C(v.rpc.resourceUnavailable, n),
    transactionRejected: (n) => C(v.rpc.transactionRejected, n),
    methodNotSupported: (n) => C(v.rpc.methodNotSupported, n),
    limitExceeded: (n) => C(v.rpc.limitExceeded, n)
  },
  provider: {
    userRejectedRequest: (n) => D(v.provider.userRejectedRequest, n),
    unauthorized: (n) => D(v.provider.unauthorized, n),
    unsupportedMethod: (n) => D(v.provider.unsupportedMethod, n),
    disconnected: (n) => D(v.provider.disconnected, n),
    chainDisconnected: (n) => D(v.provider.chainDisconnected, n),
    unsupportedChain: (n) => D(v.provider.unsupportedChain, n),
    custom: (n) => {
      if (!n || typeof n != "object" || Array.isArray(n))
        throw new Error("Ethereum Provider custom errors must provide single object argument.");
      const { code: e, message: t, data: s } = n;
      if (!t || typeof t != "string")
        throw new Error('"message" must be a nonempty string');
      return new Ne(e, t, s);
    }
  }
};
function C(n, e) {
  const [t, s] = Le(e);
  return new Te(n, t || ie(n), s);
}
function D(n, e) {
  const [t, s] = Le(e);
  return new Ne(n, t || ie(n), s);
}
function Le(n) {
  if (n) {
    if (typeof n == "string")
      return [n];
    if (typeof n == "object" && !Array.isArray(n)) {
      const { message: e, data: t } = n;
      if (e && typeof e != "string")
        throw new Error("Must specify string message.");
      return [e || void 0, t];
    }
  }
  return [];
}
class Te extends Error {
  constructor(e, t, s) {
    if (!Number.isInteger(e))
      throw new Error('"code" must be an integer.');
    if (!t || typeof t != "string")
      throw new Error('"message" must be a nonempty string.');
    super(t), this.code = e, s !== void 0 && (this.data = s);
  }
}
class Ne extends Te {
  /**
   * Create an Ethereum Provider JSON-RPC error.
   * `code` must be an integer in the 1000 <= 4999 range.
   */
  constructor(e, t, s) {
    if (!at(e))
      throw new Error('"code" must be an integer such that: 1000 <= code <= 4999');
    super(e, t, s);
  }
}
function at(n) {
  return Number.isInteger(n) && n >= 1e3 && n <= 4999;
}
function re() {
  return (n) => n;
}
const K = re(), ot = re(), ct = re();
function x(n) {
  return Math.floor(n);
}
const De = /^[0-9]*$/, Oe = /^[a-f0-9]*$/;
function N(n) {
  return ae(crypto.getRandomValues(new Uint8Array(n)));
}
function ae(n) {
  return [...n].map((e) => e.toString(16).padStart(2, "0")).join("");
}
function F(n) {
  return new Uint8Array(n.match(/.{1,2}/g).map((e) => Number.parseInt(e, 16)));
}
function W(n, e = !1) {
  const t = n.toString("hex");
  return K(e ? `0x${t}` : t);
}
function G(n) {
  return W(ne(n), !0);
}
function M(n) {
  return ct(n.toString(10));
}
function T(n) {
  return K(`0x${BigInt(n).toString(16)}`);
}
function je(n) {
  return n.startsWith("0x") || n.startsWith("0X");
}
function oe(n) {
  return je(n) ? n.slice(2) : n;
}
function Ue(n) {
  return je(n) ? `0x${n.slice(2)}` : `0x${n}`;
}
function H(n) {
  if (typeof n != "string")
    return !1;
  const e = oe(n).toLowerCase();
  return Oe.test(e);
}
function dt(n, e = !1) {
  if (typeof n == "string") {
    const t = oe(n).toLowerCase();
    if (Oe.test(t))
      return K(e ? `0x${t}` : t);
  }
  throw m.rpc.invalidParams(`"${String(n)}" is not a hexadecimal string`);
}
function ce(n, e = !1) {
  let t = dt(n, !1);
  return t.length % 2 === 1 && (t = K(`0${t}`)), e ? K(`0x${t}`) : t;
}
function L(n) {
  if (typeof n == "string") {
    const e = oe(n).toLowerCase();
    if (H(e) && e.length === 40)
      return ot(Ue(e));
  }
  throw m.rpc.invalidParams(`Invalid Ethereum address: ${String(n)}`);
}
function ne(n) {
  if (y.isBuffer(n))
    return n;
  if (typeof n == "string") {
    if (H(n)) {
      const e = ce(n, !1);
      return y.from(e, "hex");
    }
    return y.from(n, "utf8");
  }
  throw m.rpc.invalidParams(`Not binary data: ${String(n)}`);
}
function q(n) {
  if (typeof n == "number" && Number.isInteger(n))
    return x(n);
  if (typeof n == "string") {
    if (De.test(n))
      return x(Number(n));
    if (H(n))
      return x(Number(BigInt(ce(n, !0))));
  }
  throw m.rpc.invalidParams(`Not an integer: ${String(n)}`);
}
function j(n) {
  if (n !== null && (typeof n == "bigint" || ut(n)))
    return BigInt(n.toString(10));
  if (typeof n == "number")
    return BigInt(q(n));
  if (typeof n == "string") {
    if (De.test(n))
      return BigInt(n);
    if (H(n))
      return BigInt(ce(n, !0));
  }
  throw m.rpc.invalidParams(`Not an integer: ${String(n)}`);
}
function lt(n) {
  if (typeof n == "string")
    return JSON.parse(n);
  if (typeof n == "object")
    return n;
  throw m.rpc.invalidParams(`Not a JSON string or an object: ${String(n)}`);
}
function ut(n) {
  if (n == null || typeof n.constructor != "function")
    return !1;
  const { constructor: e } = n;
  return typeof e.config == "function" && typeof e.EUCLID == "number";
}
async function ht() {
  return crypto.subtle.generateKey({
    name: "ECDH",
    namedCurve: "P-256"
  }, !0, ["deriveKey"]);
}
async function pt(n, e) {
  return crypto.subtle.deriveKey({
    name: "ECDH",
    public: e
  }, n, {
    name: "AES-GCM",
    length: 256
  }, !1, ["encrypt", "decrypt"]);
}
async function ft(n, e) {
  const t = crypto.getRandomValues(new Uint8Array(12)), s = await crypto.subtle.encrypt({
    name: "AES-GCM",
    iv: t
  }, n, new TextEncoder().encode(e));
  return { iv: t, cipherText: s };
}
async function gt(n, { iv: e, cipherText: t }) {
  const s = await crypto.subtle.decrypt({
    name: "AES-GCM",
    iv: e
  }, n, t);
  return new TextDecoder().decode(s);
}
function We(n) {
  switch (n) {
    case "public":
      return "spki";
    case "private":
      return "pkcs8";
  }
}
async function qe(n, e) {
  const t = We(n), s = await crypto.subtle.exportKey(t, e);
  return ae(new Uint8Array(s));
}
async function Ke(n, e) {
  const t = We(n), s = F(e).buffer;
  return await crypto.subtle.importKey(t, new Uint8Array(s), {
    name: "ECDH",
    namedCurve: "P-256"
  }, !0, n === "private" ? ["deriveKey"] : []);
}
async function mt(n, e) {
  const t = JSON.stringify(n, (s, r) => {
    if (!(r instanceof Error))
      return r;
    const i = r;
    return Object.assign(Object.assign({}, i.code ? { code: i.code } : {}), { message: i.message });
  });
  return ft(e, t);
}
async function bt(n, e) {
  return JSON.parse(await gt(e, n));
}
const Y = {
  storageKey: "ownPrivateKey",
  keyType: "private"
}, $ = {
  storageKey: "ownPublicKey",
  keyType: "public"
}, J = {
  storageKey: "peerPublicKey",
  keyType: "public"
};
class wt {
  constructor() {
    this.storage = new A("CBWSDK", "SCWKeyManager"), this.ownPrivateKey = null, this.ownPublicKey = null, this.peerPublicKey = null, this.sharedSecret = null;
  }
  async getOwnPublicKey() {
    return await this.loadKeysIfNeeded(), this.ownPublicKey;
  }
  // returns null if the shared secret is not yet derived
  async getSharedSecret() {
    return await this.loadKeysIfNeeded(), this.sharedSecret;
  }
  async setPeerPublicKey(e) {
    this.sharedSecret = null, this.peerPublicKey = e, await this.storeKey(J, e), await this.loadKeysIfNeeded();
  }
  async clear() {
    this.ownPrivateKey = null, this.ownPublicKey = null, this.peerPublicKey = null, this.sharedSecret = null, this.storage.removeItem($.storageKey), this.storage.removeItem(Y.storageKey), this.storage.removeItem(J.storageKey);
  }
  async generateKeyPair() {
    const e = await ht();
    this.ownPrivateKey = e.privateKey, this.ownPublicKey = e.publicKey, await this.storeKey(Y, e.privateKey), await this.storeKey($, e.publicKey);
  }
  async loadKeysIfNeeded() {
    if (this.ownPrivateKey === null && (this.ownPrivateKey = await this.loadKey(Y)), this.ownPublicKey === null && (this.ownPublicKey = await this.loadKey($)), (this.ownPrivateKey === null || this.ownPublicKey === null) && await this.generateKeyPair(), this.peerPublicKey === null && (this.peerPublicKey = await this.loadKey(J)), this.sharedSecret === null) {
      if (this.ownPrivateKey === null || this.peerPublicKey === null)
        return;
      this.sharedSecret = await pt(this.ownPrivateKey, this.peerPublicKey);
    }
  }
  // storage methods
  async loadKey(e) {
    const t = this.storage.getItem(e.storageKey);
    return t ? Ke(e.keyType, t) : null;
  }
  async storeKey(e, t) {
    const s = await qe(e.keyType, t);
    this.storage.setItem(e.storageKey, s);
  }
}
const z = "4.3.0", ze = "@coinbase/wallet-sdk";
async function de(n, e) {
  const t = Object.assign(Object.assign({}, n), { jsonrpc: "2.0", id: crypto.randomUUID() }), s = await window.fetch(e, {
    method: "POST",
    body: JSON.stringify(t),
    mode: "cors",
    headers: {
      "Content-Type": "application/json",
      "X-Cbw-Sdk-Version": z,
      "X-Cbw-Sdk-Platform": ze
    }
  }), { result: r, error: i } = await s.json();
  if (i)
    throw i;
  return r;
}
function yt() {
  return globalThis.coinbaseWalletExtension;
}
function vt() {
  var n, e;
  try {
    const t = globalThis;
    return (n = t.ethereum) !== null && n !== void 0 ? n : (e = t.top) === null || e === void 0 ? void 0 : e.ethereum;
  } catch {
    return;
  }
}
function kt({ metadata: n, preference: e }) {
  var t, s;
  const { appName: r, appLogoUrl: i, appChainIds: a } = n;
  if (e.options !== "smartWalletOnly") {
    const l = yt();
    if (l)
      return (t = l.setAppInfo) === null || t === void 0 || t.call(l, r, i, a, e), l;
  }
  const c = vt();
  if (c != null && c.isCoinbaseBrowser)
    return (s = c.setAppInfo) === null || s === void 0 || s.call(c, r, i, a, e), c;
}
function Et(n) {
  if (!n || typeof n != "object" || Array.isArray(n))
    throw m.rpc.invalidParams({
      message: "Expected a single, non-array, object argument.",
      data: n
    });
  const { method: e, params: t } = n;
  if (typeof e != "string" || e.length === 0)
    throw m.rpc.invalidParams({
      message: "'args.method' must be a non-empty string.",
      data: n
    });
  if (t !== void 0 && !Array.isArray(t) && (typeof t != "object" || t === null))
    throw m.rpc.invalidParams({
      message: "'args.params' must be an object or array if provided.",
      data: n
    });
  switch (e) {
    case "eth_sign":
    case "eth_signTypedData_v2":
    case "eth_subscribe":
    case "eth_unsubscribe":
      throw m.provider.unsupportedMethod();
  }
}
const pe = "accounts", fe = "activeChain", ge = "availableChains", me = "walletCapabilities";
class It {
  constructor(e) {
    var t, s, r;
    this.metadata = e.metadata, this.communicator = e.communicator, this.callback = e.callback, this.keyManager = new wt(), this.storage = new A("CBWSDK", "SCWStateManager"), this.accounts = (t = this.storage.loadObject(pe)) !== null && t !== void 0 ? t : [], this.chain = this.storage.loadObject(fe) || {
      id: (r = (s = e.metadata.appChainIds) === null || s === void 0 ? void 0 : s[0]) !== null && r !== void 0 ? r : 1
    }, this.handshake = this.handshake.bind(this), this.request = this.request.bind(this), this.createRequestMessage = this.createRequestMessage.bind(this), this.decryptResponseMessage = this.decryptResponseMessage.bind(this);
  }
  async handshake(e) {
    var t, s, r, i;
    await ((s = (t = this.communicator).waitForPopupLoaded) === null || s === void 0 ? void 0 : s.call(t));
    const a = await this.createRequestMessage({
      handshake: {
        method: e.method,
        params: Object.assign({}, this.metadata, (r = e.params) !== null && r !== void 0 ? r : {})
      }
    }), c = await this.communicator.postRequestAndWaitForResponse(a);
    if ("failure" in c.content)
      throw c.content.failure;
    const l = await Ke("public", c.sender);
    await this.keyManager.setPeerPublicKey(l);
    const p = (await this.decryptResponseMessage(c)).result;
    if ("error" in p)
      throw p.error;
    switch (e.method) {
      case "eth_requestAccounts": {
        const f = p.value;
        this.accounts = f, this.storage.storeObject(pe, f), (i = this.callback) === null || i === void 0 || i.call(this, "accountsChanged", f);
        break;
      }
    }
  }
  async request(e) {
    var t;
    if (this.accounts.length === 0)
      switch (e.method) {
        case "wallet_sendCalls":
          return this.sendRequestToPopup(e);
        default:
          throw m.provider.unauthorized();
      }
    switch (e.method) {
      case "eth_requestAccounts":
        return (t = this.callback) === null || t === void 0 || t.call(this, "connect", { chainId: T(this.chain.id) }), this.accounts;
      case "eth_accounts":
        return this.accounts;
      case "eth_coinbase":
        return this.accounts[0];
      case "net_version":
        return this.chain.id;
      case "eth_chainId":
        return T(this.chain.id);
      case "wallet_getCapabilities":
        return this.storage.loadObject(me);
      case "wallet_switchEthereumChain":
        return this.handleSwitchChainRequest(e);
      case "eth_ecRecover":
      case "personal_sign":
      case "wallet_sign":
      case "personal_ecRecover":
      case "eth_signTransaction":
      case "eth_sendTransaction":
      case "eth_signTypedData_v1":
      case "eth_signTypedData_v3":
      case "eth_signTypedData_v4":
      case "eth_signTypedData":
      case "wallet_addEthereumChain":
      case "wallet_watchAsset":
      case "wallet_sendCalls":
      case "wallet_showCallsStatus":
      case "wallet_grantPermissions":
        return this.sendRequestToPopup(e);
      default:
        if (!this.chain.rpcUrl)
          throw m.rpc.internal("No RPC URL set for chain");
        return de(e, this.chain.rpcUrl);
    }
  }
  async sendRequestToPopup(e) {
    var t, s;
    await ((s = (t = this.communicator).waitForPopupLoaded) === null || s === void 0 ? void 0 : s.call(t));
    const r = await this.sendEncryptedRequest(e), a = (await this.decryptResponseMessage(r)).result;
    if ("error" in a)
      throw a.error;
    return a.value;
  }
  async cleanup() {
    var e, t;
    this.storage.clear(), await this.keyManager.clear(), this.accounts = [], this.chain = {
      id: (t = (e = this.metadata.appChainIds) === null || e === void 0 ? void 0 : e[0]) !== null && t !== void 0 ? t : 1
    };
  }
  /**
   * @returns `null` if the request was successful.
   * https://eips.ethereum.org/EIPS/eip-3326#wallet_switchethereumchain
   */
  async handleSwitchChainRequest(e) {
    var t;
    const s = e.params;
    if (!s || !(!((t = s[0]) === null || t === void 0) && t.chainId))
      throw m.rpc.invalidParams();
    const r = q(s[0].chainId);
    if (this.updateChain(r))
      return null;
    const a = await this.sendRequestToPopup(e);
    return a === null && this.updateChain(r), a;
  }
  async sendEncryptedRequest(e) {
    const t = await this.keyManager.getSharedSecret();
    if (!t)
      throw m.provider.unauthorized("No valid session found, try requestAccounts before other methods");
    const s = await mt({
      action: e,
      chainId: this.chain.id
    }, t), r = await this.createRequestMessage({ encrypted: s });
    return this.communicator.postRequestAndWaitForResponse(r);
  }
  async createRequestMessage(e) {
    const t = await qe("public", await this.keyManager.getOwnPublicKey());
    return {
      id: crypto.randomUUID(),
      sender: t,
      content: e,
      timestamp: /* @__PURE__ */ new Date()
    };
  }
  async decryptResponseMessage(e) {
    var t, s;
    const r = e.content;
    if ("failure" in r)
      throw r.failure;
    const i = await this.keyManager.getSharedSecret();
    if (!i)
      throw m.provider.unauthorized("Invalid session");
    const a = await bt(r.encrypted, i), c = (t = a.data) === null || t === void 0 ? void 0 : t.chains;
    if (c) {
      const h = Object.entries(c).map(([p, f]) => ({
        id: Number(p),
        rpcUrl: f
      }));
      this.storage.storeObject(ge, h), this.updateChain(this.chain.id, h);
    }
    const l = (s = a.data) === null || s === void 0 ? void 0 : s.capabilities;
    return l && this.storage.storeObject(me, l), a;
  }
  updateChain(e, t) {
    var s;
    const r = t ?? this.storage.loadObject(ge), i = r == null ? void 0 : r.find((a) => a.id === e);
    return i ? (i !== this.chain && (this.chain = i, this.storage.storeObject(fe, i), (s = this.callback) === null || s === void 0 || s.call(this, "chainChanged", T(i.id))), !0) : !1;
  }
}
var V, be;
function Be() {
  if (be) return V;
  be = 1;
  const { keccak_256: n } = /* @__PURE__ */ tt();
  function e(o) {
    return y.allocUnsafe(o).fill(0);
  }
  function t(o) {
    return o.toString(2).length;
  }
  function s(o, u) {
    let b = o.toString(16);
    b.length % 2 !== 0 && (b = "0" + b);
    const g = b.match(/.{1,2}/g).map((k) => parseInt(k, 16));
    for (; g.length < u; )
      g.unshift(0);
    return y.from(g);
  }
  function r(o, u) {
    const b = o < 0n;
    let g;
    if (b) {
      const k = (1n << BigInt(u)) - 1n;
      g = (~o & k) + 1n;
    } else
      g = o;
    return g &= (1n << BigInt(u)) - 1n, g;
  }
  function i(o, u, b) {
    const g = e(u);
    return o = c(o), b ? o.length < u ? (o.copy(g), g) : o.slice(0, u) : o.length < u ? (o.copy(g, u - o.length), g) : o.slice(-u);
  }
  function a(o, u) {
    return i(o, u, !0);
  }
  function c(o) {
    if (!y.isBuffer(o))
      if (Array.isArray(o))
        o = y.from(o);
      else if (typeof o == "string")
        f(o) ? o = y.from(p(d(o)), "hex") : o = y.from(o);
      else if (typeof o == "number")
        o = intToBuffer(o);
      else if (o == null)
        o = y.allocUnsafe(0);
      else if (typeof o == "bigint")
        o = s(o);
      else if (o.toArray)
        o = y.from(o.toArray());
      else
        throw new Error("invalid type");
    return o;
  }
  function l(o) {
    return o = c(o), "0x" + o.toString("hex");
  }
  function h(o, u) {
    if (o = c(o), u || (u = 256), u !== 256)
      throw new Error("unsupported");
    return y.from(n(new Uint8Array(o)));
  }
  function p(o) {
    return o.length % 2 ? "0" + o : o;
  }
  function f(o) {
    return typeof o == "string" && o.match(/^0x[0-9A-Fa-f]*$/);
  }
  function d(o) {
    return typeof o == "string" && o.startsWith("0x") ? o.slice(2) : o;
  }
  return V = {
    zeros: e,
    setLength: i,
    setLengthRight: a,
    isHexString: f,
    stripHexPrefix: d,
    toBuffer: c,
    bufferToHex: l,
    keccak: h,
    bitLengthFromBigInt: t,
    bufferBEFromBigInt: s,
    twosFromBigInt: r
  }, V;
}
var Q, we;
function St() {
  if (we) return Q;
  we = 1;
  const n = /* @__PURE__ */ Be();
  function e(d) {
    return d.startsWith("int[") ? "int256" + d.slice(3) : d === "int" ? "int256" : d.startsWith("uint[") ? "uint256" + d.slice(4) : d === "uint" ? "uint256" : d.startsWith("fixed[") ? "fixed128x128" + d.slice(5) : d === "fixed" ? "fixed128x128" : d.startsWith("ufixed[") ? "ufixed128x128" + d.slice(6) : d === "ufixed" ? "ufixed128x128" : d;
  }
  function t(d) {
    return Number.parseInt(/^\D+(\d+)$/.exec(d)[1], 10);
  }
  function s(d) {
    var o = /^\D+(\d+)x(\d+)$/.exec(d);
    return [Number.parseInt(o[1], 10), Number.parseInt(o[2], 10)];
  }
  function r(d) {
    var o = d.match(/(.*)\[(.*?)\]$/);
    return o ? o[2] === "" ? "dynamic" : Number.parseInt(o[2], 10) : null;
  }
  function i(d) {
    var o = typeof d;
    if (o === "string" || o === "number")
      return BigInt(d);
    if (o === "bigint")
      return d;
    throw new Error("Argument is not a number");
  }
  function a(d, o) {
    var u, b, g, k;
    if (d === "address")
      return a("uint160", i(o));
    if (d === "bool")
      return a("uint8", o ? 1 : 0);
    if (d === "string")
      return a("bytes", new y(o, "utf8"));
    if (l(d)) {
      if (typeof o.length > "u")
        throw new Error("Not an array?");
      if (u = r(d), u !== "dynamic" && u !== 0 && o.length > u)
        throw new Error("Elements exceed array size: " + u);
      g = [], d = d.slice(0, d.lastIndexOf("[")), typeof o == "string" && (o = JSON.parse(o));
      for (k in o)
        g.push(a(d, o[k]));
      if (u === "dynamic") {
        var I = a("uint256", o.length);
        g.unshift(I);
      }
      return y.concat(g);
    } else {
      if (d === "bytes")
        return o = new y(o), g = y.concat([a("uint256", o.length), o]), o.length % 32 !== 0 && (g = y.concat([g, n.zeros(32 - o.length % 32)])), g;
      if (d.startsWith("bytes")) {
        if (u = t(d), u < 1 || u > 32)
          throw new Error("Invalid bytes<N> width: " + u);
        return n.setLengthRight(o, 32);
      } else if (d.startsWith("uint")) {
        if (u = t(d), u % 8 || u < 8 || u > 256)
          throw new Error("Invalid uint<N> width: " + u);
        b = i(o);
        const S = n.bitLengthFromBigInt(b);
        if (S > u)
          throw new Error("Supplied uint exceeds width: " + u + " vs " + S);
        if (b < 0)
          throw new Error("Supplied uint is negative");
        return n.bufferBEFromBigInt(b, 32);
      } else if (d.startsWith("int")) {
        if (u = t(d), u % 8 || u < 8 || u > 256)
          throw new Error("Invalid int<N> width: " + u);
        b = i(o);
        const S = n.bitLengthFromBigInt(b);
        if (S > u)
          throw new Error("Supplied int exceeds width: " + u + " vs " + S);
        const _ = n.twosFromBigInt(b, 256);
        return n.bufferBEFromBigInt(_, 32);
      } else if (d.startsWith("ufixed")) {
        if (u = s(d), b = i(o), b < 0)
          throw new Error("Supplied ufixed is negative");
        return a("uint256", b * BigInt(2) ** BigInt(u[1]));
      } else if (d.startsWith("fixed"))
        return u = s(d), a("int256", i(o) * BigInt(2) ** BigInt(u[1]));
    }
    throw new Error("Unsupported or invalid type: " + d);
  }
  function c(d) {
    return d === "string" || d === "bytes" || r(d) === "dynamic";
  }
  function l(d) {
    return d.lastIndexOf("]") === d.length - 1;
  }
  function h(d, o) {
    var u = [], b = [], g = 32 * d.length;
    for (var k in d) {
      var I = e(d[k]), S = o[k], _ = a(I, S);
      c(I) ? (u.push(a("uint256", g)), b.push(_), g += _.length) : u.push(_);
    }
    return y.concat(u.concat(b));
  }
  function p(d, o) {
    if (d.length !== o.length)
      throw new Error("Number of types are not matching the values");
    for (var u, b, g = [], k = 0; k < d.length; k++) {
      var I = e(d[k]), S = o[k];
      if (I === "bytes")
        g.push(S);
      else if (I === "string")
        g.push(new y(S, "utf8"));
      else if (I === "bool")
        g.push(new y(S ? "01" : "00", "hex"));
      else if (I === "address")
        g.push(n.setLength(S, 20));
      else if (I.startsWith("bytes")) {
        if (u = t(I), u < 1 || u > 32)
          throw new Error("Invalid bytes<N> width: " + u);
        g.push(n.setLengthRight(S, u));
      } else if (I.startsWith("uint")) {
        if (u = t(I), u % 8 || u < 8 || u > 256)
          throw new Error("Invalid uint<N> width: " + u);
        b = i(S);
        const _ = n.bitLengthFromBigInt(b);
        if (_ > u)
          throw new Error("Supplied uint exceeds width: " + u + " vs " + _);
        g.push(n.bufferBEFromBigInt(b, u / 8));
      } else if (I.startsWith("int")) {
        if (u = t(I), u % 8 || u < 8 || u > 256)
          throw new Error("Invalid int<N> width: " + u);
        b = i(S);
        const _ = n.bitLengthFromBigInt(b);
        if (_ > u)
          throw new Error("Supplied int exceeds width: " + u + " vs " + _);
        const Ve = n.twosFromBigInt(b, u);
        g.push(n.bufferBEFromBigInt(Ve, u / 8));
      } else
        throw new Error("Unsupported or invalid type: " + I);
    }
    return y.concat(g);
  }
  function f(d, o) {
    return n.keccak(p(d, o));
  }
  return Q = {
    rawEncode: h,
    solidityPack: p,
    soliditySHA3: f
  }, Q;
}
var Z, ye;
function Ct() {
  if (ye) return Z;
  ye = 1;
  const n = /* @__PURE__ */ Be(), e = /* @__PURE__ */ St(), t = {
    type: "object",
    properties: {
      types: {
        type: "object",
        additionalProperties: {
          type: "array",
          items: {
            type: "object",
            properties: {
              name: { type: "string" },
              type: { type: "string" }
            },
            required: ["name", "type"]
          }
        }
      },
      primaryType: { type: "string" },
      domain: { type: "object" },
      message: { type: "object" }
    },
    required: ["types", "primaryType", "domain", "message"]
  }, s = {
    /**
     * Encodes an object by encoding and concatenating each of its members
     *
     * @param {string} primaryType - Root type
     * @param {Object} data - Object to encode
     * @param {Object} types - Type definitions
     * @returns {string} - Encoded representation of an object
     */
    encodeData(i, a, c, l = !0) {
      const h = ["bytes32"], p = [this.hashType(i, c)];
      if (l) {
        const f = (d, o, u) => {
          if (c[o] !== void 0)
            return ["bytes32", u == null ? "0x0000000000000000000000000000000000000000000000000000000000000000" : n.keccak(this.encodeData(o, u, c, l))];
          if (u === void 0)
            throw new Error(`missing value for field ${d} of type ${o}`);
          if (o === "bytes")
            return ["bytes32", n.keccak(u)];
          if (o === "string")
            return typeof u == "string" && (u = y.from(u, "utf8")), ["bytes32", n.keccak(u)];
          if (o.lastIndexOf("]") === o.length - 1) {
            const b = o.slice(0, o.lastIndexOf("[")), g = u.map((k) => f(d, b, k));
            return ["bytes32", n.keccak(e.rawEncode(
              g.map(([k]) => k),
              g.map(([, k]) => k)
            ))];
          }
          return [o, u];
        };
        for (const d of c[i]) {
          const [o, u] = f(d.name, d.type, a[d.name]);
          h.push(o), p.push(u);
        }
      } else
        for (const f of c[i]) {
          let d = a[f.name];
          if (d !== void 0)
            if (f.type === "bytes")
              h.push("bytes32"), d = n.keccak(d), p.push(d);
            else if (f.type === "string")
              h.push("bytes32"), typeof d == "string" && (d = y.from(d, "utf8")), d = n.keccak(d), p.push(d);
            else if (c[f.type] !== void 0)
              h.push("bytes32"), d = n.keccak(this.encodeData(f.type, d, c, l)), p.push(d);
            else {
              if (f.type.lastIndexOf("]") === f.type.length - 1)
                throw new Error("Arrays currently unimplemented in encodeData");
              h.push(f.type), p.push(d);
            }
        }
      return e.rawEncode(h, p);
    },
    /**
     * Encodes the type of an object by encoding a comma delimited list of its members
     *
     * @param {string} primaryType - Root type to encode
     * @param {Object} types - Type definitions
     * @returns {string} - Encoded representation of the type of an object
     */
    encodeType(i, a) {
      let c = "", l = this.findTypeDependencies(i, a).filter((h) => h !== i);
      l = [i].concat(l.sort());
      for (const h of l) {
        if (!a[h])
          throw new Error("No type definition specified: " + h);
        c += h + "(" + a[h].map(({ name: f, type: d }) => d + " " + f).join(",") + ")";
      }
      return c;
    },
    /**
     * Finds all types within a type definition object
     *
     * @param {string} primaryType - Root type
     * @param {Object} types - Type definitions
     * @param {Array} results - current set of accumulated types
     * @returns {Array} - Set of all types found in the type definition
     */
    findTypeDependencies(i, a, c = []) {
      if (i = i.match(/^\w*/)[0], c.includes(i) || a[i] === void 0)
        return c;
      c.push(i);
      for (const l of a[i])
        for (const h of this.findTypeDependencies(l.type, a, c))
          !c.includes(h) && c.push(h);
      return c;
    },
    /**
     * Hashes an object
     *
     * @param {string} primaryType - Root type
     * @param {Object} data - Object to hash
     * @param {Object} types - Type definitions
     * @returns {Buffer} - Hash of an object
     */
    hashStruct(i, a, c, l = !0) {
      return n.keccak(this.encodeData(i, a, c, l));
    },
    /**
     * Hashes the type of an object
     *
     * @param {string} primaryType - Root type to hash
     * @param {Object} types - Type definitions
     * @returns {string} - Hash of an object
     */
    hashType(i, a) {
      return n.keccak(this.encodeType(i, a));
    },
    /**
     * Removes properties from a message object that are not defined per EIP-712
     *
     * @param {Object} data - typed message object
     * @returns {Object} - typed message object with only allowed fields
     */
    sanitizeData(i) {
      const a = {};
      for (const c in t.properties)
        i[c] && (a[c] = i[c]);
      return a.types && (a.types = Object.assign({ EIP712Domain: [] }, a.types)), a;
    },
    /**
     * Returns the hash of a typed message as per EIP-712 for signing
     *
     * @param {Object} typedData - Types message data to sign
     * @returns {string} - sha3 hash for signing
     */
    hash(i, a = !0) {
      const c = this.sanitizeData(i), l = [y.from("1901", "hex")];
      return l.push(this.hashStruct("EIP712Domain", c.domain, c.types, a)), c.primaryType !== "EIP712Domain" && l.push(this.hashStruct(c.primaryType, c.message, c.types, a)), n.keccak(y.concat(l));
    }
  };
  Z = {
    TYPED_MESSAGE_SCHEMA: t,
    TypedDataUtils: s,
    hashForSignTypedDataLegacy: function(i) {
      return r(i.data);
    },
    hashForSignTypedData_v3: function(i) {
      return s.hash(i.data, !1);
    },
    hashForSignTypedData_v4: function(i) {
      return s.hash(i.data);
    }
  };
  function r(i) {
    const a = new Error("Expect argument to be non-empty array");
    if (typeof i != "object" || !i.length) throw a;
    const c = i.map(function(p) {
      return p.type === "bytes" ? n.toBuffer(p.value) : p.value;
    }), l = i.map(function(p) {
      return p.type;
    }), h = i.map(function(p) {
      if (!p.name) throw a;
      return p.type + " " + p.name;
    });
    return e.soliditySHA3(
      ["bytes32", "bytes32"],
      [
        e.soliditySHA3(new Array(i.length).fill("string"), h),
        e.soliditySHA3(l, c)
      ]
    );
  }
  return Z;
}
var _t = /* @__PURE__ */ Ct();
const B = /* @__PURE__ */ Qe(_t), Mt = "walletUsername", se = "Addresses", At = "AppVersion";
function E(n) {
  return n.errorMessage !== void 0;
}
class xt {
  // @param secret hex representation of 32-byte secret
  constructor(e) {
    this.secret = e;
  }
  /**
   *
   * @param plainText string to be encrypted
   * returns hex string representation of bytes in the order: initialization vector (iv),
   * auth tag, encrypted plaintext. IV is 12 bytes. Auth tag is 16 bytes. Remaining bytes are the
   * encrypted plainText.
   */
  async encrypt(e) {
    const t = this.secret;
    if (t.length !== 64)
      throw Error("secret must be 256 bits");
    const s = crypto.getRandomValues(new Uint8Array(12)), r = await crypto.subtle.importKey("raw", F(t), { name: "aes-gcm" }, !1, ["encrypt", "decrypt"]), i = new TextEncoder(), a = await window.crypto.subtle.encrypt({
      name: "AES-GCM",
      iv: s
    }, r, i.encode(e)), c = 16, l = a.slice(a.byteLength - c), h = a.slice(0, a.byteLength - c), p = new Uint8Array(l), f = new Uint8Array(h), d = new Uint8Array([...s, ...p, ...f]);
    return ae(d);
  }
  /**
   *
   * @param cipherText hex string representation of bytes in the order: initialization vector (iv),
   * auth tag, encrypted plaintext. IV is 12 bytes. Auth tag is 16 bytes.
   */
  async decrypt(e) {
    const t = this.secret;
    if (t.length !== 64)
      throw Error("secret must be 256 bits");
    return new Promise((s, r) => {
      (async function() {
        const i = await crypto.subtle.importKey("raw", F(t), { name: "aes-gcm" }, !1, ["encrypt", "decrypt"]), a = F(e), c = a.slice(0, 12), l = a.slice(12, 28), h = a.slice(28), p = new Uint8Array([...h, ...l]), f = {
          name: "AES-GCM",
          iv: new Uint8Array(c)
        };
        try {
          const d = await window.crypto.subtle.decrypt(f, i, p), o = new TextDecoder();
          s(o.decode(d));
        } catch (d) {
          r(d);
        }
      })();
    });
  }
}
class Rt {
  constructor(e, t, s) {
    this.linkAPIUrl = e, this.sessionId = t;
    const r = `${t}:${s}`;
    this.auth = `Basic ${btoa(r)}`;
  }
  // mark unseen events as seen
  async markUnseenEventsAsSeen(e) {
    return Promise.all(e.map((t) => fetch(`${this.linkAPIUrl}/events/${t.eventId}/seen`, {
      method: "POST",
      headers: {
        Authorization: this.auth
      }
    }))).catch((t) => console.error("Unabled to mark event as failed:", t));
  }
  async fetchUnseenEvents() {
    var e;
    const t = await fetch(`${this.linkAPIUrl}/events?unseen=true`, {
      headers: {
        Authorization: this.auth
      }
    });
    if (t.ok) {
      const { events: s, error: r } = await t.json();
      if (r)
        throw new Error(`Check unseen events failed: ${r}`);
      const i = (e = s == null ? void 0 : s.filter((a) => a.event === "Web3Response").map((a) => ({
        type: "Event",
        sessionId: this.sessionId,
        eventId: a.id,
        event: a.event,
        data: a.data
      }))) !== null && e !== void 0 ? e : [];
      return this.markUnseenEventsAsSeen(i), i;
    }
    throw new Error(`Check unseen events failed: ${t.status}`);
  }
}
var P;
(function(n) {
  n[n.DISCONNECTED = 0] = "DISCONNECTED", n[n.CONNECTING = 1] = "CONNECTING", n[n.CONNECTED = 2] = "CONNECTED";
})(P || (P = {}));
class Pt {
  setConnectionStateListener(e) {
    this.connectionStateListener = e;
  }
  setIncomingDataListener(e) {
    this.incomingDataListener = e;
  }
  /**
   * Constructor
   * @param url WebSocket server URL
   * @param [WebSocketClass] Custom WebSocket implementation
   */
  constructor(e, t = WebSocket) {
    this.WebSocketClass = t, this.webSocket = null, this.pendingData = [], this.url = e.replace(/^http/, "ws");
  }
  /**
   * Make a websocket connection
   * @returns a Promise that resolves when connected
   */
  async connect() {
    if (this.webSocket)
      throw new Error("webSocket object is not null");
    return new Promise((e, t) => {
      var s;
      let r;
      try {
        this.webSocket = r = new this.WebSocketClass(this.url);
      } catch (i) {
        t(i);
        return;
      }
      (s = this.connectionStateListener) === null || s === void 0 || s.call(this, P.CONNECTING), r.onclose = (i) => {
        var a;
        this.clearWebSocket(), t(new Error(`websocket error ${i.code}: ${i.reason}`)), (a = this.connectionStateListener) === null || a === void 0 || a.call(this, P.DISCONNECTED);
      }, r.onopen = (i) => {
        var a;
        e(), (a = this.connectionStateListener) === null || a === void 0 || a.call(this, P.CONNECTED), this.pendingData.length > 0 && ([...this.pendingData].forEach((l) => this.sendData(l)), this.pendingData = []);
      }, r.onmessage = (i) => {
        var a, c;
        if (i.data === "h")
          (a = this.incomingDataListener) === null || a === void 0 || a.call(this, {
            type: "Heartbeat"
          });
        else
          try {
            const l = JSON.parse(i.data);
            (c = this.incomingDataListener) === null || c === void 0 || c.call(this, l);
          } catch {
          }
      };
    });
  }
  /**
   * Disconnect from server
   */
  disconnect() {
    var e;
    const { webSocket: t } = this;
    if (t) {
      this.clearWebSocket(), (e = this.connectionStateListener) === null || e === void 0 || e.call(this, P.DISCONNECTED), this.connectionStateListener = void 0, this.incomingDataListener = void 0;
      try {
        t.close();
      } catch {
      }
    }
  }
  /**
   * Send data to server
   * @param data text to send
   */
  sendData(e) {
    const { webSocket: t } = this;
    if (!t) {
      this.pendingData.push(e), this.connect();
      return;
    }
    t.send(e);
  }
  clearWebSocket() {
    const { webSocket: e } = this;
    e && (this.webSocket = null, e.onclose = null, e.onerror = null, e.onmessage = null, e.onopen = null);
  }
}
const ve = 1e4, Lt = 6e4;
class Tt {
  /**
   * Constructor
   * @param session Session
   * @param linkAPIUrl Coinbase Wallet link server URL
   * @param listener WalletLinkConnectionUpdateListener
   * @param [WebSocketClass] Custom WebSocket implementation
   */
  constructor({ session: e, linkAPIUrl: t, listener: s }) {
    this.destroyed = !1, this.lastHeartbeatResponse = 0, this.nextReqId = x(1), this._connected = !1, this._linked = !1, this.shouldFetchUnseenEventsOnConnect = !1, this.requestResolutions = /* @__PURE__ */ new Map(), this.handleSessionMetadataUpdated = (i) => {
      if (!i)
        return;
      (/* @__PURE__ */ new Map([
        ["__destroyed", this.handleDestroyed],
        ["EthereumAddress", this.handleAccountUpdated],
        ["WalletUsername", this.handleWalletUsernameUpdated],
        ["AppVersion", this.handleAppVersionUpdated],
        [
          "ChainId",
          // ChainId and JsonRpcUrl are always updated together
          (c) => i.JsonRpcUrl && this.handleChainUpdated(c, i.JsonRpcUrl)
        ]
      ])).forEach((c, l) => {
        const h = i[l];
        h !== void 0 && c(h);
      });
    }, this.handleDestroyed = (i) => {
      var a;
      i === "1" && ((a = this.listener) === null || a === void 0 || a.resetAndReload());
    }, this.handleAccountUpdated = async (i) => {
      var a;
      const c = await this.cipher.decrypt(i);
      (a = this.listener) === null || a === void 0 || a.accountUpdated(c);
    }, this.handleMetadataUpdated = async (i, a) => {
      var c;
      const l = await this.cipher.decrypt(a);
      (c = this.listener) === null || c === void 0 || c.metadataUpdated(i, l);
    }, this.handleWalletUsernameUpdated = async (i) => {
      this.handleMetadataUpdated(Mt, i);
    }, this.handleAppVersionUpdated = async (i) => {
      this.handleMetadataUpdated(At, i);
    }, this.handleChainUpdated = async (i, a) => {
      var c;
      const l = await this.cipher.decrypt(i), h = await this.cipher.decrypt(a);
      (c = this.listener) === null || c === void 0 || c.chainUpdated(l, h);
    }, this.session = e, this.cipher = new xt(e.secret), this.listener = s;
    const r = new Pt(`${t}/rpc`, WebSocket);
    r.setConnectionStateListener(async (i) => {
      let a = !1;
      switch (i) {
        case P.DISCONNECTED:
          if (!this.destroyed) {
            const c = async () => {
              await new Promise((l) => setTimeout(l, 5e3)), this.destroyed || r.connect().catch(() => {
                c();
              });
            };
            c();
          }
          break;
        case P.CONNECTED:
          a = await this.handleConnected(), this.updateLastHeartbeat(), setInterval(() => {
            this.heartbeat();
          }, ve), this.shouldFetchUnseenEventsOnConnect && this.fetchUnseenEventsAPI();
          break;
        case P.CONNECTING:
          break;
      }
      this.connected !== a && (this.connected = a);
    }), r.setIncomingDataListener((i) => {
      var a;
      switch (i.type) {
        // handle server's heartbeat responses
        case "Heartbeat":
          this.updateLastHeartbeat();
          return;
        // handle link status updates
        case "IsLinkedOK":
        case "Linked": {
          const c = i.type === "IsLinkedOK" ? i.linked : void 0;
          this.linked = c || i.onlineGuests > 0;
          break;
        }
        // handle session config updates
        case "GetSessionConfigOK":
        case "SessionConfigUpdated": {
          this.handleSessionMetadataUpdated(i.metadata);
          break;
        }
        case "Event": {
          this.handleIncomingEvent(i);
          break;
        }
      }
      i.id !== void 0 && ((a = this.requestResolutions.get(i.id)) === null || a === void 0 || a(i));
    }), this.ws = r, this.http = new Rt(t, e.id, e.key);
  }
  /**
   * Make a connection to the server
   */
  connect() {
    if (this.destroyed)
      throw new Error("instance is destroyed");
    this.ws.connect();
  }
  /**
   * Terminate connection, and mark as destroyed. To reconnect, create a new
   * instance of WalletSDKConnection
   */
  async destroy() {
    this.destroyed || (await this.makeRequest({
      type: "SetSessionConfig",
      id: x(this.nextReqId++),
      sessionId: this.session.id,
      metadata: { __destroyed: "1" }
    }, { timeout: 1e3 }), this.destroyed = !0, this.ws.disconnect(), this.listener = void 0);
  }
  get connected() {
    return this._connected;
  }
  set connected(e) {
    this._connected = e;
  }
  get linked() {
    return this._linked;
  }
  set linked(e) {
    var t, s;
    this._linked = e, e && ((t = this.onceLinked) === null || t === void 0 || t.call(this)), (s = this.listener) === null || s === void 0 || s.linkedUpdated(e);
  }
  setOnceLinked(e) {
    return new Promise((t) => {
      this.linked ? e().then(t) : this.onceLinked = () => {
        e().then(t), this.onceLinked = void 0;
      };
    });
  }
  async handleIncomingEvent(e) {
    var t;
    if (e.type !== "Event" || e.event !== "Web3Response")
      return;
    const s = await this.cipher.decrypt(e.data), r = JSON.parse(s);
    if (r.type !== "WEB3_RESPONSE")
      return;
    const { id: i, response: a } = r;
    (t = this.listener) === null || t === void 0 || t.handleWeb3ResponseMessage(i, a);
  }
  async checkUnseenEvents() {
    if (!this.connected) {
      this.shouldFetchUnseenEventsOnConnect = !0;
      return;
    }
    await new Promise((e) => setTimeout(e, 250));
    try {
      await this.fetchUnseenEventsAPI();
    } catch (e) {
      console.error("Unable to check for unseen events", e);
    }
  }
  async fetchUnseenEventsAPI() {
    this.shouldFetchUnseenEventsOnConnect = !1, (await this.http.fetchUnseenEvents()).forEach((t) => this.handleIncomingEvent(t));
  }
  /**
   * Publish an event and emit event ID when successful
   * @param event event name
   * @param unencryptedData unencrypted event data
   * @param callWebhook whether the webhook should be invoked
   * @returns a Promise that emits event ID when successful
   */
  async publishEvent(e, t, s = !1) {
    const r = await this.cipher.encrypt(JSON.stringify(Object.assign(Object.assign({}, t), { origin: location.origin, location: location.href, relaySource: "coinbaseWalletExtension" in window && window.coinbaseWalletExtension ? "injected_sdk" : "sdk" }))), i = {
      type: "PublishEvent",
      id: x(this.nextReqId++),
      sessionId: this.session.id,
      event: e,
      data: r,
      callWebhook: s
    };
    return this.setOnceLinked(async () => {
      const a = await this.makeRequest(i);
      if (a.type === "Fail")
        throw new Error(a.error || "failed to publish event");
      return a.eventId;
    });
  }
  sendData(e) {
    this.ws.sendData(JSON.stringify(e));
  }
  updateLastHeartbeat() {
    this.lastHeartbeatResponse = Date.now();
  }
  heartbeat() {
    if (Date.now() - this.lastHeartbeatResponse > ve * 2) {
      this.ws.disconnect();
      return;
    }
    try {
      this.ws.sendData("h");
    } catch {
    }
  }
  async makeRequest(e, t = { timeout: Lt }) {
    const s = e.id;
    this.sendData(e);
    let r;
    return Promise.race([
      new Promise((i, a) => {
        r = window.setTimeout(() => {
          a(new Error(`request ${s} timed out`));
        }, t.timeout);
      }),
      new Promise((i) => {
        this.requestResolutions.set(s, (a) => {
          clearTimeout(r), i(a), this.requestResolutions.delete(s);
        });
      })
    ]);
  }
  async handleConnected() {
    return (await this.makeRequest({
      type: "HostSession",
      id: x(this.nextReqId++),
      sessionId: this.session.id,
      sessionKey: this.session.key
    })).type === "Fail" ? !1 : (this.sendData({
      type: "IsLinked",
      id: x(this.nextReqId++),
      sessionId: this.session.id
    }), this.sendData({
      type: "GetSessionConfig",
      id: x(this.nextReqId++),
      sessionId: this.session.id
    }), !0);
  }
}
class Nt {
  constructor() {
    this._nextRequestId = 0, this.callbacks = /* @__PURE__ */ new Map();
  }
  makeRequestId() {
    this._nextRequestId = (this._nextRequestId + 1) % 2147483647;
    const e = this._nextRequestId, t = Ue(e.toString(16));
    return this.callbacks.get(t) && this.callbacks.delete(t), e;
  }
}
const ke = "session:id", Ee = "session:secret", Ie = "session:linked";
class O {
  constructor(e, t, s, r = !1) {
    this.storage = e, this.id = t, this.secret = s, this.key = Ze(Xe(`${t}, ${s} WalletLink`)), this._linked = !!r;
  }
  static create(e) {
    const t = N(16), s = N(32);
    return new O(e, t, s).save();
  }
  static load(e) {
    const t = e.getItem(ke), s = e.getItem(Ie), r = e.getItem(Ee);
    return t && r ? new O(e, t, r, s === "1") : null;
  }
  get linked() {
    return this._linked;
  }
  set linked(e) {
    this._linked = e, this.persistLinked();
  }
  save() {
    return this.storage.setItem(ke, this.id), this.storage.setItem(Ee, this.secret), this.persistLinked(), this;
  }
  persistLinked() {
    this.storage.setItem(Ie, this._linked ? "1" : "0");
  }
}
function Dt() {
  try {
    return window.frameElement !== null;
  } catch {
    return !1;
  }
}
function Ot() {
  try {
    return Dt() && window.top ? window.top.location : window.location;
  } catch {
    return window.location;
  }
}
function jt() {
  var n;
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test((n = window == null ? void 0 : window.navigator) === null || n === void 0 ? void 0 : n.userAgent);
}
function Fe() {
  var n, e;
  return (e = (n = window == null ? void 0 : window.matchMedia) === null || n === void 0 ? void 0 : n.call(window, "(prefers-color-scheme: dark)").matches) !== null && e !== void 0 ? e : !1;
}
const Ut = '@namespace svg "http://www.w3.org/2000/svg";.-cbwsdk-css-reset,.-cbwsdk-css-reset *{animation:none;animation-delay:0;animation-direction:normal;animation-duration:0;animation-fill-mode:none;animation-iteration-count:1;animation-name:none;animation-play-state:running;animation-timing-function:ease;backface-visibility:visible;background:0;background-attachment:scroll;background-clip:border-box;background-color:rgba(0,0,0,0);background-image:none;background-origin:padding-box;background-position:0 0;background-position-x:0;background-position-y:0;background-repeat:repeat;background-size:auto auto;border:0;border-style:none;border-width:medium;border-color:inherit;border-bottom:0;border-bottom-color:inherit;border-bottom-left-radius:0;border-bottom-right-radius:0;border-bottom-style:none;border-bottom-width:medium;border-collapse:separate;border-image:none;border-left:0;border-left-color:inherit;border-left-style:none;border-left-width:medium;border-radius:0;border-right:0;border-right-color:inherit;border-right-style:none;border-right-width:medium;border-spacing:0;border-top:0;border-top-color:inherit;border-top-left-radius:0;border-top-right-radius:0;border-top-style:none;border-top-width:medium;box-shadow:none;box-sizing:border-box;caption-side:top;clear:none;clip:auto;color:inherit;columns:auto;column-count:auto;column-fill:balance;column-gap:normal;column-rule:medium none currentColor;column-rule-color:currentColor;column-rule-style:none;column-rule-width:none;column-span:1;column-width:auto;counter-increment:none;counter-reset:none;direction:ltr;empty-cells:show;float:none;font:normal;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Helvetica Neue",Arial,sans-serif;font-size:medium;font-style:normal;font-variant:normal;font-weight:normal;height:auto;hyphens:none;letter-spacing:normal;line-height:normal;list-style:none;list-style-image:none;list-style-position:outside;list-style-type:disc;margin:0;margin-bottom:0;margin-left:0;margin-right:0;margin-top:0;opacity:1;orphans:0;outline:0;outline-color:invert;outline-style:none;outline-width:medium;overflow:visible;overflow-x:visible;overflow-y:visible;padding:0;padding-bottom:0;padding-left:0;padding-right:0;padding-top:0;page-break-after:auto;page-break-before:auto;page-break-inside:auto;perspective:none;perspective-origin:50% 50%;pointer-events:auto;position:static;quotes:"\\201C" "\\201D" "\\2018" "\\2019";tab-size:8;table-layout:auto;text-align:inherit;text-align-last:auto;text-decoration:none;text-decoration-color:inherit;text-decoration-line:none;text-decoration-style:solid;text-indent:0;text-shadow:none;text-transform:none;transform:none;transform-style:flat;transition:none;transition-delay:0s;transition-duration:0s;transition-property:none;transition-timing-function:ease;unicode-bidi:normal;vertical-align:baseline;visibility:visible;white-space:normal;widows:0;word-spacing:normal;z-index:auto}.-cbwsdk-css-reset strong{font-weight:bold}.-cbwsdk-css-reset *{box-sizing:border-box;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Helvetica Neue",Arial,sans-serif;line-height:1}.-cbwsdk-css-reset [class*=container]{margin:0;padding:0}.-cbwsdk-css-reset style{display:none}';
function He() {
  const n = document.createElement("style");
  n.type = "text/css", n.appendChild(document.createTextNode(Ut)), document.documentElement.appendChild(n);
}
const Wt = ".-cbwsdk-css-reset .-gear-container{margin-left:16px !important;margin-right:9px !important;display:flex;align-items:center;justify-content:center;width:24px;height:24px;transition:opacity .25s}.-cbwsdk-css-reset .-gear-container *{user-select:none}.-cbwsdk-css-reset .-gear-container svg{opacity:0;position:absolute}.-cbwsdk-css-reset .-gear-icon{height:12px;width:12px;z-index:10000}.-cbwsdk-css-reset .-cbwsdk-snackbar{align-items:flex-end;display:flex;flex-direction:column;position:fixed;right:0;top:0;z-index:2147483647}.-cbwsdk-css-reset .-cbwsdk-snackbar *{user-select:none}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance{display:flex;flex-direction:column;margin:8px 16px 0 16px;overflow:visible;text-align:left;transform:translateX(0);transition:opacity .25s,transform .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-header:hover .-gear-container svg{opacity:1}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-header{display:flex;align-items:center;background:#fff;overflow:hidden;border:1px solid #e7ebee;box-sizing:border-box;border-radius:8px;cursor:pointer}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-header-cblogo{margin:8px 8px 8px 8px}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-header *{cursor:pointer}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-header-message{color:#000;font-size:13px;line-height:1.5;user-select:none}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu{background:#fff;transition:opacity .25s ease-in-out,transform .25s linear,visibility 0s;visibility:hidden;border:1px solid #e7ebee;box-sizing:border-box;border-radius:8px;opacity:0;flex-direction:column;padding-left:8px;padding-right:8px}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item:last-child{margin-bottom:8px !important}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item:hover{background:#f5f7f8;border-radius:6px;transition:background .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item:hover span{color:#050f19;transition:color .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item:hover svg path{fill:#000;transition:fill .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item{visibility:inherit;height:35px;margin-top:8px;margin-bottom:0;display:flex;flex-direction:row;align-items:center;padding:8px;cursor:pointer}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item *{visibility:inherit;cursor:pointer}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item-is-red:hover{background:rgba(223,95,103,.2);transition:background .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item-is-red:hover *{cursor:pointer}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item-is-red:hover svg path{fill:#df5f67;transition:fill .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item-is-red:hover span{color:#df5f67;transition:color .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item-info{color:#aaa;font-size:13px;margin:0 8px 0 32px;position:absolute}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-hidden{opacity:0;text-align:left;transform:translateX(25%);transition:opacity .5s linear}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-expanded .-cbwsdk-snackbar-instance-menu{opacity:1;display:flex;transform:translateY(8px);visibility:visible}", qt = "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHBhdGggZD0iTTEuNDkyIDEwLjQxOWE4LjkzIDguOTMgMCAwMTguOTMtOC45M2gxMS4xNjNhOC45MyA4LjkzIDAgMDE4LjkzIDguOTN2MTEuMTYzYTguOTMgOC45MyAwIDAxLTguOTMgOC45M0gxMC40MjJhOC45MyA4LjkzIDAgMDEtOC45My04LjkzVjEwLjQxOXoiIGZpbGw9IiMxNjUyRjAiLz48cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZD0iTTEwLjQxOSAwSDIxLjU4QzI3LjMzNSAwIDMyIDQuNjY1IDMyIDEwLjQxOVYyMS41OEMzMiAyNy4zMzUgMjcuMzM1IDMyIDIxLjU4MSAzMkgxMC40MkM0LjY2NSAzMiAwIDI3LjMzNSAwIDIxLjU4MVYxMC40MkMwIDQuNjY1IDQuNjY1IDAgMTAuNDE5IDB6bTAgMS40ODhhOC45MyA4LjkzIDAgMDAtOC45MyA4LjkzdjExLjE2M2E4LjkzIDguOTMgMCAwMDguOTMgOC45M0gyMS41OGE4LjkzIDguOTMgMCAwMDguOTMtOC45M1YxMC40MmE4LjkzIDguOTMgMCAwMC04LjkzLTguOTNIMTAuNDJ6IiBmaWxsPSIjZmZmIi8+PHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik0xNS45OTggMjYuMDQ5Yy01LjU0OSAwLTEwLjA0Ny00LjQ5OC0xMC4wNDctMTAuMDQ3IDAtNS41NDggNC40OTgtMTAuMDQ2IDEwLjA0Ny0xMC4wNDYgNS41NDggMCAxMC4wNDYgNC40OTggMTAuMDQ2IDEwLjA0NiAwIDUuNTQ5LTQuNDk4IDEwLjA0Ny0xMC4wNDYgMTAuMDQ3eiIgZmlsbD0iI2ZmZiIvPjxwYXRoIGQ9Ik0xMi43NjIgMTQuMjU0YzAtLjgyMi42NjctMS40ODkgMS40ODktMS40ODloMy40OTdjLjgyMiAwIDEuNDg4LjY2NiAxLjQ4OCAxLjQ4OXYzLjQ5N2MwIC44MjItLjY2NiAxLjQ4OC0xLjQ4OCAxLjQ4OGgtMy40OTdhMS40ODggMS40ODggMCAwMS0xLjQ4OS0xLjQ4OHYtMy40OTh6IiBmaWxsPSIjMTY1MkYwIi8+PC9zdmc+", Kt = "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTIiIGhlaWdodD0iMTIiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHBhdGggZD0iTTEyIDYuNzV2LTEuNWwtMS43Mi0uNTdjLS4wOC0uMjctLjE5LS41Mi0uMzItLjc3bC44MS0xLjYyLTEuMDYtMS4wNi0xLjYyLjgxYy0uMjQtLjEzLS41LS4yNC0uNzctLjMyTDYuNzUgMGgtMS41bC0uNTcgMS43MmMtLjI3LjA4LS41My4xOS0uNzcuMzJsLTEuNjItLjgxLTEuMDYgMS4wNi44MSAxLjYyYy0uMTMuMjQtLjI0LjUtLjMyLjc3TDAgNS4yNXYxLjVsMS43Mi41N2MuMDguMjcuMTkuNTMuMzIuNzdsLS44MSAxLjYyIDEuMDYgMS4wNiAxLjYyLS44MWMuMjQuMTMuNS4yMy43Ny4zMkw1LjI1IDEyaDEuNWwuNTctMS43MmMuMjctLjA4LjUyLS4xOS43Ny0uMzJsMS42Mi44MSAxLjA2LTEuMDYtLjgxLTEuNjJjLjEzLS4yNC4yMy0uNS4zMi0uNzdMMTIgNi43NXpNNiA4LjVhMi41IDIuNSAwIDAxMC01IDIuNSAyLjUgMCAwMTAgNXoiIGZpbGw9IiMwNTBGMTkiLz48L3N2Zz4=";
class zt {
  constructor() {
    this.items = /* @__PURE__ */ new Map(), this.nextItemKey = 0, this.root = null, this.darkMode = Fe();
  }
  attach(e) {
    this.root = document.createElement("div"), this.root.className = "-cbwsdk-snackbar-root", e.appendChild(this.root), this.render();
  }
  presentItem(e) {
    const t = this.nextItemKey++;
    return this.items.set(t, e), this.render(), () => {
      this.items.delete(t), this.render();
    };
  }
  clear() {
    this.items.clear(), this.render();
  }
  render() {
    this.root && X(w(
      "div",
      null,
      w(Ge, { darkMode: this.darkMode }, Array.from(this.items.entries()).map(([e, t]) => w(Bt, Object.assign({}, t, { key: e }))))
    ), this.root);
  }
}
const Ge = (n) => w(
  "div",
  { class: U("-cbwsdk-snackbar-container") },
  w("style", null, Wt),
  w("div", { class: "-cbwsdk-snackbar" }, n.children)
), Bt = ({ autoExpand: n, message: e, menuItems: t }) => {
  const [s, r] = le(!0), [i, a] = le(n ?? !1);
  nt(() => {
    const l = [
      window.setTimeout(() => {
        r(!1);
      }, 1),
      window.setTimeout(() => {
        a(!0);
      }, 1e4)
    ];
    return () => {
      l.forEach(window.clearTimeout);
    };
  });
  const c = () => {
    a(!i);
  };
  return w(
    "div",
    { class: U("-cbwsdk-snackbar-instance", s && "-cbwsdk-snackbar-instance-hidden", i && "-cbwsdk-snackbar-instance-expanded") },
    w(
      "div",
      { class: "-cbwsdk-snackbar-instance-header", onClick: c },
      w("img", { src: qt, class: "-cbwsdk-snackbar-instance-header-cblogo" }),
      " ",
      w("div", { class: "-cbwsdk-snackbar-instance-header-message" }, e),
      w(
        "div",
        { class: "-gear-container" },
        !i && w(
          "svg",
          { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/svg" },
          w("circle", { cx: "12", cy: "12", r: "12", fill: "#F5F7F8" })
        ),
        w("img", { src: Kt, class: "-gear-icon", title: "Expand" })
      )
    ),
    t && t.length > 0 && w("div", { class: "-cbwsdk-snackbar-instance-menu" }, t.map((l, h) => w(
      "div",
      { class: U("-cbwsdk-snackbar-instance-menu-item", l.isRed && "-cbwsdk-snackbar-instance-menu-item-is-red"), onClick: l.onClick, key: h },
      w(
        "svg",
        { width: l.svgWidth, height: l.svgHeight, viewBox: "0 0 10 11", fill: "none", xmlns: "http://www.w3.org/2000/svg" },
        w("path", { "fill-rule": l.defaultFillRule, "clip-rule": l.defaultClipRule, d: l.path, fill: "#AAAAAA" })
      ),
      w("span", { class: U("-cbwsdk-snackbar-instance-menu-item-info", l.isRed && "-cbwsdk-snackbar-instance-menu-item-info-is-red") }, l.info)
    )))
  );
};
class Ft {
  constructor() {
    this.attached = !1, this.snackbar = new zt();
  }
  attach() {
    if (this.attached)
      throw new Error("Coinbase Wallet SDK UI is already attached");
    const e = document.documentElement, t = document.createElement("div");
    t.className = "-cbwsdk-css-reset", e.appendChild(t), this.snackbar.attach(t), this.attached = !0, He();
  }
  showConnecting(e) {
    let t;
    return e.isUnlinkedErrorState ? t = {
      autoExpand: !0,
      message: "Connection lost",
      menuItems: [
        {
          isRed: !1,
          info: "Reset connection",
          svgWidth: "10",
          svgHeight: "11",
          path: "M5.00008 0.96875C6.73133 0.96875 8.23758 1.94375 9.00008 3.375L10.0001 2.375V5.5H9.53133H7.96883H6.87508L7.80633 4.56875C7.41258 3.3875 6.31258 2.53125 5.00008 2.53125C3.76258 2.53125 2.70633 3.2875 2.25633 4.36875L0.812576 3.76875C1.50008 2.125 3.11258 0.96875 5.00008 0.96875ZM2.19375 6.43125C2.5875 7.6125 3.6875 8.46875 5 8.46875C6.2375 8.46875 7.29375 7.7125 7.74375 6.63125L9.1875 7.23125C8.5 8.875 6.8875 10.0312 5 10.0312C3.26875 10.0312 1.7625 9.05625 1 7.625L0 8.625V5.5H0.46875H2.03125H3.125L2.19375 6.43125Z",
          defaultFillRule: "evenodd",
          defaultClipRule: "evenodd",
          onClick: e.onResetConnection
        }
      ]
    } : t = {
      message: "Confirm on phone",
      menuItems: [
        {
          isRed: !0,
          info: "Cancel transaction",
          svgWidth: "11",
          svgHeight: "11",
          path: "M10.3711 1.52346L9.21775 0.370117L5.37109 4.21022L1.52444 0.370117L0.371094 1.52346L4.2112 5.37012L0.371094 9.21677L1.52444 10.3701L5.37109 6.53001L9.21775 10.3701L10.3711 9.21677L6.53099 5.37012L10.3711 1.52346Z",
          defaultFillRule: "inherit",
          defaultClipRule: "inherit",
          onClick: e.onCancel
        },
        {
          isRed: !1,
          info: "Reset connection",
          svgWidth: "10",
          svgHeight: "11",
          path: "M5.00008 0.96875C6.73133 0.96875 8.23758 1.94375 9.00008 3.375L10.0001 2.375V5.5H9.53133H7.96883H6.87508L7.80633 4.56875C7.41258 3.3875 6.31258 2.53125 5.00008 2.53125C3.76258 2.53125 2.70633 3.2875 2.25633 4.36875L0.812576 3.76875C1.50008 2.125 3.11258 0.96875 5.00008 0.96875ZM2.19375 6.43125C2.5875 7.6125 3.6875 8.46875 5 8.46875C6.2375 8.46875 7.29375 7.7125 7.74375 6.63125L9.1875 7.23125C8.5 8.875 6.8875 10.0312 5 10.0312C3.26875 10.0312 1.7625 9.05625 1 7.625L0 8.625V5.5H0.46875H2.03125H3.125L2.19375 6.43125Z",
          defaultFillRule: "evenodd",
          defaultClipRule: "evenodd",
          onClick: e.onResetConnection
        }
      ]
    }, this.snackbar.presentItem(t);
  }
}
const Ht = ".-cbwsdk-css-reset .-cbwsdk-redirect-dialog-backdrop{position:fixed;top:0;left:0;right:0;bottom:0;transition:opacity .25s;background-color:rgba(10,11,13,.5)}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-backdrop-hidden{opacity:0}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box{display:block;position:fixed;top:50%;left:50%;transform:translate(-50%, -50%);padding:20px;border-radius:8px;background-color:#fff;color:#0a0b0d}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box p{display:block;font-weight:400;font-size:14px;line-height:20px;padding-bottom:12px;color:#5b636e}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box button{appearance:none;border:none;background:none;color:#0052ff;padding:0;text-decoration:none;display:block;font-weight:600;font-size:16px;line-height:24px}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box.dark{background-color:#0a0b0d;color:#fff}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box.dark button{color:#0052ff}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box.light{background-color:#fff;color:#0a0b0d}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box.light button{color:#0052ff}";
class Gt {
  constructor() {
    this.root = null, this.darkMode = Fe();
  }
  attach() {
    const e = document.documentElement;
    this.root = document.createElement("div"), this.root.className = "-cbwsdk-css-reset", e.appendChild(this.root), He();
  }
  present(e) {
    this.render(e);
  }
  clear() {
    this.render(null);
  }
  render(e) {
    this.root && (X(null, this.root), e && X(w(Yt, Object.assign({}, e, { onDismiss: () => {
      this.clear();
    }, darkMode: this.darkMode })), this.root));
  }
}
const Yt = ({ title: n, buttonText: e, darkMode: t, onButtonClick: s, onDismiss: r }) => {
  const i = t ? "dark" : "light";
  return w(
    Ge,
    { darkMode: t },
    w(
      "div",
      { class: "-cbwsdk-redirect-dialog" },
      w("style", null, Ht),
      w("div", { class: "-cbwsdk-redirect-dialog-backdrop", onClick: r }),
      w(
        "div",
        { class: U("-cbwsdk-redirect-dialog-box", i) },
        w("p", null, n),
        w("button", { onClick: s }, e)
      )
    )
  );
}, $t = "https://keys.coinbase.com/connect", Jt = "http://rpc.wallet.coinbase.com", Se = "https://www.walletlink.org", Vt = "https://go.cb-w.com/walletlink";
class Ce {
  constructor() {
    this.attached = !1, this.redirectDialog = new Gt();
  }
  attach() {
    if (this.attached)
      throw new Error("Coinbase Wallet SDK UI is already attached");
    this.redirectDialog.attach(), this.attached = !0;
  }
  redirectToCoinbaseWallet(e) {
    const t = new URL(Vt);
    t.searchParams.append("redirect_url", Ot().href), e && t.searchParams.append("wl_url", e);
    const s = document.createElement("a");
    s.target = "cbw-opener", s.href = t.href, s.rel = "noreferrer noopener", s.click();
  }
  openCoinbaseWalletDeeplink(e) {
    this.redirectDialog.present({
      title: "Redirecting to Coinbase Wallet...",
      buttonText: "Open",
      onButtonClick: () => {
        this.redirectToCoinbaseWallet(e);
      }
    }), setTimeout(() => {
      this.redirectToCoinbaseWallet(e);
    }, 99);
  }
  showConnecting(e) {
    return () => {
      this.redirectDialog.clear();
    };
  }
}
class R {
  constructor(e) {
    this.chainCallbackParams = { chainId: "", jsonRpcUrl: "" }, this.isMobileWeb = jt(), this.linkedUpdated = (i) => {
      this.isLinked = i;
      const a = this.storage.getItem(se);
      if (i && (this._session.linked = i), this.isUnlinkedErrorState = !1, a) {
        const c = a.split(" "), l = this.storage.getItem("IsStandaloneSigning") === "true";
        c[0] !== "" && !i && this._session.linked && !l && (this.isUnlinkedErrorState = !0);
      }
    }, this.metadataUpdated = (i, a) => {
      this.storage.setItem(i, a);
    }, this.chainUpdated = (i, a) => {
      this.chainCallbackParams.chainId === i && this.chainCallbackParams.jsonRpcUrl === a || (this.chainCallbackParams = {
        chainId: i,
        jsonRpcUrl: a
      }, this.chainCallback && this.chainCallback(a, Number.parseInt(i, 10)));
    }, this.accountUpdated = (i) => {
      this.accountsCallback && this.accountsCallback([i]), R.accountRequestCallbackIds.size > 0 && (Array.from(R.accountRequestCallbackIds.values()).forEach((a) => {
        this.invokeCallback(a, {
          method: "requestEthereumAccounts",
          result: [i]
        });
      }), R.accountRequestCallbackIds.clear());
    }, this.resetAndReload = this.resetAndReload.bind(this), this.linkAPIUrl = e.linkAPIUrl, this.storage = e.storage, this.metadata = e.metadata, this.accountsCallback = e.accountsCallback, this.chainCallback = e.chainCallback;
    const { session: t, ui: s, connection: r } = this.subscribe();
    this._session = t, this.connection = r, this.relayEventManager = new Nt(), this.ui = s, this.ui.attach();
  }
  subscribe() {
    const e = O.load(this.storage) || O.create(this.storage), { linkAPIUrl: t } = this, s = new Tt({
      session: e,
      linkAPIUrl: t,
      listener: this
    }), r = this.isMobileWeb ? new Ce() : new Ft();
    return s.connect(), { session: e, ui: r, connection: s };
  }
  resetAndReload() {
    this.connection.destroy().then(() => {
      const e = O.load(this.storage);
      (e == null ? void 0 : e.id) === this._session.id && A.clearAll(), document.location.reload();
    }).catch((e) => {
    });
  }
  signEthereumTransaction(e) {
    return this.sendRequest({
      method: "signEthereumTransaction",
      params: {
        fromAddress: e.fromAddress,
        toAddress: e.toAddress,
        weiValue: M(e.weiValue),
        data: W(e.data, !0),
        nonce: e.nonce,
        gasPriceInWei: e.gasPriceInWei ? M(e.gasPriceInWei) : null,
        maxFeePerGas: e.gasPriceInWei ? M(e.gasPriceInWei) : null,
        maxPriorityFeePerGas: e.gasPriceInWei ? M(e.gasPriceInWei) : null,
        gasLimit: e.gasLimit ? M(e.gasLimit) : null,
        chainId: e.chainId,
        shouldSubmit: !1
      }
    });
  }
  signAndSubmitEthereumTransaction(e) {
    return this.sendRequest({
      method: "signEthereumTransaction",
      params: {
        fromAddress: e.fromAddress,
        toAddress: e.toAddress,
        weiValue: M(e.weiValue),
        data: W(e.data, !0),
        nonce: e.nonce,
        gasPriceInWei: e.gasPriceInWei ? M(e.gasPriceInWei) : null,
        maxFeePerGas: e.maxFeePerGas ? M(e.maxFeePerGas) : null,
        maxPriorityFeePerGas: e.maxPriorityFeePerGas ? M(e.maxPriorityFeePerGas) : null,
        gasLimit: e.gasLimit ? M(e.gasLimit) : null,
        chainId: e.chainId,
        shouldSubmit: !0
      }
    });
  }
  submitEthereumTransaction(e, t) {
    return this.sendRequest({
      method: "submitEthereumTransaction",
      params: {
        signedTransaction: W(e, !0),
        chainId: t
      }
    });
  }
  getWalletLinkSession() {
    return this._session;
  }
  sendRequest(e) {
    let t = null;
    const s = N(8), r = (i) => {
      this.publishWeb3RequestCanceledEvent(s), this.handleErrorResponse(s, e.method, i), t == null || t();
    };
    return new Promise((i, a) => {
      t = this.ui.showConnecting({
        isUnlinkedErrorState: this.isUnlinkedErrorState,
        onCancel: r,
        onResetConnection: this.resetAndReload
        // eslint-disable-line @typescript-eslint/unbound-method
      }), this.relayEventManager.callbacks.set(s, (c) => {
        if (t == null || t(), E(c))
          return a(new Error(c.errorMessage));
        i(c);
      }), this.publishWeb3RequestEvent(s, e);
    });
  }
  publishWeb3RequestEvent(e, t) {
    const s = { type: "WEB3_REQUEST", id: e, request: t };
    this.publishEvent("Web3Request", s, !0).then((r) => {
    }).catch((r) => {
      this.handleWeb3ResponseMessage(s.id, {
        method: t.method,
        errorMessage: r.message
      });
    }), this.isMobileWeb && this.openCoinbaseWalletDeeplink(t.method);
  }
  // copied from MobileRelay
  openCoinbaseWalletDeeplink(e) {
    if (this.ui instanceof Ce)
      switch (e) {
        case "requestEthereumAccounts":
        // requestEthereumAccounts is handled via popup
        case "switchEthereumChain":
          return;
        default:
          window.addEventListener("blur", () => {
            window.addEventListener("focus", () => {
              this.connection.checkUnseenEvents();
            }, { once: !0 });
          }, { once: !0 }), this.ui.openCoinbaseWalletDeeplink();
          break;
      }
  }
  publishWeb3RequestCanceledEvent(e) {
    const t = {
      type: "WEB3_REQUEST_CANCELED",
      id: e
    };
    this.publishEvent("Web3RequestCanceled", t, !1).then();
  }
  publishEvent(e, t, s) {
    return this.connection.publishEvent(e, t, s);
  }
  handleWeb3ResponseMessage(e, t) {
    if (t.method === "requestEthereumAccounts") {
      R.accountRequestCallbackIds.forEach((s) => this.invokeCallback(s, t)), R.accountRequestCallbackIds.clear();
      return;
    }
    this.invokeCallback(e, t);
  }
  handleErrorResponse(e, t, s) {
    var r;
    const i = (r = s == null ? void 0 : s.message) !== null && r !== void 0 ? r : "Unspecified error message.";
    this.handleWeb3ResponseMessage(e, {
      method: t,
      errorMessage: i
    });
  }
  invokeCallback(e, t) {
    const s = this.relayEventManager.callbacks.get(e);
    s && (s(t), this.relayEventManager.callbacks.delete(e));
  }
  requestEthereumAccounts() {
    const { appName: e, appLogoUrl: t } = this.metadata, s = {
      method: "requestEthereumAccounts",
      params: {
        appName: e,
        appLogoUrl: t
      }
    }, r = N(8);
    return new Promise((i, a) => {
      this.relayEventManager.callbacks.set(r, (c) => {
        if (E(c))
          return a(new Error(c.errorMessage));
        i(c);
      }), R.accountRequestCallbackIds.add(r), this.publishWeb3RequestEvent(r, s);
    });
  }
  watchAsset(e, t, s, r, i, a) {
    const c = {
      method: "watchAsset",
      params: {
        type: e,
        options: {
          address: t,
          symbol: s,
          decimals: r,
          image: i
        },
        chainId: a
      }
    };
    let l = null;
    const h = N(8), p = (f) => {
      this.publishWeb3RequestCanceledEvent(h), this.handleErrorResponse(h, c.method, f), l == null || l();
    };
    return l = this.ui.showConnecting({
      isUnlinkedErrorState: this.isUnlinkedErrorState,
      onCancel: p,
      onResetConnection: this.resetAndReload
      // eslint-disable-line @typescript-eslint/unbound-method
    }), new Promise((f, d) => {
      this.relayEventManager.callbacks.set(h, (o) => {
        if (l == null || l(), E(o))
          return d(new Error(o.errorMessage));
        f(o);
      }), this.publishWeb3RequestEvent(h, c);
    });
  }
  addEthereumChain(e, t, s, r, i, a) {
    const c = {
      method: "addEthereumChain",
      params: {
        chainId: e,
        rpcUrls: t,
        blockExplorerUrls: r,
        chainName: i,
        iconUrls: s,
        nativeCurrency: a
      }
    };
    let l = null;
    const h = N(8), p = (f) => {
      this.publishWeb3RequestCanceledEvent(h), this.handleErrorResponse(h, c.method, f), l == null || l();
    };
    return l = this.ui.showConnecting({
      isUnlinkedErrorState: this.isUnlinkedErrorState,
      onCancel: p,
      onResetConnection: this.resetAndReload
      // eslint-disable-line @typescript-eslint/unbound-method
    }), new Promise((f, d) => {
      this.relayEventManager.callbacks.set(h, (o) => {
        if (l == null || l(), E(o))
          return d(new Error(o.errorMessage));
        f(o);
      }), this.publishWeb3RequestEvent(h, c);
    });
  }
  switchEthereumChain(e, t) {
    const s = {
      method: "switchEthereumChain",
      params: Object.assign({ chainId: e }, { address: t })
    };
    let r = null;
    const i = N(8), a = (c) => {
      this.publishWeb3RequestCanceledEvent(i), this.handleErrorResponse(i, s.method, c), r == null || r();
    };
    return r = this.ui.showConnecting({
      isUnlinkedErrorState: this.isUnlinkedErrorState,
      onCancel: a,
      onResetConnection: this.resetAndReload
      // eslint-disable-line @typescript-eslint/unbound-method
    }), new Promise((c, l) => {
      this.relayEventManager.callbacks.set(i, (h) => {
        if (r == null || r(), E(h) && h.errorCode)
          return l(m.provider.custom({
            code: h.errorCode,
            message: "Unrecognized chain ID. Try adding the chain using addEthereumChain first."
          }));
        if (E(h))
          return l(new Error(h.errorMessage));
        c(h);
      }), this.publishWeb3RequestEvent(i, s);
    });
  }
}
R.accountRequestCallbackIds = /* @__PURE__ */ new Set();
const _e = "DefaultChainId", Me = "DefaultJsonRpcUrl";
class Ye {
  constructor(e) {
    this._relay = null, this._addresses = [], this.metadata = e.metadata, this._storage = new A("walletlink", Se), this.callback = e.callback || null;
    const t = this._storage.getItem(se);
    if (t) {
      const s = t.split(" ");
      s[0] !== "" && (this._addresses = s.map((r) => L(r)));
    }
    this.initializeRelay();
  }
  getSession() {
    const e = this.initializeRelay(), { id: t, secret: s } = e.getWalletLinkSession();
    return { id: t, secret: s };
  }
  async handshake() {
    await this._eth_requestAccounts();
  }
  get selectedAddress() {
    return this._addresses[0] || void 0;
  }
  get jsonRpcUrl() {
    var e;
    return (e = this._storage.getItem(Me)) !== null && e !== void 0 ? e : void 0;
  }
  set jsonRpcUrl(e) {
    this._storage.setItem(Me, e);
  }
  updateProviderInfo(e, t) {
    var s;
    this.jsonRpcUrl = e;
    const r = this.getChainId();
    this._storage.setItem(_e, t.toString(10)), q(t) !== r && ((s = this.callback) === null || s === void 0 || s.call(this, "chainChanged", T(t)));
  }
  async watchAsset(e) {
    const t = Array.isArray(e) ? e[0] : e;
    if (!t.type)
      throw m.rpc.invalidParams("Type is required");
    if ((t == null ? void 0 : t.type) !== "ERC20")
      throw m.rpc.invalidParams(`Asset of type '${t.type}' is not supported`);
    if (!(t != null && t.options))
      throw m.rpc.invalidParams("Options are required");
    if (!(t != null && t.options.address))
      throw m.rpc.invalidParams("Address is required");
    const s = this.getChainId(), { address: r, symbol: i, image: a, decimals: c } = t.options, h = await this.initializeRelay().watchAsset(t.type, r, i, c, a, s == null ? void 0 : s.toString());
    return E(h) ? !1 : !!h.result;
  }
  async addEthereumChain(e) {
    var t, s;
    const r = e[0];
    if (((t = r.rpcUrls) === null || t === void 0 ? void 0 : t.length) === 0)
      throw m.rpc.invalidParams("please pass in at least 1 rpcUrl");
    if (!r.chainName || r.chainName.trim() === "")
      throw m.rpc.invalidParams("chainName is a required field");
    if (!r.nativeCurrency)
      throw m.rpc.invalidParams("nativeCurrency is a required field");
    const i = Number.parseInt(r.chainId, 16);
    if (i === this.getChainId())
      return !1;
    const a = this.initializeRelay(), { rpcUrls: c = [], blockExplorerUrls: l = [], chainName: h, iconUrls: p = [], nativeCurrency: f } = r, d = await a.addEthereumChain(i.toString(), c, p, l, h, f);
    if (E(d))
      return !1;
    if (((s = d.result) === null || s === void 0 ? void 0 : s.isApproved) === !0)
      return this.updateProviderInfo(c[0], i), null;
    throw m.rpc.internal("unable to add ethereum chain");
  }
  async switchEthereumChain(e) {
    const t = e[0], s = Number.parseInt(t.chainId, 16), i = await this.initializeRelay().switchEthereumChain(s.toString(10), this.selectedAddress || void 0);
    if (E(i))
      throw i;
    const a = i.result;
    return a.isApproved && a.rpcUrl.length > 0 && this.updateProviderInfo(a.rpcUrl, s), null;
  }
  async cleanup() {
    this.callback = null, this._relay && this._relay.resetAndReload(), this._storage.clear();
  }
  _setAddresses(e, t) {
    var s;
    if (!Array.isArray(e))
      throw new Error("addresses is not an array");
    const r = e.map((i) => L(i));
    JSON.stringify(r) !== JSON.stringify(this._addresses) && (this._addresses = r, (s = this.callback) === null || s === void 0 || s.call(this, "accountsChanged", r), this._storage.setItem(se, r.join(" ")));
  }
  async request(e) {
    const t = e.params || [];
    switch (e.method) {
      case "eth_accounts":
        return [...this._addresses];
      case "eth_coinbase":
        return this.selectedAddress || null;
      case "net_version":
        return this.getChainId().toString(10);
      case "eth_chainId":
        return T(this.getChainId());
      case "eth_requestAccounts":
        return this._eth_requestAccounts();
      case "eth_ecRecover":
      case "personal_ecRecover":
        return this.ecRecover(e);
      case "personal_sign":
        return this.personalSign(e);
      case "eth_signTransaction":
        return this._eth_signTransaction(t);
      case "eth_sendRawTransaction":
        return this._eth_sendRawTransaction(t);
      case "eth_sendTransaction":
        return this._eth_sendTransaction(t);
      case "eth_signTypedData_v1":
      case "eth_signTypedData_v3":
      case "eth_signTypedData_v4":
      case "eth_signTypedData":
        return this.signTypedData(e);
      case "wallet_addEthereumChain":
        return this.addEthereumChain(t);
      case "wallet_switchEthereumChain":
        return this.switchEthereumChain(t);
      case "wallet_watchAsset":
        return this.watchAsset(t);
      default:
        if (!this.jsonRpcUrl)
          throw m.rpc.internal("No RPC URL set for chain");
        return de(e, this.jsonRpcUrl);
    }
  }
  _ensureKnownAddress(e) {
    const t = L(e);
    if (!this._addresses.map((r) => L(r)).includes(t))
      throw new Error("Unknown Ethereum address");
  }
  _prepareTransactionParams(e) {
    const t = e.from ? L(e.from) : this.selectedAddress;
    if (!t)
      throw new Error("Ethereum address is unavailable");
    this._ensureKnownAddress(t);
    const s = e.to ? L(e.to) : null, r = e.value != null ? j(e.value) : BigInt(0), i = e.data ? ne(e.data) : y.alloc(0), a = e.nonce != null ? q(e.nonce) : null, c = e.gasPrice != null ? j(e.gasPrice) : null, l = e.maxFeePerGas != null ? j(e.maxFeePerGas) : null, h = e.maxPriorityFeePerGas != null ? j(e.maxPriorityFeePerGas) : null, p = e.gas != null ? j(e.gas) : null, f = e.chainId ? q(e.chainId) : this.getChainId();
    return {
      fromAddress: t,
      toAddress: s,
      weiValue: r,
      data: i,
      nonce: a,
      gasPriceInWei: c,
      maxFeePerGas: l,
      maxPriorityFeePerGas: h,
      gasLimit: p,
      chainId: f
    };
  }
  async ecRecover(e) {
    const { method: t, params: s } = e;
    if (!Array.isArray(s))
      throw m.rpc.invalidParams();
    const i = await this.initializeRelay().sendRequest({
      method: "ethereumAddressFromSignedMessage",
      params: {
        message: G(s[0]),
        signature: G(s[1]),
        addPrefix: t === "personal_ecRecover"
      }
    });
    if (E(i))
      throw i;
    return i.result;
  }
  getChainId() {
    var e;
    return Number.parseInt((e = this._storage.getItem(_e)) !== null && e !== void 0 ? e : "1", 10);
  }
  async _eth_requestAccounts() {
    var e, t;
    if (this._addresses.length > 0)
      return (e = this.callback) === null || e === void 0 || e.call(this, "connect", { chainId: T(this.getChainId()) }), this._addresses;
    const r = await this.initializeRelay().requestEthereumAccounts();
    if (E(r))
      throw r;
    if (!r.result)
      throw new Error("accounts received is empty");
    return this._setAddresses(r.result), (t = this.callback) === null || t === void 0 || t.call(this, "connect", { chainId: T(this.getChainId()) }), this._addresses;
  }
  async personalSign({ params: e }) {
    if (!Array.isArray(e))
      throw m.rpc.invalidParams();
    const t = e[1], s = e[0];
    this._ensureKnownAddress(t);
    const i = await this.initializeRelay().sendRequest({
      method: "signEthereumMessage",
      params: {
        address: L(t),
        message: G(s),
        addPrefix: !0,
        typedDataJson: null
      }
    });
    if (E(i))
      throw i;
    return i.result;
  }
  async _eth_signTransaction(e) {
    const t = this._prepareTransactionParams(e[0] || {}), r = await this.initializeRelay().signEthereumTransaction(t);
    if (E(r))
      throw r;
    return r.result;
  }
  async _eth_sendRawTransaction(e) {
    const t = ne(e[0]), r = await this.initializeRelay().submitEthereumTransaction(t, this.getChainId());
    if (E(r))
      throw r;
    return r.result;
  }
  async _eth_sendTransaction(e) {
    const t = this._prepareTransactionParams(e[0] || {}), r = await this.initializeRelay().signAndSubmitEthereumTransaction(t);
    if (E(r))
      throw r;
    return r.result;
  }
  async signTypedData(e) {
    const { method: t, params: s } = e;
    if (!Array.isArray(s))
      throw m.rpc.invalidParams();
    const r = (h) => {
      const p = {
        eth_signTypedData_v1: B.hashForSignTypedDataLegacy,
        eth_signTypedData_v3: B.hashForSignTypedData_v3,
        eth_signTypedData_v4: B.hashForSignTypedData_v4,
        eth_signTypedData: B.hashForSignTypedData_v4
      };
      return W(p[t]({
        data: lt(h)
      }), !0);
    }, i = s[t === "eth_signTypedData_v1" ? 1 : 0], a = s[t === "eth_signTypedData_v1" ? 0 : 1];
    this._ensureKnownAddress(i);
    const l = await this.initializeRelay().sendRequest({
      method: "signEthereumMessage",
      params: {
        address: L(i),
        message: r(a),
        typedDataJson: JSON.stringify(a, null, 2),
        addPrefix: !1
      }
    });
    if (E(l))
      throw l;
    return l.result;
  }
  initializeRelay() {
    return this._relay || (this._relay = new R({
      linkAPIUrl: Se,
      storage: this._storage,
      metadata: this.metadata,
      accountsCallback: this._setAddresses.bind(this),
      chainCallback: this.updateProviderInfo.bind(this)
    })), this._relay;
  }
}
const $e = "SignerType", Je = new A("CBWSDK", "SignerConfigurator");
function Qt() {
  return Je.getItem($e);
}
function Zt(n) {
  Je.setItem($e, n);
}
async function Xt(n) {
  const { communicator: e, metadata: t, handshakeRequest: s, callback: r } = n;
  tn(e, t, r).catch(() => {
  });
  const i = {
    id: crypto.randomUUID(),
    event: "selectSignerType",
    data: Object.assign(Object.assign({}, n.preference), { handshakeRequest: s })
  }, { data: a } = await e.postRequestAndWaitForResponse(i);
  return a;
}
function en(n) {
  const { signerType: e, metadata: t, communicator: s, callback: r } = n;
  switch (e) {
    case "scw":
      return new It({
        metadata: t,
        callback: r,
        communicator: s
      });
    case "walletlink":
      return new Ye({
        metadata: t,
        callback: r
      });
  }
}
async function tn(n, e, t) {
  await n.onMessage(({ event: r }) => r === "WalletLinkSessionRequest");
  const s = new Ye({
    metadata: e,
    callback: t
  });
  n.postMessage({
    event: "WalletLinkUpdate",
    data: { session: s.getSession() }
  }), await s.handshake(), n.postMessage({
    event: "WalletLinkUpdate",
    data: { connected: !0 }
  });
}
const nn = `Coinbase Wallet SDK requires the Cross-Origin-Opener-Policy header to not be set to 'same-origin'. This is to ensure that the SDK can communicate with the Coinbase Smart Wallet app.

Please see https://www.smartwallet.dev/guides/tips/popup-tips#cross-origin-opener-policy for more information.`, sn = () => {
  let n;
  return {
    getCrossOriginOpenerPolicy: () => n === void 0 ? "undefined" : n,
    checkCrossOriginOpenerPolicy: async () => {
      if (typeof window > "u") {
        n = "non-browser-env";
        return;
      }
      try {
        const e = `${window.location.origin}${window.location.pathname}`, t = await fetch(e, {
          method: "HEAD"
        });
        if (!t.ok)
          throw new Error(`HTTP error! status: ${t.status}`);
        const s = t.headers.get("Cross-Origin-Opener-Policy");
        n = s ?? "null", n === "same-origin" && console.error(nn);
      } catch (e) {
        console.error("Error checking Cross-Origin-Opener-Policy:", e.message), n = "error";
      }
    }
  };
}, { checkCrossOriginOpenerPolicy: rn, getCrossOriginOpenerPolicy: an } = sn(), Ae = 420, xe = 540;
function on(n) {
  const e = (window.innerWidth - Ae) / 2 + window.screenX, t = (window.innerHeight - xe) / 2 + window.screenY;
  dn(n);
  const s = `wallet_${crypto.randomUUID()}`, r = window.open(n, s, `width=${Ae}, height=${xe}, left=${e}, top=${t}`);
  if (r == null || r.focus(), !r)
    throw m.rpc.internal("Pop up window failed to open");
  return r;
}
function cn(n) {
  n && !n.closed && n.close();
}
function dn(n) {
  const e = {
    sdkName: ze,
    sdkVersion: z,
    origin: window.location.origin,
    coop: an()
  };
  for (const [t, s] of Object.entries(e))
    n.searchParams.append(t, s.toString());
}
class ln {
  constructor({ url: e = $t, metadata: t, preference: s }) {
    this.popup = null, this.listeners = /* @__PURE__ */ new Map(), this.postMessage = async (r) => {
      (await this.waitForPopupLoaded()).postMessage(r, this.url.origin);
    }, this.postRequestAndWaitForResponse = async (r) => {
      const i = this.onMessage(({ requestId: a }) => a === r.id);
      return this.postMessage(r), await i;
    }, this.onMessage = async (r) => new Promise((i, a) => {
      const c = (l) => {
        if (l.origin !== this.url.origin)
          return;
        const h = l.data;
        r(h) && (i(h), window.removeEventListener("message", c), this.listeners.delete(c));
      };
      window.addEventListener("message", c), this.listeners.set(c, { reject: a });
    }), this.disconnect = () => {
      cn(this.popup), this.popup = null, this.listeners.forEach(({ reject: r }, i) => {
        r(m.provider.userRejectedRequest("Request rejected")), window.removeEventListener("message", i);
      }), this.listeners.clear();
    }, this.waitForPopupLoaded = async () => this.popup && !this.popup.closed ? (this.popup.focus(), this.popup) : (this.popup = on(this.url), this.onMessage(({ event: r }) => r === "PopupUnload").then(this.disconnect).catch(() => {
    }), this.onMessage(({ event: r }) => r === "PopupLoaded").then((r) => {
      this.postMessage({
        requestId: r.id,
        data: {
          version: z,
          metadata: this.metadata,
          preference: this.preference,
          location: window.location.toString()
        }
      });
    }).then(() => {
      if (!this.popup)
        throw m.rpc.internal();
      return this.popup;
    })), this.url = new URL(e), this.metadata = t, this.preference = s;
  }
}
function un(n) {
  const e = rt(hn(n), {
    shouldIncludeStack: !0
  }), t = new URL("https://docs.cloud.coinbase.com/wallet-sdk/docs/errors");
  return t.searchParams.set("version", z), t.searchParams.set("code", e.code.toString()), t.searchParams.set("message", e.message), Object.assign(Object.assign({}, e), { docUrl: t.href });
}
function hn(n) {
  var e;
  if (typeof n == "string")
    return {
      message: n,
      code: v.rpc.internal
    };
  if (E(n)) {
    const t = n.errorMessage, s = (e = n.errorCode) !== null && e !== void 0 ? e : t.match(/(denied|rejected)/i) ? v.provider.userRejectedRequest : void 0;
    return Object.assign(Object.assign({}, n), {
      message: t,
      code: s,
      data: { method: n.method }
    });
  }
  return n;
}
class pn extends et {
}
var fn = function(n, e) {
  var t = {};
  for (var s in n) Object.prototype.hasOwnProperty.call(n, s) && e.indexOf(s) < 0 && (t[s] = n[s]);
  if (n != null && typeof Object.getOwnPropertySymbols == "function")
    for (var r = 0, s = Object.getOwnPropertySymbols(n); r < s.length; r++)
      e.indexOf(s[r]) < 0 && Object.prototype.propertyIsEnumerable.call(n, s[r]) && (t[s[r]] = n[s[r]]);
  return t;
};
class gn extends pn {
  constructor(e) {
    var { metadata: t } = e, s = e.preference, { keysUrl: r } = s, i = fn(s, ["keysUrl"]);
    super(), this.signer = null, this.isCoinbaseWallet = !0, this.metadata = t, this.preference = i, this.communicator = new ln({
      url: r,
      metadata: t,
      preference: i
    });
    const a = Qt();
    a && (this.signer = this.initSigner(a));
  }
  async request(e) {
    try {
      if (Et(e), !this.signer)
        switch (e.method) {
          case "eth_requestAccounts": {
            const t = await this.requestSignerSelection(e), s = this.initSigner(t);
            await s.handshake(e), this.signer = s, Zt(t);
            break;
          }
          case "wallet_sendCalls": {
            const t = this.initSigner("scw");
            await t.handshake({ method: "handshake" });
            const s = await t.request(e);
            return await t.cleanup(), s;
          }
          case "wallet_getCallsStatus":
            return de(e, Jt);
          case "net_version":
            return 1;
          // default value
          case "eth_chainId":
            return T(1);
          // default value
          default:
            throw m.provider.unauthorized("Must call 'eth_requestAccounts' before other methods");
        }
      return await this.signer.request(e);
    } catch (t) {
      const { code: s } = t;
      return s === v.provider.unauthorized && this.disconnect(), Promise.reject(un(t));
    }
  }
  /** @deprecated Use `.request({ method: 'eth_requestAccounts' })` instead. */
  async enable() {
    return console.warn('.enable() has been deprecated. Please use .request({ method: "eth_requestAccounts" }) instead.'), await this.request({
      method: "eth_requestAccounts"
    });
  }
  async disconnect() {
    var e;
    await ((e = this.signer) === null || e === void 0 ? void 0 : e.cleanup()), this.signer = null, A.clearAll(), this.emit("disconnect", m.provider.disconnected("User initiated disconnection"));
  }
  requestSignerSelection(e) {
    return Xt({
      communicator: this.communicator,
      preference: this.preference,
      metadata: this.metadata,
      handshakeRequest: e,
      callback: this.emit.bind(this)
    });
  }
  initSigner(e) {
    return en({
      signerType: e,
      metadata: this.metadata,
      communicator: this.communicator,
      callback: this.emit.bind(this)
    });
  }
}
function mn(n) {
  if (n) {
    if (!["all", "smartWalletOnly", "eoaOnly"].includes(n.options))
      throw new Error(`Invalid options: ${n.options}`);
    if (n.attribution && n.attribution.auto !== void 0 && n.attribution.dataSuffix !== void 0)
      throw new Error("Attribution cannot contain both auto and dataSuffix properties");
  }
}
function bn(n) {
  var e;
  const t = {
    metadata: n.metadata,
    preference: n.preference
  };
  return (e = kt(t)) !== null && e !== void 0 ? e : new gn(t);
}
const wn = {
  options: "all"
};
function In(n) {
  var e;
  new A("CBWSDK").setItem("VERSION", z), rn();
  const s = {
    metadata: {
      appName: n.appName || "Dapp",
      appLogoUrl: n.appLogoUrl || "",
      appChainIds: n.appChainIds || []
    },
    preference: Object.assign(wn, (e = n.preference) !== null && e !== void 0 ? e : {})
  };
  mn(s.preference);
  let r = null;
  return {
    getProvider: () => (r || (r = bn(s)), r)
  };
}
export {
  In as createCoinbaseWalletSDK
};
//# sourceMappingURL=index-BUC8k4WZ.js.map
