decentraland / decentraland/core-libs
Add createAuthChainHeaders utility to @dcl/crypto for signed-fetch header serialization
- Dominant language
- TypeScript
- Stars
- 1
- Forks
- 0
- Avg merge
- 16h 22m
- Merged PRs (30d)
- 8
Description
## Description
`libs/crypto` (`@dcl/crypto`) owns the signing logic (`Authenticator.signPayload`) and the `x-identity-*` header name constants (`AUTH_CHAIN_HEADER_PREFIX`, `AUTH_TIMESTAMP_HEADER`, `AUTH_METADATA_HEADER`, defined in `libs/crypto/src/headers.ts`), but it does not expose the step that serializes an `AuthChain` into those headers.
As a result, this exact serialization logic is currently duplicated in this same monorepo and downstream:
- `libs/crypto-middleware/src/createAuthChainHeaders.ts` — its own `createAuthChainHeaders`, already importing the header constants from `@dcl/crypto` via a deprecated re-export in `libs/crypto-middleware/src/types.ts`
- `@dcl/pulse-client` — the serialization half of `buildAuthChain`
Centralizing this in `@dcl/crypto`, next to the constants it depends on, removes that duplication.
## Proposed change
Add a named export to `libs/crypto`:
```ts
export function createAuthChainHeaders(
authChain: AuthChain,
timestamp: number,
metadata: Record = {}
): Record
```
Behavior (must match the existing signed-fetch header format DCL servers verify against, mirroring `libs/crypto-middleware/src/createAuthChainHeaders.ts`):
- for each link at index `i`: `headers[AUTH_CHAIN_HEADER_PREFIX + i] = JSON.stringify(link)`
- `headers[AUTH_TIMESTAMP_HEADER] = String(timestamp)`
- `headers[AUTH_METADATA_HEADER] = JSON.stringify(metadata)`
Constraints:
- Import the three header constants from `libs/crypto/src/headers.ts` — do not redefine them.
- Place the function alongside the existing header constants/helpers, and export it from `libs/crypto`'s public entrypoint (`libs/crypto/src/index.ts`).
- Return the headers object (`Record`), not a JSON string — consumers needing a serialized blob (e.g. Pulse's proto `auth_chain` field) will `JSON.stringify()` it themselves.
- Serialization only — do not sign. Signing stays with `Authenticator.signPayload`.
- Purely additive — no changes to existing exports/signatures.
## Context
Downstream adoption in `libs/crypto-middleware` and `@dcl/pulse-client` (replacing their local copies) is a follow-up, gated on a published release of `@dcl/crypto` containing this change.
---
Requested by Gabriel Díaz via Slack.
Contributor guide
Research direction
Start by reading libs/crypto-middleware/src/createAuthChainHeaders.ts and the constants in libs/crypto/src/headers.ts. Add the utility alongside the existing crypto header helpers, export it from libs/crypto/src/index.ts, and verify that its returned Record matches the specified signed-fetch header format without performing signing or changing existing exports.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, security
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 85/100