// Both the vault workspace and example dapp discover only the capabilities they need. const ids = new WeakMap(); let nextId = 0; const chain = 'solana:devnet'; function supportsAction(chains, features, capability) { const action = features?.['solana:' + capability]; return Array.isArray(chains) && chains.includes(chain) && typeof action?.[capability] === 'function' && (capability !== 'signTransaction' || (Array.isArray(action.supportedTransactionVersions) && action.supportedTransactionVersions.includes('legacy'))); } function eligible(account, capability) { return typeof account?.address === 'string' && Array.isArray(account.chains) && account.chains.includes(chain) && Array.isArray(account.features) && account.features.includes('solana:' + capability); } export function walletChoices(registry, capability, host = globalThis.window) { if (!host) return []; const choices = []; const phantom = host.phantom?.solana ?? (host.solana?.isPhantom ? host.solana : null); const usePhantomExtension = capability === 'signTransaction' && typeof phantom?.connect === 'function' && typeof phantom.signTransaction === 'function'; for (const wallet of registry.get()) { if (typeof wallet?.name !== 'string' || typeof wallet.features?.['standard:connect']?.connect !== 'function' || !supportsAction(wallet.chains,wallet.features,capability)) continue; // Phantom's injected API operates on the account selected inside the extension. if (usePhantomExtension && wallet.name.toLowerCase() === 'phantom') continue; if (!ids.has(wallet)) ids.set(wallet, 'standard-' + ++nextId); choices.push({id:ids.get(wallet),name:wallet.name,available:true,standard:wallet}); } for (const [id,name,provider] of [ ['phantom','Phantom',phantom], ['solflare','Solflare',host.solflare?.isSolflare ? host.solflare : null], ]) { const available = typeof provider?.connect === 'function' && typeof provider?.[capability] === 'function'; if (!choices.some(choice=>choice.name.toLowerCase() === id)) choices.push({id,name,provider,available}); } return choices; } export async function openWallet(choice, capability, requestedAddress) { if (!choice?.available) throw Error('Enable your wallet extension or open this page in its wallet browser.'); if (!choice.standard) { const p = choice.provider; const connected = await p.connect(); const address = p.publicKey?.toBase58(); if (!address || (requestedAddress !== undefined && requestedAddress !== address) || (connected?.publicKey && connected.publicKey.toBase58() !== address)) throw Error('Select your wallet account and reconnect.'); function checkAccount() { if (p.publicKey?.toBase58() !== address) throw Error('Wallet account changed. Reconnect to continue.'); } return { get address(){return p.publicKey?.toBase58() === address ? address : undefined;}, disconnect:()=>p.disconnect?.(), ...(typeof p.signMessage === 'function' && {signMessage:async bytes=>{ checkAccount(); const signed = await p.signMessage(bytes,'utf8'); checkAccount(); return signed.signature ?? signed; }}), ...(typeof p.signTransaction === 'function' && {signTransaction:async tx=>{ checkAccount(); const signed = await p.signTransaction(tx); checkAccount(); return signed; }}), watch:fn=>{ const changed = key=>{if (key?.toBase58?.() !== address) fn();}; p.on?.('accountChanged',changed); p.on?.('disconnect',fn); return ()=>{p.removeListener?.('accountChanged',changed);p.removeListener?.('disconnect',fn);}; }, }; } const w = choice.standard; const {accounts} = requestedAddress === undefined ? await w.features['standard:connect'].connect() : w; const candidates = accounts.filter(account => eligible(account, capability)); if (candidates.length === 0) throw Error('Choose an account that supports this action on Solana devnet.'); if (requestedAddress === undefined && capability === 'signTransaction' && candidates.length > 1) { const error = new Error('Choose the vault account you want to use.'); error.code = 'SOLBOUND_ACCOUNT_SELECTION_REQUIRED'; error.accounts = candidates.map(account=>account.address); throw error; } const selected = requestedAddress === undefined ? candidates[0] : candidates.find(account=>account.address===requestedAddress); if (!selected) throw Error('That vault account is no longer available. Reconnect your wallet.'); const address = selected.address; function current(action = capability, change = {}) { if (!supportsAction(change.chains ?? w.chains, change.features ?? w.features, action)) return undefined; return (change.accounts ?? w.accounts).find(account => account.address === address && eligible(account,action)); } function checked(action) { const account = current(action); if (!account) throw Error('Wallet account or signing capability changed. Reconnect to continue.'); return account; } checked(capability); return { get address(){return current()?.address;}, disconnect:()=>w.features['standard:disconnect']?.disconnect(), watch:fn=>w.features['standard:events']?.on('change',change=>{if (!current(capability,change)) fn();}) ?? (()=>{}), ...(current('signMessage') && {signMessage:async bytes=>{ const message = bytes.slice(); const [result] = await w.features['solana:signMessage'].signMessage({account:checked('signMessage'),message:message.slice()}); checked('signMessage'); if (result.signedMessage.length !== message.length || !message.every((v,i)=>result.signedMessage[i] === v)) throw Error('The signed message changed.'); return result.signature; }}), ...(current('signTransaction') && {signTransaction:async tx=>{ const [result] = await w.features['solana:signTransaction'].signTransaction({account:checked('signTransaction'),chain, transaction:tx.serialize({requireAllSignatures:false,verifySignatures:false})}); checked('signTransaction'); return tx.constructor.from(result.signedTransaction); }}), }; }