Pull second copies from `/download` with per-piece FOC grants

Open
#1,196 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@JAG-UK is already working on this.

Since Dec 4, 2026.

  • #1199 by @JAG-UK — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

Start with manager.ts at the batched and unbatched pull paths, then trace createPieceUrlPDP, createPullBatcher, pullPieces, and the upload and StorageContext.pull options. Read pull-pieces.ts around line 242 and the existing MSW pull tests to understand body retries and status polls. Done means both pull paths build grant-aware /download sources lazily, preserve extraData on resends, avoid new grants during polls, and scrub grants from errors and callbacks.

Written by the indexing model from the issue text.

Description

Description

Secondary pulls always use /public/{cid} source URLs, built with createPieceUrlPDP. That is true on both the batched path (manager.ts:605, the default) and the unbatched one (manager.ts:760). A restricted piece (#1197) is not served on /public, so a secondary can only copy it from /download/{cid}, with a FOC grant.

filecoin-project/curio#1643 accepts that grant on a pull as basic-auth userinfo on each source URL. The username is FOC Grant (or FOC%20Grant) and the password is the token, which must not be empty (pdp/pull_grant_url.go). Curio keeps a URL that already carries userinfo as it is, sends the grant to the primary as basic auth, and redacts it from its logs.

This issue adds the grant-aware pull path without turning it on. Nothing changes until #1197 passes a read mode and a grant.

Proposed solution

  • URLs. Give createPieceUrlPDP a read mode, or add a /download builder next to it. Restricted gives /download/{cid}; absent or public gives /public/{cid}, as today.
  • Grant callback. Add an optional getGrant(pieceCid) => Promise<string>:
    • Where it goes: on each call's options (upload, StorageContext.pull, and core createPullBatcher / pullPieces), and passed on to replacement secondaries. Don't store it on the context: StorageManager reuses cached default contexts when copies, exclusions and metadata match (manager.ts:1158-1170), so a callback stored on the context would leak into later calls that pass a different one.
    • What it returns: exactly what keysmith's authorizationHeader() returns (FOC-Grant <base64url>). The SDK strips the prefix.
    • What it signs: the same PieceCIDv2 that appears in the URL.
    • Lifetime: sign pull grants with expiresIn = MAX_REQUEST_LIFETIME (300 s). Keysmith and Curio both accept up to 300 s plus 60 s of clock skew.
  • Source URL: https://FOC%20Grant:<token>@<primary>/download/<cid>.
  • Lazy sources. PullSource and PullBatchInput.sourceUrl accept a lazy, async source. Widening these types is additive for callers.
  • When grants are made.
    • Create them when the pull body is built, after extraData is signed, one getGrant call at a time per batch. A batch holds at most 10 pieces (PULL_BATCH_SIZE).
    • Rebuild the body only for resends Curio did not accept (429 or 5xx). Curio sends a 429 before it stores anything.
    • Keep extraData unchanged on every resend. It is Curio's idempotency key, and a 5xx can arrive after Curio has already accepted the pull. In that case Curio returns the existing pull and ignores the new URLs, so its stored grants are the originals.
    • Status polls reuse the accepted body. Today iso-web resends one fixed body (pull-pieces.ts:242), so the 429 loop moves out of iso-web's retry.
  • Scrubbing. A Curio 400 can echo the source URL. That text reaches PullError.details, and PullError.fromHttpError also keeps the HttpError (with the response) as cause; from there it reaches onCopyFailed and failedAttempts. Remove FOC%20Grant:…@ userinfo before the PullError is built, from both the details and the cause. Returned URLs never contain a grant.

Done criteria

  • With no read mode and no getGrant, pull URLs and request bodies are byte-for-byte what they are today
  • A /download source URL carries one grant per piece, signed for the same PieceCIDv2 that is in the URL
  • Batched and unbatched pulls both accept lazy, async sources
  • Status polls never create new grants; resends Curio did not accept do, and every resend keeps the same extraData
  • A cached default context never uses a previous call's getGrant
  • No grant appears in returned URLs, PullError (details or cause), onCopyFailed or failedAttempts
  • MSW pull mocks capture sourceUrl; tests cover both URL forms, the poll and resend rules, and scrubbing

Trade-offs and risks

  • Grant lifetime. If the secondary's queue time plus Curio's retries (3 attempts, 30 s then 2 min apart) outlast the grant, the primary returns 403. Curio treats that as permanent, and the copy fails. Longer pull grants would need a Curio change.
  • Wallet prompts. Each grant is one signTypedData. Callers should sign with a session or delegated key, not a wallet.

Notes

  • Can land before Curio releases #1643, because the path is off by default.
  • Part of #1088. Prerequisite for #1197. Downloads with grants: #1193.
  • Curio: filecoin-project/curio#1643 (grants on pulls), #1660 (design).
Dominant language
TypeScript
Stars
27
Forks
36
Avg merge
2d 4h
Merged PRs (30d)
65

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from FilOzone/synapse-sdk

All issues in FilOzone/synapse-sdk

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.