Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions lib/fully-remote-proof.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ class SlimSession {
}
}

module.exports = { verify, proof }
module.exports = { verify, proof, encode: encodeProof, decode: decodeProof }

async function verify(storage, buffer, { referrer = null } = {}) {
const state = { buffer, start: 0, end: buffer.byteLength }

const discoveryKey = c.fixed32.decode(state)
const proof = messages.wire.data.decode(state)
const { discoveryKey, proof } = decodeProof(buffer)

const result = {
key: null,
Expand Down Expand Up @@ -109,29 +106,46 @@ async function verify(storage, buffer, { referrer = null } = {}) {
return result
}

async function proof(sender, { index, block = null, upgrade = null } = {}) {
const proof = await sender.proof({
block: block ? { index, nodes: 0 } : null,
upgrade: upgrade ? upgrade : { start: 0, length: sender.length }
})
function decodeProof(buffer) {
const state = { buffer, start: 0, end: buffer.byteLength }

if (block) proof.block.value = block
proof.manifest = sender.core.header.manifest
const discoveryKey = c.fixed32.decode(state)
const proof = messages.wire.data.decode(state)

return { discoveryKey, proof }
}

function encodeProof({ discoveryKey, proof }) {
const state = { buffer: null, start: 0, end: 0 }
const data = { request: 0, ...proof }

c.fixed32.preencode(state, sender.discoveryKey)
messages.wire.data.preencode(state, data)
c.fixed32.preencode(state, discoveryKey)
messages.wire.data.preencode(state, proof)

state.buffer = b4a.allocUnsafe(state.end)

c.fixed32.encode(state, sender.discoveryKey)
messages.wire.data.encode(state, data)
c.fixed32.encode(state, discoveryKey)
messages.wire.data.encode(state, proof)

return state.buffer
}

async function proof(sender, { index, block = null, upgrade = null, encode = true } = {}) {
const proof = await sender.proof({
block: block ? { index, nodes: 0 } : null,
upgrade: upgrade ? upgrade : { start: 0, length: sender.length }
})

if (block) proof.block.value = block
proof.manifest = sender.core.header.manifest

const result = {
discoveryKey: sender.discoveryKey,
proof: { request: 0, ...proof }
}

return encode ? encodeProof(result) : result

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 New proof helpers lack tests

The new encode: false branch and exported encode/decode helpers have no direct tests. Existing tests only use the default buffer return, so a broken object return or export could go unnoticed. Add coverage that encodes an unencoded proof, verifies it, and checks that encode(decode(buffer)) preserves the bytes.

Prompt To Fix With AI
This is a comment left during a code review.
Path: lib/fully-remote-proof.js
Line: 146

Comment:
**New proof helpers lack tests**

The new `encode: false` branch and exported `encode`/`decode` helpers have no direct tests. Existing tests only use the default buffer return, so a broken object return or export could go unnoticed. Add coverage that encodes an unencoded proof, verifies it, and checks that `encode(decode(buffer))` preserves the bytes.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code

}

function isNull(x) {
return x === null
}
Loading