-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
834ae5b
commit f09d10f
Showing
2 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
packages/core/src/chains/nostr/sdkNostr/__tests__/nostr.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { decrypt, encrypt } from '..'; | ||
|
||
describe('Nostr Crypto Functions', () => { | ||
const testPrivateKey = | ||
'0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'; | ||
const testPublicKey = | ||
'a8b5e5163c1d78754dd9229a42047f3ff4b069b99868580da3bb915960e7e9d8'; | ||
const testPlaintext = 'Hello, Nostr!'; | ||
|
||
it('should encrypt data with correct format', async () => { | ||
const encrypted = await encrypt( | ||
testPrivateKey, | ||
testPublicKey, | ||
testPlaintext, | ||
); | ||
const [ciphertext, iv] = encrypted.split('?iv='); | ||
|
||
// Validate format | ||
expect(encrypted).toMatch(/^[A-Za-z0-9+/]+=*\?iv=[A-Za-z0-9+/]+=*$/); | ||
|
||
// Validate IV length (16 bytes) | ||
const ivBuffer = Buffer.from(iv, 'base64'); | ||
expect(ivBuffer.length).toBe(16); | ||
|
||
// Validate ciphertext is non-empty and base64 | ||
expect(ciphertext.length).toBeGreaterThan(0); | ||
expect(() => Buffer.from(ciphertext, 'base64')).not.toThrow(); | ||
}); | ||
|
||
it('should decrypt encrypted data correctly', async () => { | ||
const encrypted = await encrypt( | ||
testPrivateKey, | ||
testPublicKey, | ||
testPlaintext, | ||
); | ||
const decrypted = await decrypt(testPrivateKey, testPublicKey, encrypted); | ||
expect(decrypted).toBe(testPlaintext); | ||
}); | ||
|
||
it('should perform round-trip encryption/decryption', async () => { | ||
const encrypted = await encrypt( | ||
testPrivateKey, | ||
testPublicKey, | ||
testPlaintext, | ||
); | ||
const decrypted = await decrypt(testPrivateKey, testPublicKey, encrypted); | ||
expect(decrypted).toBe(testPlaintext); | ||
|
||
// Verify encrypted data format | ||
expect(encrypted).toContain('?iv='); | ||
const [ciphertext, iv] = encrypted.split('?iv='); | ||
expect(Buffer.from(iv, 'base64').length).toBe(16); | ||
expect(ciphertext.length).toBeGreaterThan(0); | ||
}); | ||
|
||
it('should generate unique IVs for each encryption', async () => { | ||
const encrypted1 = await encrypt( | ||
testPrivateKey, | ||
testPublicKey, | ||
testPlaintext, | ||
); | ||
const encrypted2 = await encrypt( | ||
testPrivateKey, | ||
testPublicKey, | ||
testPlaintext, | ||
); | ||
|
||
const [, iv1] = encrypted1.split('?iv='); | ||
const [, iv2] = encrypted2.split('?iv='); | ||
|
||
// IVs should be different for each encryption | ||
expect(iv1).not.toBe(iv2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters