// The reMarkable cloud (sync v3) is a content-addressed blob store. Two kinds // of "index" blobs tie it together: // // root.docSchema lists every document (one line per document UUID) // .docSchema lists the files that make up one document // // Both are line-oriented text: // // // [ 0:.::] // :::: // ... // // Hashing rules (what the cloud, the tablet, and the other clients agree on): // // * a file blob's hash is the SHA-256 of its bytes; // * a document's hash is the SHA-256 of the concatenated *binary* hashes of // its files, sorted by file name — not of the index text; // * the root's hash is the SHA-256 of the serialised root index text (schema // 4). Servers now reject freshly written schema-3 roots, so we always // write 4, but we still read 3. import { createHash } from 'node:crypto'; export const FILE_TYPE = '0'; export const DOC_TYPE_V3 = '80000000'; export function sha256 (data) { return createHash('sha256').update(data).digest('hex'); } function cmp (a, b) { return a < b ? -1 : a > b ? 1 : 0; } export function parseEntry (line) { const fields = line.split(':'); if (fields.length < 5) throw new Error(`malformed index line: ${line}`); const size = Number(fields[fields.length - 1]); const subfiles = Number(fields[fields.length - 2]); if (!Number.isInteger(size) || !Number.isInteger(subfiles)) throw new Error(`malformed index line: ${line}`); return { hash: fields[0], type: fields[1], name: fields.slice(2, -2).join(':'), subfiles, size, }; } // Returns { schema, entries: [{ hash, type, name, subfiles, size }] }. export function parseIndex (text) { const lines = String(text).split('\n').map(l => l.replace(/\r$/, '')).filter(l => l !== ''); if (!lines.length) throw new Error('empty index'); const schema = lines.shift(); if (schema !== '3' && schema !== '4') throw new Error(`unsupported index schema "${schema}"`); if (schema === '4') { const header = lines.shift(); if (!header || !header.startsWith('0:.:')) throw new Error(`schema 4 index is missing its header line`); } return { schema, entries: lines.map(parseEntry) }; } // files: [{ name, hash, size }]. Returns the index text to upload as // `.docSchema`, plus the document's hash, total size and file count. export function docIndex (files) { const sorted = [...files].sort((a, b) => cmp(a.name, b.name)); const h = createHash('sha256'); for (const f of sorted) h.update(Buffer.from(f.hash, 'hex')); const lines = sorted.map(f => `${f.hash}:${FILE_TYPE}:${f.name}:0:${f.size}`); return { text: `3\n${lines.join('\n')}${lines.length ? '\n' : ''}`, hash: h.digest('hex'), size: sorted.reduce((s, f) => s + f.size, 0), subfiles: sorted.length, }; } // docs: [{ id, hash, subfiles, size }]. Returns the schema-4 root index text // to upload as `root.docSchema` plus its hash. export function rootIndex (docs) { const sorted = [...docs].sort((a, b) => cmp(a.id, b.id)); const total = sorted.reduce((s, d) => s + d.size, 0); const lines = sorted.map(d => `${d.hash}:${FILE_TYPE}:${d.id}:${d.subfiles}:${d.size}`); const text = `4\n0:.:${sorted.length}:${total}\n${lines.join('\n')}${lines.length ? '\n' : ''}`; return { text, hash: sha256(text) }; }