const e = require("autobase"),
i = require("hyperbee"),
t = require("@holepunchto/hyperindex"),
o = require("compact-encoding"),
a = require("b4a"),
{ mnemonicToEntropy: r } = require("bip39-mnemonic"),
{
IndexFilePointer: n,
FileRecord: c,
RoomRecord: l,
BookmarksRecord: s,
PendingInvitationRecord: d,
LocalIdentityRecord: y,
} = require("./messages.js"),
{ getLegacyUserData: u, getLegacyLocalKey: m, generateCompatSeed: p } = require("./apply"),
{ generateRoomCapabilities: g } = require("../../invitations/index.js"),
{ roomMigrationsStats: v } = require("../../contract/stats/local")
async function f(e, i, t) {
const o = await e.createKeyPair(t.identity.identityPublicKey),
n = await u(e, t.privateProfileKey, "keet-seed-phrase"),
c = n ? r(a.toString(n)) : null,
l = {
publicKey: t.identity.identityPublicKey,
privateProfileKey: t.privateProfileKey,
deviceKeyPair: o,
deviceProof: t.identity.deviceProof,
status: { isConfirmed: !0 },
entropy: c,
swap: null,
}
await i.insert("@local/identity", l)
}
function w(e) {
return e.createCollections([
{ keyEncoding: t.BUFFER, valueEncoding: l },
{ keyEncoding: t.UINT, valueEncoding: s },
{ keyEncoding: n, valueEncoding: c },
{ keyEncoding: t.BUFFER, valueEncoding: d },
{ keyEncoding: t.BUFFER },
{ keyEncoding: [t.BUFFER, t.BUFFER] },
])
}
module.exports = async function (r, n) {
const c = r.store,
l = c.get({ name: "local", compat: !1, active: !1 })
if ((await l.ready(), !l.length)) return void (await l.close())
const s = new t(new i(c.get({ name: "local", compat: !1, active: !1 }))),
d = new i(c.get({ name: "local-recents-3", compat: !1, active: !1 })),
K = new i(c.get({ name: "keet-local-identity", compat: !1, active: !1 })),
E = w(s),
k = E[0],
P = E[2],
R = E[5],
b = [],
h = [],
F = await K.get(a.from([1]), { valueEncoding: y }),
q = F?.value
if (q?.identity) {
const e = await k.get(F.value.privateProfileKey)
;(e && b.push({ priority: 0, record: e }), await f(c, n, q))
}
const x = d.createReadStream({
valueEncoding: o.array(o.fixed32),
keyEncoding: o.lexint,
reverse: !0,
})
for await (const { key: e, value: i } of x)
for (const t of i) {
const i = await k.get(t)
i && !i.value.hidden && b.push({ priority: e, record: i })
}
for await (const {
_id: [e, i, t],
value: { roomKey: o, cleared: a },
} of P.createReadStream()) {
const r = i.startsWith("/") ? i.slice(1) : i
await n.insert("@local/drive-files", {
file: { pointer: { externalDrive: { key: e, path: r, version: t, size: 0 } } },
roomKey: o,
encryptionKey: null,
cleared: a,
own: !1,
lastAccessed: 0,
})
}
for await (const { _id: e } of R.createReadStream()) h.push(e[1])
for (const {
priority: i,
record: { _id: t, value: o },
} of b) {
const [r, l] = await Promise.all([u(c, t, "autobase/encryption"), m(c, t)])
if (!r) continue
const s = p(r),
d = g(t, s)
let y = null
if ((q && a.equals(q.privateProfileKey, t) && (y = { blockedMembers: h }), o.hidden && !y))
continue
const f = c.get({ key: l, active: !1 })
await f.ready()
const w = f.length
await f.close()
const K = {
roomKey: t,
priority: i,
v3: {
encryptionKey: r,
seed: s,
capabilities: d,
initiator: { key: l, length: w },
fastForwardTo: (await e.getBootRecord(c, t)).key,
privateProfile: y,
},
errored: !1,
timestamp: Date.now(),
},
E = await n.get("@local/room-migrations", { roomKey: t })
;(await n.insert("@local/room-migrations", K),
await v(n, K, E),
await n.insert("@local/writers", { mailbox: d.mailboxKeyPair.publicKey, key: l, roomKey: t }))
}
await Promise.allSettled([s.close(), d.close(), K.close()])
}