Monorepo for Aesthetic.Computer
aesthetic.computer
1// Keeps Wallet - Content Script
2// Relays Beacon SDK postMessage protocol AND existing KEEPS_* custom protocol
3
4const EXTENSION_ID = 'keeps-wallet';
5const WALLET_NAME = 'Keeps Wallet';
6// Data URI for a small purple diamond icon (inline to avoid network requests)
7const ICON_URL =
8 'data:image/svg+xml;base64,' +
9 btoa(
10 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">' +
11 '<rect width="64" height="64" rx="12" fill="#cd5c9b"/>' +
12 '<text x="32" y="44" text-anchor="middle" font-size="36" fill="white">K</text>' +
13 '</svg>',
14 );
15
16// Inject the inpage script that creates window.keeps
17const script = document.createElement('script');
18script.src = chrome.runtime.getURL('inpage.js');
19script.onload = () => script.remove();
20(document.head || document.documentElement).appendChild(script);
21
22// --- Beacon Protocol Relay ---
23
24window.addEventListener('message', async (event) => {
25 // Only accept messages from same window
26 if (event.source !== window) return;
27 const msg = event.data;
28 if (!msg) return;
29
30 // --- Beacon: toExtension messages ---
31 if (msg.target === 'toExtension') {
32 // Ping → respond with pong immediately (discovery)
33 if (msg.payload === 'ping') {
34 window.postMessage(
35 {
36 target: 'toPage',
37 payload: 'pong',
38 sender: {
39 id: EXTENSION_ID,
40 name: WALLET_NAME,
41 iconURL: ICON_URL,
42 },
43 },
44 '*',
45 );
46 return;
47 }
48
49 // Forward everything else (pairing requests, encrypted messages) to background
50 try {
51 const response = await chrome.runtime.sendMessage({
52 type: 'BEACON_MESSAGE',
53 payload: msg,
54 });
55 if (response) {
56 // Background may return a direct response to post to page
57 if (response.postToPage) {
58 window.postMessage(response.postToPage, '*');
59 }
60 }
61 } catch (error) {
62 console.error('Keeps Wallet: Beacon relay error', error);
63 }
64 return;
65 }
66
67 // --- Existing KEEPS_* custom protocol ---
68 if (msg.type && msg.type.startsWith('KEEPS_')) {
69 try {
70 const response = await chrome.runtime.sendMessage(msg);
71 window.postMessage(
72 {
73 type: msg.type + '_RESPONSE',
74 requestId: msg.requestId,
75 payload: response,
76 },
77 '*',
78 );
79 } catch (error) {
80 window.postMessage(
81 {
82 type: msg.type + '_RESPONSE',
83 requestId: msg.requestId,
84 payload: { error: error.message },
85 },
86 '*',
87 );
88 }
89 }
90});
91
92// --- Listen for messages FROM background (extension → page) ---
93chrome.runtime.onMessage.addListener((message) => {
94 if (message.type === 'BEACON_RESPONSE') {
95 window.postMessage(message.data, '*');
96 }
97 if (message.type === 'KEEPS_LOCKED') {
98 window.postMessage({ type: 'KEEPS_LOCKED' }, '*');
99 }
100});
101
102console.log('Keeps Wallet content script loaded');