|
| 1 | +#!/usr/bin/env node |
| 2 | +import { createDecipheriv, createECDH, hkdfSync } from 'node:crypto'; |
| 3 | +import { readFileSync } from 'node:fs'; |
| 4 | +import { dirname, resolve } from 'node:path'; |
| 5 | +import { stdin, stdout } from 'node:process'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | + |
| 8 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | + |
| 10 | +const ECDH_SALT = Buffer.from('key-publish-ecdh-salt-v1'); |
| 11 | +const ECDH_INFO_PREFIX = Buffer.from('key-publish-api-key:v1:'); |
| 12 | +const AES_NONCE_SIZE = 12; |
| 13 | +const AUTH_TAG_SIZE = 16; |
| 14 | + |
| 15 | +async function promptHidden(prompt) { |
| 16 | + if (!stdin.isTTY || !stdout.isTTY || typeof stdin.setRawMode !== 'function') { |
| 17 | + throw new Error( |
| 18 | + 'Interactive TTY required. Run this script from a terminal session.', |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + stdout.write(prompt); |
| 23 | + stdin.setEncoding('utf8'); |
| 24 | + stdin.setRawMode(true); |
| 25 | + stdin.resume(); |
| 26 | + |
| 27 | + return await new Promise((resolve, reject) => { |
| 28 | + let value = ''; |
| 29 | + |
| 30 | + const cleanup = () => { |
| 31 | + stdin.removeListener('data', onData); |
| 32 | + stdin.setRawMode(false); |
| 33 | + stdin.pause(); |
| 34 | + }; |
| 35 | + |
| 36 | + const onData = (chunk) => { |
| 37 | + const text = typeof chunk === 'string' ? chunk : chunk.toString('utf8'); |
| 38 | + for (const ch of text) { |
| 39 | + if (ch === '\r' || ch === '\n') { |
| 40 | + cleanup(); |
| 41 | + stdout.write('\n'); |
| 42 | + resolve(value); |
| 43 | + return; |
| 44 | + } |
| 45 | + if (ch === '\u0003') { |
| 46 | + cleanup(); |
| 47 | + stdout.write('\n'); |
| 48 | + reject(new Error('Input cancelled by user.')); |
| 49 | + return; |
| 50 | + } |
| 51 | + if (ch === '\u007f' || ch === '\b' || ch === '\x08') { |
| 52 | + if (value.length > 0) { |
| 53 | + value = value.slice(0, -1); |
| 54 | + } |
| 55 | + continue; |
| 56 | + } |
| 57 | + value += ch; |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + stdin.on('data', onData); |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +async function loadPrivateKey() { |
| 66 | + const raw = await promptHidden('Signing policy private key: '); |
| 67 | + let s = raw.trim().toLowerCase(); |
| 68 | + if (s.startsWith('0x')) s = s.slice(2); |
| 69 | + if (!/^[0-9a-f]{64}$/.test(s)) { |
| 70 | + throw new Error('PRIVATE_KEY must be a 32-byte hex string.'); |
| 71 | + } |
| 72 | + return Buffer.from(s, 'hex'); |
| 73 | +} |
| 74 | + |
| 75 | +function decrypt(privateKey, signingAddress, encryptedB64) { |
| 76 | + const payload = Buffer.from(encryptedB64, 'base64'); |
| 77 | + if (payload.length <= 65 + AES_NONCE_SIZE + AUTH_TAG_SIZE) { |
| 78 | + throw new Error('Encrypted payload too short'); |
| 79 | + } |
| 80 | + |
| 81 | + const ephPub = payload.subarray(0, 65); |
| 82 | + const nonce = payload.subarray(65, 65 + AES_NONCE_SIZE); |
| 83 | + const ciphertext = payload.subarray(65 + AES_NONCE_SIZE, -AUTH_TAG_SIZE); |
| 84 | + const authTag = payload.subarray(-AUTH_TAG_SIZE); |
| 85 | + |
| 86 | + const ecdh = createECDH('secp256k1'); |
| 87 | + ecdh.setPrivateKey(privateKey); |
| 88 | + const shared = ecdh.computeSecret(ephPub); |
| 89 | + let aesKey; |
| 90 | + |
| 91 | + try { |
| 92 | + const info = Buffer.concat([ |
| 93 | + ECDH_INFO_PREFIX, |
| 94 | + Buffer.from(signingAddress, 'utf8'), |
| 95 | + ]); |
| 96 | + aesKey = Buffer.from(hkdfSync('sha256', shared, ECDH_SALT, info, 32)); |
| 97 | + |
| 98 | + const decipher = createDecipheriv('aes-256-gcm', aesKey, nonce); |
| 99 | + decipher.setAuthTag(authTag); |
| 100 | + return Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString( |
| 101 | + 'utf8', |
| 102 | + ); |
| 103 | + } finally { |
| 104 | + shared.fill(0); |
| 105 | + if (aesKey) { |
| 106 | + aesKey.fill(0); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Derive Ethereum address requires keccak-256 which Node.js doesn't expose, |
| 112 | +// so we try decrypting each record until one's auth tag validates. |
| 113 | +function findAndDecrypt(records, privateKey) { |
| 114 | + for (const item of records) { |
| 115 | + const addr = item?.signing_policy_address?.toString(); |
| 116 | + const enc = item?.encrypted_API_key?.toString(); |
| 117 | + if (!addr || !enc) continue; |
| 118 | + |
| 119 | + try { |
| 120 | + return { signingAddress: addr, apiKey: decrypt(privateKey, addr, enc) }; |
| 121 | + } catch { |
| 122 | + // Auth tag mismatch — not our record, keep going. |
| 123 | + } |
| 124 | + } |
| 125 | + return undefined; |
| 126 | +} |
| 127 | + |
| 128 | +async function main() { |
| 129 | + let privateKey; |
| 130 | + try { |
| 131 | + privateKey = await loadPrivateKey(); |
| 132 | + const file = readFileSync(resolve(__dirname, 'ignite-api-keys.json'), 'utf8'); |
| 133 | + const records = JSON.parse(file)?.data; |
| 134 | + |
| 135 | + if (!Array.isArray(records)) { |
| 136 | + throw new Error('ignite-api-keys.json does not contain a "data" array.'); |
| 137 | + } |
| 138 | + |
| 139 | + const match = findAndDecrypt(records, privateKey); |
| 140 | + if (!match) { |
| 141 | + throw new Error('No decryptable entry found for this private key.'); |
| 142 | + } |
| 143 | + |
| 144 | + console.log(`Signing policy address: ${match.signingAddress}`); |
| 145 | + console.log(match.apiKey); |
| 146 | + } catch (err) { |
| 147 | + console.error(err.message); |
| 148 | + process.exitCode = 1; |
| 149 | + } finally { |
| 150 | + if (privateKey) { |
| 151 | + privateKey.fill(0); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +void main(); |
0 commit comments