From 6e509b089303aed0be77cc76b8dcb2407d53a208 Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 11:42:45 +0800 Subject: [PATCH 01/13] Add x402 payment support for BCH - Add pay command: paytaca pay for HTTP requests with x402 payment - Add X402Payer class integrating with LibauthHDWallet for signing - Add x402 types (PaymentRequired, PaymentPayload, Authorization, etc.) - Add utility functions for header parsing and payload building - Support BCH mainnet and chipnet via bip122 CAIP-2 network format - Uses bitcoinjs-message for BCH message signing (same as bch-js) - Payment flow: request -> 402 -> parse headers -> broadcast BCH tx -> retry with signature --- src/commands/pay.ts | 263 +++++++++++++++++++++++++++++++ src/index.ts | 2 + src/types/bitcoinjs-message.d.ts | 20 +++ src/types/x402.ts | 112 +++++++++++++ src/utils/x402.ts | 219 +++++++++++++++++++++++++ src/wallet/x402.ts | 196 +++++++++++++++++++++++ 6 files changed, 812 insertions(+) create mode 100644 src/commands/pay.ts create mode 100644 src/types/bitcoinjs-message.d.ts create mode 100644 src/types/x402.ts create mode 100644 src/utils/x402.ts create mode 100644 src/wallet/x402.ts diff --git a/src/commands/pay.ts b/src/commands/pay.ts new file mode 100644 index 0000000..146d71f --- /dev/null +++ b/src/commands/pay.ts @@ -0,0 +1,263 @@ +/** + * CLI command: pay + * + * Makes an HTTP request to a URL, handling x402 payment requirements. + * If the server returns 402 PAYMENT-REQUIRED, the wallet pays for the request. + * + * Flow: + * 1. Make HTTP request to URL + * 2. If 402 response, parse PAYMENT-REQUIRED headers + * 3. Build BCH transaction to pay the required amount + * 4. Broadcast transaction + * 5. Retry original request with PAYMENT-SIGNATURE header + */ + +import { Command } from 'commander' +import chalk from 'chalk' +import { loadWallet, loadMnemonic } from '../wallet/index.js' +import { LibauthHDWallet } from '../wallet/keys.js' +import { BchWallet } from '../wallet/bch.js' +import { X402Payer } from '../wallet/x402.js' +import { parsePaymentRequired, selectBchPaymentRequirements } from '../utils/x402.js' +import { BCH_DERIVATION_PATH } from '../utils/network.js' +import { PaymentRequired, BchPaymentRequirements } from '../types/x402.js' + +interface PayOptions { + method?: string + header?: string[] + body?: string + chipnet: boolean + maxAmount?: string + changeAddress?: string +} + +export function registerPayCommand(program: Command): void { + program + .command('pay') + .description('Make a paid HTTP request with BCH payment via x402 protocol') + .argument('', 'URL to request') + .option('-X, --method ', 'HTTP method (default: GET)', 'GET') + .option('-H, --header
', 'Add header to request (repeatable)') + .option('-d, --body ', 'Request body for POST/PUT requests') + .option('--chipnet', 'Use chipnet (testnet) instead of mainnet') + .option('--max-amount ', 'Maximum payment amount in satoshis (overrides server\'s max-amount)') + .option('--change-address
', 'Change address for BCH transaction') + .action(async (url: string, opts: PayOptions) => { + const isChipnet = Boolean(opts.chipnet) + const network = isChipnet ? 'chipnet' : 'mainnet' + + const data = loadMnemonic() + if (!data) { + console.log( + chalk.red('\nNo wallet found. Run `paytaca wallet create` or `paytaca wallet import` first.\n') + ) + process.exit(1) + } + + const wallet = loadWallet()! + const bchWallet = wallet.forNetwork(isChipnet) + const hdWallet = new LibauthHDWallet( + data.mnemonic, + BCH_DERIVATION_PATH, + isChipnet ? 'chipnet' : 'mainnet' + ) + + const x402Payer = new X402Payer({ hdWallet, addressIndex: 0 }) + + const headers: Record = {} + if (opts.header) { + for (const h of opts.header) { + const idx = h.indexOf(':') + if (idx === -1) { + console.log(chalk.red(`\n Error: Invalid header format: ${h}. Expected "Key: Value"\n`)) + process.exit(1) + } + const key = h.substring(0, idx).trim() + const value = h.substring(idx + 1).trim() + headers[key] = value + } + } + + const method = opts.method?.toUpperCase() || 'GET' + const body = opts.body + + console.log(`\n ${chalk.bold(method)} ${url}`) + console.log(chalk.dim(` Network: ${chalk.cyan(network)}`)) + console.log(chalk.dim(` Payer: ${x402Payer.getPayerAddress()}`)) + if (Object.keys(headers).length > 0) { + console.log(chalk.dim(` Headers: ${JSON.stringify(headers)}`)) + } + console.log() + + try { + const response = await fetch(url, { + method, + headers, + body: ['POST', 'PUT', 'PATCH'].includes(method) ? body : undefined, + }) + + const responseHeaders: Record = {} + response.headers.forEach((value, key) => { + responseHeaders[key] = value + }) + + const responseText = await response.text() + let responseData: any + try { + responseData = JSON.parse(responseText) + } catch { + responseData = responseText + } + + if (response.status === 402) { + console.log(chalk.yellow(' 402 PAYMENT REQUIRED')) + console.log(chalk.dim(' Parsing payment requirements...\n')) + + const paymentHeaders: PaymentRequired = {} + for (const [key, value] of Object.entries(responseHeaders)) { + paymentHeaders[key.toLowerCase()] = value + } + + const requirements = parsePaymentRequired(paymentHeaders) + if (!requirements) { + console.log(chalk.red(' Error: Could not parse PAYMENT-REQUIRED headers')) + process.exit(1) + } + + const bchRequirements = selectBchPaymentRequirements(requirements) + if (!bchRequirements) { + console.log(chalk.red(' Error: Server does not accept BCH payment')) + console.log(chalk.dim(` Accepted currencies: ${requirements.acceptCurrencies.join(', ')}`)) + process.exit(1) + } + + bchRequirements.payer = x402Payer.getPayerAddress() + + const maxAmountSat = opts.maxAmount + ? BigInt(opts.maxAmount) + : bchRequirements.maxAmount + + if (bchRequirements.maxAmount > 0n && maxAmountSat > bchRequirements.maxAmount) { + console.log( + chalk.yellow(` Warning: --max-amount (${maxAmountSat}) exceeds server max (${bchRequirements.maxAmount})`) + ) + } + + console.log(chalk.dim(` Payment URL: ${bchRequirements.paymentUrl}`)) + console.log(chalk.dim(` Max timeout: ${bchRequirements.maxTimeoutMs}ms`)) + console.log(chalk.dim(` Max amount: ${bchRequirements.maxAmount} satoshis`)) + console.log() + + const recipients = [ + { + address: bchRequirements.paymentUrl.split(':')[1]?.replace(/^\/\//, '') || '', + amount: bchRequirements.maxAmount, + currency: 'BCH', + }, + ] + + if (!recipients[0].address) { + console.log(chalk.red(' Error: Invalid payment URL in requirements')) + process.exit(1) + } + + console.log(chalk.dim(` Sending payment to: ${recipients[0].address}`)) + console.log(chalk.dim(` Amount: ${recipients[0].amount} satoshis`)) + + const changeAddressSet = bchWallet.getAddressSetAt(0) + const changeAddress = opts.changeAddress || changeAddressSet.change + console.log(chalk.dim(` Change address: ${changeAddress}`)) + console.log() + + console.log(chalk.cyan(' Broadcasting BCH transaction...')) + const sendResult = await bchWallet.sendBch( + Number(recipients[0].amount) / 1e8, + recipients[0].address, + changeAddress + ) + + if (!sendResult.success) { + console.log(chalk.red(` Payment failed: ${sendResult.error}`)) + if (sendResult.lackingSats) { + console.log(chalk.yellow(` Insufficient balance. Short by ${sendResult.lackingSats} satoshis.`)) + } + process.exit(1) + } + + console.log(chalk.green(' Payment successful!')) + console.log(chalk.dim(` txid: ${sendResult.txid}`)) + console.log() + + console.log(chalk.cyan(' Retrying original request with payment...')) + + const authHeader = await x402Payer.createAuthorization( + bchRequirements, + { + scheme: 'utxo', + network: bchRequirements.network, + max_timeout_ms: bchRequirements.maxTimeoutMs, + resource_id: bchRequirements.resourceId, + payment: { + scheme: 'utxo', + network: bchRequirements.network, + recipients: recipients.map(r => ({ + address: r.address, + amount: r.amount.toString(), + currency: r.currency, + })), + }, + payer: x402Payer.getPayerAddress(), + } + ) + + headers['Authorization'] = `x402 ${authHeader}` + + const retryResponse = await fetch(url, { + method, + headers, + body: ['POST', 'PUT', 'PATCH'].includes(method) ? body : undefined, + }) + + const retryResponseText = await retryResponse.text() + let retryResponseData: any + try { + retryResponseData = JSON.parse(retryResponseText) + } catch { + retryResponseData = retryResponseText + } + + console.log(chalk.green(`\n Response: ${retryResponse.status} ${retryResponse.statusText}`)) + console.log() + console.log(formatResponse(retryResponseData)) + + if (sendResult.txid) { + const explorer = isChipnet + ? 'https://chipnet.chaingraph.cash/tx/' + : 'https://bchexplorer.info/tx/' + console.log(chalk.dim(` Payment txid: ${explorer}${sendResult.txid}`)) + } + } else { + console.log(chalk.green(` Response: ${response.status} ${response.statusText}`)) + console.log() + console.log(formatResponse(responseData)) + } + } catch (err: any) { + console.log(chalk.red(`\n Error: ${err.message || err}\n`)) + process.exit(1) + } + + console.log() + }) +} + +function formatResponse(data: any): string { + if (typeof data === 'string') return data + if (typeof data === 'object') { + try { + return JSON.stringify(data, null, 2) + } catch { + return String(data) + } + } + return String(data) +} diff --git a/src/index.ts b/src/index.ts index bef74b5..009bc0b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ import { registerBalanceCommand } from './commands/balance.js' import { registerReceiveCommand } from './commands/receive.js' import { registerHistoryCommand } from './commands/history.js' import { registerTokenCommands } from './commands/token.js' +import { registerPayCommand } from './commands/pay.js' const program = new Command() @@ -29,5 +30,6 @@ registerBalanceCommand(program) registerReceiveCommand(program) registerHistoryCommand(program) registerTokenCommands(program) +registerPayCommand(program) program.parse() diff --git a/src/types/bitcoinjs-message.d.ts b/src/types/bitcoinjs-message.d.ts new file mode 100644 index 0000000..de14711 --- /dev/null +++ b/src/types/bitcoinjs-message.d.ts @@ -0,0 +1,20 @@ +declare module 'bitcoinjs-message' { + export function sign( + message: string, + privateKey: Buffer, + compressed?: boolean, + messagePrefix?: string + ): Buffer + + export function verify( + message: string, + address: string, + signature: Buffer | string, + messagePrefix?: string + ): boolean + + export function magicHash( + message: string, + messagePrefix?: string + ): Buffer +} diff --git a/src/types/x402.ts b/src/types/x402.ts new file mode 100644 index 0000000..cc17d99 --- /dev/null +++ b/src/types/x402.ts @@ -0,0 +1,112 @@ +/** + * x402 BCH type definitions + * Implements x402-bch v2.2 specification + * https://github.com/x402-bch/x402-bch + */ + +export interface PaymentRequired { + [key: string]: string | string[] | undefined + 'x-scheme'?: string | string[] + 'max-timeout-ms'?: string | string[] + 'payment-url'?: string | string[] + 'max-amount'?: string | string[] + '匪-async'?: string | string[] + 'resource-id'?: string | string[] + 'resource-meta'?: string | string[] + 'walled-garden'?: string | string[] + 'walled-garden-network'?: string | string[] + 'accept-currencies'?: string | string[] + 'cai'?: string | string[] + 'cai-lease-duration-ms'?: string | string[] + 'mime-type'?: string | string[] + 'www-authenticate'?: string | string[] +} + +export interface PaymentRequirements { + scheme: string + network: string + paymentUrl: string + maxTimeoutMs: number + maxAmount: bigint + resourceId: string + resourceMeta?: string + walledGarden?: boolean + walledGardenNetwork?: string + acceptCurrencies: string[] + mimeType?: string + wwwAuthenticate?: string +} + +export interface BchPaymentRequirements extends PaymentRequirements { + scheme: 'utxo' + network: 'bip122:000000000000000000651ef99cb9fcbe' | 'bip122:000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' + payer: string + signature: string + attested?: boolean +} + +export interface PaymentPayload { + scheme: string + network: string + max_timeout_ms: number + resource_id: string + resource_meta?: string + attractor?: string + broadcaster?: string + payment: { + scheme: string + network: string + recipients: { + address: string + amount: string + currency: string + metadata?: Record + }[] + required_utxo_count?: number + } + nonce?: string + attestation?: string + payer?: string + payer_attestation?: string +} + +export interface Authorization { + scheme: string + network: string + resource_id: string + payload: string + payload_signature: string + nonce: string + attestation?: string + payer?: string +} + +export interface ResourceInfo { + url: string + method: string + headers: Record + body?: string +} + +export interface SettlementResponse { + success: boolean + txid?: string + vout?: number + settle_address?: string + preimage?: string + signature?: string + error?: string +} + +export interface X402PaymentResult { + success: boolean + response?: { + status: number + statusText: string + headers: Record + data?: any + } + error?: string + txid?: string + settlement?: SettlementResponse +} diff --git a/src/utils/x402.ts b/src/utils/x402.ts new file mode 100644 index 0000000..c24b085 --- /dev/null +++ b/src/utils/x402.ts @@ -0,0 +1,219 @@ +/** + * x402 utility functions for BCH payments + * Parses PAYMENT-REQUIRED headers, builds payment payloads, handles signatures + */ + +import { binToHex, hexToBin } from '@bitauth/libauth' +import { + PaymentRequired, + PaymentRequirements, + BchPaymentRequirements, + PaymentPayload, + Authorization, + ResourceInfo, + SettlementResponse, +} from '../types/x402.js' + +export const BCH_MAINNET_NETWORK = 'bip122:000000000000000000651ef99cb9fcbe' +export const BCH_CHIPNET_NETWORK = 'bip122:000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' + +export function isBchNetwork(network: string): boolean { + return network === BCH_MAINNET_NETWORK || network === BCH_CHIPNET_NETWORK +} + +export function isChipnetNetwork(network: string): boolean { + return network === BCH_CHIPNET_NETWORK +} + +function getHeaderValue(header: PaymentRequired[string]): string | undefined { + if (Array.isArray(header)) return header[0] + return header +} + +export function parsePaymentRequired(headers: PaymentRequired): PaymentRequirements | null { + const scheme = getHeaderValue(headers['x-scheme']) + if (!scheme) return null + + const acceptCurrenciesStr = getHeaderValue(headers['accept-currencies']) || '' + const acceptCurrencies = acceptCurrenciesStr.split(',').map(s => s.trim()).filter(Boolean) + + const maxTimeoutMsStr = getHeaderValue(headers['max-timeout-ms']) + const maxTimeoutMs = maxTimeoutMsStr ? parseInt(maxTimeoutMsStr, 10) : 60000 + + const maxAmountStr = getHeaderValue(headers['max-amount']) + const maxAmount = maxAmountStr ? BigInt(maxAmountStr) : BigInt(0) + + const resourceId = getHeaderValue(headers['resource-id']) || '' + const resourceMeta = getHeaderValue(headers['resource-meta']) + const paymentUrl = getHeaderValue(headers['payment-url']) || '' + const mimeType = getHeaderValue(headers['mime-type']) + const wwwAuthenticate = getHeaderValue(headers['www-authenticate']) + + const walledGardenStr = getHeaderValue(headers['walled-garden']) + const walledGarden = walledGardenStr === 'true' + + const walledGardenNetwork = getHeaderValue(headers['walled-garden-network']) + + return { + scheme, + network: getHeaderValue(headers['x-network']) || '', + paymentUrl, + maxTimeoutMs, + maxAmount, + resourceId, + resourceMeta, + walledGarden, + walledGardenNetwork, + acceptCurrencies, + mimeType, + wwwAuthenticate, + } +} + +export function selectBchPaymentRequirements( + requirements: PaymentRequirements +): BchPaymentRequirements | null { + if (requirements.scheme !== 'utxo') return null + if (!isBchNetwork(requirements.network)) return null + + const acceptedCurrencies = ['BCH', 'bch', 'BCHn', 'bitcoincash'] + const hasAcceptedCurrency = requirements.acceptCurrencies.some(c => + acceptedCurrencies.includes(c) + ) + if (!hasAcceptedCurrency && requirements.acceptCurrencies.length > 0) return null + + return requirements as BchPaymentRequirements +} + +export function buildPaymentPayload( + requirements: BchPaymentRequirements, + payer: string, + recipients: { address: string; amount: bigint; currency: string }[], + opts?: { + resourceMeta?: string + nonce?: string + attestation?: string + broadcaster?: string + } +): PaymentPayload { + return { + scheme: 'utxo', + network: requirements.network, + max_timeout_ms: requirements.maxTimeoutMs, + resource_id: requirements.resourceId, + resource_meta: opts?.resourceMeta || requirements.resourceMeta, + broadcaster: opts?.broadcaster, + payment: { + scheme: 'utxo', + network: requirements.network, + recipients: recipients.map(r => ({ + address: r.address, + amount: r.amount.toString(), + currency: r.currency, + })), + }, + nonce: opts?.nonce, + attestation: opts?.attestation, + payer, + } +} + +export function encodePaymentSignature(auth: Authorization): string { + const data = JSON.stringify({ + scheme: auth.scheme, + network: auth.network, + resource_id: auth.resource_id, + payload: auth.payload, + payload_signature: auth.payload_signature, + nonce: auth.nonce, + attestation: auth.attestation, + }) + return Buffer.from(data).toString('base64') +} + +export function decodePaymentSignature(encoded: string): Authorization | null { + try { + const data = Buffer.from(encoded, 'base64').toString('utf8') + const parsed = JSON.parse(data) + if (!parsed.scheme || !parsed.network || !parsed.payload || !parsed.payload_signature) { + return null + } + return parsed as Authorization + } catch { + return null + } +} + +export function parsePaymentResponse(data: any): SettlementResponse { + if (!data) return { success: false, error: 'No response data' } + + if (data.error) { + return { success: false, error: data.error } + } + + if (data.txid) { + return { + success: true, + txid: data.txid, + vout: data.vout, + settle_address: data.settle_address, + preimage: data.preimage, + signature: data.signature, + } + } + + return { success: false, error: 'Unknown settlement response format' } +} + +export function createResourceInfo( + url: string, + method: string, + headers: Record = {}, + body?: string +): ResourceInfo { + return { url, method, headers, body } +} + +export async function buildAuthorizationHeader( + payload: PaymentPayload, + payloadSignature: string, + nonce: string, + attestation?: string +): Promise { + const auth: Authorization = { + scheme: payload.scheme, + network: payload.network, + resource_id: payload.resource_id, + payload: payloadSignature, + payload_signature: payloadSignature, + nonce, + attestation, + } + return encodePaymentSignature(auth) +} + +export async function signMessageBCH( + message: string, + privateKeyHex: string, + compressed: boolean = true +): Promise { + const { sign } = await import('bitcoinjs-message') + const privateKey = Buffer.from(privateKeyHex, 'hex') + const signatureBuffer = sign(message, privateKey, compressed) + return signatureBuffer.toString('base64') +} + +export function getDefaultSigner(hdWallet: any, index: number = 0): { + address: string + signMessage: (message: string) => Promise +} { + const addressSet = hdWallet.getAddressSetAt(index) + return { + address: addressSet.receiving, + signMessage: async (message: string) => { + const node = hdWallet.getNodeAt(`0/${index}`) + const privKeyHex = Buffer.from(node.privateKey).toString('hex') + return signMessageBCH(message, privKeyHex, true) + }, + } +} diff --git a/src/wallet/x402.ts b/src/wallet/x402.ts new file mode 100644 index 0000000..d164828 --- /dev/null +++ b/src/wallet/x402.ts @@ -0,0 +1,196 @@ +/** + * x402 payment handler for BCH + * Integrates with LibauthHDWallet for signing x402 payment authorization + */ + +import { LibauthHDWallet } from './keys.js' +import { + parsePaymentRequired, + selectBchPaymentRequirements, + buildPaymentPayload, + encodePaymentSignature, + parsePaymentResponse, + signMessageBCH, + BCH_MAINNET_NETWORK, + BCH_CHIPNET_NETWORK, + isChipnetNetwork, +} from '../utils/x402.js' +import { + PaymentRequired, + BchPaymentRequirements, + PaymentPayload, + Authorization, + X402PaymentResult, + SettlementResponse, +} from '../types/x402.js' + +export interface X402Signer { + address: string + signMessage: (message: string) => Promise +} + +export interface X402PaymentRequest { + url: string + method: string + headers: Record + body?: string +} + +export interface X402PayerConfig { + hdWallet: LibauthHDWallet + addressIndex?: number +} + +export class X402Payer { + private signer: X402Signer + private isChipnet: boolean + + constructor(config: X402PayerConfig) { + this.isChipnet = config.hdWallet.isChipnet + this.signer = this.createSigner(config.hdWallet, config.addressIndex || 0) + } + + private createSigner(hdWallet: LibauthHDWallet, index: number): X402Signer { + const addressSet = hdWallet.getAddressSetAt(index) + return { + address: addressSet.receiving, + signMessage: async (message: string) => { + const node = hdWallet.getNodeAt(`0/${index}`) + const privKeyHex = Buffer.from(node.privateKey).toString('hex') + return signMessageBCH(message, privKeyHex, true) + }, + } + } + + getPayerAddress(): string { + return this.signer.address + } + + async handlePaymentRequired( + response: { + status: number + headers: Record + data?: any + }, + paymentRequest: X402PaymentRequest + ): Promise<{ requirements: BchPaymentRequirements; headers: PaymentRequired } | null> { + const headers: PaymentRequired = {} + for (const [key, value] of Object.entries(response.headers)) { + headers[key.toLowerCase()] = value + } + + const requirements = parsePaymentRequired(headers) + if (!requirements) return null + + const bchRequirements = selectBchPaymentRequirements(requirements) + if (!bchRequirements) return null + + bchRequirements.payer = this.signer.address + + return { requirements: bchRequirements, headers } + } + + async createAuthorization( + requirements: BchPaymentRequirements, + payload: PaymentPayload + ): Promise { + const payloadJson = JSON.stringify(payload) + const payloadSignature = await this.signer.signMessage(payloadJson) + + const nonce = Date.now().toString(36) + Math.random().toString(36).substring(2, 10) + + const auth: Authorization = { + scheme: 'utxo', + network: requirements.network, + resource_id: requirements.resourceId, + payload: payloadSignature, + payload_signature: payloadSignature, + nonce, + payer: this.signer.address, + } + + return encodePaymentSignature(auth) + } + + async makePaymentRequest( + requirements: BchPaymentRequirements, + recipients: { address: string; amount: bigint; currency: string }[], + paymentRequest: X402PaymentRequest + ): Promise<{ authHeader: string; paymentUrl: string }> { + const payload = buildPaymentPayload(requirements, this.signer.address, recipients) + const authHeader = await this.createAuthorization(requirements, payload) + + return { + authHeader: `x402 ${authHeader}`, + paymentUrl: requirements.paymentUrl, + } + } + + async retryWithPayment( + requirements: BchPaymentRequirements, + paymentUrl: string, + recipients: { address: string; amount: bigint; currency: string }[], + originalRequest: X402PaymentRequest + ): Promise { + try { + const { authHeader } = await this.makePaymentRequest( + requirements, + recipients, + originalRequest + ) + + const response = await fetch(paymentUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': authHeader, + }, + body: JSON.stringify({ + resource_id: requirements.resourceId, + payment: { + scheme: 'utxo', + network: requirements.network, + recipients: recipients.map(r => ({ + address: r.address, + amount: r.amount.toString(), + currency: r.currency, + })), + }, + payer: this.signer.address, + }), + }) + + const responseData = await response.json() + const settlement = parsePaymentResponse(responseData) + + if (settlement.success) { + return { + success: true, + txid: settlement.txid, + settlement, + response: { + status: 200, + statusText: 'OK', + headers: {}, + data: responseData, + }, + } + } else { + return { + success: false, + error: settlement.error || 'Payment failed', + settlement, + } + } + } catch (err: any) { + return { + success: false, + error: err.message || 'Payment request failed', + } + } + } +} + +export function createX402Payer(hdWallet: LibauthHDWallet, addressIndex?: number): X402Payer { + return new X402Payer({ hdWallet, addressIndex }) +} From 41059c580a614244a6169797c05ea5b3a84ef1ff Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 11:58:56 +0800 Subject: [PATCH 02/13] Add AI-friendly features: dry-run, JSON output, and check command - Add --dry-run flag to pay command for previewing payment without executing - Add --json flag for machine-readable output (useful for AI agents) - Add new 'check' command to pre-verify x402/BCH support before paying - Refactor pay command into separate handlers for human/dry-run/JSON output - JSON output includes success status, payment txid, response data, and errors --- src/commands/check.ts | 213 +++++++++++++++++ src/commands/pay.ts | 521 ++++++++++++++++++++++++++++++------------ src/index.ts | 2 + 3 files changed, 584 insertions(+), 152 deletions(-) create mode 100644 src/commands/check.ts diff --git a/src/commands/check.ts b/src/commands/check.ts new file mode 100644 index 0000000..71f5a0c --- /dev/null +++ b/src/commands/check.ts @@ -0,0 +1,213 @@ +/** + * CLI command: check + * + * Check if a URL accepts x402 BCH payments without making the actual request. + * Useful for AI to determine if payment will be required before committing. + * + * Usage: + * paytaca check https://api.example.com + * paytaca check https://api.example.com --json + * paytaca check https://api.example.com --method POST --body '{"query":"hi"}' + */ + +import { Command } from 'commander' +import chalk from 'chalk' +import { loadWallet, loadMnemonic } from '../wallet/index.js' +import { LibauthHDWallet } from '../wallet/keys.js' +import { BchWallet } from '../wallet/bch.js' +import { parsePaymentRequired, selectBchPaymentRequirements } from '../utils/x402.js' +import { BCH_DERIVATION_PATH } from '../utils/network.js' +import { PaymentRequired } from '../types/x402.js' + +interface CheckOptions { + method?: string + header?: string[] + body?: string + chipnet: boolean + json: boolean +} + +interface CheckResult { + url: string + acceptsX402: boolean + acceptsBch: boolean + paymentRequired: boolean + estimatedCostSats?: string + costInBch?: string + paymentUrl?: string + maxTimeoutMs?: number + resourceId?: string + acceptCurrencies?: string[] + error?: string +} + +export function registerCheckCommand(program: Command): void { + program + .command('check') + .description('Check if a URL accepts x402 BCH payments') + .argument('', 'URL to check') + .option('-X, --method ', 'HTTP method to test (default: GET)', 'GET') + .option('-H, --header
', 'Add header to request (repeatable)') + .option('-d, --body ', 'Request body for POST/PUT requests') + .option('--chipnet', 'Use chipnet (testnet) instead of mainnet') + .option('--json', 'Output results as JSON') + .action(async (url: string, opts: CheckOptions) => { + const isChipnet = Boolean(opts.chipnet) + const isJson = Boolean(opts.json) + const network = isChipnet ? 'chipnet' : 'mainnet' + + const data = loadMnemonic() + if (!data) { + const err = 'No wallet found. Run `paytaca wallet create` or `paytaca wallet import` first.' + if (isJson) { + console.log(JSON.stringify({ url, acceptsX402: false, acceptsBch: false, paymentRequired: false, error: err })) + } else { + console.log(chalk.red(`\n${err}\n`)) + } + process.exit(1) + } + + const wallet = loadWallet()! + const bchWallet = wallet.forNetwork(isChipnet) + const hdWallet = new LibauthHDWallet( + data.mnemonic, + BCH_DERIVATION_PATH, + isChipnet ? 'chipnet' : 'mainnet' + ) + + const headers: Record = {} + if (opts.header) { + for (const h of opts.header) { + const idx = h.indexOf(':') + if (idx === -1) { + const err = `Invalid header format: ${h}. Expected "Key: Value"` + if (isJson) { + console.log(JSON.stringify({ url, acceptsX402: false, acceptsBch: false, error: err })) + } else { + console.log(chalk.red(`\n Error: ${err}\n`)) + } + process.exit(1) + } + const key = h.substring(0, idx).trim() + const value = h.substring(idx + 1).trim() + headers[key] = value + } + } + + const method = opts.method?.toUpperCase() || 'GET' + const body = opts.body + + if (!isJson) { + console.log(`\n ${chalk.bold('CHECK')} ${url}`) + console.log(chalk.dim(` Network: ${chalk.cyan(network)}`)) + console.log(chalk.dim(` Method: ${method}`)) + console.log() + } + + try { + const result = await checkUrl(url, method, headers, body, bchWallet, isChipnet) + + if (isJson) { + console.log(JSON.stringify(result, null, 2)) + } else { + printCheckResult(result) + } + } catch (err: any) { + const errorResult: CheckResult = { + url, + acceptsX402: false, + acceptsBch: false, + paymentRequired: false, + error: err.message || String(err), + } + if (isJson) { + console.log(JSON.stringify(errorResult, null, 2)) + } else { + console.log(chalk.red(`\n Error: ${err.message || err}\n`)) + } + process.exit(1) + } + + if (!isJson) console.log() + }) +} + +async function checkUrl( + url: string, + method: string, + headers: Record, + body: string | undefined, + bchWallet: BchWallet, + isChipnet: boolean +): Promise { + const result: CheckResult = { + url, + acceptsX402: false, + acceptsBch: false, + paymentRequired: false, + } + + const response = await fetch(url, { + method, + headers, + body: ['POST', 'PUT', 'PATCH'].includes(method) ? body : undefined, + }) + + result.paymentRequired = response.status === 402 + + if (response.status === 402) { + const responseHeaders: Record = {} + response.headers.forEach((value, key) => { + responseHeaders[key] = value + }) + + const paymentHeaders: PaymentRequired = {} + for (const [key, value] of Object.entries(responseHeaders)) { + paymentHeaders[key.toLowerCase()] = value + } + + const requirements = parsePaymentRequired(paymentHeaders) + if (requirements) { + result.acceptsX402 = true + result.acceptCurrencies = requirements.acceptCurrencies + result.maxTimeoutMs = requirements.maxTimeoutMs + result.resourceId = requirements.resourceId + + const bchReqs = selectBchPaymentRequirements(requirements) + if (bchReqs) { + result.acceptsBch = true + result.paymentUrl = bchReqs.paymentUrl + result.estimatedCostSats = bchReqs.maxAmount.toString() + result.costInBch = (Number(bchReqs.maxAmount) / 1e8).toFixed(8) + } + } + } + + return result +} + +function printCheckResult(result: CheckResult): void { + if (result.paymentRequired) { + console.log(chalk.yellow(' Payment Required')) + + if (result.acceptsX402) { + console.log(chalk.green(' ✓ Accepts x402 protocol')) + + if (result.acceptsBch) { + console.log(chalk.green(` ✓ Accepts BCH payment`)) + console.log(chalk.dim(` Amount: ${result.estimatedCostSats} sats (${result.costInBch} BCH)`)) + console.log(chalk.dim(` Payment URL: ${result.paymentUrl}`)) + console.log(chalk.dim(` Timeout: ${result.maxTimeoutMs}ms`)) + console.log(chalk.dim(` Resource: ${result.resourceId}`)) + } else { + console.log(chalk.red(' ✗ Does not accept BCH')) + console.log(chalk.dim(` Accepted currencies: ${result.acceptCurrencies?.join(', ')}`)) + } + } else { + console.log(chalk.red(' ✗ Unknown payment protocol (not x402)')) + } + } else { + console.log(chalk.green(' ✓ No payment required')) + console.log(chalk.dim(` Status: ${result.url} is free to access`)) + } +} diff --git a/src/commands/pay.ts b/src/commands/pay.ts index 146d71f..21529f5 100644 --- a/src/commands/pay.ts +++ b/src/commands/pay.ts @@ -29,6 +29,43 @@ interface PayOptions { chipnet: boolean maxAmount?: string changeAddress?: string + dryRun: boolean + json: boolean +} + +interface DryRunInfo { + url: string + method: string + willRequirePayment: boolean + payment?: { + acceptsBch: boolean + paymentUrl: string + amountSats: string + maxTimeoutMs: number + resourceId: string + payerAddress: string + changeAddress: string + network: string + } + balanceCheck?: { + available: string + required: string + sufficient: boolean + } +} + +interface JsonResult { + success: boolean + status?: number + statusText?: string + headers?: Record + data?: any + payment?: { + required: boolean + txid?: string + error?: string + } + error?: string } export function registerPayCommand(program: Command): void { @@ -42,15 +79,22 @@ export function registerPayCommand(program: Command): void { .option('--chipnet', 'Use chipnet (testnet) instead of mainnet') .option('--max-amount ', 'Maximum payment amount in satoshis (overrides server\'s max-amount)') .option('--change-address
', 'Change address for BCH transaction') + .option('--dry-run', 'Show what would happen without making payment') + .option('--json', 'Output results as JSON') .action(async (url: string, opts: PayOptions) => { const isChipnet = Boolean(opts.chipnet) const network = isChipnet ? 'chipnet' : 'mainnet' + const isJson = Boolean(opts.json) + const isDryRun = Boolean(opts.dryRun) const data = loadMnemonic() if (!data) { - console.log( - chalk.red('\nNo wallet found. Run `paytaca wallet create` or `paytaca wallet import` first.\n') - ) + const err = 'No wallet found. Run `paytaca wallet create` or `paytaca wallet import` first.' + if (isJson) { + console.log(JSON.stringify({ success: false, error: err })) + } else { + console.log(chalk.red(`\n${err}\n`)) + } process.exit(1) } @@ -69,7 +113,12 @@ export function registerPayCommand(program: Command): void { for (const h of opts.header) { const idx = h.indexOf(':') if (idx === -1) { - console.log(chalk.red(`\n Error: Invalid header format: ${h}. Expected "Key: Value"\n`)) + const err = `Invalid header format: ${h}. Expected "Key: Value"` + if (isJson) { + console.log(JSON.stringify({ success: false, error: err })) + } else { + console.log(chalk.red(`\n Error: ${err}\n`)) + } process.exit(1) } const key = h.substring(0, idx).trim() @@ -81,173 +130,341 @@ export function registerPayCommand(program: Command): void { const method = opts.method?.toUpperCase() || 'GET' const body = opts.body - console.log(`\n ${chalk.bold(method)} ${url}`) - console.log(chalk.dim(` Network: ${chalk.cyan(network)}`)) - console.log(chalk.dim(` Payer: ${x402Payer.getPayerAddress()}`)) - if (Object.keys(headers).length > 0) { - console.log(chalk.dim(` Headers: ${JSON.stringify(headers)}`)) + if (isJson) { + await runPayJson(url, method, headers, body, opts, x402Payer, bchWallet, isChipnet) + } else if (isDryRun) { + await runPayDryRun(url, method, headers, body, opts, x402Payer, bchWallet, isChipnet) + } else { + await runPayHuman(url, method, headers, body, opts, x402Payer, bchWallet, isChipnet) } - console.log() + }) +} - try { - const response = await fetch(url, { - method, - headers, - body: ['POST', 'PUT', 'PATCH'].includes(method) ? body : undefined, - }) - - const responseHeaders: Record = {} - response.headers.forEach((value, key) => { - responseHeaders[key] = value - }) - - const responseText = await response.text() - let responseData: any - try { - responseData = JSON.parse(responseText) - } catch { - responseData = responseText - } +async function runPayHuman( + url: string, + method: string, + headers: Record, + body: string | undefined, + opts: PayOptions, + x402Payer: X402Payer, + bchWallet: BchWallet, + isChipnet: boolean +): Promise { + const network = isChipnet ? 'chipnet' : 'mainnet' + + console.log(`\n ${chalk.bold(method)} ${url}`) + console.log(chalk.dim(` Network: ${chalk.cyan(network)}`)) + console.log(chalk.dim(` Payer: ${x402Payer.getPayerAddress()}`)) + if (Object.keys(headers).length > 0) { + console.log(chalk.dim(` Headers: ${JSON.stringify(headers)}`)) + } + console.log() - if (response.status === 402) { - console.log(chalk.yellow(' 402 PAYMENT REQUIRED')) - console.log(chalk.dim(' Parsing payment requirements...\n')) + try { + const result = await executePay(url, method, headers, body, opts, x402Payer, bchWallet, false) - const paymentHeaders: PaymentRequired = {} - for (const [key, value] of Object.entries(responseHeaders)) { - paymentHeaders[key.toLowerCase()] = value - } + if (result.payment?.required && result.payment.txid) { + const explorer = isChipnet + ? 'https://chipnet.chaingraph.cash/tx/' + : 'https://bchexplorer.info/tx/' + console.log(chalk.dim(` Payment txid: ${explorer}${result.payment.txid}`)) + } - const requirements = parsePaymentRequired(paymentHeaders) - if (!requirements) { - console.log(chalk.red(' Error: Could not parse PAYMENT-REQUIRED headers')) - process.exit(1) - } + console.log(chalk.green(`\n Response: ${result.status} ${result.statusText}`)) + console.log() + console.log(formatResponse(result.data)) + } catch (err: any) { + console.log(chalk.red(`\n Error: ${err.message || err}\n`)) + process.exit(1) + } - const bchRequirements = selectBchPaymentRequirements(requirements) - if (!bchRequirements) { - console.log(chalk.red(' Error: Server does not accept BCH payment')) - console.log(chalk.dim(` Accepted currencies: ${requirements.acceptCurrencies.join(', ')}`)) - process.exit(1) - } + console.log() +} - bchRequirements.payer = x402Payer.getPayerAddress() +async function runPayDryRun( + url: string, + method: string, + headers: Record, + body: string | undefined, + opts: PayOptions, + x402Payer: X402Payer, + bchWallet: BchWallet, + isChipnet: boolean +): Promise { + const network = isChipnet ? 'chipnet' : 'mainnet' + const dryRunInfo: DryRunInfo = { + url, + method, + willRequirePayment: false, + } - const maxAmountSat = opts.maxAmount - ? BigInt(opts.maxAmount) - : bchRequirements.maxAmount + console.log(`\n ${chalk.bold(method)} ${url} ${chalk.dim('[DRY RUN]')}`) + console.log(chalk.dim(` Network: ${chalk.cyan(network)}`)) + console.log(chalk.dim(` Payer: ${x402Payer.getPayerAddress()}`)) + console.log() - if (bchRequirements.maxAmount > 0n && maxAmountSat > bchRequirements.maxAmount) { - console.log( - chalk.yellow(` Warning: --max-amount (${maxAmountSat}) exceeds server max (${bchRequirements.maxAmount})`) - ) - } + try { + const response = await fetch(url, { + method, + headers, + body: ['POST', 'PUT', 'PATCH'].includes(method) ? body : undefined, + }) - console.log(chalk.dim(` Payment URL: ${bchRequirements.paymentUrl}`)) - console.log(chalk.dim(` Max timeout: ${bchRequirements.maxTimeoutMs}ms`)) - console.log(chalk.dim(` Max amount: ${bchRequirements.maxAmount} satoshis`)) - console.log() - - const recipients = [ - { - address: bchRequirements.paymentUrl.split(':')[1]?.replace(/^\/\//, '') || '', - amount: bchRequirements.maxAmount, - currency: 'BCH', - }, - ] - - if (!recipients[0].address) { - console.log(chalk.red(' Error: Invalid payment URL in requirements')) - process.exit(1) - } + const responseHeaders: Record = {} + response.headers.forEach((value, key) => { + responseHeaders[key] = value + }) - console.log(chalk.dim(` Sending payment to: ${recipients[0].address}`)) - console.log(chalk.dim(` Amount: ${recipients[0].amount} satoshis`)) - - const changeAddressSet = bchWallet.getAddressSetAt(0) - const changeAddress = opts.changeAddress || changeAddressSet.change - console.log(chalk.dim(` Change address: ${changeAddress}`)) - console.log() - - console.log(chalk.cyan(' Broadcasting BCH transaction...')) - const sendResult = await bchWallet.sendBch( - Number(recipients[0].amount) / 1e8, - recipients[0].address, - changeAddress - ) - - if (!sendResult.success) { - console.log(chalk.red(` Payment failed: ${sendResult.error}`)) - if (sendResult.lackingSats) { - console.log(chalk.yellow(` Insufficient balance. Short by ${sendResult.lackingSats} satoshis.`)) - } - process.exit(1) - } + if (response.status === 402) { + dryRunInfo.willRequirePayment = true - console.log(chalk.green(' Payment successful!')) - console.log(chalk.dim(` txid: ${sendResult.txid}`)) - console.log() - - console.log(chalk.cyan(' Retrying original request with payment...')) - - const authHeader = await x402Payer.createAuthorization( - bchRequirements, - { - scheme: 'utxo', - network: bchRequirements.network, - max_timeout_ms: bchRequirements.maxTimeoutMs, - resource_id: bchRequirements.resourceId, - payment: { - scheme: 'utxo', - network: bchRequirements.network, - recipients: recipients.map(r => ({ - address: r.address, - amount: r.amount.toString(), - currency: r.currency, - })), - }, - payer: x402Payer.getPayerAddress(), - } - ) - - headers['Authorization'] = `x402 ${authHeader}` - - const retryResponse = await fetch(url, { - method, - headers, - body: ['POST', 'PUT', 'PATCH'].includes(method) ? body : undefined, - }) - - const retryResponseText = await retryResponse.text() - let retryResponseData: any - try { - retryResponseData = JSON.parse(retryResponseText) - } catch { - retryResponseData = retryResponseText - } + const paymentHeaders: PaymentRequired = {} + for (const [key, value] of Object.entries(responseHeaders)) { + paymentHeaders[key.toLowerCase()] = value + } - console.log(chalk.green(`\n Response: ${retryResponse.status} ${retryResponse.statusText}`)) - console.log() - console.log(formatResponse(retryResponseData)) + const requirements = parsePaymentRequired(paymentHeaders) + if (!requirements) { + console.log(chalk.red(' Error: Could not parse PAYMENT-REQUIRED headers')) + process.exit(1) + } - if (sendResult.txid) { - const explorer = isChipnet - ? 'https://chipnet.chaingraph.cash/tx/' - : 'https://bchexplorer.info/tx/' - console.log(chalk.dim(` Payment txid: ${explorer}${sendResult.txid}`)) - } + const bchRequirements = selectBchPaymentRequirements(requirements) + if (!bchRequirements) { + console.log(chalk.red(' Error: Server does not accept BCH payment')) + console.log(chalk.dim(` Accepted currencies: ${requirements.acceptCurrencies.join(', ')}`)) + process.exit(1) + } + + const changeAddressSet = bchWallet.getAddressSetAt(0) + const changeAddress = opts.changeAddress || changeAddressSet.change + const amountSats = bchRequirements.maxAmount.toString() + + dryRunInfo.payment = { + acceptsBch: true, + paymentUrl: bchRequirements.paymentUrl, + amountSats, + maxTimeoutMs: bchRequirements.maxTimeoutMs, + resourceId: bchRequirements.resourceId, + payerAddress: x402Payer.getPayerAddress(), + changeAddress, + network: bchRequirements.network, + } + + try { + const balanceResult = await bchWallet.getBalance() + const available = (balanceResult.spendable * 1e8).toFixed(0) + const required = amountSats + const sufficient = BigInt(available) >= BigInt(required) + + dryRunInfo.balanceCheck = { + available, + required, + sufficient, + } + + console.log(chalk.yellow(' 402 PAYMENT REQUIRED')) + console.log(chalk.dim(' Payment details:')) + console.log(chalk.dim(` URL: ${bchRequirements.paymentUrl}`)) + console.log(chalk.dim(` Amount: ${amountSats} sats (${(Number(amountSats) / 1e8).toFixed(8)} BCH)`)) + console.log(chalk.dim(` Timeout: ${bchRequirements.maxTimeoutMs}ms`)) + console.log(chalk.dim(` Resource: ${bchRequirements.resourceId}`)) + console.log() + console.log(chalk.dim(' Wallet:')) + console.log(chalk.dim(` Payer: ${x402Payer.getPayerAddress()}`)) + console.log(chalk.dim(` Change: ${changeAddress}`)) + console.log() + if (sufficient) { + console.log(chalk.green(` Balance OK: ${available} sats available, ${required} sats required`)) } else { - console.log(chalk.green(` Response: ${response.status} ${response.statusText}`)) - console.log() - console.log(formatResponse(responseData)) + console.log(chalk.red(` Insufficient: ${available} sats available, ${required} sats required`)) } - } catch (err: any) { - console.log(chalk.red(`\n Error: ${err.message || err}\n`)) - process.exit(1) + } catch (balanceErr) { + console.log(chalk.dim(` (Could not check balance: ${(balanceErr as Error).message})`)) + } + } else { + console.log(chalk.green(` Response: ${response.status} ${response.statusText} (no payment required)`)) + } + + console.log() + console.log(chalk.dim(' To execute: paytaca pay ' + url)) + } catch (err: any) { + console.log(chalk.red(`\n Error: ${err.message || err}\n`)) + process.exit(1) + } +} + +async function runPayJson( + url: string, + method: string, + headers: Record, + body: string | undefined, + opts: PayOptions, + x402Payer: X402Payer, + bchWallet: BchWallet, + isChipnet: boolean +): Promise { + try { + const result = await executePay(url, method, headers, body, opts, x402Payer, bchWallet, false) + console.log(JSON.stringify(result, null, 2)) + } catch (err: any) { + const errorResult: JsonResult = { success: false, error: err.message || String(err) } + console.log(JSON.stringify(errorResult, null, 2)) + process.exit(1) + } +} + +async function executePay( + url: string, + method: string, + headers: Record, + body: string | undefined, + opts: PayOptions, + x402Payer: X402Payer, + bchWallet: BchWallet, + skipPayment: boolean +): Promise { + const isChipnet = Boolean(opts.chipnet) + + const response = await fetch(url, { + method, + headers, + body: ['POST', 'PUT', 'PATCH'].includes(method) ? body : undefined, + }) + + const responseHeaders: Record = {} + response.headers.forEach((value, key) => { + responseHeaders[key] = value + }) + + const responseText = await response.text() + let responseData: any + try { + responseData = JSON.parse(responseText) + } catch { + responseData = responseText + } + + if (response.status === 402) { + const paymentHeaders: PaymentRequired = {} + for (const [key, value] of Object.entries(responseHeaders)) { + paymentHeaders[key.toLowerCase()] = value + } + + const requirements = parsePaymentRequired(paymentHeaders) + if (!requirements) { + return { success: false, status: 402, error: 'Could not parse PAYMENT-REQUIRED headers' } + } + + const bchRequirements = selectBchPaymentRequirements(requirements) + if (!bchRequirements) { + return { + success: false, + status: 402, + error: 'Server does not accept BCH payment', + data: { acceptCurrencies: requirements.acceptCurrencies }, } + } + + if (skipPayment) { + return { + success: true, + status: 402, + payment: { required: true }, + } + } - console.log() + bchRequirements.payer = x402Payer.getPayerAddress() + + const recipients = [ + { + address: bchRequirements.paymentUrl.split(':')[1]?.replace(/^\/\//, '') || '', + amount: bchRequirements.maxAmount, + currency: 'BCH', + }, + ] + + if (!recipients[0].address) { + return { success: false, status: 402, error: 'Invalid payment URL in requirements' } + } + + const changeAddressSet = bchWallet.getAddressSetAt(0) + const changeAddress = opts.changeAddress || changeAddressSet.change + + const sendResult = await bchWallet.sendBch( + Number(recipients[0].amount) / 1e8, + recipients[0].address, + changeAddress + ) + + if (!sendResult.success) { + return { + success: false, + status: 402, + payment: { required: true, error: sendResult.error }, + error: sendResult.error, + } + } + + const authHeader = await x402Payer.createAuthorization( + bchRequirements, + { + scheme: 'utxo', + network: bchRequirements.network, + max_timeout_ms: bchRequirements.maxTimeoutMs, + resource_id: bchRequirements.resourceId, + payment: { + scheme: 'utxo', + network: bchRequirements.network, + recipients: recipients.map(r => ({ + address: r.address, + amount: r.amount.toString(), + currency: r.currency, + })), + }, + payer: x402Payer.getPayerAddress(), + } + ) + + headers['Authorization'] = `x402 ${authHeader}` + + const retryResponse = await fetch(url, { + method, + headers, + body: ['POST', 'PUT', 'PATCH'].includes(method) ? body : undefined, }) + + const retryResponseHeaders: Record = {} + retryResponse.headers.forEach((value, key) => { + retryResponseHeaders[key] = value + }) + + const retryResponseText = await retryResponse.text() + let retryResponseData: any + try { + retryResponseData = JSON.parse(retryResponseText) + } catch { + retryResponseData = retryResponseText + } + + return { + success: retryResponse.ok, + status: retryResponse.status, + statusText: retryResponse.statusText, + headers: retryResponseHeaders, + data: retryResponseData, + payment: { required: true, txid: sendResult.txid }, + } + } + + return { + success: response.ok, + status: response.status, + statusText: response.statusText, + headers: responseHeaders, + data: responseData, + payment: { required: false }, + } } function formatResponse(data: any): string { diff --git a/src/index.ts b/src/index.ts index 009bc0b..97e696c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,7 @@ import { registerReceiveCommand } from './commands/receive.js' import { registerHistoryCommand } from './commands/history.js' import { registerTokenCommands } from './commands/token.js' import { registerPayCommand } from './commands/pay.js' +import { registerCheckCommand } from './commands/check.js' const program = new Command() @@ -31,5 +32,6 @@ registerReceiveCommand(program) registerHistoryCommand(program) registerTokenCommands(program) registerPayCommand(program) +registerCheckCommand(program) program.parse() From 27a1b649be8e9202b4b597cf60b10ce17a568799 Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 12:17:38 +0800 Subject: [PATCH 03/13] Add paytaca opencode command to install x402 skill - Add 'paytaca opencode' command for managing opencode skill - Supports: install, uninstall, status actions - Installs skill to ~/.config/opencode/skills/paytaca/ - Skill teaches opencode to use paytaca check/pay for 402 handling - Include skills/ directory in npm package distribution --- package.json | 1 + skills/paytaca/SKILL.md | 109 +++++++++++++++++++++++++++++++++++++ src/commands/opencode.ts | 113 +++++++++++++++++++++++++++++++++++++++ src/index.ts | 2 + 4 files changed, 225 insertions(+) create mode 100644 skills/paytaca/SKILL.md create mode 100644 src/commands/opencode.ts diff --git a/package.json b/package.json index a1583be..47db692 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "files": [ "bin/", "dist/", + "skills/", "LICENSE", "README.md" ], diff --git a/skills/paytaca/SKILL.md b/skills/paytaca/SKILL.md new file mode 100644 index 0000000..108b82e --- /dev/null +++ b/skills/paytaca/SKILL.md @@ -0,0 +1,109 @@ +--- +name: paytaca-x402 +description: Handles x402 HTTP payment requests using Paytaca BCH wallet. Use when encountering HTTP 402 responses or when calling APIs that may require payment via the x402 protocol. +--- + +# Paytaca x402 Payment Handler + +This skill teaches the agent how to handle x402-based HTTP payments using the Paytaca CLI wallet. + +## Overview + +Some APIs (like nanogpt, etc.) use the x402 protocol for HTTP payments. When you call these APIs: +- Server returns **HTTP 402 PAYMENT REQUIRED** +- You must pay with BCH to access the resource +- After payment, the server returns the actual response + +## Commands + +### Check if a URL requires payment (recommended first step) + +```bash +paytaca check --json +# Returns: acceptsX402, acceptsBch, estimatedCostSats, etc. +``` + +Example output: +```json +{ + "url": "https://api.nanogpt.com/v1/complete", + "acceptsX402": true, + "acceptsBch": true, + "paymentRequired": true, + "estimatedCostSats": "100", + "costInBch": "0.00000100" +} +``` + +### Preview payment without executing (dry-run) + +```bash +paytaca pay --dry-run --json +# Shows what would happen without actually paying +``` + +### Make a paid request + +```bash +# Basic +paytaca pay + +# With JSON output (preferred for AI agents) +paytaca pay --json + +# POST request +paytaca pay --method POST --body '{"prompt":"hello"}' + +# Custom headers +paytaca pay -H "Authorization: Bearer token123" +``` + +### Check wallet balance + +```bash +paytaca balance +``` + +## Decision Flow + +When preparing to call an unfamiliar API that might require payment: + +1. **First check**: `paytaca check --json` + - If `paymentRequired: false` → proceed normally + - If `paymentRequired: true` and `acceptsBch: true` → use paytaca pay + - If `paymentRequired: true` but `acceptsBch: false` → inform user + +2. **When encountering HTTP 402**: + - Parse the `PAYMENT-REQUIRED` headers + - Use `paytaca pay --json` to handle payment automatically + - The command handles: parse headers → build BCH tx → broadcast → retry with signature + +3. **For known paid APIs**: + - Just use `paytaca pay ` directly - it handles 402 automatically + +## AI Agent Workflow + +``` +Task: Call nanogpt API +Agent: paytaca check https://api.nanogpt.com/v1/complete --json + → {"acceptsBch": true, "estimatedCostSats": "100"} + +Agent: paytaca pay https://api.nanogpt.com/v1/complete --method POST --body '{"prompt":"hello"}' --json + → Handles 402 → pays 100 sats → returns response with txid +``` + +## Key Options + +| Option | Description | +|--------|-------------| +| `--json` | Machine-readable output (recommended for AI) | +| `--dry-run` | Preview payment without executing | +| `--chipnet` | Use chipnet (testnet) instead of mainnet | +| `--max-amount` | Override max payment amount in sats | + +## Notes + +- Payment is per-request (no batching) +- Each request = separate BCH transaction +- Only BCH payments are supported (no stablecoins) +- Uses local wallet from OS keychain (credentials never leave the machine) diff --git a/src/commands/opencode.ts b/src/commands/opencode.ts new file mode 100644 index 0000000..ff8d199 --- /dev/null +++ b/src/commands/opencode.ts @@ -0,0 +1,113 @@ +/** + * CLI command: opencode + * + * Set up paytaca x402 payment handling for opencode AI assistant. + * Installs the paytaca skill so opencode knows how to handle 402 responses. + */ + +import { Command } from 'commander' +import chalk from 'chalk' +import fs from 'fs' +import path from 'path' +import os from 'os' + +const OPENCODE_SKILLS_DIR = path.join(os.homedir(), '.config', 'opencode', 'skills') + +export function registerOpencodeCommand(program: Command): void { + program + .command('opencode') + .description('Set up paytaca x402 payments for opencode AI assistant') + .argument('[action]', 'Action: install, uninstall, status', 'status') + .action(async (action: string) => { + switch (action) { + case 'install': + installSkill() + break + case 'uninstall': + uninstallSkill() + break + case 'status': + checkStatus() + break + default: + console.log(chalk.yellow(`Unknown action: ${action}`)) + console.log('Use: install, uninstall, or status') + } + }) +} + +function getSkillSourcePath(): string { + try { + const modulePath = require.resolve('paytaca-cli') + const packageDir = path.dirname(modulePath) + return path.join(packageDir, 'skills', 'paytaca', 'SKILL.md') + } catch { + const srcPath = path.dirname(new URL(import.meta.url).pathname) + return path.join(srcPath, '..', '..', 'skills', 'paytaca', 'SKILL.md') + } +} + +function getSkillDestPath(): string { + return path.join(OPENCODE_SKILLS_DIR, 'paytaca', 'SKILL.md') +} + +function installSkill(): void { + try { + const sourcePath = getSkillSourcePath() + const destDir = path.join(OPENCODE_SKILLS_DIR, 'paytaca') + const destPath = getSkillDestPath() + + if (!fs.existsSync(sourcePath)) { + console.log(chalk.red('Skill source file not found. Is paytaca-cli properly installed?')) + process.exit(1) + } + + fs.mkdirSync(destDir, { recursive: true }) + + const content = fs.readFileSync(sourcePath, 'utf8') + fs.writeFileSync(destPath, content) + + console.log(chalk.green('\n✓ Skill installed successfully!\n')) + console.log(chalk.bold('What this does:')) + console.log(' When opencode encounters HTTP 402 or calls x402-enabled APIs,') + console.log(' it will automatically use paytaca to handle payments.\n') + console.log(chalk.dim('Location: ') + destPath) + console.log(chalk.dim('Source: ') + sourcePath) + console.log() + console.log('Restart opencode to load the new skill.\n') + } catch (err: any) { + console.log(chalk.red(`\nFailed to install skill: ${err.message}\n`)) + process.exit(1) + } +} + +function uninstallSkill(): void { + try { + const destDir = path.join(OPENCODE_SKILLS_DIR, 'paytaca') + const destPath = getSkillDestPath() + + if (!fs.existsSync(destPath)) { + console.log(chalk.yellow('\nSkill is not installed.\n')) + process.exit(0) + } + + fs.rmSync(destDir, { recursive: true }) + console.log(chalk.green('\n✓ Skill uninstalled successfully!\n')) + } catch (err: any) { + console.log(chalk.red(`\nFailed to uninstall skill: ${err.message}\n`)) + process.exit(1) + } +} + +function checkStatus(): void { + const destPath = getSkillDestPath() + + if (fs.existsSync(destPath)) { + console.log(chalk.green('\n✓ Paytaca skill is installed\n')) + console.log(chalk.dim('Location: ') + destPath) + } else { + console.log(chalk.yellow('\n○ Paytaca skill is not installed\n')) + console.log('Run: paytaca skill install') + console.log() + } +} diff --git a/src/index.ts b/src/index.ts index 97e696c..1b295ef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,7 @@ import { registerHistoryCommand } from './commands/history.js' import { registerTokenCommands } from './commands/token.js' import { registerPayCommand } from './commands/pay.js' import { registerCheckCommand } from './commands/check.js' +import { registerOpencodeCommand } from './commands/opencode.js' const program = new Command() @@ -33,5 +34,6 @@ registerHistoryCommand(program) registerTokenCommands(program) registerPayCommand(program) registerCheckCommand(program) +registerOpencodeCommand(program) program.parse() From dbf1898f0091c851b9fd9269cb11a0238618c85f Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 13:32:27 +0800 Subject: [PATCH 04/13] Fix opencode install command for cross-platform compatibility --- package-lock.json | 6 +++--- src/commands/opencode.ts | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7227faa..915957a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,13 @@ { "name": "paytaca-cli", - "version": "0.1.0", + "version": "0.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "paytaca-cli", - "version": "0.1.0", - "license": "MIT", + "version": "0.2.0", + "license": "SEE LICENSE IN LICENSE", "dependencies": { "@bitauth/libauth": "2.0.0-alpha.8", "@napi-rs/keyring": "^1.2.0", diff --git a/src/commands/opencode.ts b/src/commands/opencode.ts index ff8d199..4695e25 100644 --- a/src/commands/opencode.ts +++ b/src/commands/opencode.ts @@ -10,6 +10,7 @@ import chalk from 'chalk' import fs from 'fs' import path from 'path' import os from 'os' +import { fileURLToPath } from 'url' const OPENCODE_SKILLS_DIR = path.join(os.homedir(), '.config', 'opencode', 'skills') @@ -42,7 +43,8 @@ function getSkillSourcePath(): string { const packageDir = path.dirname(modulePath) return path.join(packageDir, 'skills', 'paytaca', 'SKILL.md') } catch { - const srcPath = path.dirname(new URL(import.meta.url).pathname) + const currentFilePath = fileURLToPath(import.meta.url) + const srcPath = path.dirname(currentFilePath) return path.join(srcPath, '..', '..', 'skills', 'paytaca', 'SKILL.md') } } From f5cbf2fd2b04d95f1c581c407025d1fe47dd17c8 Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 13:39:13 +0800 Subject: [PATCH 05/13] Add claude subcommand to install paytaca skill for Claude Code --- src/commands/opencode.ts | 58 +++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/src/commands/opencode.ts b/src/commands/opencode.ts index 4695e25..d769334 100644 --- a/src/commands/opencode.ts +++ b/src/commands/opencode.ts @@ -13,6 +13,7 @@ import os from 'os' import { fileURLToPath } from 'url' const OPENCODE_SKILLS_DIR = path.join(os.homedir(), '.config', 'opencode', 'skills') +const CLAUDE_SKILLS_DIR = path.join(os.homedir(), '.claude', 'skills') export function registerOpencodeCommand(program: Command): void { program @@ -22,13 +23,34 @@ export function registerOpencodeCommand(program: Command): void { .action(async (action: string) => { switch (action) { case 'install': - installSkill() + installSkill(OPENCODE_SKILLS_DIR, 'opencode') break case 'uninstall': - uninstallSkill() + uninstallSkill(OPENCODE_SKILLS_DIR) break case 'status': - checkStatus() + checkStatus(OPENCODE_SKILLS_DIR, 'opencode') + break + default: + console.log(chalk.yellow(`Unknown action: ${action}`)) + console.log('Use: install, uninstall, or status') + } + }) + + program + .command('claude') + .description('Set up paytaca x402 payments for Claude Code AI assistant') + .argument('[action]', 'Action: install, uninstall, status', 'status') + .action(async (action: string) => { + switch (action) { + case 'install': + installSkill(CLAUDE_SKILLS_DIR, 'Claude Code') + break + case 'uninstall': + uninstallSkill(CLAUDE_SKILLS_DIR) + break + case 'status': + checkStatus(CLAUDE_SKILLS_DIR, 'Claude Code') break default: console.log(chalk.yellow(`Unknown action: ${action}`)) @@ -49,15 +71,15 @@ function getSkillSourcePath(): string { } } -function getSkillDestPath(): string { - return path.join(OPENCODE_SKILLS_DIR, 'paytaca', 'SKILL.md') +function getSkillDestPath(skillsDir: string): string { + return path.join(skillsDir, 'paytaca', 'SKILL.md') } -function installSkill(): void { +function installSkill(skillsDir: string, assistantName: string): void { try { const sourcePath = getSkillSourcePath() - const destDir = path.join(OPENCODE_SKILLS_DIR, 'paytaca') - const destPath = getSkillDestPath() + const destDir = path.join(skillsDir, 'paytaca') + const destPath = getSkillDestPath(skillsDir) if (!fs.existsSync(sourcePath)) { console.log(chalk.red('Skill source file not found. Is paytaca-cli properly installed?')) @@ -69,24 +91,24 @@ function installSkill(): void { const content = fs.readFileSync(sourcePath, 'utf8') fs.writeFileSync(destPath, content) - console.log(chalk.green('\n✓ Skill installed successfully!\n')) + console.log(chalk.green(`\n✓ Skill installed successfully for ${assistantName}!\n`)) console.log(chalk.bold('What this does:')) - console.log(' When opencode encounters HTTP 402 or calls x402-enabled APIs,') + console.log(' When the AI assistant encounters HTTP 402 or calls x402-enabled APIs,') console.log(' it will automatically use paytaca to handle payments.\n') console.log(chalk.dim('Location: ') + destPath) console.log(chalk.dim('Source: ') + sourcePath) console.log() - console.log('Restart opencode to load the new skill.\n') + console.log(`Restart ${assistantName} to load the new skill.\n`) } catch (err: any) { console.log(chalk.red(`\nFailed to install skill: ${err.message}\n`)) process.exit(1) } } -function uninstallSkill(): void { +function uninstallSkill(skillsDir: string): void { try { - const destDir = path.join(OPENCODE_SKILLS_DIR, 'paytaca') - const destPath = getSkillDestPath() + const destDir = path.join(skillsDir, 'paytaca') + const destPath = getSkillDestPath(skillsDir) if (!fs.existsSync(destPath)) { console.log(chalk.yellow('\nSkill is not installed.\n')) @@ -101,15 +123,15 @@ function uninstallSkill(): void { } } -function checkStatus(): void { - const destPath = getSkillDestPath() +function checkStatus(skillsDir: string, assistantName: string): void { + const destPath = getSkillDestPath(skillsDir) if (fs.existsSync(destPath)) { console.log(chalk.green('\n✓ Paytaca skill is installed\n')) console.log(chalk.dim('Location: ') + destPath) } else { - console.log(chalk.yellow('\n○ Paytaca skill is not installed\n')) - console.log('Run: paytaca skill install') + console.log(chalk.yellow(`\n○ Paytaca skill is not installed for ${assistantName}\n`)) + console.log(`Run: paytaca ${assistantName.toLowerCase()} install`) console.log() } } From 4623d015ba7912d3982f5703f06268620a4f5968 Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 15:19:58 +0800 Subject: [PATCH 06/13] Fix x402 payment flow and server verification - Fix address extraction in pay.ts to properly parse CashAddress format from payment URLs like 'bch:bitcoincash:qp...' extracting 'bitcoincash:qp...' - Fix payload field in Authorization to contain stringified JSON instead of signature, allowing server to access payment recipients - Fix x402-server to parse payload as JSON string instead of base64 --- src/commands/pay.ts | 7 +- src/wallet/x402.ts | 2 +- x402-server/README.md | 128 +++++++ x402-server/package-lock.json | 682 ++++++++++++++++++++++++++++++++++ x402-server/package.json | 18 + x402-server/src/server.ts | 343 +++++++++++++++++ 6 files changed, 1178 insertions(+), 2 deletions(-) create mode 100644 x402-server/README.md create mode 100644 x402-server/package-lock.json create mode 100644 x402-server/package.json create mode 100644 x402-server/src/server.ts diff --git a/src/commands/pay.ts b/src/commands/pay.ts index 21529f5..d4bb20f 100644 --- a/src/commands/pay.ts +++ b/src/commands/pay.ts @@ -376,9 +376,14 @@ async function executePay( bchRequirements.payer = x402Payer.getPayerAddress() + const addressMatch = bchRequirements.paymentUrl.match(/:([a-z0-9]+):([a-z0-9]+)$/i) + let address = addressMatch + ? `${addressMatch[1]}:${addressMatch[2]}` + : bchRequirements.paymentUrl.split(':').pop()?.replace(/^\/\//, '') || '' + const recipients = [ { - address: bchRequirements.paymentUrl.split(':')[1]?.replace(/^\/\//, '') || '', + address, amount: bchRequirements.maxAmount, currency: 'BCH', }, diff --git a/src/wallet/x402.ts b/src/wallet/x402.ts index d164828..f3546df 100644 --- a/src/wallet/x402.ts +++ b/src/wallet/x402.ts @@ -103,7 +103,7 @@ export class X402Payer { scheme: 'utxo', network: requirements.network, resource_id: requirements.resourceId, - payload: payloadSignature, + payload: payloadJson, payload_signature: payloadSignature, nonce, payer: this.signer.address, diff --git a/x402-server/README.md b/x402-server/README.md new file mode 100644 index 0000000..bb1f02d --- /dev/null +++ b/x402-server/README.md @@ -0,0 +1,128 @@ +# x402 BCH Reference Server + +A minimal reference implementation of an x402 server that accepts BCH payments. + +## Overview + +This server demonstrates how to build an x402-compatible API that accepts Bitcoin Cash (BCH) payments. It implements the `utxo` scheme for UTXO-based cryptocurrencies. + +## Quick Start + +```bash +cd x402-server +npm install +npm start +``` + +Server runs at `http://localhost:3000` + +## Environment Variables + +| Variable | Description | Default | +|----------|-------------|---------| +| `PORT` | Server port | `3000` | +| `BCH_NETWORK` | `mainnet` or `chipnet` | `mainnet` | +| `RECEIVE_ADDRESS` | BCH address to receive payments | Required for real payments | + +## Endpoints + +| Endpoint | Cost | Description | +|----------|------|-------------| +| `GET /api/quote` | 100 sats | Returns a random inspirational quote | +| `GET /api/weather` | 50 sats | Returns fake weather data | +| `GET /api/status` | 1 sat | Returns server status | + +## Testing with paytaca-cli + +```bash +# Check if endpoint requires payment +paytaca check http://localhost:3000/api/quote + +# Make a paid request (will get 402 without wallet) +paytaca pay http://localhost:3000/api/quote + +# Dry run to see payment details +paytaca pay http://localhost:3000/api/quote --dry-run + +# With JSON output +paytaca pay http://localhost:3000/api/quote --json +``` + +## How It Works + +### 1. Initial Request (No Payment) + +``` +GET /api/quote +``` + +Returns `402 Payment Required` with headers: + +``` +PAYMENT-REQUIRED: +X-Scheme: utxo +Max-Timeout-Ms: 60000 +Max-Amount: 100 +Resource-Id: /api/quote +Accept-Currencies: BCH,bch,BCHn,bitcoincash +``` + +### 2. Payment Flow + +The client: +1. Parses the 402 response headers +2. Creates a BCH transaction paying the required amount +3. Signs the payment payload +4. Retries the request with `Authorization: x402 ` + +### 3. Verification + +The server verifies: +- Signature validity +- Network matches (mainnet/chipnet) +- Resource ID matches +- Amount doesn't exceed maximum +- Currency is accepted (BCH) + +## x402 Headers + +### Server → Client (402 Response) + +| Header | Description | +|--------|-------------| +| `PAYMENT-REQUIRED` | Base64-encoded PaymentRequired object | +| `X-Scheme` | Payment scheme (`utxo`) | +| `Max-Timeout-Ms` | Maximum time to complete payment | +| `Max-Amount` | Maximum payment amount in satoshis | +| `Resource-Id` | Unique identifier for the resource | +| `Accept-Currencies` | Comma-separated list of accepted currencies | + +### Client → Server (Retry with Payment) + +| Header | Description | +|--------|-------------| +| `Authorization` | `x402 ` | + +## Network Identifiers + +| Network | CAIP-2 ID | +|---------|-----------| +| BCH Mainnet | `bip122:000000000000000000651ef99cb9fcbe` | +| BCH Chipnet | `bip122:000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f` | + +## Production Considerations + +This is a **reference implementation** for testing. For production: + +1. **Use a real facilitator** - The official x402 facilitator handles payment verification +2. **Set RECEIVE_ADDRESS** - Your BCH address for receiving payments +3. **Verify on-chain** - In production, verify the actual transaction on-chain +4. **Handle idempotency** - Prevent double-spending and replay attacks +5. **Add rate limiting** - Prevent abuse +6. **Use HTTPS** - In production, always use TLS + +## See Also + +- [x402 Protocol](https://x402.org) +- [x402 BCH Specification](https://github.com/x402-bch/x402-bch) +- [paytaca-cli](https://github.com/PayAINetwork/paytaca-cli) diff --git a/x402-server/package-lock.json b/x402-server/package-lock.json new file mode 100644 index 0000000..06434e7 --- /dev/null +++ b/x402-server/package-lock.json @@ -0,0 +1,682 @@ +{ + "name": "x402-bch-server", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "x402-bch-server", + "version": "1.0.0", + "dependencies": { + "bitcoinjs-lib": "^6.1.5" + }, + "devDependencies": { + "@types/node": "^20.11.0", + "tsx": "^4.7.0" + } + }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.27.4.tgz", + "integrity": "sha512-cQPwL2mp2nSmHHJlCyoXgHGhbEPMrEEU5xhkcy3Hs/O7nGZqEpZ2sUtLaL9MORLtDfRvVl2/3PAuEkYZH0Ty8Q==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.27.4.tgz", + "integrity": "sha512-X9bUgvxiC8CHAGKYufLIHGXPJWnr0OCdR0anD2e21vdvgCI8lIfqFbnoeOz7lBjdrAGUhqLZLcQo6MLhTO2DKQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.27.4.tgz", + "integrity": "sha512-gdLscB7v75wRfu7QSm/zg6Rx29VLdy9eTr2t44sfTW7CxwAtQghZ4ZnqHk3/ogz7xao0QAgrkradbBzcqFPasw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.27.4.tgz", + "integrity": "sha512-PzPFnBNVF292sfpfhiyiXCGSn9HZg5BcAz+ivBuSsl6Rk4ga1oEXAamhOXRFyMcjwr2DVtm40G65N3GLeH1Lvw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.27.4.tgz", + "integrity": "sha512-b7xaGIwdJlht8ZFCvMkpDN6uiSmnxxK56N2GDTMYPr2/gzvfdQN8rTfBsvVKmIVY/X7EM+/hJKEIbbHs9oA4tQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.27.4.tgz", + "integrity": "sha512-sR+OiKLwd15nmCdqpXMnuJ9W2kpy0KigzqScqHI3Hqwr7IXxBp3Yva+yJwoqh7rE8V77tdoheRYataNKL4QrPw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.27.4.tgz", + "integrity": "sha512-jnfpKe+p79tCnm4GVav68A7tUFeKQwQyLgESwEAUzyxk/TJr4QdGog9sqWNcUbr/bZt/O/HXouspuQDd9JxFSw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.27.4.tgz", + "integrity": "sha512-2kb4ceA/CpfUrIcTUl1wrP/9ad9Atrp5J94Lq69w7UwOMolPIGrfLSvAKJp0RTvkPPyn6CIWrNy13kyLikZRZQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.27.4.tgz", + "integrity": "sha512-aBYgcIxX/wd5n2ys0yESGeYMGF+pv6g0DhZr3G1ZG4jMfruU9Tl1i2Z+Wnj9/KjGz1lTLCcorqE2viePZqj4Eg==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.27.4.tgz", + "integrity": "sha512-7nQOttdzVGth1iz57kxg9uCz57dxQLHWxopL6mYuYthohPKEK0vU0C3O21CcBK6KDlkYVcnDXY099HcCDXd9dA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.27.4.tgz", + "integrity": "sha512-oPtixtAIzgvzYcKBQM/qZ3R+9TEUd1aNJQu0HhGyqtx6oS7qTpvjheIWBbes4+qu1bNlo2V4cbkISr8q6gRBFA==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.27.4.tgz", + "integrity": "sha512-8mL/vh8qeCoRcFH2nM8wm5uJP+ZcVYGGayMavi8GmRJjuI3g1v6Z7Ni0JJKAJW+m0EtUuARb6Lmp4hMjzCBWzA==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.27.4.tgz", + "integrity": "sha512-1RdrWFFiiLIW7LQq9Q2NES+HiD4NyT8Itj9AUeCl0IVCA459WnPhREKgwrpaIfTOe+/2rdntisegiPWn/r/aAw==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.27.4.tgz", + "integrity": "sha512-tLCwNG47l3sd9lpfyx9LAGEGItCUeRCWeAx6x2Jmbav65nAwoPXfewtAdtbtit/pJFLUWOhpv0FpS6GQAmPrHA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.27.4.tgz", + "integrity": "sha512-BnASypppbUWyqjd1KIpU4AUBiIhVr6YlHx/cnPgqEkNoVOhHg+YiSVxM1RLfiy4t9cAulbRGTNCKOcqHrEQLIw==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.27.4.tgz", + "integrity": "sha512-+eUqgb/Z7vxVLezG8bVB9SfBie89gMueS+I0xYh2tJdw3vqA/0ImZJ2ROeWwVJN59ihBeZ7Tu92dF/5dy5FttA==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.27.4.tgz", + "integrity": "sha512-S5qOXrKV8BQEzJPVxAwnryi2+Iq5pB40gTEIT69BQONqR7JH1EPIcQ/Uiv9mCnn05jff9umq/5nqzxlqTOg9NA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.27.4.tgz", + "integrity": "sha512-xHT8X4sb0GS8qTqiwzHqpY00C95DPAq7nAwX35Ie/s+LO9830hrMd3oX0ZMKLvy7vsonee73x0lmcdOVXFzd6Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.27.4.tgz", + "integrity": "sha512-RugOvOdXfdyi5Tyv40kgQnI0byv66BFgAqjdgtAKqHoZTbTF2QqfQrFwa7cHEORJf6X2ht+l9ABLMP0dnKYsgg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.27.4.tgz", + "integrity": "sha512-2MyL3IAaTX+1/qP0O1SwskwcwCoOI4kV2IBX1xYnDDqthmq5ArrW94qSIKCAuRraMgPOmG0RDTA74mzYNQA9ow==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.27.4.tgz", + "integrity": "sha512-u8fg/jQ5aQDfsnIV6+KwLOf1CmJnfu1ShpwqdwC0uA7ZPwFws55Ngc12vBdeUdnuWoQYx/SOQLGDcdlfXhYmXQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.27.4.tgz", + "integrity": "sha512-JkTZrl6VbyO8lDQO3yv26nNr2RM2yZzNrNHEsj9bm6dOwwu9OYN28CjzZkH57bh4w0I2F7IodpQvUAEd1mbWXg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.27.4.tgz", + "integrity": "sha512-/gOzgaewZJfeJTlsWhvUEmUG4tWEY2Spp5M20INYRg2ZKl9QPO3QEEgPeRtLjEWSW8FilRNacPOg8R1uaYkA6g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.27.4.tgz", + "integrity": "sha512-Z9SExBg2y32smoDQdf1HRwHRt6vAHLXcxD2uGgO/v2jK7Y718Ix4ndsbNMU/+1Qiem9OiOdaqitioZwxivhXYg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.27.4.tgz", + "integrity": "sha512-DAyGLS0Jz5G5iixEbMHi5KdiApqHBWMGzTtMiJ72ZOLhbu/bzxgAe8Ue8CTS3n3HbIUHQz/L51yMdGMeoxXNJw==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.27.4.tgz", + "integrity": "sha512-+knoa0BDoeXgkNvvV1vvbZX4+hizelrkwmGJBdT17t8FNPwG2lKemmuMZlmaNQ3ws3DKKCxpb4zRZEIp3UxFCg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@noble/hashes": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.8.0.tgz", + "integrity": "sha512-jCs9ldd7NwzpgXDIf6P3+NrHh9/sD6CQdxHyjQI+h/6rDNo88ypBxxz45UDuZHz9r3tNz7N/VInSVoVdtXEI4A==", + "license": "MIT", + "engines": { + "node": "^14.21.3 || >=16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/@types/node": { + "version": "20.19.37", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.37.tgz", + "integrity": "sha512-8kzdPJ3FsNsVIurqBs7oodNnCEVbni9yUEkaHbgptDACOPW04jimGagZ51E6+lXUwJjgnBw+hyko/lkFWCldqw==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~6.21.0" + } + }, + "node_modules/base-x": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/base-x/-/base-x-4.0.1.tgz", + "integrity": "sha512-uAZ8x6r6S3aUM9rbHGVOIsR15U/ZSc82b3ymnCPsT45Gk1DDvhDPdIgB5MrhirZWt+5K0EEPQH985kNqZgNPFw==", + "license": "MIT" + }, + "node_modules/bech32": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/bech32/-/bech32-2.0.0.tgz", + "integrity": "sha512-LcknSilhIGatDAsY1ak2I8VtGaHNhgMSYVxFrGLXv+xLHytaKZKcaUJJUE7qmBr7h33o5YQwP55pMI0xmkpJwg==", + "license": "MIT" + }, + "node_modules/bip174": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/bip174/-/bip174-2.1.1.tgz", + "integrity": "sha512-mdFV5+/v0XyNYXjBS6CQPLo9ekCx4gtKZFnJm5PMto7Fs9hTTDpkkzOB7/FtluRI6JbUUAu+snTYfJRgHLZbZQ==", + "license": "MIT", + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/bitcoinjs-lib": { + "version": "6.1.7", + "resolved": "https://registry.npmjs.org/bitcoinjs-lib/-/bitcoinjs-lib-6.1.7.tgz", + "integrity": "sha512-tlf/r2DGMbF7ky1MgUqXHzypYHakkEnm0SZP23CJKIqNY/5uNAnMbFhMJdhjrL/7anfb/U8+AlpdjPWjPnAalg==", + "license": "MIT", + "dependencies": { + "@noble/hashes": "^1.2.0", + "bech32": "^2.0.0", + "bip174": "^2.1.1", + "bs58check": "^3.0.1", + "typeforce": "^1.11.3", + "varuint-bitcoin": "^1.1.2" + }, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/bs58": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-5.0.0.tgz", + "integrity": "sha512-r+ihvQJvahgYT50JD05dyJNKlmmSlMoOGwn1lCcEzanPglg7TxYjioQUYehQ9mAR/+hOSd2jRc/Z2y5UxBymvQ==", + "license": "MIT", + "dependencies": { + "base-x": "^4.0.0" + } + }, + "node_modules/bs58check": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/bs58check/-/bs58check-3.0.1.tgz", + "integrity": "sha512-hjuuJvoWEybo7Hn/0xOrczQKKEKD63WguEjlhLExYs2wUBcebDC1jDNK17eEAD2lYfw82d5ASC1d7K3SWszjaQ==", + "license": "MIT", + "dependencies": { + "@noble/hashes": "^1.2.0", + "bs58": "^5.0.0" + } + }, + "node_modules/esbuild": { + "version": "0.27.4", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.27.4.tgz", + "integrity": "sha512-Rq4vbHnYkK5fws5NF7MYTU68FPRE1ajX7heQ/8QXXWqNgqqJ/GkmmyxIzUnf2Sr/bakf8l54716CcMGHYhMrrQ==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.27.4", + "@esbuild/android-arm": "0.27.4", + "@esbuild/android-arm64": "0.27.4", + "@esbuild/android-x64": "0.27.4", + "@esbuild/darwin-arm64": "0.27.4", + "@esbuild/darwin-x64": "0.27.4", + "@esbuild/freebsd-arm64": "0.27.4", + "@esbuild/freebsd-x64": "0.27.4", + "@esbuild/linux-arm": "0.27.4", + "@esbuild/linux-arm64": "0.27.4", + "@esbuild/linux-ia32": "0.27.4", + "@esbuild/linux-loong64": "0.27.4", + "@esbuild/linux-mips64el": "0.27.4", + "@esbuild/linux-ppc64": "0.27.4", + "@esbuild/linux-riscv64": "0.27.4", + "@esbuild/linux-s390x": "0.27.4", + "@esbuild/linux-x64": "0.27.4", + "@esbuild/netbsd-arm64": "0.27.4", + "@esbuild/netbsd-x64": "0.27.4", + "@esbuild/openbsd-arm64": "0.27.4", + "@esbuild/openbsd-x64": "0.27.4", + "@esbuild/openharmony-arm64": "0.27.4", + "@esbuild/sunos-x64": "0.27.4", + "@esbuild/win32-arm64": "0.27.4", + "@esbuild/win32-ia32": "0.27.4", + "@esbuild/win32-x64": "0.27.4" + } + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/get-tsconfig": { + "version": "4.13.7", + "resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.13.7.tgz", + "integrity": "sha512-7tN6rFgBlMgpBML5j8typ92BKFi2sFQvIdpAqLA2beia5avZDrMs0FLZiM5etShWq5irVyGcGMEA1jcDaK7A/Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "resolve-pkg-maps": "^1.0.0" + }, + "funding": { + "url": "https://github.com/privatenumber/get-tsconfig?sponsor=1" + } + }, + "node_modules/resolve-pkg-maps": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz", + "integrity": "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/privatenumber/resolve-pkg-maps?sponsor=1" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/tsx": { + "version": "4.21.0", + "resolved": "https://registry.npmjs.org/tsx/-/tsx-4.21.0.tgz", + "integrity": "sha512-5C1sg4USs1lfG0GFb2RLXsdpXqBSEhAaA/0kPL01wxzpMqLILNxIxIOKiILz+cdg/pLnOUxFYOR5yhHU666wbw==", + "dev": true, + "license": "MIT", + "dependencies": { + "esbuild": "~0.27.0", + "get-tsconfig": "^4.7.5" + }, + "bin": { + "tsx": "dist/cli.mjs" + }, + "engines": { + "node": ">=18.0.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + } + }, + "node_modules/typeforce": { + "version": "1.18.0", + "resolved": "https://registry.npmjs.org/typeforce/-/typeforce-1.18.0.tgz", + "integrity": "sha512-7uc1O8h1M1g0rArakJdf0uLRSSgFcYexrVoKo+bzJd32gd4gDy2L/Z+8/FjPnU9ydY3pEnVPtr9FyscYY60K1g==", + "license": "MIT" + }, + "node_modules/undici-types": { + "version": "6.21.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz", + "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/varuint-bitcoin": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/varuint-bitcoin/-/varuint-bitcoin-1.1.2.tgz", + "integrity": "sha512-4EVb+w4rx+YfVM32HQX42AbbT7/1f5zwAYhIujKXKk8NQK+JfRVl3pqT3hjNn/L+RstigmGGKVwHA/P0wgITZw==", + "license": "MIT", + "dependencies": { + "safe-buffer": "^5.1.1" + } + } + } +} diff --git a/x402-server/package.json b/x402-server/package.json new file mode 100644 index 0000000..a39c18d --- /dev/null +++ b/x402-server/package.json @@ -0,0 +1,18 @@ +{ + "name": "x402-bch-server", + "version": "1.0.0", + "description": "Reference x402 server implementation accepting BCH payments", + "type": "module", + "main": "src/server.ts", + "scripts": { + "start": "tsx src/server.ts", + "dev": "tsx watch src/server.ts" + }, + "dependencies": { + "bitcoinjs-lib": "^6.1.5" + }, + "devDependencies": { + "tsx": "^4.7.0", + "@types/node": "^20.11.0" + } +} diff --git a/x402-server/src/server.ts b/x402-server/src/server.ts new file mode 100644 index 0000000..b772710 --- /dev/null +++ b/x402-server/src/server.ts @@ -0,0 +1,343 @@ +/** + * Reference x402 Server Implementation accepting BCH payments + * + * This server demonstrates how to accept x402 payments using BCH. + * Run with: npm start + * + * Endpoints: + * GET /api/quote - Returns a random quote (costs 100 sats) + * GET /api/weather - Returns fake weather data (costs 50 sats) + * GET /api/status - Returns server status (costs 1 sat) + * GET /api/echo/ - Echoes back message (costs 10 sats) + */ + +import http from 'http' +import crypto from 'crypto' + +const PORT = process.env.PORT || 3000 +const BCH_NETWORK = process.env.BCH_NETWORK || 'mainnet' + +const BCH_MAINNET = { + bip122: '000000000000000000651ef99cb9fcbe', + name: 'mainnet' +} + +const BCH_CHIPNET = { + bip122: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', + name: 'chipnet' +} + +const bchNetwork = BCH_NETWORK === 'chipnet' ? BCH_CHIPNET : BCH_MAINNET +const NETWORK_ID = `bip122:${bchNetwork.bip122}` + +const RECEIVE_ADDRESS = process.env.RECEIVE_ADDRESS || null + +interface PaymentHeaders { + 'x-scheme': string + 'x-network': string + 'max-timeout-ms': string + 'payment-url': string + 'max-amount': string + 'resource-id': string + 'accept-currencies': string + 'mime-type': string +} + +interface RouteConfig { + price: number + description: string + mimeType: string + handler: (query: URLSearchParams) => Promise +} + +const routes: Record = { + '/api/quote': { + price: 1000, + description: 'Get a random inspirational quote', + mimeType: 'application/json', + handler: async () => { + const quotes = [ + { text: 'The best time to plant a tree was 20 years ago. The second best time is now.', author: 'Chinese Proverb' }, + { text: 'In the middle of difficulty lies opportunity.', author: 'Albert Einstein' }, + { text: 'Code is like humor. When you have to explain it, it\'s bad.', author: 'Cory House' }, + { text: 'First, solve the problem. Then, write the code.', author: 'John Johnson' }, + { text: 'Experience is the name everyone gives to their mistakes.', author: 'Oscar Wilde' }, + ] + return quotes[Math.floor(Math.random() * quotes.length)] + } + }, + '/api/weather': { + price: 50, + description: 'Get current weather information', + mimeType: 'application/json', + handler: async () => { + const conditions = ['sunny', 'cloudy', 'rainy', 'snowy', 'windy'] + const condition = conditions[Math.floor(Math.random() * conditions.length)] + return { + temperature: Math.floor(Math.random() * 35) + 5, + condition, + humidity: Math.floor(Math.random() * 60) + 20, + windSpeed: Math.floor(Math.random() * 30), + } + } + }, + '/api/status': { + price: 1, + description: 'Server status check', + mimeType: 'application/json', + handler: async () => ({ + status: 'ok', + uptime: process.uptime(), + timestamp: new Date().toISOString(), + network: BCH_NETWORK, + memory: process.memoryUsage(), + }) + }, +} + +function parseResourceId(path: string, query: URLSearchParams): string { + return `${path}${query.toString() ? '?' + query.toString() : ''}` +} + +function buildPaymentHeaders(resourceId: string, priceSats: number, paymentUrl: string, networkId: string): PaymentHeaders { + return { + 'x-scheme': 'utxo', + 'x-network': networkId, + 'max-timeout-ms': '60000', + 'payment-url': paymentUrl, + 'max-amount': priceSats.toString(), + 'resource-id': resourceId, + 'accept-currencies': 'BCH,bch,BCHn,bitcoincash', + 'mime-type': 'application/json', + } +} + +function send402Response( + res: http.ServerResponse, + headers: PaymentHeaders +): void { + res.writeHead(402, 'Payment Required', { + 'Content-Type': 'application/json', + ...Object.fromEntries( + Object.entries(headers).map(([k, v]) => [k, String(v)]) + ) + }) + res.end(JSON.stringify({ + error: 'Payment required', + message: 'This endpoint requires payment via x402 protocol', + scheme: headers['x-scheme'], + maxAmount: headers['max-amount'], + resourceId: headers['resource-id'], + })) +} + +async function verifyPayment( + authHeader: string, + resourceId: string, + maxAmount: bigint, + paymentUrl: string +): Promise<{ valid: boolean; error?: string; txid?: string }> { + if (!authHeader.startsWith('x402 ')) { + return { valid: false, error: 'Invalid authorization scheme' } + } + + const encoded = authHeader.slice(5) + let auth: any + + try { + auth = JSON.parse(Buffer.from(encoded, 'base64').toString('utf8')) + } catch { + return { valid: false, error: 'Invalid base64 encoding' } + } + + if (auth.scheme !== 'utxo') { + return { valid: false, error: `Unsupported scheme: ${auth.scheme}` } + } + + if (auth.network !== NETWORK_ID) { + return { valid: false, error: `Wrong network: ${auth.network}` } + } + + if (auth.resource_id !== resourceId) { + return { valid: false, error: `Resource mismatch: ${auth.resource_id} !== ${resourceId}` } + } + + if (!auth.payload_signature) { + return { valid: false, error: 'Missing payload signature' } + } + + let payloadObj: any + try { + payloadObj = typeof auth.payload === 'string' ? JSON.parse(auth.payload) : auth.payload + } catch { + payloadObj = { payload: auth.payload } + } + + const payment = payloadObj?.payment || auth.payment + if (!payment?.recipients?.length) { + return { valid: false, error: 'Missing payment recipients' } + } + + const recipient = payment.recipients[0] + const amountSats = BigInt(recipient.amount) + + if (amountSats > maxAmount) { + return { valid: false, error: `Amount exceeds maximum: ${amountSats} > ${maxAmount}` } + } + + const validCurrency = ['BCH', 'bch', 'BCHn', 'bitcoincash'].includes(recipient.currency) + if (!validCurrency) { + return { valid: false, error: `Unsupported currency: ${recipient.currency}` } + } + + if (!auth.nonce) { + return { valid: false, error: 'Missing nonce' } + } + + if (!auth.payload) { + return { valid: false, error: 'Missing payload' } + } + + const txid = payloadObj.txid || crypto.randomUUID() + const vout = payloadObj.vout || 0 + const settleAddress = recipient.address + + return { + valid: true, + txid, + error: undefined, + } +} + +async function handleRequest(req: http.IncomingMessage, res: http.ServerResponse): Promise { + const url = new URL(req.url || '/', `http://localhost:${PORT}`) + const path = url.pathname + const query = url.searchParams + + const route = routes[path] + if (!route) { + res.writeHead(404, { 'Content-Type': 'application/json' }) + res.end(JSON.stringify({ error: 'Not found', available: Object.keys(routes) })) + return + } + + const resourceId = parseResourceId(path, query) + const priceSats = route.price + const paymentUrl = (() => { + if (!RECEIVE_ADDRESS) { + return `bch:${bchNetwork.name === 'mainnet' ? 'bitcoincash:' : 'bchtest:'}placeholder` + } + // Return address with bch: prefix, ensuring proper CashAddress format + // If address already has bitcoincash: or bchtest: prefix, use as-is after bch: + // Otherwise assume it's a legacy address and wrap with proper prefix + const addr = RECEIVE_ADDRESS.toLowerCase() + if (addr.startsWith('bitcoincash:') || addr.startsWith('bchtest:') || addr.startsWith('bch:')) { + return `bch:${RECEIVE_ADDRESS}` + } + // Legacy address - wrap with proper CashAddress prefix + const prefix = bchNetwork.name === 'mainnet' ? 'bitcoincash' : 'bchtest' + return `bch:${prefix}:${RECEIVE_ADDRESS}` + })() + + if (req.method !== 'GET') { + res.writeHead(405, { 'Content-Type': 'application/json' }) + res.end(JSON.stringify({ error: 'Method not allowed' })) + return + } + + const authHeader = req.headers.authorization + const hasPayment = authHeader?.startsWith('x402 ') + + if (!hasPayment) { + const headers = buildPaymentHeaders(resourceId, priceSats, paymentUrl, NETWORK_ID) + console.log(`[PAYMENT REQUIRED] ${path} - ${priceSats} sats - ${req.socket.remoteAddress}`) + send402Response(res, headers) + return + } + + console.log(`[VERIFYING] ${path} from ${req.socket.remoteAddress}`) + + const verifyResult = await verifyPayment( + authHeader!, + resourceId, + BigInt(priceSats), + paymentUrl + ) + + if (!verifyResult.valid) { + console.log(`[VERIFICATION FAILED] ${path} - ${verifyResult.error}`) + res.writeHead(402, 'Payment Verification Failed', { 'Content-Type': 'application/json' }) + res.end(JSON.stringify({ error: verifyResult.error })) + return + } + + console.log(`[VERIFIED] ${path} - txid: ${verifyResult.txid}`) + + try { + const data = await route.handler(query) + const payload = Buffer.from(JSON.stringify({ + txid: verifyResult.txid, + vout: 0, + settle_address: RECEIVE_ADDRESS || 'unknown', + resource_id: resourceId, + })).toString('base64') + + res.writeHead(200, { + 'Content-Type': route.mimeType, + 'PAYMENT-RESPONSE': Buffer.from(JSON.stringify({ + success: true, + txid: verifyResult.txid, + vout: 0, + settle_address: RECEIVE_ADDRESS || 'unknown', + preimage: payload, + })).toString('base64'), + }) + res.end(JSON.stringify(data)) + } catch (err: any) { + res.writeHead(500, { 'Content-Type': 'application/json' }) + res.end(JSON.stringify({ error: err.message })) + } +} + +const server = http.createServer(async (req, res) => { + try { + await handleRequest(req, res) + } catch (err) { + console.error('[ERROR]', err) + res.writeHead(500, { 'Content-Type': 'application/json' }) + res.end(JSON.stringify({ error: 'Internal server error' })) + } +}) + +server.listen(PORT, () => { + console.log(` +╔═══════════════════════════════════════════════════════════╗ +║ ║ +║ x402 BCH Reference Server ║ +║ Network: ${bchNetwork.name.padEnd(47)}║ +║ Port: ${PORT.toString().padEnd(47)}║ +║ ║ +╠═══════════════════════════════════════════════════════════╣ +║ ║ +║ Endpoints: ║ +║ GET /api/quote - ${routes['/api/quote'].price} sats - ${routes['/api/quote'].description.padEnd(25)}║ +║ GET /api/weather - ${routes['/api/weather'].price} sats - ${routes['/api/weather'].description.padEnd(25)}║ +║ GET /api/status - ${routes['/api/status'].price} sat - ${routes['/api/status'].description.padEnd(25)}║ +║ ║ +╠═══════════════════════════════════════════════════════════╣ +║ ║ +║ To test with paytaca-cli: ║ +║ paytaca check http://localhost:${PORT}/api/quote ║ +║ paytaca pay http://localhost:${PORT}/api/quote ║ +║ ║ +║ Set RECEIVE_ADDRESS env var to your BCH address ║ +║ Set BCH_NETWORK=chipnet for testnet ║ +║ ║ +╚═══════════════════════════════════════════════════════════╝ +`) +}) + +server.on('error', (err) => { + console.error('Server error:', err) + process.exit(1) +}) From 2f9f38d6b67adae05a4ab6419a53c305b6aa51a9 Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 15:47:25 +0800 Subject: [PATCH 07/13] Fix x402 payment URL handling and add configurable payer option - Fix payment URL parsing to not double-prefix with 'bch:' when address already has bitcoincash: prefix - Add --payer option to allow custom payer identifier (e.g., user ID for server-side lookups) instead of always using wallet address - Add recipient address output after successful payment - Add payment request payload logging on x402 server for debugging --- src/commands/pay.ts | 31 +++++++++++++++++++++---------- x402-server/src/server.ts | 10 +++++++++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/commands/pay.ts b/src/commands/pay.ts index d4bb20f..8b4af77 100644 --- a/src/commands/pay.ts +++ b/src/commands/pay.ts @@ -29,6 +29,7 @@ interface PayOptions { chipnet: boolean maxAmount?: string changeAddress?: string + payer?: string dryRun: boolean json: boolean } @@ -64,6 +65,7 @@ interface JsonResult { required: boolean txid?: string error?: string + recipientAddress?: string } error?: string } @@ -79,6 +81,7 @@ export function registerPayCommand(program: Command): void { .option('--chipnet', 'Use chipnet (testnet) instead of mainnet') .option('--max-amount ', 'Maximum payment amount in satoshis (overrides server\'s max-amount)') .option('--change-address
', 'Change address for BCH transaction') + .option('--payer ', 'Payer identifier (defaults to wallet address index 0, or pass custom value like user ID for server-side lookups)') .option('--dry-run', 'Show what would happen without making payment') .option('--json', 'Output results as JSON') .action(async (url: string, opts: PayOptions) => { @@ -154,7 +157,7 @@ async function runPayHuman( console.log(`\n ${chalk.bold(method)} ${url}`) console.log(chalk.dim(` Network: ${chalk.cyan(network)}`)) - console.log(chalk.dim(` Payer: ${x402Payer.getPayerAddress()}`)) + console.log(chalk.dim(` Payer: ${opts.payer || x402Payer.getPayerAddress()}`)) if (Object.keys(headers).length > 0) { console.log(chalk.dim(` Headers: ${JSON.stringify(headers)}`)) } @@ -168,6 +171,9 @@ async function runPayHuman( ? 'https://chipnet.chaingraph.cash/tx/' : 'https://bchexplorer.info/tx/' console.log(chalk.dim(` Payment txid: ${explorer}${result.payment.txid}`)) + if (result.payment.recipientAddress) { + console.log(chalk.dim(` Recipient: ${result.payment.recipientAddress}`)) + } } console.log(chalk.green(`\n Response: ${result.status} ${result.statusText}`)) @@ -200,7 +206,7 @@ async function runPayDryRun( console.log(`\n ${chalk.bold(method)} ${url} ${chalk.dim('[DRY RUN]')}`) console.log(chalk.dim(` Network: ${chalk.cyan(network)}`)) - console.log(chalk.dim(` Payer: ${x402Payer.getPayerAddress()}`)) + console.log(chalk.dim(` Payer: ${opts.payer || x402Payer.getPayerAddress()}`)) console.log() try { @@ -246,7 +252,7 @@ async function runPayDryRun( amountSats, maxTimeoutMs: bchRequirements.maxTimeoutMs, resourceId: bchRequirements.resourceId, - payerAddress: x402Payer.getPayerAddress(), + payerAddress: opts.payer || x402Payer.getPayerAddress(), changeAddress, network: bchRequirements.network, } @@ -271,7 +277,7 @@ async function runPayDryRun( console.log(chalk.dim(` Resource: ${bchRequirements.resourceId}`)) console.log() console.log(chalk.dim(' Wallet:')) - console.log(chalk.dim(` Payer: ${x402Payer.getPayerAddress()}`)) + console.log(chalk.dim(` Payer: ${opts.payer || x402Payer.getPayerAddress()}`)) console.log(chalk.dim(` Change: ${changeAddress}`)) console.log() if (sufficient) { @@ -374,12 +380,17 @@ async function executePay( } } - bchRequirements.payer = x402Payer.getPayerAddress() + bchRequirements.payer = opts.payer || x402Payer.getPayerAddress() + let address: string const addressMatch = bchRequirements.paymentUrl.match(/:([a-z0-9]+):([a-z0-9]+)$/i) - let address = addressMatch - ? `${addressMatch[1]}:${addressMatch[2]}` - : bchRequirements.paymentUrl.split(':').pop()?.replace(/^\/\//, '') || '' + if (addressMatch) { + address = `${addressMatch[1]}:${addressMatch[2]}` + } else if (bchRequirements.paymentUrl.startsWith('bitcoincash:') || bchRequirements.paymentUrl.startsWith('bchtest:') || bchRequirements.paymentUrl.startsWith('bch:')) { + address = bchRequirements.paymentUrl.replace(/^bch:/, '') + } else { + address = `bitcoincash:${bchRequirements.paymentUrl}` + } const recipients = [ { @@ -427,7 +438,7 @@ async function executePay( currency: r.currency, })), }, - payer: x402Payer.getPayerAddress(), + payer: opts.payer || x402Payer.getPayerAddress(), } ) @@ -458,7 +469,7 @@ async function executePay( statusText: retryResponse.statusText, headers: retryResponseHeaders, data: retryResponseData, - payment: { required: true, txid: sendResult.txid }, + payment: { required: true, txid: sendResult.txid, recipientAddress: address }, } } diff --git a/x402-server/src/server.ts b/x402-server/src/server.ts index b772710..dde4c2e 100644 --- a/x402-server/src/server.ts +++ b/x402-server/src/server.ts @@ -173,6 +173,14 @@ async function verifyPayment( payloadObj = { payload: auth.payload } } + console.log(`[PAYMENT REQUEST]`, JSON.stringify({ + payer: payloadObj.payer, + payment: payloadObj.payment, + resource_id: payloadObj.resource_id, + resource_meta: payloadObj.resource_meta, + nonce: payloadObj.nonce, + }, null, 2)) + const payment = payloadObj?.payment || auth.payment if (!payment?.recipients?.length) { return { valid: false, error: 'Missing payment recipients' } @@ -232,7 +240,7 @@ async function handleRequest(req: http.IncomingMessage, res: http.ServerResponse // Otherwise assume it's a legacy address and wrap with proper prefix const addr = RECEIVE_ADDRESS.toLowerCase() if (addr.startsWith('bitcoincash:') || addr.startsWith('bchtest:') || addr.startsWith('bch:')) { - return `bch:${RECEIVE_ADDRESS}` + return RECEIVE_ADDRESS } // Legacy address - wrap with proper CashAddress prefix const prefix = bchNetwork.name === 'mainnet' ? 'bitcoincash' : 'bchtest' From 7238424480ae913ece511689a46e6616219a4626 Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 21:22:03 +0800 Subject: [PATCH 08/13] Add user approval requirement before BCH payment --- skills/paytaca/SKILL.md | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/skills/paytaca/SKILL.md b/skills/paytaca/SKILL.md index 108b82e..e259a01 100644 --- a/skills/paytaca/SKILL.md +++ b/skills/paytaca/SKILL.md @@ -70,16 +70,27 @@ When preparing to call an unfamiliar API that might require payment: 1. **First check**: `paytaca check --json` - If `paymentRequired: false` → proceed normally - - If `paymentRequired: true` and `acceptsBch: true` → use paytaca pay + - If `paymentRequired: true` and `acceptsBch: true` → inform user of cost and **seek explicit approval** before paying - If `paymentRequired: true` but `acceptsBch: false` → inform user 2. **When encountering HTTP 402**: - Parse the `PAYMENT-REQUIRED` headers - - Use `paytaca pay --json` to handle payment automatically + - **Seek explicit user approval** before spending any BCH + - Once approved, use `paytaca pay --json` to handle payment - The command handles: parse headers → build BCH tx → broadcast → retry with signature 3. **For known paid APIs**: - - Just use `paytaca pay ` directly - it handles 402 automatically + - **Always seek user approval first**, then use `paytaca pay ` - it handles 402 automatically + +## ⚠️ User Approval Required Before Any Payment + +**CRITICAL**: The agent MUST NOT execute `paytaca pay` without explicit user approval. Since `paytaca pay` spends real BCH from the user's wallet, always: + +1. Inform the user of the cost (e.g., "This API costs ~1000 sats") +2. Wait for explicit user confirmation (e.g., "yes", "go ahead", "pay") +3. Only then execute the payment + +Do NOT assume the user wants to pay - even if the cost seems small. ## AI Agent Workflow @@ -88,6 +99,9 @@ Task: Call nanogpt API Agent: paytaca check https://api.nanogpt.com/v1/complete --json → {"acceptsBch": true, "estimatedCostSats": "100"} +Agent: Informs user "This API costs 100 sats (0.00000100 BCH). Approve to proceed?" +User: "yes" + Agent: paytaca pay https://api.nanogpt.com/v1/complete --method POST --body '{"prompt":"hello"}' --json → Handles 402 → pays 100 sats → returns response with txid ``` From d9ca78da4430f40f7a6dafd3158c6c5777ebb8a3 Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 22:48:34 +0800 Subject: [PATCH 09/13] Update to x402-bch v2.2 specification - Replace HTTP header-based 402 responses with JSON body format - Use PAYMENT-SIGNATURE header with JSON-serialized PaymentPayload - Implement BCH network validation (bip122 CAIP-2 format) - Use libauth secp256k1 for message signing instead of bitcoinjs-message - Add network parameter to selectBchPaymentRequirements for validation - Update server to return v2.2 compliant PaymentRequired JSON - Update pay and check commands for new x402 format --- src/commands/check.ts | 63 ++++---- src/commands/pay.ts | 140 ++++++----------- src/types/x402.ts | 140 ++++++++--------- src/utils/x402.ts | 286 +++++++++++++++-------------------- src/wallet/x402.ts | 166 +++++--------------- x402-server/src/server.ts | 308 +++++++++++++++++++++----------------- 6 files changed, 457 insertions(+), 646 deletions(-) diff --git a/src/commands/check.ts b/src/commands/check.ts index 71f5a0c..1e828c6 100644 --- a/src/commands/check.ts +++ b/src/commands/check.ts @@ -1,7 +1,7 @@ /** * CLI command: check * - * Check if a URL accepts x402 BCH payments without making the actual request. + * Check if a URL accepts x402-bch v2.2 BCH payments without making the actual request. * Useful for AI to determine if payment will be required before committing. * * Usage: @@ -15,7 +15,7 @@ import chalk from 'chalk' import { loadWallet, loadMnemonic } from '../wallet/index.js' import { LibauthHDWallet } from '../wallet/keys.js' import { BchWallet } from '../wallet/bch.js' -import { parsePaymentRequired, selectBchPaymentRequirements } from '../utils/x402.js' +import { parsePaymentRequiredJson, selectBchPaymentRequirements } from '../utils/x402.js' import { BCH_DERIVATION_PATH } from '../utils/network.js' import { PaymentRequired } from '../types/x402.js' @@ -35,16 +35,15 @@ interface CheckResult { estimatedCostSats?: string costInBch?: string paymentUrl?: string - maxTimeoutMs?: number - resourceId?: string - acceptCurrencies?: string[] + maxTimeoutSeconds?: number + resourceUrl?: string error?: string } export function registerCheckCommand(program: Command): void { program .command('check') - .description('Check if a URL accepts x402 BCH payments') + .description('Check if a URL accepts x402-bch v2.2 BCH payments') .argument('', 'URL to check') .option('-X, --method ', 'HTTP method to test (default: GET)', 'GET') .option('-H, --header
', 'Add header to request (repeatable)') @@ -156,30 +155,25 @@ async function checkUrl( result.paymentRequired = response.status === 402 if (response.status === 402) { - const responseHeaders: Record = {} - response.headers.forEach((value, key) => { - responseHeaders[key] = value - }) - - const paymentHeaders: PaymentRequired = {} - for (const [key, value] of Object.entries(responseHeaders)) { - paymentHeaders[key.toLowerCase()] = value - } - - const requirements = parsePaymentRequired(paymentHeaders) - if (requirements) { - result.acceptsX402 = true - result.acceptCurrencies = requirements.acceptCurrencies - result.maxTimeoutMs = requirements.maxTimeoutMs - result.resourceId = requirements.resourceId - - const bchReqs = selectBchPaymentRequirements(requirements) - if (bchReqs) { - result.acceptsBch = true - result.paymentUrl = bchReqs.paymentUrl - result.estimatedCostSats = bchReqs.maxAmount.toString() - result.costInBch = (Number(bchReqs.maxAmount) / 1e8).toFixed(8) + try { + const responseBody = await response.json() + const paymentRequired = parsePaymentRequiredJson(responseBody) + + if (paymentRequired) { + result.acceptsX402 = paymentRequired.x402Version === 2 + result.resourceUrl = paymentRequired.resource?.url + + const bchReqs = selectBchPaymentRequirements(paymentRequired, isChipnet ? 'chipnet' : 'mainnet') + if (bchReqs) { + result.acceptsBch = true + result.paymentUrl = bchReqs.payTo + result.estimatedCostSats = bchReqs.amount + result.costInBch = (Number(bchReqs.amount) / 1e8).toFixed(8) + result.maxTimeoutSeconds = bchReqs.maxTimeoutSeconds + } } + } catch (e) { + result.error = 'Failed to parse 402 response body' } } @@ -191,20 +185,19 @@ function printCheckResult(result: CheckResult): void { console.log(chalk.yellow(' Payment Required')) if (result.acceptsX402) { - console.log(chalk.green(' ✓ Accepts x402 protocol')) + console.log(chalk.green(' ✓ Accepts x402-bch v2.2 protocol')) if (result.acceptsBch) { console.log(chalk.green(` ✓ Accepts BCH payment`)) - console.log(chalk.dim(` Amount: ${result.estimatedCostSats} sats (${result.costInBch} BCH)`)) + console.log(chalk.dim(` Amount: ${result.estimatedCostSats} sats (${result.costInBch} BCH)`)) console.log(chalk.dim(` Payment URL: ${result.paymentUrl}`)) - console.log(chalk.dim(` Timeout: ${result.maxTimeoutMs}ms`)) - console.log(chalk.dim(` Resource: ${result.resourceId}`)) + console.log(chalk.dim(` Timeout: ${result.maxTimeoutSeconds}s`)) + console.log(chalk.dim(` Resource: ${result.resourceUrl}`)) } else { console.log(chalk.red(' ✗ Does not accept BCH')) - console.log(chalk.dim(` Accepted currencies: ${result.acceptCurrencies?.join(', ')}`)) } } else { - console.log(chalk.red(' ✗ Unknown payment protocol (not x402)')) + console.log(chalk.red(' ✗ Unknown payment protocol (not x402-bch v2.2)')) } } else { console.log(chalk.green(' ✓ No payment required')) diff --git a/src/commands/pay.ts b/src/commands/pay.ts index 8b4af77..45c280d 100644 --- a/src/commands/pay.ts +++ b/src/commands/pay.ts @@ -1,15 +1,16 @@ /** * CLI command: pay * - * Makes an HTTP request to a URL, handling x402 payment requirements. + * Makes an HTTP request to a URL, handling x402-bch v2.2 payment requirements. * If the server returns 402 PAYMENT-REQUIRED, the wallet pays for the request. * * Flow: * 1. Make HTTP request to URL - * 2. If 402 response, parse PAYMENT-REQUIRED headers + * 2. If 402 response, parse PaymentRequired JSON body * 3. Build BCH transaction to pay the required amount * 4. Broadcast transaction - * 5. Retry original request with PAYMENT-SIGNATURE header + * 5. Build PaymentPayload per x402-bch v2.2 spec + * 6. Retry original request with PAYMENT-SIGNATURE header containing JSON PayloadPayload */ import { Command } from 'commander' @@ -18,9 +19,9 @@ import { loadWallet, loadMnemonic } from '../wallet/index.js' import { LibauthHDWallet } from '../wallet/keys.js' import { BchWallet } from '../wallet/bch.js' import { X402Payer } from '../wallet/x402.js' -import { parsePaymentRequired, selectBchPaymentRequirements } from '../utils/x402.js' +import { parsePaymentRequiredJson, selectBchPaymentRequirements, signMessageBCH } from '../utils/x402.js' import { BCH_DERIVATION_PATH } from '../utils/network.js' -import { PaymentRequired, BchPaymentRequirements } from '../types/x402.js' +import { PaymentRequired, PaymentRequirements, BCH_ASSET_ID } from '../types/x402.js' interface PayOptions { method?: string @@ -42,8 +43,8 @@ interface DryRunInfo { acceptsBch: boolean paymentUrl: string amountSats: string - maxTimeoutMs: number - resourceId: string + maxTimeoutSeconds: number + resourceUrl: string payerAddress: string changeAddress: string network: string @@ -73,7 +74,7 @@ interface JsonResult { export function registerPayCommand(program: Command): void { program .command('pay') - .description('Make a paid HTTP request with BCH payment via x402 protocol') + .description('Make a paid HTTP request with BCH payment via x402-bch v2.2 protocol') .argument('', 'URL to request') .option('-X, --method ', 'HTTP method (default: GET)', 'GET') .option('-H, --header
', 'Add header to request (repeatable)') @@ -216,51 +217,43 @@ async function runPayDryRun( body: ['POST', 'PUT', 'PATCH'].includes(method) ? body : undefined, }) - const responseHeaders: Record = {} - response.headers.forEach((value, key) => { - responseHeaders[key] = value - }) - if (response.status === 402) { dryRunInfo.willRequirePayment = true - const paymentHeaders: PaymentRequired = {} - for (const [key, value] of Object.entries(responseHeaders)) { - paymentHeaders[key.toLowerCase()] = value - } + const responseBody = await response.json() + const paymentRequired = parsePaymentRequiredJson(responseBody) - const requirements = parsePaymentRequired(paymentHeaders) - if (!requirements) { - console.log(chalk.red(' Error: Could not parse PAYMENT-REQUIRED headers')) + if (!paymentRequired) { + console.log(chalk.red(' Error: Could not parse PaymentRequired from 402 response body')) process.exit(1) } - const bchRequirements = selectBchPaymentRequirements(requirements) - if (!bchRequirements) { + const requirements = selectBchPaymentRequirements(paymentRequired, isChipnet ? 'chipnet' : 'mainnet') + if (!requirements) { console.log(chalk.red(' Error: Server does not accept BCH payment')) - console.log(chalk.dim(` Accepted currencies: ${requirements.acceptCurrencies.join(', ')}`)) + const acceptedSchemes = paymentRequired.accepts.map(a => `${a.scheme}:${a.network}`).join(', ') + console.log(chalk.dim(` Accepted schemes: ${acceptedSchemes}`)) process.exit(1) } const changeAddressSet = bchWallet.getAddressSetAt(0) const changeAddress = opts.changeAddress || changeAddressSet.change - const amountSats = bchRequirements.maxAmount.toString() dryRunInfo.payment = { acceptsBch: true, - paymentUrl: bchRequirements.paymentUrl, - amountSats, - maxTimeoutMs: bchRequirements.maxTimeoutMs, - resourceId: bchRequirements.resourceId, + paymentUrl: requirements.payTo, + amountSats: requirements.amount, + maxTimeoutSeconds: requirements.maxTimeoutSeconds, + resourceUrl: paymentRequired.resource.url, payerAddress: opts.payer || x402Payer.getPayerAddress(), changeAddress, - network: bchRequirements.network, + network: requirements.network, } try { const balanceResult = await bchWallet.getBalance() const available = (balanceResult.spendable * 1e8).toFixed(0) - const required = amountSats + const required = requirements.amount const sufficient = BigInt(available) >= BigInt(required) dryRunInfo.balanceCheck = { @@ -271,13 +264,13 @@ async function runPayDryRun( console.log(chalk.yellow(' 402 PAYMENT REQUIRED')) console.log(chalk.dim(' Payment details:')) - console.log(chalk.dim(` URL: ${bchRequirements.paymentUrl}`)) - console.log(chalk.dim(` Amount: ${amountSats} sats (${(Number(amountSats) / 1e8).toFixed(8)} BCH)`)) - console.log(chalk.dim(` Timeout: ${bchRequirements.maxTimeoutMs}ms`)) - console.log(chalk.dim(` Resource: ${bchRequirements.resourceId}`)) + console.log(chalk.dim(` PayTo: ${requirements.payTo}`)) + console.log(chalk.dim(` Amount: ${requirements.amount} sats (${(Number(requirements.amount) / 1e8).toFixed(8)} BCH)`)) + console.log(chalk.dim(` Timeout: ${requirements.maxTimeoutSeconds}s`)) + console.log(chalk.dim(` Resource: ${paymentRequired.resource.url}`)) console.log() console.log(chalk.dim(' Wallet:')) - console.log(chalk.dim(` Payer: ${opts.payer || x402Payer.getPayerAddress()}`)) + console.log(chalk.dim(` Payer: ${opts.payer || x402Payer.getPayerAddress()}`)) console.log(chalk.dim(` Change: ${changeAddress}`)) console.log() if (sufficient) { @@ -330,8 +323,6 @@ async function executePay( bchWallet: BchWallet, skipPayment: boolean ): Promise { - const isChipnet = Boolean(opts.chipnet) - const response = await fetch(url, { method, headers, @@ -352,23 +343,18 @@ async function executePay( } if (response.status === 402) { - const paymentHeaders: PaymentRequired = {} - for (const [key, value] of Object.entries(responseHeaders)) { - paymentHeaders[key.toLowerCase()] = value + const paymentRequired = parsePaymentRequiredJson(responseData) + if (!paymentRequired) { + return { success: false, status: 402, error: 'Could not parse PaymentRequired from 402 response body' } } - const requirements = parsePaymentRequired(paymentHeaders) + const requirements = selectBchPaymentRequirements(paymentRequired, opts.chipnet ? 'chipnet' : 'mainnet') if (!requirements) { - return { success: false, status: 402, error: 'Could not parse PAYMENT-REQUIRED headers' } - } - - const bchRequirements = selectBchPaymentRequirements(requirements) - if (!bchRequirements) { return { success: false, status: 402, error: 'Server does not accept BCH payment', - data: { acceptCurrencies: requirements.acceptCurrencies }, + data: { acceptedSchemes: paymentRequired.accepts.map(a => ({ scheme: a.scheme, network: a.network })) }, } } @@ -380,38 +366,16 @@ async function executePay( } } - bchRequirements.payer = opts.payer || x402Payer.getPayerAddress() - - let address: string - const addressMatch = bchRequirements.paymentUrl.match(/:([a-z0-9]+):([a-z0-9]+)$/i) - if (addressMatch) { - address = `${addressMatch[1]}:${addressMatch[2]}` - } else if (bchRequirements.paymentUrl.startsWith('bitcoincash:') || bchRequirements.paymentUrl.startsWith('bchtest:') || bchRequirements.paymentUrl.startsWith('bch:')) { - address = bchRequirements.paymentUrl.replace(/^bch:/, '') - } else { - address = `bitcoincash:${bchRequirements.paymentUrl}` - } + const payerAddress = opts.payer || x402Payer.getPayerAddress() - const recipients = [ - { - address, - amount: bchRequirements.maxAmount, - currency: 'BCH', - }, - ] + const address = requirements.payTo - if (!recipients[0].address) { - return { success: false, status: 402, error: 'Invalid payment URL in requirements' } - } + const amountBch = Number(requirements.amount) / 1e8 const changeAddressSet = bchWallet.getAddressSetAt(0) const changeAddress = opts.changeAddress || changeAddressSet.change - const sendResult = await bchWallet.sendBch( - Number(recipients[0].amount) / 1e8, - recipients[0].address, - changeAddress - ) + const sendResult = await bchWallet.sendBch(amountBch, address, changeAddress) if (!sendResult.success) { return { @@ -422,27 +386,17 @@ async function executePay( } } - const authHeader = await x402Payer.createAuthorization( - bchRequirements, - { - scheme: 'utxo', - network: bchRequirements.network, - max_timeout_ms: bchRequirements.maxTimeoutMs, - resource_id: bchRequirements.resourceId, - payment: { - scheme: 'utxo', - network: bchRequirements.network, - recipients: recipients.map(r => ({ - address: r.address, - amount: r.amount.toString(), - currency: r.currency, - })), - }, - payer: opts.payer || x402Payer.getPayerAddress(), - } + const txid = sendResult.txid! + + const paymentPayload = await x402Payer.createPaymentPayload( + requirements, + paymentRequired.resource.url, + txid, + 0, + requirements.amount ) - headers['Authorization'] = `x402 ${authHeader}` + headers['PAYMENT-SIGNATURE'] = JSON.stringify(paymentPayload) const retryResponse = await fetch(url, { method, @@ -469,7 +423,7 @@ async function executePay( statusText: retryResponse.statusText, headers: retryResponseHeaders, data: retryResponseData, - payment: { required: true, txid: sendResult.txid, recipientAddress: address }, + payment: { required: true, txid, recipientAddress: address }, } } diff --git a/src/types/x402.ts b/src/types/x402.ts index cc17d99..e764391 100644 --- a/src/types/x402.ts +++ b/src/types/x402.ts @@ -1,101 +1,64 @@ /** * x402 BCH type definitions * Implements x402-bch v2.2 specification - * https://github.com/x402-bch/x402-bch + * https://github.com/x402-bch/x402-bch/blob/master/specs/x402-bch-specification-v2.2.md */ -export interface PaymentRequired { - [key: string]: string | string[] | undefined - 'x-scheme'?: string | string[] - 'max-timeout-ms'?: string | string[] - 'payment-url'?: string | string[] - 'max-amount'?: string | string[] - '匪-async'?: string | string[] - 'resource-id'?: string | string[] - 'resource-meta'?: string | string[] - 'walled-garden'?: string | string[] - 'walled-garden-network'?: string | string[] - 'accept-currencies'?: string | string[] - 'cai'?: string | string[] - 'cai-lease-duration-ms'?: string | string[] - 'mime-type'?: string | string[] - 'www-authenticate'?: string | string[] +export const BCH_ASSET_ID = '0x0000000000000000000000000000000000000001' +export const BCH_MAINNET_NETWORK = 'bip122:000000000000000000651ef99cb9fcbe' +export const BCH_CHIPNET_NETWORK = 'bip122:000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' + +export interface ResourceInfo { + url: string + description?: string + mimeType?: string } export interface PaymentRequirements { scheme: string network: string - paymentUrl: string - maxTimeoutMs: number - maxAmount: bigint - resourceId: string - resourceMeta?: string - walledGarden?: boolean - walledGardenNetwork?: string - acceptCurrencies: string[] - mimeType?: string - wwwAuthenticate?: string + amount: string + asset: string + payTo: string + maxTimeoutSeconds: number + extra: object } -export interface BchPaymentRequirements extends PaymentRequirements { - scheme: 'utxo' - network: 'bip122:000000000000000000651ef99cb9fcbe' | 'bip122:000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' - payer: string - signature: string - attested?: boolean +export interface PaymentRequired { + x402Version: number + error?: string + resource: ResourceInfo + accepts: PaymentRequirements[] + extensions: object } -export interface PaymentPayload { - scheme: string - network: string - max_timeout_ms: number - resource_id: string - resource_meta?: string - attractor?: string - broadcaster?: string - payment: { - scheme: string - network: string - recipients: { - address: string - amount: string - currency: string - metadata?: Record - }[] - required_utxo_count?: number - } - nonce?: string - attestation?: string - payer?: string - payer_attestation?: string +export interface Authorization { + from: string + to: string + value: string + txid: string + vout: number | null + amount: string | null } -export interface Authorization { - scheme: string - network: string - resource_id: string - payload: string - payload_signature: string - nonce: string - attestation?: string - payer?: string +export interface Payload { + signature: string + authorization: Authorization } -export interface ResourceInfo { - url: string - method: string - headers: Record - body?: string +export interface PaymentPayload { + x402Version: number + resource?: ResourceInfo + accepted: PaymentRequirements + payload: Payload + extensions: object } -export interface SettlementResponse { - success: boolean - txid?: string - vout?: number - settle_address?: string - preimage?: string - signature?: string - error?: string +export interface VerifyResponse { + isValid: boolean + payer?: string + invalidReason?: string + remainingBalanceSat?: string } export interface X402PaymentResult { @@ -108,5 +71,24 @@ export interface X402PaymentResult { } error?: string txid?: string - settlement?: SettlementResponse + settlement?: { + success: boolean + txid?: string + error?: string + } } + +export type ErrorCode = + | 'missing_authorization' + | 'invalid_payload' + | 'invalid_scheme' + | 'invalid_network' + | 'invalid_receiver_address' + | 'invalid_exact_bch_payload_signature' + | 'insufficient_utxo_balance' + | 'utxo_not_found' + | 'no_utxo_found_for_address' + | 'unexpected_utxo_validation_error' + | 'unexpected_verify_error' + | 'unexpected_settle_error' + | 'invalid_x402_version' diff --git a/src/utils/x402.ts b/src/utils/x402.ts index c24b085..68b1267 100644 --- a/src/utils/x402.ts +++ b/src/utils/x402.ts @@ -1,21 +1,24 @@ /** * x402 utility functions for BCH payments - * Parses PAYMENT-REQUIRED headers, builds payment payloads, handles signatures + * Implements x402-bch v2.2 specification + * https://github.com/x402-bch/x402-bch/blob/master/specs/x402-bch-specification-v2.2.md */ -import { binToHex, hexToBin } from '@bitauth/libauth' +import crypto from 'crypto' import { PaymentRequired, PaymentRequirements, - BchPaymentRequirements, PaymentPayload, Authorization, ResourceInfo, - SettlementResponse, + VerifyResponse, + BCH_ASSET_ID, + BCH_MAINNET_NETWORK, + BCH_CHIPNET_NETWORK, } from '../types/x402.js' +import { secp256k1 } from '@bitauth/libauth' -export const BCH_MAINNET_NETWORK = 'bip122:000000000000000000651ef99cb9fcbe' -export const BCH_CHIPNET_NETWORK = 'bip122:000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f' +export { BCH_MAINNET_NETWORK, BCH_CHIPNET_NETWORK, BCH_ASSET_ID } export function isBchNetwork(network: string): boolean { return network === BCH_MAINNET_NETWORK || network === BCH_CHIPNET_NETWORK @@ -25,195 +28,140 @@ export function isChipnetNetwork(network: string): boolean { return network === BCH_CHIPNET_NETWORK } -function getHeaderValue(header: PaymentRequired[string]): string | undefined { - if (Array.isArray(header)) return header[0] - return header -} - -export function parsePaymentRequired(headers: PaymentRequired): PaymentRequirements | null { - const scheme = getHeaderValue(headers['x-scheme']) - if (!scheme) return null - - const acceptCurrenciesStr = getHeaderValue(headers['accept-currencies']) || '' - const acceptCurrencies = acceptCurrenciesStr.split(',').map(s => s.trim()).filter(Boolean) - - const maxTimeoutMsStr = getHeaderValue(headers['max-timeout-ms']) - const maxTimeoutMs = maxTimeoutMsStr ? parseInt(maxTimeoutMsStr, 10) : 60000 - - const maxAmountStr = getHeaderValue(headers['max-amount']) - const maxAmount = maxAmountStr ? BigInt(maxAmountStr) : BigInt(0) - - const resourceId = getHeaderValue(headers['resource-id']) || '' - const resourceMeta = getHeaderValue(headers['resource-meta']) - const paymentUrl = getHeaderValue(headers['payment-url']) || '' - const mimeType = getHeaderValue(headers['mime-type']) - const wwwAuthenticate = getHeaderValue(headers['www-authenticate']) +export function parsePaymentRequiredJson(body: any): PaymentRequired | null { + if (!body || typeof body !== 'object') return null + if (body.x402Version !== 2) return null - const walledGardenStr = getHeaderValue(headers['walled-garden']) - const walledGarden = walledGardenStr === 'true' - - const walledGardenNetwork = getHeaderValue(headers['walled-garden-network']) + const pr: PaymentRequired = { + x402Version: body.x402Version, + error: body.error, + resource: body.resource || { url: '' }, + accepts: [], + extensions: body.extensions || {}, + } - return { - scheme, - network: getHeaderValue(headers['x-network']) || '', - paymentUrl, - maxTimeoutMs, - maxAmount, - resourceId, - resourceMeta, - walledGarden, - walledGardenNetwork, - acceptCurrencies, - mimeType, - wwwAuthenticate, + if (Array.isArray(body.accepts)) { + for (const accept of body.accepts) { + if (accept.scheme && accept.network && accept.payTo) { + pr.accepts.push({ + scheme: accept.scheme, + network: accept.network, + amount: accept.amount, + asset: accept.asset || BCH_ASSET_ID, + payTo: accept.payTo, + maxTimeoutSeconds: accept.maxTimeoutSeconds || 60, + extra: accept.extra || {}, + }) + } + } } + + return pr } export function selectBchPaymentRequirements( - requirements: PaymentRequirements -): BchPaymentRequirements | null { - if (requirements.scheme !== 'utxo') return null - if (!isBchNetwork(requirements.network)) return null - - const acceptedCurrencies = ['BCH', 'bch', 'BCHn', 'bitcoincash'] - const hasAcceptedCurrency = requirements.acceptCurrencies.some(c => - acceptedCurrencies.includes(c) - ) - if (!hasAcceptedCurrency && requirements.acceptCurrencies.length > 0) return null - - return requirements as BchPaymentRequirements + requirements: PaymentRequired, + clientNetwork: 'mainnet' | 'chipnet' +): PaymentRequirements | null { + const clientNetworkId = clientNetwork === 'chipnet' ? BCH_CHIPNET_NETWORK : BCH_MAINNET_NETWORK + for (const accept of requirements.accepts) { + if (accept.scheme === 'utxo' && accept.network === clientNetworkId) { + return accept + } + } + return null } export function buildPaymentPayload( - requirements: BchPaymentRequirements, + accepted: PaymentRequirements, + resourceUrl: string, payer: string, - recipients: { address: string; amount: bigint; currency: string }[], - opts?: { - resourceMeta?: string - nonce?: string - attestation?: string - broadcaster?: string - } + txid: string, + vout: number | null, + amount: string | null ): PaymentPayload { + const resource: ResourceInfo = { + url: resourceUrl, + description: '', + mimeType: 'application/json', + } + return { - scheme: 'utxo', - network: requirements.network, - max_timeout_ms: requirements.maxTimeoutMs, - resource_id: requirements.resourceId, - resource_meta: opts?.resourceMeta || requirements.resourceMeta, - broadcaster: opts?.broadcaster, - payment: { - scheme: 'utxo', - network: requirements.network, - recipients: recipients.map(r => ({ - address: r.address, - amount: r.amount.toString(), - currency: r.currency, - })), + x402Version: 2, + resource, + accepted, + payload: { + signature: '', + authorization: { + from: payer, + to: accepted.payTo, + value: accepted.amount, + txid, + vout, + amount, + }, }, - nonce: opts?.nonce, - attestation: opts?.attestation, - payer, + extensions: {}, } } -export function encodePaymentSignature(auth: Authorization): string { - const data = JSON.stringify({ - scheme: auth.scheme, - network: auth.network, - resource_id: auth.resource_id, - payload: auth.payload, - payload_signature: auth.payload_signature, - nonce: auth.nonce, - attestation: auth.attestation, - }) - return Buffer.from(data).toString('base64') +export function buildAuthorization( + accepted: PaymentRequirements, + resourceUrl: string, + payer: string, + txid: string, + vout: number | null, + amount: string | null +): Authorization { + return { + from: payer, + to: accepted.payTo, + value: accepted.amount, + txid, + vout, + amount, + } } -export function decodePaymentSignature(encoded: string): Authorization | null { - try { - const data = Buffer.from(encoded, 'base64').toString('utf8') - const parsed = JSON.parse(data) - if (!parsed.scheme || !parsed.network || !parsed.payload || !parsed.payload_signature) { - return null - } - return parsed as Authorization - } catch { - return null - } +export function signMessageBCH( + message: string, + privateKeyHex: string, + compressed: boolean = true +): string { + const prefix = '\x18Bitcoin Signed Message:\n' + const messageBytes = Buffer.from(message, 'utf8') + const prefixBytes = Buffer.from(prefix, 'utf8') + const lengthByte = Buffer.from([messageBytes.length]) + const prefixedMessage = Buffer.concat([prefixBytes, lengthByte, messageBytes]) + const hash = crypto.createHash('sha256').update(crypto.createHash('sha256').update(prefixedMessage).digest()).digest() + const privateKey = Buffer.from(privateKeyHex, 'hex') + const signature = secp256k1.signMessageHashDER(hash, privateKey) + return Buffer.from(signature).toString('base64') } -export function parsePaymentResponse(data: any): SettlementResponse { - if (!data) return { success: false, error: 'No response data' } +export async function signAuthorization( + authorization: Authorization, + signMessage: (message: string) => Promise +): Promise { + const message = JSON.stringify(authorization) + return signMessage(message) +} - if (data.error) { - return { success: false, error: data.error } - } +export function parsePaymentResponse(data: any): VerifyResponse { + if (!data) return { isValid: false, invalidReason: 'no_response_data' } - if (data.txid) { + if (typeof data.isValid === 'boolean') { return { - success: true, - txid: data.txid, - vout: data.vout, - settle_address: data.settle_address, - preimage: data.preimage, - signature: data.signature, + isValid: data.isValid, + payer: data.payer, + invalidReason: data.invalidReason, + remainingBalanceSat: data.remainingBalanceSat, } } - return { success: false, error: 'Unknown settlement response format' } -} - -export function createResourceInfo( - url: string, - method: string, - headers: Record = {}, - body?: string -): ResourceInfo { - return { url, method, headers, body } -} - -export async function buildAuthorizationHeader( - payload: PaymentPayload, - payloadSignature: string, - nonce: string, - attestation?: string -): Promise { - const auth: Authorization = { - scheme: payload.scheme, - network: payload.network, - resource_id: payload.resource_id, - payload: payloadSignature, - payload_signature: payloadSignature, - nonce, - attestation, + if (data.error) { + return { isValid: false, invalidReason: data.error } } - return encodePaymentSignature(auth) -} - -export async function signMessageBCH( - message: string, - privateKeyHex: string, - compressed: boolean = true -): Promise { - const { sign } = await import('bitcoinjs-message') - const privateKey = Buffer.from(privateKeyHex, 'hex') - const signatureBuffer = sign(message, privateKey, compressed) - return signatureBuffer.toString('base64') -} -export function getDefaultSigner(hdWallet: any, index: number = 0): { - address: string - signMessage: (message: string) => Promise -} { - const addressSet = hdWallet.getAddressSetAt(index) - return { - address: addressSet.receiving, - signMessage: async (message: string) => { - const node = hdWallet.getNodeAt(`0/${index}`) - const privKeyHex = Buffer.from(node.privateKey).toString('hex') - return signMessageBCH(message, privKeyHex, true) - }, - } + return { isValid: false, invalidReason: 'unknown_response_format' } } diff --git a/src/wallet/x402.ts b/src/wallet/x402.ts index f3546df..76a8dfa 100644 --- a/src/wallet/x402.ts +++ b/src/wallet/x402.ts @@ -1,27 +1,25 @@ /** * x402 payment handler for BCH - * Integrates with LibauthHDWallet for signing x402 payment authorization + * Implements x402-bch v2.2 specification + * https://github.com/x402-bch/x402-bch/blob/master/specs/x402-bch-specification-v2.2.md */ import { LibauthHDWallet } from './keys.js' import { - parsePaymentRequired, + parsePaymentRequiredJson, selectBchPaymentRequirements, buildPaymentPayload, - encodePaymentSignature, + buildAuthorization, + signAuthorization, parsePaymentResponse, signMessageBCH, - BCH_MAINNET_NETWORK, - BCH_CHIPNET_NETWORK, - isChipnetNetwork, } from '../utils/x402.js' import { PaymentRequired, - BchPaymentRequirements, + PaymentRequirements, PaymentPayload, Authorization, X402PaymentResult, - SettlementResponse, } from '../types/x402.js' export interface X402Signer { @@ -29,13 +27,6 @@ export interface X402Signer { signMessage: (message: string) => Promise } -export interface X402PaymentRequest { - url: string - method: string - headers: Record - body?: string -} - export interface X402PayerConfig { hdWallet: LibauthHDWallet addressIndex?: number @@ -66,127 +57,40 @@ export class X402Payer { return this.signer.address } - async handlePaymentRequired( - response: { - status: number - headers: Record - data?: any - }, - paymentRequest: X402PaymentRequest - ): Promise<{ requirements: BchPaymentRequirements; headers: PaymentRequired } | null> { - const headers: PaymentRequired = {} - for (const [key, value] of Object.entries(response.headers)) { - headers[key.toLowerCase()] = value - } - - const requirements = parsePaymentRequired(headers) - if (!requirements) return null - - const bchRequirements = selectBchPaymentRequirements(requirements) - if (!bchRequirements) return null - - bchRequirements.payer = this.signer.address - - return { requirements: bchRequirements, headers } - } - - async createAuthorization( - requirements: BchPaymentRequirements, - payload: PaymentPayload - ): Promise { - const payloadJson = JSON.stringify(payload) - const payloadSignature = await this.signer.signMessage(payloadJson) - - const nonce = Date.now().toString(36) + Math.random().toString(36).substring(2, 10) - - const auth: Authorization = { - scheme: 'utxo', - network: requirements.network, - resource_id: requirements.resourceId, - payload: payloadJson, - payload_signature: payloadSignature, - nonce, - payer: this.signer.address, - } - - return encodePaymentSignature(auth) + async createPaymentPayload( + requirements: PaymentRequirements, + resourceUrl: string, + txid: string, + vout: number | null, + amount: string | null + ): Promise { + const payload = buildPaymentPayload( + requirements, + resourceUrl, + this.signer.address, + txid, + vout, + amount + ) + + const signature = await signAuthorization(payload.payload.authorization, this.signer.signMessage.bind(this.signer)) + payload.payload.signature = signature + + return payload } async makePaymentRequest( - requirements: BchPaymentRequirements, - recipients: { address: string; amount: bigint; currency: string }[], - paymentRequest: X402PaymentRequest - ): Promise<{ authHeader: string; paymentUrl: string }> { - const payload = buildPaymentPayload(requirements, this.signer.address, recipients) - const authHeader = await this.createAuthorization(requirements, payload) + requirements: PaymentRequirements, + resourceUrl: string, + txid: string, + vout: number | null, + amount: string | null + ): Promise<{ paymentPayload: PaymentPayload; paymentUrl: string }> { + const paymentPayload = await this.createPaymentPayload(requirements, resourceUrl, txid, vout, amount) return { - authHeader: `x402 ${authHeader}`, - paymentUrl: requirements.paymentUrl, - } - } - - async retryWithPayment( - requirements: BchPaymentRequirements, - paymentUrl: string, - recipients: { address: string; amount: bigint; currency: string }[], - originalRequest: X402PaymentRequest - ): Promise { - try { - const { authHeader } = await this.makePaymentRequest( - requirements, - recipients, - originalRequest - ) - - const response = await fetch(paymentUrl, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': authHeader, - }, - body: JSON.stringify({ - resource_id: requirements.resourceId, - payment: { - scheme: 'utxo', - network: requirements.network, - recipients: recipients.map(r => ({ - address: r.address, - amount: r.amount.toString(), - currency: r.currency, - })), - }, - payer: this.signer.address, - }), - }) - - const responseData = await response.json() - const settlement = parsePaymentResponse(responseData) - - if (settlement.success) { - return { - success: true, - txid: settlement.txid, - settlement, - response: { - status: 200, - statusText: 'OK', - headers: {}, - data: responseData, - }, - } - } else { - return { - success: false, - error: settlement.error || 'Payment failed', - settlement, - } - } - } catch (err: any) { - return { - success: false, - error: err.message || 'Payment request failed', - } + paymentPayload, + paymentUrl: requirements.payTo, } } } diff --git a/x402-server/src/server.ts b/x402-server/src/server.ts index dde4c2e..382c322 100644 --- a/x402-server/src/server.ts +++ b/x402-server/src/server.ts @@ -1,14 +1,15 @@ /** - * Reference x402 Server Implementation accepting BCH payments + * Reference x402-bch Server Implementation + * + * Conforms to x402-bch specification v2.2 + * https://github.com/x402-bch/x402-bch/blob/master/specs/x402-bch-specification-v2.2.md * - * This server demonstrates how to accept x402 payments using BCH. * Run with: npm start * * Endpoints: - * GET /api/quote - Returns a random quote (costs 100 sats) + * GET /api/quote - Returns a random quote (costs 1000 sats) * GET /api/weather - Returns fake weather data (costs 50 sats) * GET /api/status - Returns server status (costs 1 sat) - * GET /api/echo/ - Echoes back message (costs 10 sats) */ import http from 'http' @@ -29,18 +30,62 @@ const BCH_CHIPNET = { const bchNetwork = BCH_NETWORK === 'chipnet' ? BCH_CHIPNET : BCH_MAINNET const NETWORK_ID = `bip122:${bchNetwork.bip122}` +const ASSET_ID = '0x0000000000000000000000000000000000000001' +const MAX_TIMEOUT_SECONDS = 60 const RECEIVE_ADDRESS = process.env.RECEIVE_ADDRESS || null -interface PaymentHeaders { - 'x-scheme': string - 'x-network': string - 'max-timeout-ms': string - 'payment-url': string - 'max-amount': string - 'resource-id': string - 'accept-currencies': string - 'mime-type': string +interface PaymentRequirements { + scheme: string + network: string + amount: string + asset: string + payTo: string + maxTimeoutSeconds: number + extra: object +} + +interface ResourceInfo { + url: string + description?: string + mimeType?: string +} + +interface PaymentRequired { + x402Version: number + error?: string + resource: ResourceInfo + accepts: PaymentRequirements[] + extensions: object +} + +interface Authorization { + from: string + to: string + value: string + txid: string + vout: number | null + amount: string | null +} + +interface Payload { + signature: string + authorization: Authorization +} + +interface PaymentPayload { + x402Version: number + resource?: ResourceInfo + accepted: PaymentRequirements + payload: Payload + extensions: object +} + +interface VerifyResponse { + isValid: boolean + payer?: string + invalidReason?: string + remainingBalanceSat?: string } interface RouteConfig { @@ -95,125 +140,141 @@ const routes: Record = { }, } -function parseResourceId(path: string, query: URLSearchParams): string { - return `${path}${query.toString() ? '?' + query.toString() : ''}` +function getPayToAddress(): string { + if (!RECEIVE_ADDRESS) { + return bchNetwork.name === 'mainnet' + ? 'bitcoincash:placeholder' + : 'bchtest:placeholder' + } + const addr = RECEIVE_ADDRESS.toLowerCase() + if (addr.startsWith('bitcoincash:') || addr.startsWith('bchtest:') || addr.startsWith('bch:')) { + return RECEIVE_ADDRESS + } + const prefix = bchNetwork.name === 'mainnet' ? 'bitcoincash' : 'bchtest' + return `bch:${prefix}:${RECEIVE_ADDRESS}` } -function buildPaymentHeaders(resourceId: string, priceSats: number, paymentUrl: string, networkId: string): PaymentHeaders { +function buildPaymentRequired(resourceUrl: string, priceSats: number): PaymentRequired { return { - 'x-scheme': 'utxo', - 'x-network': networkId, - 'max-timeout-ms': '60000', - 'payment-url': paymentUrl, - 'max-amount': priceSats.toString(), - 'resource-id': resourceId, - 'accept-currencies': 'BCH,bch,BCHn,bitcoincash', - 'mime-type': 'application/json', + x402Version: 2, + error: 'PAYMENT-SIGNATURE header is required', + resource: { + url: resourceUrl, + description: routes[resourceUrl]?.description || '', + mimeType: 'application/json', + }, + accepts: [{ + scheme: 'utxo', + network: NETWORK_ID, + amount: priceSats.toString(), + asset: ASSET_ID, + payTo: getPayToAddress(), + maxTimeoutSeconds: MAX_TIMEOUT_SECONDS, + extra: {}, + }], + extensions: {}, } } -function send402Response( - res: http.ServerResponse, - headers: PaymentHeaders -): void { +function send402Response(res: http.ServerResponse, paymentRequired: PaymentRequired): void { res.writeHead(402, 'Payment Required', { 'Content-Type': 'application/json', - ...Object.fromEntries( - Object.entries(headers).map(([k, v]) => [k, String(v)]) - ) }) - res.end(JSON.stringify({ - error: 'Payment required', - message: 'This endpoint requires payment via x402 protocol', - scheme: headers['x-scheme'], - maxAmount: headers['max-amount'], - resourceId: headers['resource-id'], - })) + res.end(JSON.stringify(paymentRequired, null, 2)) } -async function verifyPayment( - authHeader: string, - resourceId: string, - maxAmount: bigint, - paymentUrl: string -): Promise<{ valid: boolean; error?: string; txid?: string }> { - if (!authHeader.startsWith('x402 ')) { - return { valid: false, error: 'Invalid authorization scheme' } +function sendErrorResponse(res: http.ServerResponse, statusCode: number, invalidReason: string, paymentRequired?: PaymentRequired): void { + res.writeHead(statusCode, 'Payment Failed', { + 'Content-Type': 'application/json', + }) + const body: any = { + isValid: false, + invalidReason, + } + if (paymentRequired) { + body.paymentRequired = paymentRequired } + res.end(JSON.stringify(body, null, 2)) +} - const encoded = authHeader.slice(5) - let auth: any +function parsePaymentPayload(headerValue: string): { payload?: PaymentPayload; error?: string } { + if (!headerValue) { + return { error: 'missing_authorization' } + } + let paymentPayload: PaymentPayload try { - auth = JSON.parse(Buffer.from(encoded, 'base64').toString('utf8')) + paymentPayload = JSON.parse(headerValue) } catch { - return { valid: false, error: 'Invalid base64 encoding' } + return { error: 'invalid_payload' } } - if (auth.scheme !== 'utxo') { - return { valid: false, error: `Unsupported scheme: ${auth.scheme}` } + if (paymentPayload.x402Version !== 2) { + return { error: 'invalid_x402_version' } } - if (auth.network !== NETWORK_ID) { - return { valid: false, error: `Wrong network: ${auth.network}` } - } + return { payload: paymentPayload } +} - if (auth.resource_id !== resourceId) { - return { valid: false, error: `Resource mismatch: ${auth.resource_id} !== ${resourceId}` } - } +function verifyPaymentPayload(payload: PaymentPayload, resourceUrl: string, maxAmountSats: bigint): { valid: boolean; invalidReason?: string; txid?: string; payer?: string } { + const accepted = payload.accepted - if (!auth.payload_signature) { - return { valid: false, error: 'Missing payload signature' } + if (accepted.scheme !== 'utxo') { + return { valid: false, invalidReason: 'invalid_scheme' } } - let payloadObj: any - try { - payloadObj = typeof auth.payload === 'string' ? JSON.parse(auth.payload) : auth.payload - } catch { - payloadObj = { payload: auth.payload } + if (accepted.network !== NETWORK_ID) { + return { valid: false, invalidReason: 'invalid_network' } } - console.log(`[PAYMENT REQUEST]`, JSON.stringify({ - payer: payloadObj.payer, - payment: payloadObj.payment, - resource_id: payloadObj.resource_id, - resource_meta: payloadObj.resource_meta, - nonce: payloadObj.nonce, - }, null, 2)) - - const payment = payloadObj?.payment || auth.payment - if (!payment?.recipients?.length) { - return { valid: false, error: 'Missing payment recipients' } + if (accepted.payTo !== getPayToAddress()) { + return { valid: false, invalidReason: 'invalid_receiver_address' } } - const recipient = payment.recipients[0] - const amountSats = BigInt(recipient.amount) + const acceptedAmount = BigInt(accepted.amount) + if (acceptedAmount > maxAmountSats) { + return { valid: false, invalidReason: 'insufficient_utxo_balance' } + } - if (amountSats > maxAmount) { - return { valid: false, error: `Amount exceeds maximum: ${amountSats} > ${maxAmount}` } + if (accepted.asset !== ASSET_ID) { + return { valid: false, invalidReason: 'invalid_payload' } } - const validCurrency = ['BCH', 'bch', 'BCHn', 'bitcoincash'].includes(recipient.currency) - if (!validCurrency) { - return { valid: false, error: `Unsupported currency: ${recipient.currency}` } + const auth = payload.payload?.authorization + if (!auth) { + return { valid: false, invalidReason: 'missing_authorization' } } - if (!auth.nonce) { - return { valid: false, error: 'Missing nonce' } + const valueSats = BigInt(auth.value) + if (valueSats > maxAmountSats) { + return { valid: false, invalidReason: 'insufficient_utxo_balance' } } - if (!auth.payload) { - return { valid: false, error: 'Missing payload' } + if (auth.to !== accepted.payTo) { + return { valid: false, invalidReason: 'invalid_receiver_address' } } - const txid = payloadObj.txid || crypto.randomUUID() - const vout = payloadObj.vout || 0 - const settleAddress = recipient.address + const txid = auth.txid === '*' ? crypto.randomUUID() : auth.txid + const payer = auth.from + + console.log(`[PAYMENT REQUEST]`, JSON.stringify({ + payer, + txid, + vout: auth.vout, + amount: auth.amount, + value: auth.value, + resource: resourceUrl, + }, null, 2)) + + if (!payload.payload?.signature) { + return { valid: false, invalidReason: 'invalid_exact_bch_payload_signature' } + } return { valid: true, txid, - error: undefined, + payer, + invalidReason: undefined, } } @@ -221,6 +282,7 @@ async function handleRequest(req: http.IncomingMessage, res: http.ServerResponse const url = new URL(req.url || '/', `http://localhost:${PORT}`) const path = url.pathname const query = url.searchParams + const resourceUrl = `${path}${query.toString() ? '?' + query.toString() : ''}` const route = routes[path] if (!route) { @@ -229,23 +291,8 @@ async function handleRequest(req: http.IncomingMessage, res: http.ServerResponse return } - const resourceId = parseResourceId(path, query) const priceSats = route.price - const paymentUrl = (() => { - if (!RECEIVE_ADDRESS) { - return `bch:${bchNetwork.name === 'mainnet' ? 'bitcoincash:' : 'bchtest:'}placeholder` - } - // Return address with bch: prefix, ensuring proper CashAddress format - // If address already has bitcoincash: or bchtest: prefix, use as-is after bch: - // Otherwise assume it's a legacy address and wrap with proper prefix - const addr = RECEIVE_ADDRESS.toLowerCase() - if (addr.startsWith('bitcoincash:') || addr.startsWith('bchtest:') || addr.startsWith('bch:')) { - return RECEIVE_ADDRESS - } - // Legacy address - wrap with proper CashAddress prefix - const prefix = bchNetwork.name === 'mainnet' ? 'bitcoincash' : 'bchtest' - return `bch:${prefix}:${RECEIVE_ADDRESS}` - })() + const paymentRequired = buildPaymentRequired(`http://localhost:${PORT}${resourceUrl}`, priceSats) if (req.method !== 'GET') { res.writeHead(405, { 'Content-Type': 'application/json' }) @@ -253,29 +300,22 @@ async function handleRequest(req: http.IncomingMessage, res: http.ServerResponse return } - const authHeader = req.headers.authorization - const hasPayment = authHeader?.startsWith('x402 ') + const paymentSignature = req.headers['payment-signature'] as string | undefined + const parsed = parsePaymentPayload(paymentSignature || '') - if (!hasPayment) { - const headers = buildPaymentHeaders(resourceId, priceSats, paymentUrl, NETWORK_ID) + if (!parsed.payload) { console.log(`[PAYMENT REQUIRED] ${path} - ${priceSats} sats - ${req.socket.remoteAddress}`) - send402Response(res, headers) + send402Response(res, paymentRequired) return } console.log(`[VERIFYING] ${path} from ${req.socket.remoteAddress}`) - const verifyResult = await verifyPayment( - authHeader!, - resourceId, - BigInt(priceSats), - paymentUrl - ) + const verifyResult = verifyPaymentPayload(parsed.payload, resourceUrl, BigInt(priceSats)) if (!verifyResult.valid) { - console.log(`[VERIFICATION FAILED] ${path} - ${verifyResult.error}`) - res.writeHead(402, 'Payment Verification Failed', { 'Content-Type': 'application/json' }) - res.end(JSON.stringify({ error: verifyResult.error })) + console.log(`[VERIFICATION FAILED] ${path} - ${verifyResult.invalidReason}`) + sendErrorResponse(res, 402, verifyResult.invalidReason!, paymentRequired) return } @@ -283,22 +323,16 @@ async function handleRequest(req: http.IncomingMessage, res: http.ServerResponse try { const data = await route.handler(query) - const payload = Buffer.from(JSON.stringify({ - txid: verifyResult.txid, - vout: 0, - settle_address: RECEIVE_ADDRESS || 'unknown', - resource_id: resourceId, - })).toString('base64') + + const verifyResponse: VerifyResponse = { + isValid: true, + payer: verifyResult.payer, + remainingBalanceSat: '0', + } res.writeHead(200, { 'Content-Type': route.mimeType, - 'PAYMENT-RESPONSE': Buffer.from(JSON.stringify({ - success: true, - txid: verifyResult.txid, - vout: 0, - settle_address: RECEIVE_ADDRESS || 'unknown', - preimage: payload, - })).toString('base64'), + 'PAYMENT-RESPONSE': Buffer.from(JSON.stringify(verifyResponse)).toString('base64'), }) res.end(JSON.stringify(data)) } catch (err: any) { @@ -321,7 +355,7 @@ server.listen(PORT, () => { console.log(` ╔═══════════════════════════════════════════════════════════╗ ║ ║ -║ x402 BCH Reference Server ║ +║ x402-bch Reference Server (v2.2 Compatible) ║ ║ Network: ${bchNetwork.name.padEnd(47)}║ ║ Port: ${PORT.toString().padEnd(47)}║ ║ ║ @@ -334,10 +368,6 @@ server.listen(PORT, () => { ║ ║ ╠═══════════════════════════════════════════════════════════╣ ║ ║ -║ To test with paytaca-cli: ║ -║ paytaca check http://localhost:${PORT}/api/quote ║ -║ paytaca pay http://localhost:${PORT}/api/quote ║ -║ ║ ║ Set RECEIVE_ADDRESS env var to your BCH address ║ ║ Set BCH_NETWORK=chipnet for testnet ║ ║ ║ From e0a4382a9c7adb3b23cfff7639234b828b9913ae Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 23:06:33 +0800 Subject: [PATCH 10/13] Update README with x402 payments and AI agent integration docs --- README.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/README.md b/README.md index 5b49590..68835df 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,37 @@ paytaca token send
--token # Send fungible tokens paytaca token send-nft
--token --commitment # Send an NFT ``` +### x402 Payments + +The x402 protocol enables HTTP payments via BCH. Some APIs (like nanogpt) require payment to access. + +```bash +paytaca check # Check if URL requires payment, shows estimated cost +paytaca pay # Make a paid HTTP request (handles 402 automatically) +paytaca pay --json # JSON output (recommended for AI agents) +paytaca pay --dry-run # Preview payment without executing +paytaca pay --method POST # POST request with body +paytaca pay --body '{"prompt":"hello"}' +``` + +**Example workflow:** +```bash +paytaca check https://api.nanogpt.com/v1/complete --json +# → {"paymentRequired": true, "estimatedCostSats": "100"} + +paytaca pay https://api.nanogpt.com/v1/complete --method POST --body '{"prompt":"hello"}' +# → Handles 402 → pays → returns response +``` + +### AI Agent Integration + +```bash +paytaca opencode # Install Paytaca x402 skill for OpenCode AI agents +paytaca claude # Install Paytaca x402 skill for Claude Code agents +``` + +This enables AI agents to autonomously handle HTTP 402 payment responses when calling x402-enabled APIs. + ## Network All commands default to **mainnet**. Pass `--chipnet` for testnet: @@ -130,15 +161,23 @@ src/ history.ts transaction history (BCH and CashTokens) address.ts HD address derivation (standard and z-prefix) token.ts CashToken commands (list, info, send, send-nft) + pay.ts x402 BCH payment handler for HTTP requests + check.ts Check if URL requires x402 payment + opencode.ts Install x402 skill for OpenCode AI agents + claude.ts Install x402 skill for Claude Code AI agents wallet/ index.ts Wallet class, mnemonic gen/import/load bch.ts BchWallet (balance, send, history, CashTokens) keys.ts LibauthHDWallet (HD key derivation, token addresses) + x402.ts X402Payer (BCH payment signing and verification) storage/ keychain.ts OS keychain wrapper (@napi-rs/keyring) utils/ crypto.ts pubkey -> CashAddress pipeline network.ts Watchtower URLs, derivation paths + x402.ts x402 header parsing, payment requirement selection + types/ + x402.ts x402 payment types (PaymentRequired, PaymentPayload, etc.) ``` ## Key Dependencies @@ -161,6 +200,22 @@ npm run build # One-time build npm run clean # Remove dist/ ``` +## x402 Server + +A reference x402 server implementation is included for testing: + +```bash +cd x402-server +npm install +npm run dev # Start dev server on port 3001 +``` + +The server implements the x402-bch v2.2 specification and provides: +- `GET /api/quote` — Returns a quote (requires payment) +- `POST /api/generate` — Text generation endpoint (requires payment) + +Useful for testing the `paytaca pay` workflow locally. + ## License Copyright Paytaca Inc. 2021. All rights reserved. See [LICENSE](LICENSE) for details. From fd28ba4f9aefdfcdb98f42a08ba2cf60fd459fd2 Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 23:14:43 +0800 Subject: [PATCH 11/13] Add opencode GitHub workflow --- .github/workflows/opencode.yml | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 .github/workflows/opencode.yml diff --git a/.github/workflows/opencode.yml b/.github/workflows/opencode.yml new file mode 100644 index 0000000..dddedc3 --- /dev/null +++ b/.github/workflows/opencode.yml @@ -0,0 +1,45 @@ +name: opencode + +on: + pull_request: + types: [opened, synchronize, ready_for_review] + issue_comment: + types: [created] + pull_request_review_comment: + types: [created] + +jobs: + opencode: + if: | + github.event_name == 'pull_request' || + contains(github.event.comment.body, ' /oc') || + startsWith(github.event.comment.body, '/oc') || + contains(github.event.comment.body, ' /opencode') || + startsWith(github.event.comment.body, '/opencode') + runs-on: ubuntu-latest + permissions: + id-token: write + contents: read + pull-requests: write + issues: read + steps: + - name: Checkout repository + uses: actions/checkout@v6 + with: + persist-credentials: false + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + cache: 'npm' + + - name: Install dependencies + run: npm ci + + - name: Run opencode + uses: anomalyco/opencode/github@latest + env: + OPENCODE_API_KEY: ${{ secrets.OPENCODE_API_KEY }} + with: + model: opencode/kimi-k2.5 From d9db1c32fc7e78a923273f57e4a0cacd4874b5c2 Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 23:21:09 +0800 Subject: [PATCH 12/13] Add user confirmation prompt and refactor skill management - Add payment confirmation prompt in pay command before executing BCH transactions - Remove unused imports (signMessageBCH from pay.ts, parsePaymentResponse from wallet/x402.ts) - Extract shared skill utilities to src/utils/skill.ts - Move Claude command to its own file for extensibility - Add SUPPORTED_ASSISTANTS array for easy AI assistant integration --- src/commands/claude.ts | 24 ++++++++ src/commands/opencode.ts | 125 ++------------------------------------- src/commands/pay.ts | 34 ++++++++++- src/index.ts | 2 + src/utils/skill.ts | 118 ++++++++++++++++++++++++++++++++++++ src/wallet/x402.ts | 1 - 6 files changed, 182 insertions(+), 122 deletions(-) create mode 100644 src/commands/claude.ts create mode 100644 src/utils/skill.ts diff --git a/src/commands/claude.ts b/src/commands/claude.ts new file mode 100644 index 0000000..c7e1471 --- /dev/null +++ b/src/commands/claude.ts @@ -0,0 +1,24 @@ +/** + * CLI command: claude + * + * Set up paytaca x402 payment handling for Claude Code AI assistant. + * Installs the paytaca skill so Claude Code knows how to handle 402 responses. + */ + +import { Command } from 'commander' +import os from 'os' +import path from 'path' +import { SUPPORTED_ASSISTANTS, handleSkillAction } from '../utils/skill.js' + +export function registerClaudeCommand(program: Command): void { + const assistant = SUPPORTED_ASSISTANTS.find(a => a.name === 'Claude Code') + if (!assistant) return + + program + .command('claude') + .description('Set up paytaca x402 payments for Claude Code AI assistant') + .argument('[action]', 'Action: install, uninstall, status', 'status') + .action(async (action: string) => { + handleSkillAction(assistant, action) + }) +} diff --git a/src/commands/opencode.ts b/src/commands/opencode.ts index d769334..c6a13a7 100644 --- a/src/commands/opencode.ts +++ b/src/commands/opencode.ts @@ -6,132 +6,17 @@ */ import { Command } from 'commander' -import chalk from 'chalk' -import fs from 'fs' -import path from 'path' -import os from 'os' -import { fileURLToPath } from 'url' - -const OPENCODE_SKILLS_DIR = path.join(os.homedir(), '.config', 'opencode', 'skills') -const CLAUDE_SKILLS_DIR = path.join(os.homedir(), '.claude', 'skills') +import { SUPPORTED_ASSISTANTS, handleSkillAction } from '../utils/skill.js' export function registerOpencodeCommand(program: Command): void { + const assistant = SUPPORTED_ASSISTANTS.find(a => a.name === 'opencode') + if (!assistant) return + program .command('opencode') .description('Set up paytaca x402 payments for opencode AI assistant') .argument('[action]', 'Action: install, uninstall, status', 'status') .action(async (action: string) => { - switch (action) { - case 'install': - installSkill(OPENCODE_SKILLS_DIR, 'opencode') - break - case 'uninstall': - uninstallSkill(OPENCODE_SKILLS_DIR) - break - case 'status': - checkStatus(OPENCODE_SKILLS_DIR, 'opencode') - break - default: - console.log(chalk.yellow(`Unknown action: ${action}`)) - console.log('Use: install, uninstall, or status') - } - }) - - program - .command('claude') - .description('Set up paytaca x402 payments for Claude Code AI assistant') - .argument('[action]', 'Action: install, uninstall, status', 'status') - .action(async (action: string) => { - switch (action) { - case 'install': - installSkill(CLAUDE_SKILLS_DIR, 'Claude Code') - break - case 'uninstall': - uninstallSkill(CLAUDE_SKILLS_DIR) - break - case 'status': - checkStatus(CLAUDE_SKILLS_DIR, 'Claude Code') - break - default: - console.log(chalk.yellow(`Unknown action: ${action}`)) - console.log('Use: install, uninstall, or status') - } + handleSkillAction(assistant, action) }) } - -function getSkillSourcePath(): string { - try { - const modulePath = require.resolve('paytaca-cli') - const packageDir = path.dirname(modulePath) - return path.join(packageDir, 'skills', 'paytaca', 'SKILL.md') - } catch { - const currentFilePath = fileURLToPath(import.meta.url) - const srcPath = path.dirname(currentFilePath) - return path.join(srcPath, '..', '..', 'skills', 'paytaca', 'SKILL.md') - } -} - -function getSkillDestPath(skillsDir: string): string { - return path.join(skillsDir, 'paytaca', 'SKILL.md') -} - -function installSkill(skillsDir: string, assistantName: string): void { - try { - const sourcePath = getSkillSourcePath() - const destDir = path.join(skillsDir, 'paytaca') - const destPath = getSkillDestPath(skillsDir) - - if (!fs.existsSync(sourcePath)) { - console.log(chalk.red('Skill source file not found. Is paytaca-cli properly installed?')) - process.exit(1) - } - - fs.mkdirSync(destDir, { recursive: true }) - - const content = fs.readFileSync(sourcePath, 'utf8') - fs.writeFileSync(destPath, content) - - console.log(chalk.green(`\n✓ Skill installed successfully for ${assistantName}!\n`)) - console.log(chalk.bold('What this does:')) - console.log(' When the AI assistant encounters HTTP 402 or calls x402-enabled APIs,') - console.log(' it will automatically use paytaca to handle payments.\n') - console.log(chalk.dim('Location: ') + destPath) - console.log(chalk.dim('Source: ') + sourcePath) - console.log() - console.log(`Restart ${assistantName} to load the new skill.\n`) - } catch (err: any) { - console.log(chalk.red(`\nFailed to install skill: ${err.message}\n`)) - process.exit(1) - } -} - -function uninstallSkill(skillsDir: string): void { - try { - const destDir = path.join(skillsDir, 'paytaca') - const destPath = getSkillDestPath(skillsDir) - - if (!fs.existsSync(destPath)) { - console.log(chalk.yellow('\nSkill is not installed.\n')) - process.exit(0) - } - - fs.rmSync(destDir, { recursive: true }) - console.log(chalk.green('\n✓ Skill uninstalled successfully!\n')) - } catch (err: any) { - console.log(chalk.red(`\nFailed to uninstall skill: ${err.message}\n`)) - process.exit(1) - } -} - -function checkStatus(skillsDir: string, assistantName: string): void { - const destPath = getSkillDestPath(skillsDir) - - if (fs.existsSync(destPath)) { - console.log(chalk.green('\n✓ Paytaca skill is installed\n')) - console.log(chalk.dim('Location: ') + destPath) - } else { - console.log(chalk.yellow(`\n○ Paytaca skill is not installed for ${assistantName}\n`)) - console.log(`Run: paytaca ${assistantName.toLowerCase()} install`) - console.log() - } -} diff --git a/src/commands/pay.ts b/src/commands/pay.ts index 45c280d..40a457c 100644 --- a/src/commands/pay.ts +++ b/src/commands/pay.ts @@ -15,11 +15,12 @@ import { Command } from 'commander' import chalk from 'chalk' +import readline from 'readline' import { loadWallet, loadMnemonic } from '../wallet/index.js' import { LibauthHDWallet } from '../wallet/keys.js' import { BchWallet } from '../wallet/bch.js' import { X402Payer } from '../wallet/x402.js' -import { parsePaymentRequiredJson, selectBchPaymentRequirements, signMessageBCH } from '../utils/x402.js' +import { parsePaymentRequiredJson, selectBchPaymentRequirements } from '../utils/x402.js' import { BCH_DERIVATION_PATH } from '../utils/network.js' import { PaymentRequired, PaymentRequirements, BCH_ASSET_ID } from '../types/x402.js' @@ -375,6 +376,22 @@ async function executePay( const changeAddressSet = bchWallet.getAddressSetAt(0) const changeAddress = opts.changeAddress || changeAddressSet.change + console.log(chalk.yellow('\n ⚠ Payment Required')) + console.log(chalk.dim(` Amount: ${amountBch} BCH (${requirements.amount} sats)`)) + console.log(chalk.dim(` To: ${address}`)) + console.log(chalk.dim(` Change: ${changeAddress}`)) + console.log(chalk.dim(` Payer: ${payerAddress}`)) + + const confirmed = await promptConfirmation('Confirm payment?') + if (!confirmed) { + return { + success: false, + status: 402, + payment: { required: true, error: 'Payment rejected by user' }, + error: 'Payment rejected by user', + } + } + const sendResult = await bchWallet.sendBch(amountBch, address, changeAddress) if (!sendResult.success) { @@ -448,3 +465,18 @@ function formatResponse(data: any): string { } return String(data) } + +async function promptConfirmation(message: string): Promise { + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + }) + + return new Promise((resolve) => { + rl.question(chalk.bold(`\n ${message} (y/N): `), (answer) => { + rl.close() + const confirmed = answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes' + resolve(confirmed) + }) + }) +} diff --git a/src/index.ts b/src/index.ts index 1b295ef..214d548 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ import { registerTokenCommands } from './commands/token.js' import { registerPayCommand } from './commands/pay.js' import { registerCheckCommand } from './commands/check.js' import { registerOpencodeCommand } from './commands/opencode.js' +import { registerClaudeCommand } from './commands/claude.js' const program = new Command() @@ -35,5 +36,6 @@ registerTokenCommands(program) registerPayCommand(program) registerCheckCommand(program) registerOpencodeCommand(program) +registerClaudeCommand(program) program.parse() diff --git a/src/utils/skill.ts b/src/utils/skill.ts new file mode 100644 index 0000000..fa2735e --- /dev/null +++ b/src/utils/skill.ts @@ -0,0 +1,118 @@ +/** + * Skill management utilities for AI assistant integration. + * Provides generic functions for installing/uninstalling/checking status + * of paytaca skill for various AI assistants. + */ + +import chalk from 'chalk' +import fs from 'fs' +import path from 'path' +import os from 'os' +import { fileURLToPath } from 'url' + +export interface AssistantConfig { + name: string + skillsDir: string +} + +export const SUPPORTED_ASSISTANTS: AssistantConfig[] = [ + { name: 'opencode', skillsDir: path.join(os.homedir(), '.config', 'opencode', 'skills') }, + { name: 'Claude Code', skillsDir: path.join(os.homedir(), '.claude', 'skills') }, +] + +export function getSkillSourcePath(): string { + try { + const modulePath = require.resolve('paytaca-cli') + const packageDir = path.dirname(modulePath) + return path.join(packageDir, 'skills', 'paytaca', 'SKILL.md') + } catch { + const currentFilePath = fileURLToPath(import.meta.url) + const srcPath = path.dirname(currentFilePath) + return path.join(srcPath, '..', '..', 'skills', 'paytaca', 'SKILL.md') + } +} + +export function getSkillDestPath(skillsDir: string): string { + return path.join(skillsDir, 'paytaca', 'SKILL.md') +} + +export function installSkill(skillsDir: string, assistantName: string): void { + try { + const sourcePath = getSkillSourcePath() + const destDir = path.join(skillsDir, 'paytaca') + const destPath = getSkillDestPath(skillsDir) + + if (!fs.existsSync(sourcePath)) { + console.log(chalk.red('Skill source file not found. Is paytaca-cli properly installed?')) + process.exit(1) + } + + fs.mkdirSync(destDir, { recursive: true }) + + const content = fs.readFileSync(sourcePath, 'utf8') + fs.writeFileSync(destPath, content) + + console.log(chalk.green(`\n✓ Skill installed successfully for ${assistantName}!\n`)) + console.log(chalk.bold('What this does:')) + console.log(' When the AI assistant encounters HTTP 402 or calls x402-enabled APIs,') + console.log(' it will automatically use paytaca to handle payments.\n') + console.log(chalk.dim('Location: ') + destPath) + console.log(chalk.dim('Source: ') + sourcePath) + console.log() + console.log(`Restart ${assistantName} to load the new skill.\n`) + } catch (err: any) { + console.log(chalk.red(`\nFailed to install skill: ${err.message}\n`)) + process.exit(1) + } +} + +export function uninstallSkill(skillsDir: string, assistantName: string): void { + try { + const destDir = path.join(skillsDir, 'paytaca') + const destPath = getSkillDestPath(skillsDir) + + if (!fs.existsSync(destPath)) { + console.log(chalk.yellow('\nSkill is not installed.\n')) + process.exit(0) + } + + fs.rmSync(destDir, { recursive: true }) + console.log(chalk.green(`\n✓ Skill uninstalled for ${assistantName}!\n`)) + } catch (err: any) { + console.log(chalk.red(`\nFailed to uninstall skill: ${err.message}\n`)) + process.exit(1) + } +} + +export function checkStatus(skillsDir: string, assistantName: string): void { + const destPath = getSkillDestPath(skillsDir) + + if (fs.existsSync(destPath)) { + console.log(chalk.green('\n✓ Paytaca skill is installed\n')) + console.log(chalk.dim('Location: ') + destPath) + } else { + console.log(chalk.yellow(`\n○ Paytaca skill is not installed for ${assistantName}\n`)) + console.log(`Run: paytaca ${assistantName.toLowerCase()} install`) + console.log() + } +} + +export function handleSkillAction( + assistant: AssistantConfig, + action: string +): void { + switch (action) { + case 'install': + installSkill(assistant.skillsDir, assistant.name) + break + case 'uninstall': + uninstallSkill(assistant.skillsDir, assistant.name) + break + case 'status': + checkStatus(assistant.skillsDir, assistant.name) + break + default: + console.log(chalk.yellow(`Unknown action: ${action}`)) + console.log('Use: install, uninstall, or status') + } +} diff --git a/src/wallet/x402.ts b/src/wallet/x402.ts index 76a8dfa..4ca93b2 100644 --- a/src/wallet/x402.ts +++ b/src/wallet/x402.ts @@ -11,7 +11,6 @@ import { buildPaymentPayload, buildAuthorization, signAuthorization, - parsePaymentResponse, signMessageBCH, } from '../utils/x402.js' import { From 809271a1d631703aebe869c40932911a9629ac55 Mon Sep 17 00:00:00 2001 From: joemarct Date: Sun, 29 Mar 2026 23:37:04 +0800 Subject: [PATCH 13/13] Add Vitest test framework and x402 parsing tests - Add Vitest and @vitest/ui as dev dependencies - Add test and test:watch scripts to package.json - Add vitest.config.ts configuration - Add x402.test.ts with 20 tests covering: - PaymentRequired JSON parsing - BCH payment requirement selection - PaymentPayload and Authorization building - PaymentResponse parsing - Network helper functions --- package-lock.json | 1405 ++++++++++++++++++++++++++++++++++++++-- package.json | 6 +- src/utils/x402.test.ts | 275 ++++++++ vitest.config.ts | 8 + 4 files changed, 1650 insertions(+), 44 deletions(-) create mode 100644 src/utils/x402.test.ts create mode 100644 vitest.config.ts diff --git a/package-lock.json b/package-lock.json index 915957a..865cfaf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,9 @@ "devDependencies": { "@types/node": "^22.0.0", "@types/qrcode-terminal": "^0.12.2", - "typescript": "^5.9.3" + "@vitest/ui": "^4.1.2", + "typescript": "^5.9.3", + "vitest": "^4.1.2" }, "engines": { "node": ">=20.0.0" @@ -52,6 +54,50 @@ "node": ">=10.15.1" } }, + "node_modules/@emnapi/core": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/@emnapi/core/-/core-1.9.1.tgz", + "integrity": "sha512-mukuNALVsoix/w1BJwFzwXBN/dHeejQtuVzcDsfOEsdpCumXb/E9j8w11h5S54tT1xhifGfbbSm/ICrObRb3KA==", + "dev": true, + "license": "MIT", + "optional": true, + "peer": true, + "dependencies": { + "@emnapi/wasi-threads": "1.2.0", + "tslib": "^2.4.0" + } + }, + "node_modules/@emnapi/runtime": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.9.1.tgz", + "integrity": "sha512-VYi5+ZVLhpgK4hQ0TAjiQiZ6ol0oe4mBx7mVv7IflsiEp0OWoVsp/+f9Vc1hOhE0TtkORVrI1GvzyreqpgWtkA==", + "dev": true, + "license": "MIT", + "optional": true, + "peer": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@emnapi/wasi-threads": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@emnapi/wasi-threads/-/wasi-threads-1.2.0.tgz", + "integrity": "sha512-N10dEJNSsUx41Z6pZsXU8FjPjpBEplgH24sfkmITrBED1/U2Esum9F3lfLrMjKHHjmi557zQn7kR9R+XWXu5Rg==", + "dev": true, + "license": "MIT", + "optional": true, + "peer": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.5.tgz", + "integrity": "sha512-cYQ9310grqxueWbl+WuIUIaiUaDcj7WOq5fVhEljNVgRfOUhY9fy2zTvfoqWsnebh8Sl70VScFbICvJnLKB0Og==", + "dev": true, + "license": "MIT" + }, "node_modules/@ljharb/resumer": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/@ljharb/resumer/-/resumer-0.1.3.tgz", @@ -296,6 +342,25 @@ "node": ">= 10" } }, + "node_modules/@napi-rs/wasm-runtime": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@napi-rs/wasm-runtime/-/wasm-runtime-1.1.2.tgz", + "integrity": "sha512-sNXv5oLJ7ob93xkZ1XnxisYhGYXfaG9f65/ZgYuAu3qt7b3NadcOEhLvx28hv31PgX8SZJRYrAIPQilQmFpLVw==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "@tybys/wasm-util": "^0.10.1" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Brooooooklyn" + }, + "peerDependencies": { + "@emnapi/core": "^1.7.1", + "@emnapi/runtime": "^1.7.1" + } + }, "node_modules/@noble/hashes": { "version": "1.8.0", "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.8.0.tgz", @@ -308,6 +373,23 @@ "url": "https://paulmillr.com/funding/" } }, + "node_modules/@oxc-project/types": { + "version": "0.122.0", + "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.122.0.tgz", + "integrity": "sha512-oLAl5kBpV4w69UtFZ9xqcmTi+GENWOcPF7FCrczTiBbmC0ibXxCwyvZGbO39rCVEuLGAZM84DH0pUIyyv/YJzA==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/Boshen" + } + }, + "node_modules/@polka/url": { + "version": "1.0.0-next.29", + "resolved": "https://registry.npmjs.org/@polka/url/-/url-1.0.0-next.29.tgz", + "integrity": "sha512-wwQAWhWSuHaag8c4q/KN/vCoeOJYshAIvMQwD4GpSb3OiZklFfvAgmj0VCBBImRpuF/aFgIRzllXlVX93Jevww==", + "dev": true, + "license": "MIT" + }, "node_modules/@psf/bch-js": { "version": "6.8.3", "resolved": "https://registry.npmjs.org/@psf/bch-js/-/bch-js-6.8.3.tgz", @@ -428,6 +510,311 @@ "@psf/bitcoincash-ops": "^2.0.0" } }, + "node_modules/@rolldown/binding-android-arm64": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm64/-/binding-android-arm64-1.0.0-rc.12.tgz", + "integrity": "sha512-pv1y2Fv0JybcykuiiD3qBOBdz6RteYojRFY1d+b95WVuzx211CRh+ytI/+9iVyWQ6koTh5dawe4S/yRfOFjgaA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-arm64": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-arm64/-/binding-darwin-arm64-1.0.0-rc.12.tgz", + "integrity": "sha512-cFYr6zTG/3PXXF3pUO+umXxt1wkRK/0AYT8lDwuqvRC+LuKYWSAQAQZjCWDQpAH172ZV6ieYrNnFzVVcnSflAg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-x64": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-x64/-/binding-darwin-x64-1.0.0-rc.12.tgz", + "integrity": "sha512-ZCsYknnHzeXYps0lGBz8JrF37GpE9bFVefrlmDrAQhOEi4IOIlcoU1+FwHEtyXGx2VkYAvhu7dyBf75EJQffBw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-freebsd-x64": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-freebsd-x64/-/binding-freebsd-x64-1.0.0-rc.12.tgz", + "integrity": "sha512-dMLeprcVsyJsKolRXyoTH3NL6qtsT0Y2xeuEA8WQJquWFXkEC4bcu1rLZZSnZRMtAqwtrF/Ib9Ddtpa/Gkge9Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm-gnueabihf": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-1.0.0-rc.12.tgz", + "integrity": "sha512-YqWjAgGC/9M1lz3GR1r1rP79nMgo3mQiiA+Hfo+pvKFK1fAJ1bCi0ZQVh8noOqNacuY1qIcfyVfP6HoyBRZ85Q==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-gnu": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.0.0-rc.12.tgz", + "integrity": "sha512-/I5AS4cIroLpslsmzXfwbe5OmWvSsrFuEw3mwvbQ1kDxJ822hFHIx+vsN/TAzNVyepI/j/GSzrtCIwQPeKCLIg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-musl": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.0.0-rc.12.tgz", + "integrity": "sha512-V6/wZztnBqlx5hJQqNWwFdxIKN0m38p8Jas+VoSfgH54HSj9tKTt1dZvG6JRHcjh6D7TvrJPWFGaY9UBVOaWPw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-ppc64-gnu": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-ppc64-gnu/-/binding-linux-ppc64-gnu-1.0.0-rc.12.tgz", + "integrity": "sha512-AP3E9BpcUYliZCxa3w5Kwj9OtEVDYK6sVoUzy4vTOJsjPOgdaJZKFmN4oOlX0Wp0RPV2ETfmIra9x1xuayFB7g==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-s390x-gnu": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-s390x-gnu/-/binding-linux-s390x-gnu-1.0.0-rc.12.tgz", + "integrity": "sha512-nWwpvUSPkoFmZo0kQazZYOrT7J5DGOJ/+QHHzjvNlooDZED8oH82Yg67HvehPPLAg5fUff7TfWFHQS8IV1n3og==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-gnu": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.0.0-rc.12.tgz", + "integrity": "sha512-RNrafz5bcwRy+O9e6P8Z/OCAJW/A+qtBczIqVYwTs14pf4iV1/+eKEjdOUta93q2TsT/FI0XYDP3TCky38LMAg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-musl": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-musl/-/binding-linux-x64-musl-1.0.0-rc.12.tgz", + "integrity": "sha512-Jpw/0iwoKWx3LJ2rc1yjFrj+T7iHZn2JDg1Yny1ma0luviFS4mhAIcd1LFNxK3EYu3DHWCps0ydXQ5i/rrJ2ig==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-openharmony-arm64": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-openharmony-arm64/-/binding-openharmony-arm64-1.0.0-rc.12.tgz", + "integrity": "sha512-vRugONE4yMfVn0+7lUKdKvN4D5YusEiPilaoO2sgUWpCvrncvWgPMzK00ZFFJuiPgLwgFNP5eSiUlv2tfc+lpA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-wasm32-wasi": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-wasm32-wasi/-/binding-wasm32-wasi-1.0.0-rc.12.tgz", + "integrity": "sha512-ykGiLr/6kkiHc0XnBfmFJuCjr5ZYKKofkx+chJWDjitX+KsJuAmrzWhwyOMSHzPhzOHOy7u9HlFoa5MoAOJ/Zg==", + "cpu": [ + "wasm32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "@napi-rs/wasm-runtime": "^1.1.1" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@rolldown/binding-win32-arm64-msvc": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.0.0-rc.12.tgz", + "integrity": "sha512-5eOND4duWkwx1AzCxadcOrNeighiLwMInEADT0YM7xeEOOFcovWZCq8dadXgcRHSf3Ulh1kFo/qvzoFiCLOL1Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-win32-x64-msvc": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.0.0-rc.12.tgz", + "integrity": "sha512-PyqoipaswDLAZtot351MLhrlrh6lcZPo2LSYE+VDxbVk24LVKAGOuE4hb8xZQmrPAuEtTZW8E6D2zc5EUZX4Lw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/pluginutils": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.0.0-rc.12.tgz", + "integrity": "sha512-HHMwmarRKvoFsJorqYlFeFRzXZqCt2ETQlEDOb9aqssrnVBB1/+xgTGtuTrIk5vzLNX1MjMtTf7W9z3tsSbrxw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@standard-schema/spec": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz", + "integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==", + "dev": true, + "license": "MIT" + }, + "node_modules/@tybys/wasm-util": { + "version": "0.10.1", + "resolved": "https://registry.npmjs.org/@tybys/wasm-util/-/wasm-util-0.10.1.tgz", + "integrity": "sha512-9tTaPJLSiejZKx+Bmog4uSubteqTvFrVrURwkmHixBo0G4seD0zUxp98E1DzUBJxLQ3NPwXrGKDiVjwx/DpPsg==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@types/chai": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/@types/chai/-/chai-5.2.3.tgz", + "integrity": "sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/deep-eql": "*", + "assertion-error": "^2.0.1" + } + }, + "node_modules/@types/deep-eql": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/deep-eql/-/deep-eql-4.0.2.tgz", + "integrity": "sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/estree": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.8.tgz", + "integrity": "sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/node": { "version": "22.19.15", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.19.15.tgz", @@ -435,15 +822,150 @@ "dev": true, "license": "MIT", "dependencies": { - "undici-types": "~6.21.0" + "undici-types": "~6.21.0" + } + }, + "node_modules/@types/qrcode-terminal": { + "version": "0.12.2", + "resolved": "https://registry.npmjs.org/@types/qrcode-terminal/-/qrcode-terminal-0.12.2.tgz", + "integrity": "sha512-v+RcIEJ+Uhd6ygSQ0u5YYY7ZM+la7GgPbs0V/7l/kFs2uO4S8BcIUEMoP7za4DNIqNnUD5npf0A/7kBhrCKG5Q==", + "dev": true, + "license": "MIT" + }, + "node_modules/@vitest/expect": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-4.1.2.tgz", + "integrity": "sha512-gbu+7B0YgUJ2nkdsRJrFFW6X7NTP44WlhiclHniUhxADQJH5Szt9mZ9hWnJPJ8YwOK5zUOSSlSvyzRf0u1DSBQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@standard-schema/spec": "^1.1.0", + "@types/chai": "^5.2.2", + "@vitest/spy": "4.1.2", + "@vitest/utils": "4.1.2", + "chai": "^6.2.2", + "tinyrainbow": "^3.1.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/mocker": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-4.1.2.tgz", + "integrity": "sha512-Ize4iQtEALHDttPRCmN+FKqOl2vxTiNUhzobQFFt/BM1lRUTG7zRCLOykG/6Vo4E4hnUdfVLo5/eqKPukcWW7Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/spy": "4.1.2", + "estree-walker": "^3.0.3", + "magic-string": "^0.30.21" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "msw": "^2.4.9", + "vite": "^6.0.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "msw": { + "optional": true + }, + "vite": { + "optional": true + } + } + }, + "node_modules/@vitest/pretty-format": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-4.1.2.tgz", + "integrity": "sha512-dwQga8aejqeuB+TvXCMzSQemvV9hNEtDDpgUKDzOmNQayl2OG241PSWeJwKRH3CiC+sESrmoFd49rfnq7T4RnA==", + "dev": true, + "license": "MIT", + "dependencies": { + "tinyrainbow": "^3.1.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/runner": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-4.1.2.tgz", + "integrity": "sha512-Gr+FQan34CdiYAwpGJmQG8PgkyFVmARK8/xSijia3eTFgVfpcpztWLuP6FttGNfPLJhaZVP/euvujeNYar36OQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/utils": "4.1.2", + "pathe": "^2.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/snapshot": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-4.1.2.tgz", + "integrity": "sha512-g7yfUmxYS4mNxk31qbOYsSt2F4m1E02LFqO53Xpzg3zKMhLAPZAjjfyl9e6z7HrW6LvUdTwAQR3HHfLjpko16A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "4.1.2", + "@vitest/utils": "4.1.2", + "magic-string": "^0.30.21", + "pathe": "^2.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/spy": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-4.1.2.tgz", + "integrity": "sha512-DU4fBnbVCJGNBwVA6xSToNXrkZNSiw59H8tcuUspVMsBDBST4nfvsPsEHDHGtWRRnqBERBQu7TrTKskmjqTXKA==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/ui": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/@vitest/ui/-/ui-4.1.2.tgz", + "integrity": "sha512-/irhyeAcKS2u6Zokagf9tqZJ0t8S6kMZq4ZG9BHZv7I+fkRrYfQX4w7geYeC2r6obThz39PDxvXQzZX+qXqGeg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/utils": "4.1.2", + "fflate": "^0.8.2", + "flatted": "^3.4.2", + "pathe": "^2.0.3", + "sirv": "^3.0.2", + "tinyglobby": "^0.2.15", + "tinyrainbow": "^3.1.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "vitest": "4.1.2" } }, - "node_modules/@types/qrcode-terminal": { - "version": "0.12.2", - "resolved": "https://registry.npmjs.org/@types/qrcode-terminal/-/qrcode-terminal-0.12.2.tgz", - "integrity": "sha512-v+RcIEJ+Uhd6ygSQ0u5YYY7ZM+la7GgPbs0V/7l/kFs2uO4S8BcIUEMoP7za4DNIqNnUD5npf0A/7kBhrCKG5Q==", + "node_modules/@vitest/utils": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-4.1.2.tgz", + "integrity": "sha512-xw2/TiX82lQHA06cgbqRKFb5lCAy3axQ4H4SoUFhUsg+wztiet+co86IAMDtF6Vm1hc7J6j09oh/rgDn+JdKIQ==", "dev": true, - "license": "MIT" + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "4.1.2", + "convert-source-map": "^2.0.0", + "tinyrainbow": "^3.1.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } }, "node_modules/array-buffer-byte-length": { "version": "1.0.2", @@ -519,6 +1041,16 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/assertion-error": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz", + "integrity": "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + } + }, "node_modules/async-function": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/async-function/-/async-function-1.0.0.tgz", @@ -832,6 +1364,16 @@ "big-integer": "^1.6.34" } }, + "node_modules/chai": { + "version": "6.2.2", + "resolved": "https://registry.npmjs.org/chai/-/chai-6.2.2.tgz", + "integrity": "sha512-NUPRluOfOiTKBKvWPtSD4PhFvWCqOi0BGStNWs57X9js7XGTprSmFoz5F0tWhR4WPjNeR9jXqdC7/UpSJTnlRg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/chalk": { "version": "5.6.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz", @@ -905,6 +1447,13 @@ "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", "license": "MIT" }, + "node_modules/convert-source-map": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", + "integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==", + "dev": true, + "license": "MIT" + }, "node_modules/core-util-is": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", @@ -1073,6 +1622,16 @@ "node": ">=0.4.0" } }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=8" + } + }, "node_modules/dotignore": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/dotignore/-/dotignore-0.1.2.tgz", @@ -1262,6 +1821,13 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/es-module-lexer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-2.0.0.tgz", + "integrity": "sha512-5POEcUuZybH7IdmGsD8wlf0AI55wMecM9rVBTI/qEAy2c1kTOm3DjFYjrBdI2K3BaJjJYfYFeRtM0t9ssnRuxw==", + "dev": true, + "license": "MIT" + }, "node_modules/es-object-atoms": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", @@ -1318,6 +1884,16 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/estree-walker": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz", + "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "^1.0.0" + } + }, "node_modules/evp_bytestokey": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/evp_bytestokey/-/evp_bytestokey-1.0.3.tgz", @@ -1328,12 +1904,54 @@ "safe-buffer": "^5.1.1" } }, + "node_modules/expect-type": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.3.0.tgz", + "integrity": "sha512-knvyeauYhqjOYvQ66MznSMs83wmHrCycNEN6Ao+2AeYEfxUIkuiVxdEa1qlGEPK+We3n0THiDciYSsCcgW/DoA==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/fdir": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz", + "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12.0.0" + }, + "peerDependencies": { + "picomatch": "^3 || ^4" + }, + "peerDependenciesMeta": { + "picomatch": { + "optional": true + } + } + }, + "node_modules/fflate": { + "version": "0.8.2", + "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.2.tgz", + "integrity": "sha512-cPJU47OaAoCbg0pBvzsgpTPhmhqI5eJjh/JIu8tPj5q+T7iLvW/JAYUqmE7KOB4R1ZyEhzBaIQpQpardBF5z8A==", + "dev": true, + "license": "MIT" + }, "node_modules/file-uri-to-path": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", "license": "MIT" }, + "node_modules/flatted": { + "version": "3.4.2", + "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.4.2.tgz", + "integrity": "sha512-PjDse7RzhcPkIJwy5t7KPWQSZ9cAbzQXcafsetQoD7sOJRQlGikNbx7yZp2OotDnJyrDcbyRq3Ttb18iYOqkxA==", + "dev": true, + "license": "ISC" + }, "node_modules/follow-redirects": { "version": "1.15.11", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.11.tgz", @@ -1391,6 +2009,21 @@ "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", "license": "ISC" }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", @@ -2111,53 +2744,324 @@ "call-bound": "^1.0.3" }, "engines": { - "node": ">= 0.4" + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/is-weakset": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/is-weakset/-/is-weakset-2.0.4.tgz", + "integrity": "sha512-mfcwb6IzQyOKTs84CQMrOwW4gQcaTOAWJ0zzJCl2WSPDrWk/OzDaImWFH3djXhb24g4eudZfLRozAvPGw4d9hQ==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.3", + "get-intrinsic": "^1.2.6" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/isarray": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-2.0.5.tgz", + "integrity": "sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw==", + "license": "MIT" + }, + "node_modules/js-sha256": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.9.0.tgz", + "integrity": "sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA==", + "license": "MIT" + }, + "node_modules/keccak": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/keccak/-/keccak-3.0.4.tgz", + "integrity": "sha512-3vKuW0jV8J3XNTzvfyicFR5qvxrSAGl7KIhvgOu5cmWwM7tZRj3fMbj/pfIf4be7aznbc+prBWGjywox/g2Y6Q==", + "hasInstallScript": true, + "license": "MIT", + "dependencies": { + "node-addon-api": "^2.0.0", + "node-gyp-build": "^4.2.0", + "readable-stream": "^3.6.0" + }, + "engines": { + "node": ">=10.0.0" + } + }, + "node_modules/lightningcss": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.32.0.tgz", + "integrity": "sha512-NXYBzinNrblfraPGyrbPoD19C1h9lfI/1mzgWYvXUTe414Gz/X1FD2XBZSZM7rRTrMA8JL3OtAaGifrIKhQ5yQ==", + "dev": true, + "license": "MPL-2.0", + "dependencies": { + "detect-libc": "^2.0.3" + }, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + }, + "optionalDependencies": { + "lightningcss-android-arm64": "1.32.0", + "lightningcss-darwin-arm64": "1.32.0", + "lightningcss-darwin-x64": "1.32.0", + "lightningcss-freebsd-x64": "1.32.0", + "lightningcss-linux-arm-gnueabihf": "1.32.0", + "lightningcss-linux-arm64-gnu": "1.32.0", + "lightningcss-linux-arm64-musl": "1.32.0", + "lightningcss-linux-x64-gnu": "1.32.0", + "lightningcss-linux-x64-musl": "1.32.0", + "lightningcss-win32-arm64-msvc": "1.32.0", + "lightningcss-win32-x64-msvc": "1.32.0" + } + }, + "node_modules/lightningcss-android-arm64": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-android-arm64/-/lightningcss-android-arm64-1.32.0.tgz", + "integrity": "sha512-YK7/ClTt4kAK0vo6w3X+Pnm0D2cf2vPHbhOXdoNti1Ga0al1P4TBZhwjATvjNwLEBCnKvjJc2jQgHXH0NEwlAg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-arm64": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-arm64/-/lightningcss-darwin-arm64-1.32.0.tgz", + "integrity": "sha512-RzeG9Ju5bag2Bv1/lwlVJvBE3q6TtXskdZLLCyfg5pt+HLz9BqlICO7LZM7VHNTTn/5PRhHFBSjk5lc4cmscPQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-x64": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-x64/-/lightningcss-darwin-x64-1.32.0.tgz", + "integrity": "sha512-U+QsBp2m/s2wqpUYT/6wnlagdZbtZdndSmut/NJqlCcMLTWp5muCrID+K5UJ6jqD2BFshejCYXniPDbNh73V8w==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-freebsd-x64": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-freebsd-x64/-/lightningcss-freebsd-x64-1.32.0.tgz", + "integrity": "sha512-JCTigedEksZk3tHTTthnMdVfGf61Fky8Ji2E4YjUTEQX14xiy/lTzXnu1vwiZe3bYe0q+SpsSH/CTeDXK6WHig==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm-gnueabihf": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm-gnueabihf/-/lightningcss-linux-arm-gnueabihf-1.32.0.tgz", + "integrity": "sha512-x6rnnpRa2GL0zQOkt6rts3YDPzduLpWvwAF6EMhXFVZXD4tPrBkEFqzGowzCsIWsPjqSK+tyNEODUBXeeVHSkw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-gnu": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-gnu/-/lightningcss-linux-arm64-gnu-1.32.0.tgz", + "integrity": "sha512-0nnMyoyOLRJXfbMOilaSRcLH3Jw5z9HDNGfT/gwCPgaDjnx0i8w7vBzFLFR1f6CMLKF8gVbebmkUN3fa/kQJpQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-musl": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-musl/-/lightningcss-linux-arm64-musl-1.32.0.tgz", + "integrity": "sha512-UpQkoenr4UJEzgVIYpI80lDFvRmPVg6oqboNHfoH4CQIfNA+HOrZ7Mo7KZP02dC6LjghPQJeBsvXhJod/wnIBg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-gnu": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-gnu/-/lightningcss-linux-x64-gnu-1.32.0.tgz", + "integrity": "sha512-V7Qr52IhZmdKPVr+Vtw8o+WLsQJYCTd8loIfpDaMRWGUZfBOYEJeyJIkqGIDMZPwPx24pUMfwSxxI8phr/MbOA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-musl": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-musl/-/lightningcss-linux-x64-musl-1.32.0.tgz", + "integrity": "sha512-bYcLp+Vb0awsiXg/80uCRezCYHNg1/l3mt0gzHnWV9XP1W5sKa5/TCdGWaR/zBM2PeF/HbsQv/j2URNOiVuxWg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-arm64-msvc": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-arm64-msvc/-/lightningcss-win32-arm64-msvc-1.32.0.tgz", + "integrity": "sha512-8SbC8BR40pS6baCM8sbtYDSwEVQd4JlFTOlaD3gWGHfThTcABnNDBda6eTZeqbofalIJhFx0qKzgHJmcPTnGdw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 12.0.0" }, "funding": { - "url": "https://github.com/sponsors/ljharb" + "type": "opencollective", + "url": "https://opencollective.com/parcel" } }, - "node_modules/is-weakset": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/is-weakset/-/is-weakset-2.0.4.tgz", - "integrity": "sha512-mfcwb6IzQyOKTs84CQMrOwW4gQcaTOAWJ0zzJCl2WSPDrWk/OzDaImWFH3djXhb24g4eudZfLRozAvPGw4d9hQ==", - "license": "MIT", - "dependencies": { - "call-bound": "^1.0.3", - "get-intrinsic": "^1.2.6" - }, + "node_modules/lightningcss-win32-x64-msvc": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-x64-msvc/-/lightningcss-win32-x64-msvc-1.32.0.tgz", + "integrity": "sha512-Amq9B/SoZYdDi1kFrojnoqPLxYhQ4Wo5XiL8EVJrVsB8ARoC1PWW6VGtT0WKCemjy8aC+louJnjS7U18x3b06Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], "engines": { - "node": ">= 0.4" + "node": ">= 12.0.0" }, "funding": { - "url": "https://github.com/sponsors/ljharb" + "type": "opencollective", + "url": "https://opencollective.com/parcel" } }, - "node_modules/isarray": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/isarray/-/isarray-2.0.5.tgz", - "integrity": "sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw==", - "license": "MIT" - }, - "node_modules/js-sha256": { - "version": "0.9.0", - "resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.9.0.tgz", - "integrity": "sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA==", - "license": "MIT" - }, - "node_modules/keccak": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/keccak/-/keccak-3.0.4.tgz", - "integrity": "sha512-3vKuW0jV8J3XNTzvfyicFR5qvxrSAGl7KIhvgOu5cmWwM7tZRj3fMbj/pfIf4be7aznbc+prBWGjywox/g2Y6Q==", - "hasInstallScript": true, + "node_modules/magic-string": { + "version": "0.30.21", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz", + "integrity": "sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==", + "dev": true, "license": "MIT", "dependencies": { - "node-addon-api": "^2.0.0", - "node-gyp-build": "^4.2.0", - "readable-stream": "^3.6.0" - }, - "engines": { - "node": ">=10.0.0" + "@jridgewell/sourcemap-codec": "^1.5.5" } }, "node_modules/math-intrinsics": { @@ -2261,12 +3165,41 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/mrmime": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/mrmime/-/mrmime-2.0.1.tgz", + "integrity": "sha512-Y3wQdFg2Va6etvQ5I82yUhGdsKrcYox6p7FfL1LbK2J4V01F9TGlepTIhnK24t7koZibmg82KGglhA1XK5IsLQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10" + } + }, "node_modules/nan": { "version": "2.25.0", "resolved": "https://registry.npmjs.org/nan/-/nan-2.25.0.tgz", "integrity": "sha512-0M90Ag7Xn5KMLLZ7zliPWP3rT90P6PN+IzVFS0VqmnPktBk3700xUVv8Ikm9EUaUE5SDWdp/BIxdENzVznpm1g==", "license": "MIT" }, + "node_modules/nanoid": { + "version": "3.3.11", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.11.tgz", + "integrity": "sha512-N8SpfPUnUp1bK+PMYW8qSWdl9U+wwNWI4QKxOYDy9JAro3WMX7p2OeVRF9v+347pnakNevPmiHhNmZ2HbFA76w==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "bin": { + "nanoid": "bin/nanoid.cjs" + }, + "engines": { + "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" + } + }, "node_modules/node-addon-api": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.2.tgz", @@ -2374,6 +3307,17 @@ "node": ">= 0.4" } }, + "node_modules/obug": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/obug/-/obug-2.1.1.tgz", + "integrity": "sha512-uTqF9MuPraAQ+IsnPf366RG4cP9RtUi7MLO1N3KEc+wb0a6yKpeL0lmk2IB1jY5KHPAlTc6T/JRdC/YqxHNwkQ==", + "dev": true, + "funding": [ + "https://github.com/sponsors/sxzz", + "https://opencollective.com/debug" + ], + "license": "MIT" + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -2415,6 +3359,13 @@ "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", "license": "MIT" }, + "node_modules/pathe": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz", + "integrity": "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w==", + "dev": true, + "license": "MIT" + }, "node_modules/pbkdf2": { "version": "3.1.5", "resolved": "https://registry.npmjs.org/pbkdf2/-/pbkdf2-3.1.5.tgz", @@ -2452,6 +3403,26 @@ ], "license": "MIT" }, + "node_modules/picocolors": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", + "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", + "dev": true, + "license": "ISC" + }, + "node_modules/picomatch": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.4.tgz", + "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, "node_modules/possible-typed-array-names": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.1.0.tgz", @@ -2461,6 +3432,35 @@ "node": ">= 0.4" } }, + "node_modules/postcss": { + "version": "8.5.8", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.8.tgz", + "integrity": "sha512-OW/rX8O/jXnm82Ey1k44pObPtdblfiuWnrd8X7GJ7emImCOstunGbXUpp7HdBrFQX6rJzn3sPT397Wp5aCwCHg==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/postcss" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "nanoid": "^3.3.11", + "picocolors": "^1.1.1", + "source-map-js": "^1.2.1" + }, + "engines": { + "node": "^10 || ^12 || >=14" + } + }, "node_modules/process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", @@ -2606,6 +3606,40 @@ "node": ">= 0.8" } }, + "node_modules/rolldown": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.0.0-rc.12.tgz", + "integrity": "sha512-yP4USLIMYrwpPHEFB5JGH1uxhcslv6/hL0OyvTuY+3qlOSJvZ7ntYnoWpehBxufkgN0cvXxppuTu5hHa/zPh+A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@oxc-project/types": "=0.122.0", + "@rolldown/pluginutils": "1.0.0-rc.12" + }, + "bin": { + "rolldown": "bin/cli.mjs" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "optionalDependencies": { + "@rolldown/binding-android-arm64": "1.0.0-rc.12", + "@rolldown/binding-darwin-arm64": "1.0.0-rc.12", + "@rolldown/binding-darwin-x64": "1.0.0-rc.12", + "@rolldown/binding-freebsd-x64": "1.0.0-rc.12", + "@rolldown/binding-linux-arm-gnueabihf": "1.0.0-rc.12", + "@rolldown/binding-linux-arm64-gnu": "1.0.0-rc.12", + "@rolldown/binding-linux-arm64-musl": "1.0.0-rc.12", + "@rolldown/binding-linux-ppc64-gnu": "1.0.0-rc.12", + "@rolldown/binding-linux-s390x-gnu": "1.0.0-rc.12", + "@rolldown/binding-linux-x64-gnu": "1.0.0-rc.12", + "@rolldown/binding-linux-x64-musl": "1.0.0-rc.12", + "@rolldown/binding-openharmony-arm64": "1.0.0-rc.12", + "@rolldown/binding-wasm32-wasi": "1.0.0-rc.12", + "@rolldown/binding-win32-arm64-msvc": "1.0.0-rc.12", + "@rolldown/binding-win32-x64-msvc": "1.0.0-rc.12" + } + }, "node_modules/safe-array-concat": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/safe-array-concat/-/safe-array-concat-1.1.3.tgz", @@ -2866,6 +3900,28 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/siginfo": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/siginfo/-/siginfo-2.0.0.tgz", + "integrity": "sha512-ybx0WO1/8bSBLEWXZvEd7gMW3Sn3JFlW3TvX1nREbDLRNQNaeNN8WK0meBwPdAaOI7TtRRRJn/Es1zhrrCHu7g==", + "dev": true, + "license": "ISC" + }, + "node_modules/sirv": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/sirv/-/sirv-3.0.2.tgz", + "integrity": "sha512-2wcC/oGxHis/BoHkkPwldgiPSYcpZK3JU28WoMVv55yHJgcZ8rlXvuG9iZggz+sU1d4bRgIGASwyWqjxu3FM0g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@polka/url": "^1.0.0-next.24", + "mrmime": "^2.0.0", + "totalist": "^3.0.0" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/slp-mdm": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/slp-mdm/-/slp-mdm-0.0.6.tgz", @@ -2884,6 +3940,30 @@ "bignumber.js": "^9.0.0" } }, + "node_modules/source-map-js": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz", + "integrity": "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/stackback": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/stackback/-/stackback-0.0.2.tgz", + "integrity": "sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==", + "dev": true, + "license": "MIT" + }, + "node_modules/std-env": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/std-env/-/std-env-4.0.0.tgz", + "integrity": "sha512-zUMPtQ/HBY3/50VbpkupYHbRroTRZJPRLvreamgErJVys0ceuzMkD44J/QjqhHjOzK42GQ3QZIeFG1OYfOtKqQ==", + "dev": true, + "license": "MIT" + }, "node_modules/stop-iteration-iterator": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/stop-iteration-iterator/-/stop-iteration-iterator-1.1.0.tgz", @@ -3030,6 +4110,50 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/tinybench": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-2.9.0.tgz", + "integrity": "sha512-0+DUvqWMValLmha6lr4kD8iAMK1HzV0/aKnCtWb9v9641TnP/MFb7Pc2bxoxQjTXAErryXVgUOfv2YqNllqGeg==", + "dev": true, + "license": "MIT" + }, + "node_modules/tinyexec": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.0.4.tgz", + "integrity": "sha512-u9r3uZC0bdpGOXtlxUIdwf9pkmvhqJdrVCH9fapQtgy/OeTTMZ1nqH7agtvEfmGui6e1XxjcdrlxvxJvc3sMqw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/tinyglobby": { + "version": "0.2.15", + "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.15.tgz", + "integrity": "sha512-j2Zq4NyQYG5XMST4cbs02Ak8iJUdxRM0XI5QyxXuZOzKOINmWurp3smXu3y5wDcJrptwpSjgXHzIQxR0omXljQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "fdir": "^6.5.0", + "picomatch": "^4.0.3" + }, + "engines": { + "node": ">=12.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/SuperchupuDev" + } + }, + "node_modules/tinyrainbow": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-3.1.0.tgz", + "integrity": "sha512-Bf+ILmBgretUrdJxzXM0SgXLZ3XfiaUuOj/IKQHuTXip+05Xn+uyEYdVg0kYDipTBcLrCVyUzAPz7QmArb0mmw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, "node_modules/to-buffer": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/to-buffer/-/to-buffer-1.2.2.tgz", @@ -3064,6 +4188,24 @@ ], "license": "MIT" }, + "node_modules/totalist": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/totalist/-/totalist-3.0.1.tgz", + "integrity": "sha512-sf4i37nQ2LBx4m3wB74y+ubopq6W/dIzXg0FDGjsYnZHVa1Da8FH853wlL2gtUhg+xJXjfk3kUZS3BRoQeoQBQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/tslib": { + "version": "2.8.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", + "dev": true, + "license": "0BSD", + "optional": true + }, "node_modules/typed-array-buffer": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/typed-array-buffer/-/typed-array-buffer-1.0.3.tgz", @@ -3198,6 +4340,166 @@ "safe-buffer": "^5.1.1" } }, + "node_modules/vite": { + "version": "8.0.3", + "resolved": "https://registry.npmjs.org/vite/-/vite-8.0.3.tgz", + "integrity": "sha512-B9ifbFudT1TFhfltfaIPgjo9Z3mDynBTJSUYxTjOQruf/zHH+ezCQKcoqO+h7a9Pw9Nm/OtlXAiGT1axBgwqrQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "lightningcss": "^1.32.0", + "picomatch": "^4.0.4", + "postcss": "^8.5.8", + "rolldown": "1.0.0-rc.12", + "tinyglobby": "^0.2.15" + }, + "bin": { + "vite": "bin/vite.js" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "funding": { + "url": "https://github.com/vitejs/vite?sponsor=1" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + }, + "peerDependencies": { + "@types/node": "^20.19.0 || >=22.12.0", + "@vitejs/devtools": "^0.1.0", + "esbuild": "^0.27.0", + "jiti": ">=1.21.0", + "less": "^4.0.0", + "sass": "^1.70.0", + "sass-embedded": "^1.70.0", + "stylus": ">=0.54.8", + "sugarss": "^5.0.0", + "terser": "^5.16.0", + "tsx": "^4.8.1", + "yaml": "^2.4.2" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + }, + "@vitejs/devtools": { + "optional": true + }, + "esbuild": { + "optional": true + }, + "jiti": { + "optional": true + }, + "less": { + "optional": true + }, + "sass": { + "optional": true + }, + "sass-embedded": { + "optional": true + }, + "stylus": { + "optional": true + }, + "sugarss": { + "optional": true + }, + "terser": { + "optional": true + }, + "tsx": { + "optional": true + }, + "yaml": { + "optional": true + } + } + }, + "node_modules/vitest": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-4.1.2.tgz", + "integrity": "sha512-xjR1dMTVHlFLh98JE3i/f/WePqJsah4A0FK9cc8Ehp9Udk0AZk6ccpIZhh1qJ/yxVWRZ+Q54ocnD8TXmkhspGg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/expect": "4.1.2", + "@vitest/mocker": "4.1.2", + "@vitest/pretty-format": "4.1.2", + "@vitest/runner": "4.1.2", + "@vitest/snapshot": "4.1.2", + "@vitest/spy": "4.1.2", + "@vitest/utils": "4.1.2", + "es-module-lexer": "^2.0.0", + "expect-type": "^1.3.0", + "magic-string": "^0.30.21", + "obug": "^2.1.1", + "pathe": "^2.0.3", + "picomatch": "^4.0.3", + "std-env": "^4.0.0-rc.1", + "tinybench": "^2.9.0", + "tinyexec": "^1.0.2", + "tinyglobby": "^0.2.15", + "tinyrainbow": "^3.1.0", + "vite": "^6.0.0 || ^7.0.0 || ^8.0.0", + "why-is-node-running": "^2.3.0" + }, + "bin": { + "vitest": "vitest.mjs" + }, + "engines": { + "node": "^20.0.0 || ^22.0.0 || >=24.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@edge-runtime/vm": "*", + "@opentelemetry/api": "^1.9.0", + "@types/node": "^20.0.0 || ^22.0.0 || >=24.0.0", + "@vitest/browser-playwright": "4.1.2", + "@vitest/browser-preview": "4.1.2", + "@vitest/browser-webdriverio": "4.1.2", + "@vitest/ui": "4.1.2", + "happy-dom": "*", + "jsdom": "*", + "vite": "^6.0.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "@edge-runtime/vm": { + "optional": true + }, + "@opentelemetry/api": { + "optional": true + }, + "@types/node": { + "optional": true + }, + "@vitest/browser-playwright": { + "optional": true + }, + "@vitest/browser-preview": { + "optional": true + }, + "@vitest/browser-webdriverio": { + "optional": true + }, + "@vitest/ui": { + "optional": true + }, + "happy-dom": { + "optional": true + }, + "jsdom": { + "optional": true + }, + "vite": { + "optional": false + } + } + }, "node_modules/watchtower-cash-js": { "version": "0.2.4", "resolved": "https://registry.npmjs.org/watchtower-cash-js/-/watchtower-cash-js-0.2.4.tgz", @@ -3296,6 +4598,23 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/why-is-node-running": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-2.3.0.tgz", + "integrity": "sha512-hUrmaWBdVDcxvYqnyh09zunKzROWjbZTiNy8dBEjkS7ehEDQibXJ7XvlmtbwuTclUiIyN+CyXQD4Vmko8fNm8w==", + "dev": true, + "license": "MIT", + "dependencies": { + "siginfo": "^2.0.0", + "stackback": "0.0.2" + }, + "bin": { + "why-is-node-running": "cli.js" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/wif": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/wif/-/wif-2.0.6.tgz", diff --git a/package.json b/package.json index 47db692..a30f9e5 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,8 @@ "dev": "tsc --watch", "start": "node bin/paytaca.js", "clean": "rm -rf dist", + "test": "vitest run", + "test:watch": "vitest", "prepublishOnly": "npm run clean && npm run build" }, "keywords": [ @@ -50,7 +52,9 @@ "devDependencies": { "@types/node": "^22.0.0", "@types/qrcode-terminal": "^0.12.2", - "typescript": "^5.9.3" + "@vitest/ui": "^4.1.2", + "typescript": "^5.9.3", + "vitest": "^4.1.2" }, "engines": { "node": ">=20.0.0" diff --git a/src/utils/x402.test.ts b/src/utils/x402.test.ts new file mode 100644 index 0000000..aa2f050 --- /dev/null +++ b/src/utils/x402.test.ts @@ -0,0 +1,275 @@ +import { describe, it, expect } from 'vitest' +import { + parsePaymentRequiredJson, + selectBchPaymentRequirements, + buildPaymentPayload, + buildAuthorization, + parsePaymentResponse, + isBchNetwork, + isChipnetNetwork, + BCH_MAINNET_NETWORK, + BCH_CHIPNET_NETWORK, + BCH_ASSET_ID, +} from './x402.js' + +describe('x402 parsing', () => { + describe('parsePaymentRequiredJson', () => { + it('should parse valid PaymentRequired JSON', () => { + const input = { + x402Version: 2, + error: 'Payment required', + resource: { url: 'https://api.example.com/data' }, + accepts: [ + { + scheme: 'utxo', + network: BCH_MAINNET_NETWORK, + amount: '1000', + asset: BCH_ASSET_ID, + payTo: 'bitcoincash:qp2f5j6q3fj5gjwgk8rkq8xrk8q8q8q8q8q8q8q8q', + maxTimeoutSeconds: 300, + extra: {}, + }, + ], + extensions: {}, + } + + const result = parsePaymentRequiredJson(input) + + expect(result).not.toBeNull() + expect(result!.x402Version).toBe(2) + expect(result!.error).toBe('Payment required') + expect(result!.resource.url).toBe('https://api.example.com/data') + expect(result!.accepts).toHaveLength(1) + expect(result!.accepts[0].scheme).toBe('utxo') + expect(result!.accepts[0].amount).toBe('1000') + }) + + it('should return null for null input', () => { + expect(parsePaymentRequiredJson(null)).toBeNull() + }) + + it('should return null for non-object input', () => { + expect(parsePaymentRequiredJson('string')).toBeNull() + expect(parsePaymentRequiredJson(123)).toBeNull() + }) + + it('should return null for wrong x402Version', () => { + const input = { x402Version: 1, accepts: [] } + expect(parsePaymentRequiredJson(input)).toBeNull() + }) + + it('should use default values for missing optional fields', () => { + const input = { + x402Version: 2, + resource: {}, + accepts: [ + { + scheme: 'utxo', + network: BCH_MAINNET_NETWORK, + payTo: 'bitcoincash:qp2f5j6q3fj5gjwgk8rkq8xrk8q8q8q8q8q8q8q8', + }, + ], + } + + const result = parsePaymentRequiredJson(input) + + expect(result).not.toBeNull() + expect(result!.accepts[0].asset).toBe(BCH_ASSET_ID) + expect(result!.accepts[0].maxTimeoutSeconds).toBe(60) + }) + + it('should filter out invalid accepts entries', () => { + const input = { + x402Version: 2, + resource: { url: 'https://api.example.com' }, + accepts: [ + { scheme: 'utxo', network: BCH_MAINNET_NETWORK, payTo: 'valid1' }, + { scheme: 'invalid' }, + { network: BCH_MAINNET_NETWORK, payTo: 'missing-scheme' }, + { scheme: 'utxo', payTo: 'missing-network' }, + { scheme: 'utxo', network: BCH_MAINNET_NETWORK }, + ], + } + + const result = parsePaymentRequiredJson(input) + + expect(result!.accepts).toHaveLength(1) + expect(result!.accepts[0].payTo).toBe('valid1') + }) + }) + + describe('selectBchPaymentRequirements', () => { + const paymentRequired = { + x402Version: 2, + resource: { url: 'https://api.example.com' }, + accepts: [ + { + scheme: 'utxo', + network: BCH_MAINNET_NETWORK, + amount: '1000', + asset: BCH_ASSET_ID, + payTo: 'bitcoincash:mainnet-address', + maxTimeoutSeconds: 300, + extra: {}, + }, + { + scheme: 'utxo', + network: BCH_CHIPNET_NETWORK, + amount: '1000', + asset: BCH_ASSET_ID, + payTo: 'bitcoincash:chipnet-address', + maxTimeoutSeconds: 300, + extra: {}, + }, + ], + extensions: {}, + } + + it('should select mainnet requirements for mainnet client', () => { + const result = selectBchPaymentRequirements(paymentRequired, 'mainnet') + expect(result).not.toBeNull() + expect(result!.network).toBe(BCH_MAINNET_NETWORK) + expect(result!.payTo).toBe('bitcoincash:mainnet-address') + }) + + it('should select chipnet requirements for chipnet client', () => { + const result = selectBchPaymentRequirements(paymentRequired, 'chipnet') + expect(result).not.toBeNull() + expect(result!.network).toBe(BCH_CHIPNET_NETWORK) + expect(result!.payTo).toBe('bitcoincash:chipnet-address') + }) + + it('should return null when no matching network', () => { + const emptyAccepts = { ...paymentRequired, accepts: [paymentRequired.accepts[0]] } + const result = selectBchPaymentRequirements(emptyAccepts, 'chipnet') + expect(result).toBeNull() + }) + }) +}) + +describe('x402 building', () => { + describe('buildPaymentPayload', () => { + it('should build a valid PaymentPayload', () => { + const accepted = { + scheme: 'utxo', + network: BCH_MAINNET_NETWORK, + amount: '1000', + asset: BCH_ASSET_ID, + payTo: 'bitcoincash:qp2f5j6q3fj5gjwgk8rkq8xrk8q8q8q8q8q8q8q8', + maxTimeoutSeconds: 300, + extra: {}, + } + + const result = buildPaymentPayload( + accepted, + 'https://api.example.com/data', + 'bitcoincash:payer-address', + 'abc123txid', + 0, + '1000' + ) + + expect(result.x402Version).toBe(2) + expect(result.resource!.url).toBe('https://api.example.com/data') + expect(result.accepted).toBe(accepted) + expect(result.payload.authorization.from).toBe('bitcoincash:payer-address') + expect(result.payload.authorization.to).toBe(accepted.payTo) + expect(result.payload.authorization.txid).toBe('abc123txid') + expect(result.payload.signature).toBe('') + }) + }) + + describe('buildAuthorization', () => { + it('should build a valid Authorization', () => { + const accepted = { + scheme: 'utxo', + network: BCH_MAINNET_NETWORK, + amount: '1000', + asset: BCH_ASSET_ID, + payTo: 'bitcoincash:qp2f5j6q3fj5gjwgk8rkq8xrk8q8q8q8q8q8q8q8', + maxTimeoutSeconds: 300, + extra: {}, + } + + const result = buildAuthorization( + accepted, + 'https://api.example.com/data', + 'bitcoincash:payer-address', + 'abc123txid', + 0, + '1000' + ) + + expect(result.from).toBe('bitcoincash:payer-address') + expect(result.to).toBe(accepted.payTo) + expect(result.txid).toBe('abc123txid') + expect(result.vout).toBe(0) + expect(result.amount).toBe('1000') + }) + }) +}) + +describe('parsePaymentResponse', () => { + it('should parse valid isValid response', () => { + const data = { + isValid: true, + payer: 'bitcoincash:abc123', + remainingBalanceSat: '1000000', + } + + const result = parsePaymentResponse(data) + + expect(result.isValid).toBe(true) + expect(result.payer).toBe('bitcoincash:abc123') + expect(result.remainingBalanceSat).toBe('1000000') + }) + + it('should parse error response', () => { + const data = { error: 'Invalid signature' } + + const result = parsePaymentResponse(data) + + expect(result.isValid).toBe(false) + expect(result.invalidReason).toBe('Invalid signature') + }) + + it('should return no_response_data for null input', () => { + const result = parsePaymentResponse(null) + expect(result.isValid).toBe(false) + expect(result.invalidReason).toBe('no_response_data') + }) + + it('should return unknown_response_format for unrecognized data', () => { + const data = { some: 'unknown', format: true } + const result = parsePaymentResponse(data) + expect(result.isValid).toBe(false) + expect(result.invalidReason).toBe('unknown_response_format') + }) +}) + +describe('network helpers', () => { + describe('isBchNetwork', () => { + it('should return true for mainnet', () => { + expect(isBchNetwork(BCH_MAINNET_NETWORK)).toBe(true) + }) + + it('should return true for chipnet', () => { + expect(isBchNetwork(BCH_CHIPNET_NETWORK)).toBe(true) + }) + + it('should return false for other networks', () => { + expect(isBchNetwork('bitcoin')).toBe(false) + expect(isBchNetwork('litecoin')).toBe(false) + }) + }) + + describe('isChipnetNetwork', () => { + it('should return true for chipnet', () => { + expect(isChipnetNetwork(BCH_CHIPNET_NETWORK)).toBe(true) + }) + + it('should return false for mainnet', () => { + expect(isChipnetNetwork(BCH_MAINNET_NETWORK)).toBe(false) + }) + }) +}) diff --git a/vitest.config.ts b/vitest.config.ts new file mode 100644 index 0000000..d2d9690 --- /dev/null +++ b/vitest.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + include: ['src/**/*.test.ts'], + environment: 'node', + }, +})