#!/usr/bin/env node
/**
* Test hypercore-crypto keyPair forms
*/
import crypto from 'hypercore-crypto'
import b4a from 'b4a'
console.log('Testing hypercore-crypto keyPair forms...\n')
// Form 1: Return keypair
console.log('1. Testing return form: keyPair(seed)')
const seed = crypto.randomBytes(32)
const keypair1 = crypto.keyPair(seed)
console.log('✓ Keys generated')
console.log(' Public:', b4a.toString(keypair1.publicKey, 'base64').substring(0, 12) + '...')
console.log(' Public length:', keypair1.publicKey.length)
console.log(' Secret length:', keypair1.secretKey.length)
// Form 2: Write to buffers
console.log('\n2. Testing buffer form: keyPair(publicKey, secretKey, seed)')
const publicKey = b4a.allocUnsafe(32)
const secretKey = b4a.allocUnsafe(64)
crypto.keyPair(publicKey, secretKey, seed)
console.log('✓ Keys generated')
console.log(' Public:', b4a.toString(publicKey, 'base64').substring(0, 12) + '...')
console.log(' Public length:', publicKey.length)
console.log(' Secret length:', secretKey.length)
// Compare
console.log('\n3. Comparing keys...')
if (b4a.equals(keypair1.publicKey, publicKey)) {
console.log('✅ Public keys match!')
} else {
console.log('❌ Public keys DO NOT match!')
console.log(' Form 1:', b4a.toString(keypair1.publicKey, 'hex').substring(0, 20))
console.log(' Form 2:', b4a.toString(publicKey, 'hex').substring(0, 20))
}
if (b4a.equals(keypair1.secretKey, secretKey)) {
console.log('✅ Secret keys match!')
} else {
console.log('❌ Secret keys DO NOT match!')
}
// Test signing with both
console.log('\n4. Testing signatures...')
const message = b4a.from('test message', 'utf8')
const sig1 = crypto.sign(message, keypair1.secretKey)
const sig2 = crypto.sign(message, secretKey)
console.log(' Sig 1:', b4a.toString(sig1, 'base64').substring(0, 20) + '...')
console.log(' Sig 2:', b4a.toString(sig2, 'base64').substring(0, 20) + '...')
const valid1 = crypto.verify(message, sig1, keypair1.publicKey)
const valid2 = crypto.verify(message, sig2, publicKey)
console.log(' Verify sig1 with key1:', valid1 ? '✅ PASS' : '❌ FAIL')
console.log(' Verify sig2 with key2:', valid2 ? '✅ PASS' : '❌ FAIL')
console.log(' Verify sig1 with key2:', crypto.verify(message, sig1, publicKey) ? '✅ PASS' : '❌ FAIL')
console.log(' Verify sig2 with key1:', crypto.verify(message, sig2, keypair1.publicKey) ? '✅ PASS' : '❌ FAIL')