const { app, BrowserWindow, ipcMain, shell } = require('electron') const Path = require('node:path') const URL = require('node:url') const p = require('node:util').promisify const Keypair = require('ppppp-keypair') const awaitable = require('pull-awaitable') // WARNING monkey patch! -------------------------------------- const na = require('sodium-native') na.sodium_malloc = function sodium_malloc_monkey_patched(n) { return Buffer.alloc(n) } na.sodium_free = function sodium_free_monkey_patched() {} // Electron > 20.3.8 breaks a napi method that `sodium_malloc` // depends on to create external buffers. (see v8 memory cage) // // This crashes electron when called by various libraries, so // we monkey-patch this particular function. // ------------------------------------------------------------ process.env.ZOOBOARD_DATA ??= Path.join(app.getPath('appData'), 'zooboard') app.setPath('userData', process.env.ZOOBOARD_DATA) const path = Path.resolve(app.getPath('userData'), 'ppppp') const keypairPath = Path.join(path, 'keypair.json') const keypair = Keypair.loadOrCreateSync(keypairPath) let mainWindow let globalAccountID = null let globalAccountName = null const peer = require('secret-stack/bare')() .use(require('secret-stack/plugins/net')) .use(require('secret-handshake-ext/secret-stack')) .use(require('ppppp-net')) .use(require('ppppp-db')) .use(require('ppppp-set')) .use(require('ppppp-dict')) .use(require('ppppp-goals')) .use(require('ppppp-sync')) .use(require('ppppp-gc')) .use(require('ppppp-conductor')) .use(require('ppppp-hub-client')) .use(require('ppppp-promise')) .use(require('ppppp-invite')) .call(null, { shse: { caps: require('ppppp-caps'), }, global: { keypair, path, timers: { inactivity: 10 * 60e3, }, connections: { incoming: { tunnel: [{ transform: 'shse', scope: 'public' }], }, outgoing: { net: [{ transform: 'shse' }], tunnel: [{ transform: 'shse' }], }, }, }, net: { autostart: true, }, }) function createWindow() { mainWindow = new BrowserWindow({ width: 1200, height: 1200, title: 'Zooboard', webPreferences: { preload: Path.join(__dirname, 'preload.js'), }, }) const startUrl = process.env.ELECTRON_START_URL ?? URL.format({ pathname: Path.join(__dirname, '/../build/index.html'), protocol: 'file:', slashes: true, }) mainWindow.loadURL(startUrl) mainWindow.webContents.openDevTools({ mode: 'bottom', activate: true }) // open Web URLs in the default system browser mainWindow.webContents.on('new-window', (ev, url) => { ev.preventDefault() shell.openExternal(url) }) } async function loadAccount() { if (globalAccountID !== null) { return { id: globalAccountID, name: globalAccountName } } // Kickstart await peer.db.loaded() const id = await p(peer.db.account.findOrCreate)({ subdomain: 'person' }) globalAccountID = id await p(peer.set.load)(id) await p(peer.dict.load)(id) // Read hubs FIXME: this should be in the ppppp-net scheduler const multiaddrs = peer.set.values('hubs') for (const multiaddr of multiaddrs) { scheduleWithHub(multiaddr) } // Read profile const profile = peer.dict.read(id, 'profile') const name = profile?.name ?? '' globalAccountName = name return { id, name } } async function setProfileName(ev, name) { await p(peer.dict.update)('profile', { name }) return name } async function writeElements(ev, elements) { if (globalAccountID === null) throw new Error('account not loaded') for (const element of elements) { await p(peer.db.feed.publish)({ account: globalAccountID, domain: 'zooboardElements', data: element, }) } } let hasSubscribedToReadElements = false function subscribeToReadElements() { if (hasSubscribedToReadElements) return hasSubscribedToReadElements = true // Load initial elements and inform renderer const elementsByID = new Map() const msgIDToElemID = new Map() for (const { id: msgID, msg } of peer.db.records()) { if (msg.data && msg.metadata.domain === 'zooboardElements') { const { id: elemID, isDeleted } = msg.data if (isDeleted) { elementsByID.delete(elemID) } else { msgIDToElemID.set(msgID, elemID) elementsByID.set(elemID, msg.data) } } } const initialElements = [...elementsByID.values()] mainWindow.webContents.send('readElements', initialElements) // Subscribe to new elements and inform renderer peer.db.onRecordAdded(({ id: msgID, msg }) => { if (msg.data && msg.metadata.domain === 'zooboardElements') { const { id: elemID, isDeleted } = msg.data if (isDeleted) { elementsByID.delete(elemID) } else { msgIDToElemID.set(msgID, elemID) elementsByID.set(elemID, msg.data) } mainWindow.webContents.send('readElements', [msg.data]) } }) // Subscribe to deleted elements and inform renderer peer.db.onRecordDeletedOrErased((msgID) => { const elemID = msgIDToElemID.get(msgID) if (!elemID) return msgIDToElemID.delete(msgID) // Is there some other msgID that supports this elemID? If so, bail out for (const [, remainingElemID] of msgIDToElemID) { if (remainingElemID === elemID) { return } } // If not, delete the element elementsByID.delete(elemID) mainWindow.webContents.send('readElements', [ { id: elemID, isDeleted: true }, ]) }) // Finally safe to kickstart replication and garbage collection setTimeout(() => { peer.conductor.start( globalAccountID, [ ['profile@dict', 'zooboardElements@newest-100', 'hubs@set'], ['profile@dict', 'zooboardElements@newest-100'], ], 64_000 ) }, 32) } async function scheduleWithHub(multiaddr) { const hubRPC = await p(peer.net.connect)(multiaddr) // FIXME: this should be in the ppppp-net scheduler for await (const attendants of awaitable(hubRPC.hub.attendants())) { for (const attendant of attendants) { if (attendant !== peer.shse.pubkey) { const tunnelMultiaddr = `/tunnel/${hubRPC.shse.pubkey}.${attendant}/shse/${attendant}` peer.net.connect(tunnelMultiaddr) } } } } async function handlePPPPPUri(uri) { if (!globalAccountID) { setTimeout(handlePPPPPUri, 100, uri) return } if (!uri.startsWith('ppppp://')) return console.log('handlePPPPPUri', uri) const commands = peer.invite.parse(uri) for (const command of commands) { console.log(command) switch (command.type) { case 'join': { try { await p(peer.hubClient.addHub)(command.multiaddr) scheduleWithHub(command.multiaddr) } catch (err) { console.error('Failed to properly join hub', err) } break } case 'follow': { await p(peer.set.add)('follows', command.id) break } case 'promise.follow': { const [issuerType, issuerPubkey] = command.issuer if (issuerType !== 'pubkey') { throw new Error( 'dont know how to claim promise issued by ' + issuerType ) } // eslint-disable-next-line no-loop-func peer.addListener('rpc:connect', function onConnect(rpc) { if (rpc.shse.pubkey === issuerPubkey) { rpc.promise.follow(command.token, globalAccountID, (err) => { if (err) return console.error('Failed to use follow promise', err) peer.removeListener('rpc:connect', onConnect) }) } }) break } default: console.log('Unknown command type', command.type) } } } if (process.defaultApp) { if (process.argv.length >= 2) { app.setAsDefaultProtocolClient('ppppp', process.execPath, [ Path.resolve(process.argv[1]), ]) } } else { app.setAsDefaultProtocolClient('ppppp') } const hasLock = app.requestSingleInstanceLock() if (!hasLock) { app.quit() } else { app.on('second-instance', (ev, argv, cwd, extraData) => { if (mainWindow) { if (mainWindow.isMinimized()) mainWindow.restore() mainWindow.focus() if (argv.length > 1) { handlePPPPPUri(argv[argv.length - 1]) } } }) app.whenReady().then(() => { ipcMain.handle('loadAccount', loadAccount) ipcMain.handle('setProfileName', setProfileName) ipcMain.handle('writeElements', writeElements) ipcMain.handle('subscribeToReadElements', subscribeToReadElements) createWindow() if (process.argv.length > 1) { handlePPPPPUri(process.argv[process.argv.length - 1]) } app.on('activate', function () { if (BrowserWindow.getAllWindows().length === 0) createWindow() }) app.on('window-all-closed', function () { if (process.platform !== 'darwin') app.quit() }) }) }