@better-auth-ui/core
Version:
Authentication components and data utilities for [Better Auth](https://better-auth.com), available for React and Solid.
157 lines (156 loc) • 4.7 kB
JavaScript
import { t as e } from "../../auth-query-keys-B_bcsA3s.js";
import { t } from "../../create-auth-plugin-BXWyolC8.js";
import { skipToken as n } from "@tanstack/query-core";
//#region src/plugins/siwe/siwe-localization.ts
var r = {
ethereum: "Ethereum",
continueWithEthereum: "Continue with Ethereum",
signMessage: "Sign message",
email: "Email",
emailDescription: "Add an email address to this wallet account.",
emailOptional: "Email (optional)",
wallets: "Wallets",
walletsDescription: "Manage the Ethereum wallets connected to your account.",
connectWallet: "Connect wallet",
noWallets: "No Ethereum wallets are connected.",
primary: "Primary",
setPrimary: "Set as primary",
removeWallet: "Remove wallet",
removeWalletTitle: "Remove Ethereum wallet?",
removeWalletWarning: "You will not be able to sign in with this wallet until you connect it again.",
chain: "Chain {{chainId}}"
}, i = {
all: (t) => [...e.user(t), "siwe"],
wallets: (e) => [...i.all(e), "wallets"]
}, a = (e, t) => ({
queryKey: i.wallets(t),
queryFn: e && t ? ({ signal: t }) => e.list(t) : n
}), o = (e) => {
if (e) return e;
let t = globalThis.ethereum;
if (!t) throw Error("No injected Ethereum wallet was found.");
return t;
}, s = (e) => {
let t = new TextEncoder().encode(e);
return `0x${Array.from(t, (e) => e.toString(16).padStart(2, "0")).join("")}`;
};
function c(e = {}) {
return {
id: e.id ?? "injected",
label: e.label ?? "Ethereum wallet",
async connect() {
let t = o(e.provider), n = await t.request({ method: "eth_requestAccounts" }), r = await t.request({ method: "eth_chainId" }), i = Array.isArray(n) ? n[0] : void 0;
if (typeof i != "string" || typeof r != "string") throw Error("The wallet returned an invalid account or chain.");
return {
address: i,
chainId: Number.parseInt(r, 16)
};
},
async signMessage({ address: t, message: n }) {
let r = await o(e.provider).request({
method: "personal_sign",
params: [s(n), t]
});
if (typeof r != "string") throw Error("The wallet returned an invalid signature.");
return r;
}
};
}
function l(e) {
let t = `${e.domain} wants you to sign in with your Ethereum account:`, n = [
`URI: ${e.uri}`,
"Version: 1",
`Chain ID: ${e.chainId}`,
`Nonce: ${e.nonce}`,
`Issued At: ${(e.issuedAt ?? /* @__PURE__ */ new Date()).toISOString()}`
];
return e.expirationTime && n.push(`Expiration Time: ${e.expirationTime.toISOString()}`), e.notBefore && n.push(`Not Before: ${e.notBefore.toISOString()}`), e.requestId && n.push(`Request ID: ${e.requestId}`), e.resources?.length && n.push("Resources:", ...e.resources.map((e) => `- ${e}`)), [
t,
e.address,
e.statement,
n.join("\n")
].filter((e) => e !== void 0).join("\n\n");
}
//#endregion
//#region src/plugins/siwe/siwe-mutation-options.ts
var u = {
all: ["auth", "siwe"],
signIn: [
"auth",
"siwe",
"signIn"
],
link: [
"auth",
"siwe",
"link"
],
unlink: [
"auth",
"siwe",
"unlink"
],
setPrimary: [
"auth",
"siwe",
"setPrimary"
]
}, d = (t, n) => ({
mutationKey: u.signIn,
mutationFn: async ({ email: e } = {}) => {
let r = await n.connector.connect(), i = (await t.siwe.nonce({ fetchOptions: { throw: !0 } })).data?.nonce;
if (!i) throw Error("The SIWE nonce response was empty.");
let a = l({
...r,
domain: n.domain,
uri: n.uri,
nonce: i,
statement: n.statement
}), o = await n.connector.signMessage({
address: r.address,
message: a
});
return t.siwe.verify({
message: a,
signature: o,
...e ? { email: e } : {},
fetchOptions: { throw: !0 }
});
},
meta: { awaits: [e.session] }
}), f = (e) => ({ awaits: [i.wallets(e)] }), p = (e, t, n) => ({
mutationKey: u.link,
mutationFn: async () => {
let n = await t.connect(), r = await e.createLinkChallenge(n), i = await t.signMessage({
address: n.address,
message: r.message
});
return e.link({
...n,
message: r.message,
signature: i
});
},
meta: f(n)
}), m = (e, t) => ({
mutationKey: u.unlink,
mutationFn: (t) => e.unlink(t),
meta: f(t)
}), h = (e, t) => ({
mutationKey: u.setPrimary,
mutationFn: (t) => e.setPrimary(t),
meta: f(t)
}), g = t("siwe", (e) => ({
connector: e.connector,
domain: e.domain,
uri: e.uri,
statement: e.statement,
email: e.email ?? "optional",
walletManager: e.walletManager,
localization: {
...r,
...e.localization
}
}));
//#endregion
export { c as createEip1193WalletConnector, l as createSiweMessage, p as linkSiweWalletOptions, h as setPrimarySiweWalletOptions, d as signInSiweOptions, r as siweLocalization, u as siweMutationKeys, g as siwePlugin, i as siweQueryKeys, a as siweWalletsOptions, m as unlinkSiweWalletOptions };