a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm
101
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore(crypto): fix tests

Mary 5dfd4657 d1431eda

+28 -28
+12 -12
packages/utilities/crypto/lib/keypairs/p256.test.ts
··· 1 1 import { fromBase58Btc, fromBase64 } from '@atcute/multibase'; 2 2 import { toSha256 } from '@atcute/uint8array'; 3 3 4 - import { p256 } from '@noble/curves/p256'; 4 + import { p256 } from '@noble/curves/nist.js'; 5 5 import { describe, expect, it } from 'vitest'; 6 6 7 7 import { parseDidKey } from '../multibase.ts'; ··· 16 16 keypair.exportPublicKey('raw'), 17 17 ]); 18 18 19 - expect(p256.utils.isValidPrivateKey(privateKeyBytes)).toBe(true); 19 + expect(p256.utils.isValidSecretKey(privateKeyBytes)).toBe(true); 20 20 expect(publicKeyBytes).toEqual(p256.getPublicKey(privateKeyBytes)); 21 21 }); 22 22 23 23 it('produces valid signatures', async () => { 24 - const privateKeyBytes = p256.utils.randomPrivateKey(); 24 + const privateKeyBytes = p256.utils.randomSecretKey(); 25 25 const publicKeyBytes = p256.getPublicKey(privateKeyBytes); 26 26 27 27 const keypair = await P256PrivateKey.importRaw(privateKeyBytes); ··· 33 33 34 34 await expect(keypair.verify(sig, data)).resolves.toBe(true); 35 35 36 - expect(p256.verify(sig, hash, publicKeyBytes, { format: 'compact', lowS: true })).toBe(true); 37 - expect(() => p256.verify(sig, hash, publicKeyBytes, { format: 'der' })).toThrow(); 36 + expect(p256.verify(sig, hash, publicKeyBytes, { prehash: false, format: 'compact', lowS: true })).toBe(true); 37 + expect(p256.verify(sig, hash, publicKeyBytes, { prehash: false, format: 'der' })).toBe(false); 38 38 }); 39 39 40 40 it('verifies valid signatures', async () => { 41 - const privateKeyBytes = p256.utils.randomPrivateKey(); 41 + const privateKeyBytes = p256.utils.randomSecretKey(); 42 42 const publicKeyBytes = p256.getPublicKey(privateKeyBytes); 43 43 44 44 const keypair = await P256PublicKey.importRaw(publicKeyBytes); ··· 46 46 const data = Uint8Array.from([190, 1, 153, 17, 7, 119, 192, 24, 126, 222, 91, 27, 245, 223, 150, 162]); 47 47 48 48 const hash = await toSha256(data); 49 - const sig = p256.sign(hash, privateKeyBytes, { lowS: true }).toCompactRawBytes(); 49 + const sig = p256.sign(hash, privateKeyBytes, { prehash: false, lowS: true }); 50 50 51 51 await expect(keypair.verify(sig, data)).resolves.toBe(true); 52 52 }); ··· 95 95 96 96 describe('.importRaw()', () => { 97 97 it('imports public keys', async () => { 98 - const privateKeyBytes = p256.utils.randomPrivateKey(); 98 + const privateKeyBytes = p256.utils.randomSecretKey(); 99 99 const publicKeyBytes = p256.getPublicKey(privateKeyBytes); 100 100 101 101 await expect(P256PublicKey.importRaw(publicKeyBytes)).resolves.toBeInstanceOf(P256PublicKey); 102 102 }); 103 103 104 104 it('imports private keys without specifying public key', async () => { 105 - const privateKeyBytes = p256.utils.randomPrivateKey(); 105 + const privateKeyBytes = p256.utils.randomSecretKey(); 106 106 107 107 await expect(P256PrivateKey.importRaw(privateKeyBytes)).resolves.toBeInstanceOf(P256PrivateKey); 108 108 }); 109 109 110 110 it('imports keypairs', async () => { 111 - const privateKeyBytes = p256.utils.randomPrivateKey(); 111 + const privateKeyBytes = p256.utils.randomSecretKey(); 112 112 const publicKeyBytes = p256.getPublicKey(privateKeyBytes); 113 113 114 114 await expect(P256PrivateKey.importRaw(privateKeyBytes, publicKeyBytes)).resolves.toBeInstanceOf( ··· 117 117 }); 118 118 119 119 it('throws on mismatching public/private keys', async () => { 120 - const privateKeyBytes = p256.utils.randomPrivateKey(); 121 - const publicKeyBytes = p256.getPublicKey(p256.utils.randomPrivateKey()); 120 + const privateKeyBytes = p256.utils.randomSecretKey(); 121 + const publicKeyBytes = p256.getPublicKey(p256.utils.randomSecretKey()); 122 122 123 123 await expect(P256PrivateKey.importRaw(privateKeyBytes, publicKeyBytes)).rejects.toThrowError(TypeError); 124 124 });
+12 -12
packages/utilities/crypto/lib/keypairs/secp256k1-node.test.ts
··· 1 1 import { fromBase16, fromBase64 } from '@atcute/multibase'; 2 2 import { toSha256 } from '@atcute/uint8array'; 3 3 4 - import { secp256k1 } from '@noble/curves/secp256k1'; 4 + import { secp256k1 } from '@noble/curves/secp256k1.js'; 5 5 import { describe, expect, it } from 'vitest'; 6 6 7 7 import { parseDidKey } from '../multibase.ts'; ··· 16 16 keypair.exportPublicKey('raw'), 17 17 ]); 18 18 19 - expect(secp256k1.utils.isValidPrivateKey(privateKeyBytes)).toBe(true); 19 + expect(secp256k1.utils.isValidSecretKey(privateKeyBytes)).toBe(true); 20 20 expect(publicKeyBytes).toEqual(secp256k1.getPublicKey(privateKeyBytes)); 21 21 }); 22 22 23 23 it('produces valid signatures', async () => { 24 - const privateKeyBytes = secp256k1.utils.randomPrivateKey(); 24 + const privateKeyBytes = secp256k1.utils.randomSecretKey(); 25 25 const publicKeyBytes = secp256k1.getPublicKey(privateKeyBytes); 26 26 27 27 const keypair = await Secp256k1PrivateKey.importRaw(privateKeyBytes); ··· 33 33 34 34 await expect(keypair.verify(sig, data)).resolves.toBe(true); 35 35 36 - expect(secp256k1.verify(sig, hash, publicKeyBytes, { format: 'compact', lowS: true })).toBe(true); 37 - expect(() => secp256k1.verify(sig, hash, publicKeyBytes, { format: 'der' })).toThrow(); 36 + expect(secp256k1.verify(sig, hash, publicKeyBytes, { prehash: false, format: 'compact', lowS: true })).toBe(true); 37 + expect(secp256k1.verify(sig, hash, publicKeyBytes, { prehash: false, format: 'der' })).toBe(false); 38 38 }); 39 39 40 40 it('verifies valid signatures', async () => { 41 - const privateKeyBytes = secp256k1.utils.randomPrivateKey(); 41 + const privateKeyBytes = secp256k1.utils.randomSecretKey(); 42 42 const publicKeyBytes = secp256k1.getPublicKey(privateKeyBytes); 43 43 44 44 const keypair = await Secp256k1PublicKey.importRaw(publicKeyBytes); ··· 46 46 const data = Uint8Array.from([190, 1, 153, 17, 7, 119, 192, 24, 126, 222, 91, 27, 245, 223, 150, 162]); 47 47 48 48 const hash = await toSha256(data); 49 - const sig = secp256k1.sign(hash, privateKeyBytes, { lowS: true }).toCompactRawBytes(); 49 + const sig = secp256k1.sign(hash, privateKeyBytes, { prehash: false, lowS: true }); 50 50 51 51 await expect(keypair.verify(sig, data)).resolves.toBe(true); 52 52 }); 53 53 54 54 describe('.importRaw()', () => { 55 55 it('imports public keys', async () => { 56 - const privateKeyBytes = secp256k1.utils.randomPrivateKey(); 56 + const privateKeyBytes = secp256k1.utils.randomSecretKey(); 57 57 const publicKeyBytes = secp256k1.getPublicKey(privateKeyBytes); 58 58 59 59 await expect(Secp256k1PublicKey.importRaw(publicKeyBytes)).resolves.toBeInstanceOf(Secp256k1PublicKey); 60 60 }); 61 61 62 62 it('imports private keys without specifying public key', async () => { 63 - const privateKeyBytes = secp256k1.utils.randomPrivateKey(); 63 + const privateKeyBytes = secp256k1.utils.randomSecretKey(); 64 64 65 65 await expect(Secp256k1PrivateKey.importRaw(privateKeyBytes)).resolves.toBeInstanceOf(Secp256k1PrivateKey); 66 66 }); 67 67 68 68 it('imports keypairs', async () => { 69 - const privateKeyBytes = secp256k1.utils.randomPrivateKey(); 69 + const privateKeyBytes = secp256k1.utils.randomSecretKey(); 70 70 const publicKeyBytes = secp256k1.getPublicKey(privateKeyBytes); 71 71 72 72 await expect(Secp256k1PrivateKey.importRaw(privateKeyBytes, publicKeyBytes)).resolves.toBeInstanceOf( ··· 75 75 }); 76 76 77 77 it('throws on mismatching public/private keys', async () => { 78 - const privateKeyBytes = secp256k1.utils.randomPrivateKey(); 79 - const publicKeyBytes = secp256k1.getPublicKey(secp256k1.utils.randomPrivateKey()); 78 + const privateKeyBytes = secp256k1.utils.randomSecretKey(); 79 + const publicKeyBytes = secp256k1.getPublicKey(secp256k1.utils.randomSecretKey()); 80 80 81 81 await expect(Secp256k1PrivateKey.importRaw(privateKeyBytes, publicKeyBytes)).rejects.toThrowError( 82 82 TypeError,
+4 -4
packages/utilities/crypto/lib/keypairs/secp256k1-web.test.ts
··· 1 1 import { fromBase16, fromBase64 } from '@atcute/multibase'; 2 2 import { toSha256 } from '@atcute/uint8array'; 3 3 4 - import { secp256k1 } from '@noble/curves/secp256k1'; 4 + import { secp256k1 } from '@noble/curves/secp256k1.js'; 5 5 import { describe, expect, it } from 'vitest'; 6 6 7 7 import { parseDidKey } from '../multibase.ts'; ··· 33 33 34 34 await expect(keypair.verify(sig, data)).resolves.toBe(true); 35 35 36 - expect(secp256k1.verify(sig, hash, publicKeyBytes, { format: 'compact', lowS: true })).toBe(true); 37 - expect(() => secp256k1.verify(sig, hash, publicKeyBytes, { format: 'der' })).toThrow(); 36 + expect(secp256k1.verify(sig, hash, publicKeyBytes, { prehash: false, format: 'compact', lowS: true })).toBe(true); 37 + expect(secp256k1.verify(sig, hash, publicKeyBytes, { prehash: false, format: 'der' })).toBe(false); 38 38 }); 39 39 40 40 it('verifies valid signatures', async () => { ··· 46 46 const data = Uint8Array.from([190, 1, 153, 17, 7, 119, 192, 24, 126, 222, 91, 27, 245, 223, 150, 162]); 47 47 48 48 const hash = await toSha256(data); 49 - const sig = secp256k1.sign(hash, privateKeyBytes, { lowS: true }).toCompactRawBytes(); 49 + const sig = secp256k1.sign(hash, privateKeyBytes, { prehash: false, lowS: true }); 50 50 51 51 await expect(keypair.verify(sig, data)).resolves.toBe(true); 52 52 });