// Point wj62's profile at the newly-uploaded avatar blob. Uses the SAME ref shape the existing
// (working) profile uses — avatar.ref = {$link: <cid>} plain object (verified via inspect-profile-ref).
// Submits via the device control surface (writer-gated apply-write). The lease-holder signs it.
// bare scripts/update-profile-avatar.mjs <cid> <size> [origin|device]
import '../lib/bare-globals.js'
import Hyperswarm from 'hyperswarm'
import crypto from 'hypercore-crypto'
import b4a from 'b4a'
import c from 'compact-encoding'
import ProtomuxRPC from 'protomux-rpc'
const CONTROL = {
origin: '8ae330710aaef922dce79a907f6ff8f57e459de61d4d45f1551d5e0ff23a11d7',
device: 'e0cac0d06e67da9c79674a5ca195809300b04635d1a46254f4fd21edd1ce491d'
}
const WRITER_SEED = 'wj62-swarm-test-writer-2026-07-05'
const cid = globalThis.Bare.argv[2]
const size = Number(globalThis.Bare.argv[3])
const ck = CONTROL[(globalThis.Bare.argv[4] || 'origin').toLowerCase()]
if (!cid || !size) { console.log('usage: bare scripts/update-profile-avatar.mjs <cid> <size> [origin|device]'); globalThis.Bare.exit(1) }
// EXACT displayName + description from the current record — only the avatar changes.
const record = {
$type: 'app.bsky.actor.profile',
avatar: { $type: 'blob', ref: { $link: cid }, mimeType: 'image/jpeg', size },
description: 'Bare-native, device-attested atproto PDS on Hypercore — federated from pds.7rnx.net.',
displayName: 'P2P 7rnx'
}
const swarm = new Hyperswarm({ keyPair: crypto.keyPair(crypto.hash(b4a.from(WRITER_SEED))) })
const done = new Promise((res, rej) => {
const to = setTimeout(() => rej(new Error('timeout')), 30000)
swarm.on('connection', async conn => {
if (b4a.toString(conn.remotePublicKey, 'hex') !== ck) return
try {
const rpc = new ProtomuxRPC(conn, { protocol: 'hyper-proto-peer/device/v1', valueEncoding: c.json })
const r = await rpc.request('apply-write', { action: 'update', collection: 'app.bsky.actor.profile', rkey: 'self', record })
clearTimeout(to); res(r)
} catch (e) { clearTimeout(to); rej(e) }
})
})
swarm.join(b4a.from(ck, 'hex'), { client: true, server: true }); await swarm.flush()
try { const r = await done; console.log('PROFILE UPDATED ' + JSON.stringify(r)) } catch (e) { console.log('FAIL ' + e.message) }
await swarm.destroy(); globalThis.Bare.exit(0)