uvd-x402-sdk 2.46.0 → 2.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -0
- package/dist/adapters/index.d.mts +3 -3
- package/dist/adapters/index.d.ts +3 -3
- package/dist/backend/index.d.mts +2 -2
- package/dist/backend/index.d.ts +2 -2
- package/dist/backend/index.js +7 -1
- package/dist/backend/index.js.map +1 -1
- package/dist/backend/index.mjs +7 -1
- package/dist/backend/index.mjs.map +1 -1
- package/dist/{index-DgChnK_c.d.mts → index-CVW8AhRX.d.mts} +1 -1
- package/dist/{index-DgChnK_c.d.ts → index-CVW8AhRX.d.ts} +1 -1
- package/dist/{index-BidnFGw_.d.mts → index-Cic-RnwJ.d.mts} +1 -1
- package/dist/{index-H64xdjKh.d.ts → index-DVJ9S8lP.d.ts} +1 -1
- package/dist/index.d.mts +387 -5
- package/dist/index.d.ts +387 -5
- package/dist/index.js +375 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +364 -2
- package/dist/index.mjs.map +1 -1
- package/dist/{ows-DTDixPzO.d.ts → ows-C-KmORG9.d.ts} +1 -1
- package/dist/{ows-CYIVd4xO.d.mts → ows-Z9v4GxOZ.d.mts} +1 -1
- package/dist/providers/algorand/index.d.mts +1 -1
- package/dist/providers/algorand/index.d.ts +1 -1
- package/dist/providers/evm/index.d.mts +1 -1
- package/dist/providers/evm/index.d.ts +1 -1
- package/dist/providers/near/index.d.mts +1 -1
- package/dist/providers/near/index.d.ts +1 -1
- package/dist/providers/solana/index.d.mts +1 -1
- package/dist/providers/solana/index.d.ts +1 -1
- package/dist/providers/stellar/index.d.mts +1 -1
- package/dist/providers/stellar/index.d.ts +1 -1
- package/dist/providers/sui/index.d.mts +1 -1
- package/dist/providers/sui/index.d.ts +1 -1
- package/dist/providers/xrpl/index.d.mts +1 -1
- package/dist/providers/xrpl/index.d.ts +1 -1
- package/dist/react/index.d.mts +3 -3
- package/dist/react/index.d.ts +3 -3
- package/dist/utils/index.d.mts +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/{wallet-0cX9Pw2F.d.mts → wallet-w7BnImDG.d.mts} +1 -1
- package/dist/{wallet-0cX9Pw2F.d.ts → wallet-w7BnImDG.d.ts} +1 -1
- package/package.json +38 -13
- package/src/backend/index.ts +22 -1
- package/src/erc8128.ts +443 -0
- package/src/escrow-preauth.ts +416 -0
- package/src/index.ts +36 -0
package/src/erc8128.ts
ADDED
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* uvd-x402-sdk - ERC-8128 HTTP Message Signature Signer
|
|
3
|
+
*
|
|
4
|
+
* Signs HTTP requests per ERC-8128 (Signed HTTP Requests with Ethereum,
|
|
5
|
+
* RFC 9421 profile) so agents can authenticate against APIs that reject
|
|
6
|
+
* API keys and only accept wallet signing (e.g. Execution Market, where
|
|
7
|
+
* `EM_API_KEYS_ENABLED=false` in production).
|
|
8
|
+
*
|
|
9
|
+
* Flow:
|
|
10
|
+
* 1. Fetch a fresh single-use nonce from the server (`fetchNonce`)
|
|
11
|
+
* 2. Build the RFC 9421 signature base from request components
|
|
12
|
+
* 3. Sign with EIP-191 personal_sign
|
|
13
|
+
* 4. Produce `Signature` + `Signature-Input` (+ `Content-Digest`) headers
|
|
14
|
+
*
|
|
15
|
+
* Wire format: pinned by the F3-1 golden vectors (`src/erc8128.vectors.json`)
|
|
16
|
+
* — `alg="eip191"` always emitted, keyid ALWAYS lowercase, params in the
|
|
17
|
+
* order `created;expires;nonce;keyid;alg`. Byte-equality against the vectors
|
|
18
|
+
* is enforced in `src/erc8128.test.ts`.
|
|
19
|
+
*
|
|
20
|
+
* Three signing entry points:
|
|
21
|
+
* - {@link signRequest} — raw private key (ethers Wallet), for scripts /
|
|
22
|
+
* bots that hold the key in env.
|
|
23
|
+
* - {@link signRequestWithWallet} — any {@link SigningWalletAdapter}
|
|
24
|
+
* (EnvKeyAdapter, OWSWalletAdapter, custom); only `getAddress()` and
|
|
25
|
+
* `signMessage()` are used, the key never leaves the adapter.
|
|
26
|
+
* - {@link signRequestWithSigner} — callback-based, for browser wallets or
|
|
27
|
+
* out-of-process signers that expose a bare EIP-191 personal_sign.
|
|
28
|
+
*
|
|
29
|
+
* @example Sign a single request
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { signRequest, fetchNonce } from 'uvd-x402-sdk';
|
|
32
|
+
*
|
|
33
|
+
* const nonce = await fetchNonce('https://api.execution.market');
|
|
34
|
+
* const headers = await signRequest({
|
|
35
|
+
* privateKey: process.env.WALLET_PRIVATE_KEY!,
|
|
36
|
+
* method: 'POST',
|
|
37
|
+
* url: 'https://api.execution.market/api/v1/tasks',
|
|
38
|
+
* body: '{"title": "test"}',
|
|
39
|
+
* nonce,
|
|
40
|
+
* chainId: 8453,
|
|
41
|
+
* });
|
|
42
|
+
* // headers = { Signature: '...', 'Signature-Input': '...', 'Content-Digest': '...' }
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @example Auto-signing fetch wrapper
|
|
46
|
+
* ```typescript
|
|
47
|
+
* import { createSignedFetch, EnvKeyAdapter } from 'uvd-x402-sdk';
|
|
48
|
+
*
|
|
49
|
+
* const signedFetch = createSignedFetch({
|
|
50
|
+
* wallet: new EnvKeyAdapter(), // or privateKey: '...'
|
|
51
|
+
* apiBase: 'https://api.execution.market',
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* const resp = await signedFetch('/api/v1/tasks', {
|
|
55
|
+
* method: 'POST',
|
|
56
|
+
* body: JSON.stringify({ title: 'test' }),
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* Reference:
|
|
61
|
+
* - ERC-8128: https://eip.tools/eip/8128
|
|
62
|
+
* - RFC 9421: https://www.rfc-editor.org/rfc/rfc9421
|
|
63
|
+
* - ERC-191: https://eips.ethereum.org/EIPS/eip-191
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
import { ethers } from 'ethers';
|
|
67
|
+
import { X402Error } from './types';
|
|
68
|
+
import type { SigningWalletAdapter } from './wallet';
|
|
69
|
+
|
|
70
|
+
/** Default label for ERC-8128 signatures */
|
|
71
|
+
const DEFAULT_LABEL = 'eth';
|
|
72
|
+
|
|
73
|
+
/** Signature algorithm parameter — pinned by F3-1 (always emitted) */
|
|
74
|
+
const ALG = 'eip191';
|
|
75
|
+
|
|
76
|
+
/** Default validity window (seconds) — server policy typically caps at 300 */
|
|
77
|
+
const DEFAULT_VALIDITY_SEC = 300;
|
|
78
|
+
|
|
79
|
+
/** Default EVM chain ID for the keyid (Base mainnet, the production auth chain) */
|
|
80
|
+
const DEFAULT_CHAIN_ID = 8453;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Request components shared by every ERC-8128 signing entry point.
|
|
84
|
+
*/
|
|
85
|
+
export interface ERC8128RequestOptions {
|
|
86
|
+
/** HTTP method (GET, POST, etc.) */
|
|
87
|
+
method: string;
|
|
88
|
+
/** Full URL of the request (authority + path + query are covered) */
|
|
89
|
+
url: string;
|
|
90
|
+
/**
|
|
91
|
+
* Request body (for POST/PUT/PATCH). Omit for bodyless requests.
|
|
92
|
+
* Must be byte-identical to what is sent on the wire — the
|
|
93
|
+
* `Content-Digest` covers it.
|
|
94
|
+
*/
|
|
95
|
+
body?: string;
|
|
96
|
+
/** Single-use nonce from the server. Required by most servers. */
|
|
97
|
+
nonce?: string;
|
|
98
|
+
/** EVM chain ID for the keyid (default: 8453 = Base) */
|
|
99
|
+
chainId?: number;
|
|
100
|
+
/** Signature label (default: "eth") */
|
|
101
|
+
label?: string;
|
|
102
|
+
/** Signature validity window in seconds (default: 300) */
|
|
103
|
+
validitySec?: number;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface SignRequestOptions extends ERC8128RequestOptions {
|
|
107
|
+
/** Hex-encoded private key (with or without 0x prefix) */
|
|
108
|
+
privateKey: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface SignRequestWithSignerOptions extends ERC8128RequestOptions {
|
|
112
|
+
/** Signer address (any case — lowercased for the keyid per F3-1) */
|
|
113
|
+
address: string;
|
|
114
|
+
/**
|
|
115
|
+
* EIP-191 personal_sign callback over the RFC 9421 signature base.
|
|
116
|
+
* Must return the 65-byte signature (r||s||v) as 0x-prefixed hex.
|
|
117
|
+
* The private key never leaves the signer (OWS vault, browser wallet).
|
|
118
|
+
*/
|
|
119
|
+
signMessage: (signatureBase: string) => Promise<string> | string;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export interface SignatureHeaders {
|
|
123
|
+
Signature: string;
|
|
124
|
+
'Signature-Input': string;
|
|
125
|
+
'Content-Digest'?: string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Configuration for {@link createSignedFetch}. Provide exactly one signer:
|
|
130
|
+
* a raw `privateKey` or a `wallet` adapter.
|
|
131
|
+
*/
|
|
132
|
+
export interface CreateSignedFetchConfig {
|
|
133
|
+
/** Origin of the API (e.g. "https://api.execution.market") */
|
|
134
|
+
apiBase: string;
|
|
135
|
+
/** EVM chain ID for the keyid (default: 8453 = Base) */
|
|
136
|
+
chainId?: number;
|
|
137
|
+
/** Hex-encoded private key (with or without 0x prefix) */
|
|
138
|
+
privateKey?: string;
|
|
139
|
+
/** SigningWalletAdapter — the key never leaves the adapter */
|
|
140
|
+
wallet?: SigningWalletAdapter;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Sign an HTTP request per ERC-8128 with a raw private key.
|
|
145
|
+
*
|
|
146
|
+
* Thin wrapper over {@link signRequestWithSigner} using an ethers Wallet.
|
|
147
|
+
*
|
|
148
|
+
* @returns Headers to merge into the request (Signature, Signature-Input, Content-Digest).
|
|
149
|
+
*/
|
|
150
|
+
export async function signRequest(options: SignRequestOptions): Promise<SignatureHeaders> {
|
|
151
|
+
const key = options.privateKey.startsWith('0x')
|
|
152
|
+
? options.privateKey
|
|
153
|
+
: `0x${options.privateKey}`;
|
|
154
|
+
const wallet = new ethers.Wallet(key);
|
|
155
|
+
return signRequestWithSigner({
|
|
156
|
+
address: wallet.address,
|
|
157
|
+
signMessage: (signatureBase) => wallet.signMessage(signatureBase),
|
|
158
|
+
method: options.method,
|
|
159
|
+
url: options.url,
|
|
160
|
+
body: options.body,
|
|
161
|
+
nonce: options.nonce,
|
|
162
|
+
chainId: options.chainId,
|
|
163
|
+
label: options.label,
|
|
164
|
+
validitySec: options.validitySec,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Sign an HTTP request per ERC-8128 with a {@link SigningWalletAdapter}.
|
|
170
|
+
*
|
|
171
|
+
* Only `getAddress()` and `signMessage()` (EIP-191 personal_sign) are used —
|
|
172
|
+
* the private key never leaves the adapter. Works with EnvKeyAdapter,
|
|
173
|
+
* OWSWalletAdapter, or any custom adapter.
|
|
174
|
+
*
|
|
175
|
+
* @returns Headers to merge into the request (Signature, Signature-Input, Content-Digest).
|
|
176
|
+
*/
|
|
177
|
+
export async function signRequestWithWallet(
|
|
178
|
+
wallet: SigningWalletAdapter,
|
|
179
|
+
options: ERC8128RequestOptions
|
|
180
|
+
): Promise<SignatureHeaders> {
|
|
181
|
+
return signRequestWithSigner({
|
|
182
|
+
...options,
|
|
183
|
+
address: wallet.getAddress(),
|
|
184
|
+
signMessage: (signatureBase) => wallet.signMessage(signatureBase),
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Sign an HTTP request per ERC-8128 with a signing callback.
|
|
190
|
+
*
|
|
191
|
+
* For signers that never expose the raw key (OWS vault, browser wallets):
|
|
192
|
+
* the callback receives the RFC 9421 signature base and returns the EIP-191
|
|
193
|
+
* personal_sign signature as 0x-hex.
|
|
194
|
+
*
|
|
195
|
+
* @returns Headers to merge into the request (Signature, Signature-Input, Content-Digest).
|
|
196
|
+
*/
|
|
197
|
+
export async function signRequestWithSigner(
|
|
198
|
+
options: SignRequestWithSignerOptions
|
|
199
|
+
): Promise<SignatureHeaders> {
|
|
200
|
+
const {
|
|
201
|
+
address: rawAddress,
|
|
202
|
+
signMessage,
|
|
203
|
+
method,
|
|
204
|
+
url,
|
|
205
|
+
body,
|
|
206
|
+
nonce,
|
|
207
|
+
chainId = DEFAULT_CHAIN_ID,
|
|
208
|
+
label = DEFAULT_LABEL,
|
|
209
|
+
validitySec = DEFAULT_VALIDITY_SEC,
|
|
210
|
+
} = options;
|
|
211
|
+
|
|
212
|
+
// CRITICAL: lowercase — checksummed keyid caused the v9.x silent-auth INC.
|
|
213
|
+
const address = rawAddress.toLowerCase();
|
|
214
|
+
|
|
215
|
+
const parsed = new URL(url);
|
|
216
|
+
const authority = parsed.host;
|
|
217
|
+
const path = parsed.pathname || '/';
|
|
218
|
+
const query = parsed.search || undefined; // includes '?' prefix
|
|
219
|
+
|
|
220
|
+
const now = Math.floor(Date.now() / 1000);
|
|
221
|
+
const created = now;
|
|
222
|
+
const expires = now + validitySec;
|
|
223
|
+
|
|
224
|
+
const keyid = `erc8128:${chainId}:${address}`;
|
|
225
|
+
|
|
226
|
+
// Determine covered components
|
|
227
|
+
const covered: string[] = ['@method', '@authority', '@path'];
|
|
228
|
+
if (query) {
|
|
229
|
+
covered.push('@query');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const headers: SignatureHeaders = {
|
|
233
|
+
Signature: '',
|
|
234
|
+
'Signature-Input': '',
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
if (body !== undefined) {
|
|
238
|
+
const digest = computeContentDigest(body);
|
|
239
|
+
headers['Content-Digest'] = digest;
|
|
240
|
+
covered.push('content-digest');
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Build signature base
|
|
244
|
+
const sigBase = buildSignatureBase({
|
|
245
|
+
method,
|
|
246
|
+
authority,
|
|
247
|
+
path,
|
|
248
|
+
query,
|
|
249
|
+
contentDigest: headers['Content-Digest'],
|
|
250
|
+
covered,
|
|
251
|
+
created,
|
|
252
|
+
expires,
|
|
253
|
+
nonce,
|
|
254
|
+
keyid,
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
// EIP-191 personal_sign via the callback — the key stays in the signer
|
|
258
|
+
const sigHex = await signMessage(sigBase);
|
|
259
|
+
|
|
260
|
+
// Encode signature as base64 (RFC 8941 byte sequence)
|
|
261
|
+
const sigBytes = ethers.getBytes(sigHex.startsWith('0x') ? sigHex : `0x${sigHex}`);
|
|
262
|
+
const sigB64 = ethers.encodeBase64(sigBytes);
|
|
263
|
+
|
|
264
|
+
// Build headers
|
|
265
|
+
const sigParams = buildSignatureParams({ covered, created, expires, nonce, keyid });
|
|
266
|
+
|
|
267
|
+
headers.Signature = `${label}=:${sigB64}:`;
|
|
268
|
+
headers['Signature-Input'] = `${label}=${sigParams}`;
|
|
269
|
+
|
|
270
|
+
return headers;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Fetch a fresh single-use nonce from the server.
|
|
275
|
+
*
|
|
276
|
+
* @param apiBase - Origin of the API (e.g. "https://api.execution.market")
|
|
277
|
+
* — the `/api/v1/auth/erc8128/nonce` path is appended here.
|
|
278
|
+
* @param timeoutMs - Request timeout in milliseconds (default: 10000).
|
|
279
|
+
* @returns The nonce value (single-use, short TTL — fetch one per request).
|
|
280
|
+
*/
|
|
281
|
+
export async function fetchNonce(apiBase: string, timeoutMs = 10000): Promise<string> {
|
|
282
|
+
const url = `${apiBase.replace(/\/$/, '')}/api/v1/auth/erc8128/nonce`;
|
|
283
|
+
const controller = new AbortController();
|
|
284
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
285
|
+
|
|
286
|
+
try {
|
|
287
|
+
const resp = await fetch(url, { signal: controller.signal });
|
|
288
|
+
if (!resp.ok) {
|
|
289
|
+
throw new Error(`Failed to fetch nonce: ${resp.status} ${resp.statusText}`);
|
|
290
|
+
}
|
|
291
|
+
const data = (await resp.json()) as { nonce: string };
|
|
292
|
+
return data.nonce;
|
|
293
|
+
} finally {
|
|
294
|
+
clearTimeout(timer);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Create a fetch wrapper that auto-signs requests with ERC-8128
|
|
300
|
+
* (nonce fetch + signature headers on every call).
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```typescript
|
|
304
|
+
* const signedFetch = createSignedFetch({
|
|
305
|
+
* wallet: new EnvKeyAdapter(), // or privateKey: '...'
|
|
306
|
+
* apiBase: 'https://api.execution.market',
|
|
307
|
+
* chainId: 8453,
|
|
308
|
+
* });
|
|
309
|
+
*
|
|
310
|
+
* const resp = await signedFetch('/api/v1/tasks', {
|
|
311
|
+
* method: 'POST',
|
|
312
|
+
* body: JSON.stringify({ title: 'test' }),
|
|
313
|
+
* });
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
export function createSignedFetch(config: CreateSignedFetchConfig) {
|
|
317
|
+
const { apiBase, chainId, privateKey, wallet } = config;
|
|
318
|
+
|
|
319
|
+
if (!privateKey && !wallet) {
|
|
320
|
+
throw new X402Error(
|
|
321
|
+
'createSignedFetch requires a signer: pass privateKey or wallet.',
|
|
322
|
+
'WALLET_NOT_CONNECTED'
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return async (path: string, init?: RequestInit): Promise<Response> => {
|
|
327
|
+
const url = `${apiBase.replace(/\/$/, '')}${path}`;
|
|
328
|
+
const method = (init?.method || 'GET').toUpperCase();
|
|
329
|
+
const body = init?.body ? String(init.body) : undefined;
|
|
330
|
+
|
|
331
|
+
// Fresh single-use nonce per request
|
|
332
|
+
const nonce = await fetchNonce(apiBase);
|
|
333
|
+
|
|
334
|
+
// Sign request
|
|
335
|
+
const requestOptions: ERC8128RequestOptions = { method, url, body, nonce, chainId };
|
|
336
|
+
const sigHeaders = wallet
|
|
337
|
+
? await signRequestWithWallet(wallet, requestOptions)
|
|
338
|
+
: await signRequest({ ...requestOptions, privateKey: privateKey! });
|
|
339
|
+
|
|
340
|
+
// Merge headers
|
|
341
|
+
const headers = new Headers(init?.headers);
|
|
342
|
+
headers.set('Signature', sigHeaders.Signature);
|
|
343
|
+
headers.set('Signature-Input', sigHeaders['Signature-Input']);
|
|
344
|
+
if (sigHeaders['Content-Digest']) {
|
|
345
|
+
headers.set('Content-Digest', sigHeaders['Content-Digest']);
|
|
346
|
+
}
|
|
347
|
+
if (body) {
|
|
348
|
+
headers.set('Content-Type', headers.get('Content-Type') || 'application/json');
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return fetch(url, { ...init, headers, method });
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// ---------------------------------------------------------------------------
|
|
356
|
+
// Signature-base construction (exported: browser / out-of-process signers
|
|
357
|
+
// build the exact same base and sign it elsewhere)
|
|
358
|
+
// ---------------------------------------------------------------------------
|
|
359
|
+
|
|
360
|
+
function computeContentDigest(body: string): string {
|
|
361
|
+
const hash = ethers.sha256(ethers.toUtf8Bytes(body));
|
|
362
|
+
// hash is 0x-prefixed hex, convert to raw bytes then base64
|
|
363
|
+
const b64 = ethers.encodeBase64(ethers.getBytes(hash));
|
|
364
|
+
return `sha-256=:${b64}:`;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export interface SignatureBaseParams {
|
|
368
|
+
method: string;
|
|
369
|
+
authority: string;
|
|
370
|
+
path: string;
|
|
371
|
+
query?: string;
|
|
372
|
+
contentDigest?: string;
|
|
373
|
+
covered: string[];
|
|
374
|
+
created: number;
|
|
375
|
+
expires: number;
|
|
376
|
+
nonce?: string;
|
|
377
|
+
keyid: string;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Build the RFC 9421 signature base — the exact string that gets EIP-191
|
|
382
|
+
* personal_signed. Exported so external signers (OWS, browser) can reproduce
|
|
383
|
+
* it byte-for-byte; pinned by the F3-1 golden vectors.
|
|
384
|
+
*/
|
|
385
|
+
export function buildSignatureBase(params: SignatureBaseParams): string {
|
|
386
|
+
const lines: string[] = [];
|
|
387
|
+
|
|
388
|
+
for (const component of params.covered) {
|
|
389
|
+
switch (component) {
|
|
390
|
+
case '@method':
|
|
391
|
+
lines.push(`"@method": ${params.method.toUpperCase()}`);
|
|
392
|
+
break;
|
|
393
|
+
case '@authority':
|
|
394
|
+
lines.push(`"@authority": ${params.authority}`);
|
|
395
|
+
break;
|
|
396
|
+
case '@path':
|
|
397
|
+
lines.push(`"@path": ${params.path}`);
|
|
398
|
+
break;
|
|
399
|
+
case '@query':
|
|
400
|
+
lines.push(`"@query": ${params.query || '?'}`);
|
|
401
|
+
break;
|
|
402
|
+
case 'content-digest':
|
|
403
|
+
lines.push(`"content-digest": ${params.contentDigest || ''}`);
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const sigParams = buildSignatureParams({
|
|
409
|
+
covered: params.covered,
|
|
410
|
+
created: params.created,
|
|
411
|
+
expires: params.expires,
|
|
412
|
+
nonce: params.nonce,
|
|
413
|
+
keyid: params.keyid,
|
|
414
|
+
});
|
|
415
|
+
lines.push(`"@signature-params": ${sigParams}`);
|
|
416
|
+
|
|
417
|
+
return lines.join('\n');
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
export interface SignatureParamsInput {
|
|
421
|
+
covered: string[];
|
|
422
|
+
created: number;
|
|
423
|
+
expires: number;
|
|
424
|
+
nonce?: string;
|
|
425
|
+
keyid: string;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Build the `@signature-params` value per RFC 9421, in the pinned order
|
|
430
|
+
* `created;expires;nonce;keyid;alg` (F3-1).
|
|
431
|
+
*/
|
|
432
|
+
export function buildSignatureParams(params: SignatureParamsInput): string {
|
|
433
|
+
const compStr = params.covered.map((c) => `"${c}"`).join(' ');
|
|
434
|
+
const parts: string[] = [`(${compStr})`];
|
|
435
|
+
parts.push(`created=${params.created}`);
|
|
436
|
+
parts.push(`expires=${params.expires}`);
|
|
437
|
+
if (params.nonce) {
|
|
438
|
+
parts.push(`nonce="${params.nonce}"`);
|
|
439
|
+
}
|
|
440
|
+
parts.push(`keyid="${params.keyid}"`);
|
|
441
|
+
parts.push(`alg="${ALG}"`);
|
|
442
|
+
return parts.join(';');
|
|
443
|
+
}
|