@daimo/pay
Version:
Seamless crypto payments. Onboard users from any chain, any coin into your app with one click.
279 lines (276 loc) • 11.1 kB
JavaScript
import { jsx } from 'react/jsx-runtime';
import { assertNotNull } from '@daimo/pay-common';
import { useWallet as useWallet$1 } from '@solana/wallet-adapter-react';
import Logos, { WalletIcon, createOtherWalletsIcon, SquircleIcon } from '../assets/logos.js';
import { MobileWithLogos } from '../assets/MobileWithLogos.js';
import { MOBILE_WALLETS_THRESHOLD_FOR_OTHER, WALLET_ID_OTHER_WALLET, RABBY_CONNECTOR_ID, WALLET_ID_MOBILE_WALLETS } from '../constants/wallets.js';
import { useConnectors } from '../hooks/useConnectors.js';
import useLocales from '../hooks/useLocales.js';
import { usePayContext } from '../hooks/usePayContext.js';
import { isBaseAccountConnector, isGeminiConnector, isInjectedConnector, flattenChildren } from '../utils/index.js';
import { walletConfigs } from './walletConfigs.js';
function isExternalWallet(wallet) {
if (!wallet) return false;
if (wallet.id === WALLET_ID_MOBILE_WALLETS) return true;
const hasConnector = "connector" in wallet && !!wallet.connector;
return !!wallet.getDaimoPayDeeplink && !hasConnector;
}
const useWallet = (id) => {
const wallets = useWallets();
const wallet = wallets.find((c) => c.id === id);
if (!wallet) return null;
return wallet;
};
const useWallets = (isMobile) => {
const connectors = useConnectors();
const context = usePayContext();
const { disableMobileInjector, paymentState } = context;
const solanaWallet = useWallet$1();
const locales = useLocales();
const prioritizedId = context.paymentState.buttonProps?.prioritizedWalletId;
const walletOrder = paymentState?.externalPaymentOptions?.parsedConfig?.walletOrder ?? [];
function movePrioritizedFirst(list, prioritizedId2) {
if (!prioritizedId2) return;
if (prioritizedId2 === WALLET_ID_MOBILE_WALLETS) return;
const index = list.findIndex((w) => w.id === prioritizedId2);
if (index <= 0) return;
const [item] = list.splice(index, 1);
list.unshift(item);
}
if (isMobile) {
let addIfNotPresent2 = function(idList) {
if (mobileWallets.find((w) => idList.includes(w.id))) return;
if (mobileWallets.length >= MOBILE_WALLETS_THRESHOLD_FOR_OTHER) return;
const wallet = assertNotNull(
walletConfigs[idList],
() => `missing ${idList}`
);
mobileWallets.push({
id: idList,
...wallet
});
};
const mobileWallets = [];
if (!disableMobileInjector) {
connectors.forEach((connector) => {
if (isBaseAccountConnector(connector.id)) return;
if (isGeminiConnector(connector.id)) return;
if (!isInjectedConnector(connector.type)) return;
if (mobileWallets.some((w) => w.id === connector.id)) return;
if (connector.name?.toLowerCase().includes("walletconnect")) return;
mobileWallets.push({
id: connector.id,
connector,
shortName: connector.name,
iconConnector: connector.icon ? /* @__PURE__ */ jsx("img", { src: connector.icon, alt: connector.name }) : /* @__PURE__ */ jsx(WalletIcon, {}),
iconShape: "squircle"
});
});
}
if (walletOrder.length > 0) {
for (const optionId of walletOrder) {
const walletId = Object.keys(walletConfigs).find((id) => {
const wallet = walletConfigs[id];
const optionLower = optionId.toLowerCase();
return wallet.name?.toLowerCase() === optionLower || wallet.shortName?.toLowerCase() === optionLower || wallet.name?.toLowerCase().includes(optionLower) || id.toLowerCase() === optionLower || id.toLowerCase().includes(optionLower);
});
if (walletId && !mobileWallets.find((w) => w.id === walletId)) {
const wallet = walletConfigs[walletId];
mobileWallets.push({
id: walletId,
...wallet
});
}
}
movePrioritizedFirst(mobileWallets, prioritizedId);
const totalWallets = walletOrder.length;
if (totalWallets > MOBILE_WALLETS_THRESHOLD_FOR_OTHER || mobileWallets.length > MOBILE_WALLETS_THRESHOLD_FOR_OTHER) {
const shownWallets = mobileWallets.slice(
0,
MOBILE_WALLETS_THRESHOLD_FOR_OTHER - 1
);
const shownWalletNames = shownWallets.map((w) => w.name?.toLowerCase() || w.shortName?.toLowerCase()).filter((name) => !!name);
const remainingWalletConfigs = walletOrder.filter((walletName) => {
const nameLower = walletName.toLowerCase();
return !shownWalletNames.some(
(shown) => shown === nameLower || shown.includes(nameLower) || nameLower.includes(shown)
);
}).map((walletName) => {
const configKey = Object.keys(walletConfigs).find((key) => {
const wallet = walletConfigs[key];
const name = wallet.name?.toLowerCase() || wallet.shortName?.toLowerCase() || "";
return name.includes(walletName.toLowerCase()) || walletName.toLowerCase().includes(name);
});
return configKey ? walletConfigs[configKey] : null;
}).filter(Boolean);
if (mobileWallets.length > MOBILE_WALLETS_THRESHOLD_FOR_OTHER - 1) {
mobileWallets.splice(MOBILE_WALLETS_THRESHOLD_FOR_OTHER - 1);
}
const otherWalletsString2 = flattenChildren(locales.otherWallets).join(
""
);
const otherString2 = flattenChildren(locales.other).join("");
mobileWallets.push({
id: WALLET_ID_OTHER_WALLET,
name: otherWalletsString2,
shortName: otherString2,
iconConnector: createOtherWalletsIcon(remainingWalletConfigs),
iconShape: "square",
showInMobileConnectors: false
});
}
return mobileWallets;
}
addIfNotPresent2(
"metaMask, metaMask-io, io.metamask, io.metamask.mobile, metaMaskSDK"
);
addIfNotPresent2("com.trustwallet.app");
movePrioritizedFirst(mobileWallets, prioritizedId);
const otherWalletsString = flattenChildren(locales.otherWallets).join("");
const otherString = flattenChildren(locales.other).join("");
mobileWallets.push({
id: WALLET_ID_OTHER_WALLET,
name: otherWalletsString,
shortName: otherString,
iconConnector: /* @__PURE__ */ jsx(Logos.OtherWallets, {}),
iconShape: "square",
showInMobileConnectors: false
});
return mobileWallets;
}
const wallets = connectors.map((connector) => {
let walletConfigKey = Object.keys(walletConfigs).find(
(id) => id.split(",").map((i) => i.trim()).includes(connector.id)
);
if (!walletConfigKey && connector.name) {
walletConfigKey = Object.keys(walletConfigs).find((key) => {
const cfgName = walletConfigs[key].name?.toLowerCase();
const connName = connector.name.toLowerCase();
return cfgName && (cfgName.includes(connName) || connName.includes(cfgName));
});
}
const c = {
id: connector.id,
name: connector.name ?? connector.id ?? connector.type,
icon: connector.icon ? /* @__PURE__ */ jsx(
"img",
{
src: connector.icon,
alt: connector.name,
width: "100%",
height: "100%"
}
) : /* @__PURE__ */ jsx(WalletIcon, {}),
connector,
iconShape: connector.id === RABBY_CONNECTOR_ID ? "circle" : "squircle",
isInstalled: connector.type === "mock" || connector.type === "injected" || connector.type === "farcasterFrame" || isBaseAccountConnector(connector.id) || isGeminiConnector(connector.id)
};
if (walletConfigKey) {
const wallet = walletConfigs[walletConfigKey];
return {
...c,
iconConnector: connector.icon ? /* @__PURE__ */ jsx(
"img",
{
src: connector.icon,
alt: connector.name,
width: "100%",
height: "100%"
}
) : void 0,
...wallet
};
}
return c;
});
wallets.push(walletConfigs.world);
wallets.push(walletConfigs.minipay);
wallets.push({
id: WALLET_ID_MOBILE_WALLETS,
name: "Mobile Wallets",
shortName: "Mobile",
icon: /* @__PURE__ */ jsx(
"div",
{
style: {
width: "100%",
height: "100%",
overflow: "hidden",
display: "flex",
alignItems: "center",
justifyContent: "center",
transform: "scale(1.2)",
transformOrigin: "center center"
},
children: /* @__PURE__ */ jsx(MobileWithLogos, {})
}
),
iconConnector: /* @__PURE__ */ jsx(
"div",
{
style: {
width: "100%",
height: "100%",
overflow: "hidden",
display: "flex",
alignItems: "center",
justifyContent: "center",
transform: "scale(1.2)",
transformOrigin: "center center"
},
children: /* @__PURE__ */ jsx(MobileWithLogos, {})
}
)
});
const solanaAdapters = solanaWallet.wallets ?? [];
wallets.forEach((w) => {
if (!w.name) return;
const evm = w.name.toLowerCase();
const match = solanaAdapters.find((sw) => {
const sol = sw.adapter.name.toLowerCase();
return evm.includes(sol) || sol.includes(evm);
});
if (match) {
w.solanaConnectorName = match.adapter.name;
}
});
const unmatched = solanaAdapters.filter(
(sw) => !wallets.find((w) => w.solanaConnectorName === sw.adapter.name)
);
unmatched.forEach((sw) => {
wallets.push({
id: `solana-${sw.adapter.name}`,
name: sw.adapter.name,
shortName: sw.adapter.name,
icon: /* @__PURE__ */ jsx(SquircleIcon, { icon: sw.adapter.icon, alt: sw.adapter.name }),
iconConnector: /* @__PURE__ */ jsx(SquircleIcon, { icon: sw.adapter.icon, alt: sw.adapter.name }),
iconShape: "squircle",
solanaConnectorName: sw.adapter.name
});
});
const sorted = wallets.filter(
(wallet, index, self) => self.findIndex((w) => w.id === wallet.id) === index
).filter(
(wallet, index, self) => !(wallet.id === "coinbaseWalletSDK" && self.find((w) => w.id === "com.coinbase.wallet"))
).filter(
(wallet, index, self) => !((wallet.id === "metaMaskSDK" || wallet.id === "metaMask") && self.find(
(w) => w.id === "io.metamask" || w.id === "io.metamask.mobile"
))
).filter(
(wallet, index, self) => !(wallet.id === "com.warpcast.mobile" && self.find((w) => w.id === "farcaster"))
).sort((a, b) => {
const aIsInstalledInjected = a.isInstalled && isInjectedConnector(a.connector?.type);
const bIsInstalledInjected = b.isInstalled && isInjectedConnector(b.connector?.type);
if (aIsInstalledInjected && !bIsInstalledInjected) return -1;
if (!aIsInstalledInjected && bIsInstalledInjected) return 1;
return 0;
}).sort((a, b) => {
if (a.id === WALLET_ID_MOBILE_WALLETS) return 1;
if (b.id === WALLET_ID_MOBILE_WALLETS) return -1;
return 0;
});
movePrioritizedFirst(sorted, prioritizedId);
return sorted;
};
export { isExternalWallet, useWallet, useWallets };
//# sourceMappingURL=useWallets.js.map