OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git / test / test-basic-sign.js

56 lines · javascript ref:
#!/usr/bin/env node

/**
 * Test basic hypercore-crypto signing
 */

import crypto from 'hypercore-crypto'
import b4a from 'b4a'

console.log('Testing hypercore-crypto basic signing...\n')

// Generate keypair
console.log('1. Generating keypair...')
const seed = crypto.randomBytes(32)
const keypair = crypto.keyPair(seed)

console.log('✓ Keys generated')
console.log('  Public:', b4a.toString(keypair.publicKey, 'base64').substring(0, 12) + '...')
console.log('  Public length:', keypair.publicKey.length)
console.log('  Secret length:', keypair.secretKey.length)

// Test message
const message = 'test challenge 12345'
const messageBuffer = b4a.from(message, 'utf8')

console.log('\n2. Signing message...')
console.log('  Message:', message)

// Sign - returns signature
const signature = crypto.sign(messageBuffer, keypair.secretKey)

console.log('✓ Signature created')
console.log('  Signature:', b4a.toString(signature, 'base64').substring(0, 20) + '...')
console.log('  Length:', signature.length)

// Verify
console.log('\n3. Verifying signature...')
const isValid = crypto.verify(messageBuffer, signature, keypair.publicKey)

if (isValid) {
  console.log('✅ Signature verification PASSED!')
} else {
  console.log('❌ Signature verification FAILED!')
}

// Try the 3-argument form (writes to buffer)
console.log('\n4. Testing 3-argument sign form...')
const sig2 = Buffer.alloc(64)
crypto.sign(messageBuffer, sig2, keypair.secretKey)

console.log('  Signature:', b4a.toString(sig2, 'base64').substring(0, 20) + '...')
console.log('  Length:', sig2.length)

const isValid2 = crypto.verify(messageBuffer, sig2, keypair.publicKey)
console.log('  Verification:', isValid2 ? '✅ PASS' : '❌ FAIL')