import { transformSync } from 'esbuild' import { normalizeWalletPayloadPurpose } from '../walletPurposeCompat.js' import { loadWalletPageSource } from './walletPageSource.js' const WALLET_HTML = loadWalletHtml() function injectWalletConfig(shell: string, title: string, sessionToken: string, payload: Record): string { const config = JSON.stringify({ sessionToken, ...normalizeWalletPayloadPurpose(payload) }).replaceAll('<', '\\u003c') const injection = `` return shell .replace(/.*?<\/title>/, `<title>${escapeHtml(title)}`) .replace('', `\n ${injection}`) } export function walletPage(title: string, sessionToken: string, payload: Record): string { return injectWalletConfig(WALLET_HTML, title, sessionToken, payload) } export function walletPageFresh(title: string, sessionToken: string, payload: Record): string { return injectWalletConfig(loadWalletHtml(), title, sessionToken, payload) } export function __testWalletPage(title: string, sessionToken: string, payload: Record): string { return walletPage(title, sessionToken, payload) } function loadWalletHtml(): string { const compiled = transformSync(loadWalletPageSource(), { loader: 'ts', target: 'es2020', }).code return wrapInWalletShell(compiled) } function wrapInWalletShell(compiledJs: string): string { const safeJs = compiledJs.replaceAll(' Wallet Request ` } function escapeHtml(value: string): string { return value .replaceAll('&', '&') .replaceAll('<', '<') .replaceAll('>', '>') .replaceAll('"', '"') }