( requirement: PaymentRequirement, fromAddress: string, privateKeyHex: string, )
| 292 | * Create a signed x402 payment payload for a given requirement. |
| 293 | */ |
| 294 | export function createPayment( |
| 295 | requirement: PaymentRequirement, |
| 296 | fromAddress: string, |
| 297 | privateKeyHex: string, |
| 298 | ): PaymentPayload { |
| 299 | const nonce = '0x' + randomBytes(32).toString('hex') |
| 300 | const validAfter = '0' |
| 301 | const validBefore = String( |
| 302 | Math.floor(Date.now() / 1000) + requirement.maxTimeoutSeconds, |
| 303 | ) |
| 304 | |
| 305 | const authorization = { |
| 306 | from: fromAddress, |
| 307 | to: requirement.payTo, |
| 308 | value: requirement.maxAmountRequired, |
| 309 | validAfter, |
| 310 | validBefore, |
| 311 | nonce, |
| 312 | } |
| 313 | |
| 314 | const domain = getEIP712Domain(requirement) |
| 315 | const domainSeparator = computeDomainSeparator(domain) |
| 316 | const structHash = computeStructHash(authorization) |
| 317 | const signature = signEIP712(domainSeparator, structHash, privateKeyHex) |
| 318 | |
| 319 | return { |
| 320 | x402Version: 1, |
| 321 | scheme: requirement.scheme, |
| 322 | network: requirement.network, |
| 323 | payload: { |
| 324 | signature, |
| 325 | authorization, |
| 326 | }, |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Encode a payment payload as a base64 string for the X-Payment header. |
no test coverage detected