aboutsummaryrefslogtreecommitdiffstats
path: root/src/app.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-08-03 07:39:22 -0700
committerAdam Soltys <[email protected]>2026-08-03 07:39:22 -0700
commit848958e450a5b5aac95a2fcd981fd229da1d6190 (patch)
tree1ba922313441ad75fa41f091f96faecf76ffff38 /src/app.js
parent4b191f4eb8ad519e982f6667f2b540cdd77e539a (diff)
downloadcoinosv3-848958e450a5b5aac95a2fcd981fd229da1d6190.tar.gz
coinosv3-848958e450a5b5aac95a2fcd981fd229da1d6190.zip
Onboarding: swap a server-broken punk, self-heal the strip
punks/55 (and a few others) 502 on coinos.io right now; 36 verified good takes its place, and any picker image that fails to load removes itself instead of showing a broken glyph. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Wvj4aoeMZwA7VUGdJP9Swi
Diffstat (limited to 'src/app.js')
-rw-r--r--src/app.js3
1 files changed, 2 insertions, 1 deletions
diff --git a/src/app.js b/src/app.js
index 7e39e59..562ca99 100644
--- a/src/app.js
+++ b/src/app.js
@@ -2006,7 +2006,7 @@ function shouldOnboard() {
return !ui.onbSkip && accounts.length === 0 && !hasVault();
}
-const PUNK_PICKS = [7, 14, 21, 3, 33, 40, 47, 55, 61, 26, 12, 50];
+const PUNK_PICKS = [7, 14, 21, 3, 33, 40, 47, 36, 61, 26, 12, 50];
const punkImg = (n) => `https://coinos.io/api/public/punks/${n}.webp`;
async function onbUpload(file) {
@@ -2093,6 +2093,7 @@ function onboardScreen() {
class: 'onb-punk' + (sel === punkImg(n) ? ' sel' : ''),
src: punkImg(n), alt: '',
onClick: () => { o.avatar = punkImg(n); render(); },
+ onError: (e) => { e.target.remove(); }, // a 502'd punk removes itself
}))),
h('div', { class: 'row gap6' },
h('button', { class: 'btn-block grow', disabled: ui.onbBusy, onClick: () => document.getElementById('onb-file')?.click() }, t('onbUpload')),
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
const jwt = require("jsonwebtoken");
const uuidv4 = require("uuid/v4");
const WebSocket = require("ws");

wss = new WebSocket.Server({ clientTracking: false, noServer: true });

/* eslint-disable-next-line */
const pick = (O, ...K) => K.reduce((o, k) => ((o[k] = O[k]), o), {});
const all = {};
const subscriptions = [];

emit = (username, type, data) => {
  if (data.username !== undefined) data = pick(data, ...whitelist);

  if (type === "payment" && data.amount > 0) {
    for (let i = subscriptions.length - 1; i >= 0; i--) {
      let s = subscriptions[i];
      if (
        s.invoice.amount === data.amount ||
        s.invoice.address === data.address ||
        s.invoice.unconfidential === data.address
      ) {
        s.ws.send(JSON.stringify({ type, data }));
        subscriptions.splice(i, 1);
      }
    }
  }

  if (!sockets[username]) return;
  for (id in sockets[username]) {
    const ws = sockets[username][id];
    ws.send(JSON.stringify({ type, data }));
  }
};

broadcast = (type, data) => {
  const sent = [];
  for (const username in sockets) {
    for (const id in sockets[username]) {
      const ws = sockets[username][id];
      ws.send(JSON.stringify({ type, data }));
      sent.push(ws.id);
    }
  }

  Object.values(all).map(ws => {
    if (!sent.includes(ws.id)) ws.send(JSON.stringify({ type, data }));
  });
};

server.on("upgrade", function(req, socket, head) {
  wss.handleUpgrade(req, socket, head, function(ws) {
    wss.emit("connection", ws, req);
  });
});

const names = {};

setInterval(() => {
  for (const username in sockets) {
    for (const id in sockets[username]) {
      let ws = sockets[username][id];
      ws.beats++;
      if (ws.beats > 10) ws.close();
    }
  }
}, 2000);

wss.on("connection", function(ws, req) {
  const id = uuidv4();
  ws.id = id;
  ws.beats = 0;
  ws.send(JSON.stringify({ type: "connected", data: id }));
  all[id] = ws;

  ws.on("message", async message => {
    let type, data;
    try {
      ({ type, data } = JSON.parse(message));
    } catch (e) {
      l.error("coudn't parse socket message");
    }

    switch (type) {
      case "heartbeat":
        ws.beats = 0;
        break;

      case "login":
        let token = data;
        try {
          if (!token || token === "null") return ws.close();
          const username = jwt.decode(token).username;
          if (!username) throw new Error("Invalid JWT token");

          if (!sockets[username]) sockets[username] = {};
          const existing = Object.keys(sockets[username]);
          if (existing.length > 4) {
            const p = existing.find(sid => sid !== id);
            sockets[username][p].send(JSON.stringify({ type: "logout" }));
            setTimeout(() => {
              if (sockets[username][p]) sockets[username][p].close();
            }, 5000);
          }
          sockets[username][id] = ws;
          names[id] = username;

          let user = await getUser(username);
          if (user.locked) throw new Error("Account is locked");
          let payments = await user.getPayments({
            where: {
              account_id: user.account_id
            },
            order: [["id", "DESC"]],
            limit: 12,
            include: {
              model: db.Account,
              as: "account"
            }
          });
          let accounts = await user.getAccounts();
          let keys = await user.getKeys();
          user = user.get({ plain: true });
          user.accounts = accounts;
          user.keys = keys;
          user.payments = payments;

          ws.user = user;
          ws.send(JSON.stringify({ type: "login", data: user }));
          ws.send(
            JSON.stringify({ type: "version", data: config.clientVersion })
          );
        } catch (e) {
          l.error("caught socket error", e.message);
          if (e.message.includes("locked")) {
            ws.send(JSON.stringify({ type: "locked" }));
            ws.send(JSON.stringify({ type: "logout" }));
          } 
          if (e.message.includes("not found"))
            ws.send(JSON.stringify({ type: "logout" }));
          setTimeout(() => ws && ws.close(), 1000);
        }
        break;
      case "token":
        if (!ws.user) return;
        ws.token = data;
        break;
      case "lnurl":
        sessions[data] = ws;
        break;
      case "updateMemo":
        const payment = await db.Payment.findOne({
          where: {
            user_id: ws.user.id,
            id: data.id
          },
          include: {
            model: db.Account,
            as: "account"
          }
        });
        if (payment) {
          payment.memo = data.memo;
          await payment.save();
          emit(ws.user.username, "payment", payment);
        }
        break;
      case "subscribe":
        subscriptions.push({ invoice: data, ws });
        break;
      default:
        l.warn("received socket message of unknown type", type, data);
    }
  });

  ws.on("close", function() {
    try {
      const username = names[id];
      if (sockets[username] && sockets[username][id]) {
        delete sockets[username][id];
      }

      if (all[id]) delete all[id];
    } catch (e) {
      l.error("problem closing socket", e.message);
    }
  });
});