(privateKeyHex: string)
| 119 | * file permissions (600). |
| 120 | */ |
| 121 | export function saveX402PrivateKey(privateKeyHex: string): string { |
| 122 | const keyHex = privateKeyHex.startsWith('0x') |
| 123 | ? privateKeyHex.slice(2) |
| 124 | : privateKeyHex |
| 125 | |
| 126 | if (keyHex.length !== 64 || !/^[0-9a-fA-F]+$/.test(keyHex)) { |
| 127 | throw new Error('Invalid private key: must be 32 bytes (64 hex characters)') |
| 128 | } |
| 129 | |
| 130 | const address = deriveAddress(keyHex) |
| 131 | |
| 132 | saveGlobalConfig((config) => ({ |
| 133 | ...config, |
| 134 | x402PrivateKey: `0x${keyHex}`, |
| 135 | })) |
| 136 | |
| 137 | saveX402Config({ address }) |
| 138 | |
| 139 | logForDebugging(`[x402] Wallet configured: ${address}`) |
| 140 | return address |
| 141 | } |
| 142 | |
| 143 | /** Removes the private key and disables x402 */ |
| 144 | export function removeX402PrivateKey(): void { |
no test coverage detected