signRequest
Signs an HTTP Request and returns a new Request with RFC 9421 signature headers.
Usage
Sign a request by passing the URL (or Request object), an optional RequestInit, a signer, and optional sign options. The function returns a new Request with RFC 9421 signature headers attached.
import { signRequest } from '@slicekit/erc8128'
// Simple: just input and signer
const signedRequest = await signRequest(
'https://api.example.com/resource',
signer
)
// With RequestInit
const signedRequest = await signRequest(
'https://api.example.com/resource',
{ method: 'POST', body: '{"data": "value"}' },
signer
)
// With options
const signedRequest = await signRequest(
'https://api.example.com/resource',
{ method: 'POST', body: '{"data": "value"}' },
signer,
{ ttlSeconds: 300 }
)Returns
Promise<Request>
A new Request object with these headers added:
Signature-Input— Components and parameters per RFC 9421Signature— Base64-encoded Ethereum signatureContent-Digest— SHA-256 hash of body (if body is present)
Parameters
Overload 1: Simple
When no RequestInit is needed, pass the input and signer directly.
signRequest(input: RequestInfo, signer: EthHttpSigner, opts?: SignOptions): Promise<Request>Overload 2: With RequestInit
When you need to specify method, headers, or body, pass a RequestInit as the second argument.
signRequest(input: RequestInfo, init: RequestInit | undefined, signer: EthHttpSigner, opts?: SignOptions): Promise<Request>input
- Type:
RequestInfo
URL string or Request object to sign.
init (optional)
- Type:
RequestInit
Optional RequestInit (method, headers, body, etc.).
signer
- Type:
EthHttpSigner
The signer that provides the Ethereum address and signing function.
opts (optional)
- Type:
SignOptions
Options for customizing the signature.
Examples
Common signing patterns showing TTL customization, custom nonces, and replayable signatures.
const signedRequest = await signRequest(
'https://api.example.com/resource',
signer,
{ ttlSeconds: 300 } // Valid for 5 minutes
)