|
| 1 | +/** |
| 2 | + * Tests for Podkey cryptographic functions |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it } from 'node:test'; |
| 6 | +import assert from 'node:assert'; |
| 7 | +import { |
| 8 | + generateKeypair, |
| 9 | + getPublicKey, |
| 10 | + signEvent, |
| 11 | + verifySignature, |
| 12 | + getEventHash, |
| 13 | + isValidPublicKey |
| 14 | +} from '../src/crypto.js'; |
| 15 | + |
| 16 | +describe('Crypto Functions', () => { |
| 17 | + describe('generateKeypair', () => { |
| 18 | + it('should generate a valid keypair', async () => { |
| 19 | + const keypair = await generateKeypair(); |
| 20 | + |
| 21 | + assert(keypair, 'Keypair should be returned'); |
| 22 | + assert(keypair.privateKey, 'Private key should exist'); |
| 23 | + assert(keypair.publicKey, 'Public key should exist'); |
| 24 | + assert.strictEqual(keypair.privateKey.length, 64, 'Private key should be 64 chars'); |
| 25 | + assert.strictEqual(keypair.publicKey.length, 64, 'Public key should be 64 chars'); |
| 26 | + assert(/^[0-9a-fA-F]{64}$/.test(keypair.privateKey), 'Private key should be hex'); |
| 27 | + assert(/^[0-9a-fA-F]{64}$/.test(keypair.publicKey), 'Public key should be hex'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should generate different keypairs each time', async () => { |
| 31 | + const keypair1 = await generateKeypair(); |
| 32 | + const keypair2 = await generateKeypair(); |
| 33 | + |
| 34 | + assert.notStrictEqual(keypair1.privateKey, keypair2.privateKey, 'Private keys should differ'); |
| 35 | + assert.notStrictEqual(keypair1.publicKey, keypair2.publicKey, 'Public keys should differ'); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('getPublicKey', () => { |
| 40 | + it('should derive public key from private key', async () => { |
| 41 | + const keypair = await generateKeypair(); |
| 42 | + const derivedPublicKey = getPublicKey(keypair.privateKey); |
| 43 | + |
| 44 | + assert.strictEqual(derivedPublicKey, keypair.publicKey, 'Derived public key should match'); |
| 45 | + assert.strictEqual(derivedPublicKey.length, 64, 'Public key should be 64 chars'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should throw error for invalid private key', () => { |
| 49 | + assert.throws(() => getPublicKey('invalid'), /Private key must be/); |
| 50 | + assert.throws(() => getPublicKey('123'), /Private key must be/); |
| 51 | + assert.throws(() => getPublicKey('x'.repeat(64)), /Private key must be valid hexadecimal/); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('signEvent', () => { |
| 56 | + it('should sign an event correctly', async () => { |
| 57 | + const keypair = await generateKeypair(); |
| 58 | + const event = { |
| 59 | + kind: 1, |
| 60 | + created_at: Math.floor(Date.now() / 1000), |
| 61 | + tags: [], |
| 62 | + content: 'Test event' |
| 63 | + }; |
| 64 | + |
| 65 | + const signed = await signEvent(event, keypair.privateKey); |
| 66 | + |
| 67 | + assert(signed.id, 'Event should have id'); |
| 68 | + assert(signed.pubkey, 'Event should have pubkey'); |
| 69 | + assert(signed.sig, 'Event should have signature'); |
| 70 | + assert.strictEqual(signed.id.length, 64, 'Event ID should be 64 chars'); |
| 71 | + assert.strictEqual(signed.pubkey.length, 64, 'Pubkey should be 64 chars'); |
| 72 | + assert.strictEqual(signed.sig.length, 128, 'Signature should be 128 chars'); |
| 73 | + assert.strictEqual(signed.pubkey, keypair.publicKey, 'Pubkey should match'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should include pubkey in event hash', async () => { |
| 77 | + const keypair = await generateKeypair(); |
| 78 | + const event = { |
| 79 | + kind: 1, |
| 80 | + created_at: Math.floor(Date.now() / 1000), |
| 81 | + tags: [], |
| 82 | + content: 'Test' |
| 83 | + }; |
| 84 | + |
| 85 | + const signed = await signEvent(event, keypair.privateKey); |
| 86 | + const hashWithPubkey = getEventHash({ ...event, pubkey: keypair.publicKey }); |
| 87 | + |
| 88 | + assert.strictEqual(signed.id, hashWithPubkey, 'Event ID should match hash with pubkey'); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('verifySignature', () => { |
| 93 | + it('should verify a valid signature', async () => { |
| 94 | + const keypair = await generateKeypair(); |
| 95 | + const event = { |
| 96 | + kind: 1, |
| 97 | + created_at: Math.floor(Date.now() / 1000), |
| 98 | + tags: [], |
| 99 | + content: 'Test event' |
| 100 | + }; |
| 101 | + |
| 102 | + const signed = await signEvent(event, keypair.privateKey); |
| 103 | + const isValid = await verifySignature(signed); |
| 104 | + |
| 105 | + assert.strictEqual(isValid, true, 'Signature should be valid'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should reject invalid signature', async () => { |
| 109 | + const keypair = await generateKeypair(); |
| 110 | + const event = { |
| 111 | + kind: 1, |
| 112 | + created_at: Math.floor(Date.now() / 1000), |
| 113 | + tags: [], |
| 114 | + content: 'Test event', |
| 115 | + id: 'a'.repeat(64), |
| 116 | + pubkey: keypair.publicKey, |
| 117 | + sig: 'b'.repeat(128) // Invalid signature |
| 118 | + }; |
| 119 | + |
| 120 | + const isValid = await verifySignature(event); |
| 121 | + assert.strictEqual(isValid, false, 'Invalid signature should be rejected'); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('getEventHash', () => { |
| 126 | + it('should generate consistent hashes', () => { |
| 127 | + const event = { |
| 128 | + kind: 1, |
| 129 | + created_at: 1234567890, |
| 130 | + tags: [], |
| 131 | + content: 'Test', |
| 132 | + pubkey: 'a'.repeat(64) |
| 133 | + }; |
| 134 | + |
| 135 | + const hash1 = getEventHash(event); |
| 136 | + const hash2 = getEventHash(event); |
| 137 | + |
| 138 | + assert.strictEqual(hash1, hash2, 'Hashes should be consistent'); |
| 139 | + assert.strictEqual(hash1.length, 64, 'Hash should be 64 chars'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should include pubkey in hash', () => { |
| 143 | + const event1 = { |
| 144 | + kind: 1, |
| 145 | + created_at: 1234567890, |
| 146 | + tags: [], |
| 147 | + content: 'Test', |
| 148 | + pubkey: 'a'.repeat(64) |
| 149 | + }; |
| 150 | + |
| 151 | + const event2 = { |
| 152 | + ...event1, |
| 153 | + pubkey: 'b'.repeat(64) |
| 154 | + }; |
| 155 | + |
| 156 | + const hash1 = getEventHash(event1); |
| 157 | + const hash2 = getEventHash(event2); |
| 158 | + |
| 159 | + assert.notStrictEqual(hash1, hash2, 'Different pubkeys should produce different hashes'); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('isValidPublicKey', () => { |
| 164 | + it('should validate correct public keys', () => { |
| 165 | + assert.strictEqual(isValidPublicKey('a'.repeat(64)), true); |
| 166 | + assert.strictEqual(isValidPublicKey('0123456789abcdef'.repeat(4)), true); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should reject invalid public keys', () => { |
| 170 | + assert.strictEqual(isValidPublicKey(''), false); |
| 171 | + assert.strictEqual(isValidPublicKey('a'.repeat(63)), false); |
| 172 | + assert.strictEqual(isValidPublicKey('a'.repeat(65)), false); |
| 173 | + assert.strictEqual(isValidPublicKey('g'.repeat(64)), false); // Invalid hex |
| 174 | + assert.strictEqual(isValidPublicKey(null), false); |
| 175 | + assert.strictEqual(isValidPublicKey(undefined), false); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments