|
1 | 1 | import { verifyKey } from '../index';
|
2 | 2 | import {
|
3 |
| - invalidKeyPair, |
| 3 | + generateKeyPair, |
4 | 4 | pingRequestBody,
|
5 | 5 | signRequestWithKeyPair,
|
6 |
| - validKeyPair, |
7 | 6 | } from './utils/SharedTestUtils';
|
8 | 7 |
|
9 | 8 | describe('verify key method', () => {
|
10 |
| - it('valid ping request', () => { |
| 9 | + let validKeyPair: CryptoKeyPair; |
| 10 | + let invalidKeyPair: CryptoKeyPair; |
| 11 | + |
| 12 | + beforeAll(async () => { |
| 13 | + validKeyPair = await generateKeyPair(); |
| 14 | + invalidKeyPair = await generateKeyPair(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('valid ping request', async () => { |
11 | 18 | // Sign and verify a valid ping request
|
12 |
| - const signedRequest = signRequestWithKeyPair( |
| 19 | + const signedRequest = await signRequestWithKeyPair( |
13 | 20 | pingRequestBody,
|
14 |
| - validKeyPair.secretKey, |
15 |
| - ); |
16 |
| - expect( |
17 |
| - verifyKey( |
18 |
| - signedRequest.body, |
19 |
| - signedRequest.signature, |
20 |
| - signedRequest.timestamp, |
21 |
| - validKeyPair.publicKey, |
22 |
| - ), |
23 |
| - ).toBe(true); |
| 21 | + validKeyPair.privateKey, |
| 22 | + ); |
| 23 | + const isValid = await verifyKey( |
| 24 | + signedRequest.body, |
| 25 | + signedRequest.signature, |
| 26 | + signedRequest.timestamp, |
| 27 | + validKeyPair.publicKey, |
| 28 | + ); |
| 29 | + expect(isValid).toBe(true); |
24 | 30 | });
|
25 | 31 |
|
26 |
| - it('valid application command', () => { |
| 32 | + it('valid application command', async () => { |
27 | 33 | // Sign and verify a valid application command request
|
28 |
| - const signedRequest = signRequestWithKeyPair( |
| 34 | + const signedRequest = await signRequestWithKeyPair( |
29 | 35 | pingRequestBody,
|
30 |
| - validKeyPair.secretKey, |
31 |
| - ); |
32 |
| - expect( |
33 |
| - verifyKey( |
34 |
| - signedRequest.body, |
35 |
| - signedRequest.signature, |
36 |
| - signedRequest.timestamp, |
37 |
| - validKeyPair.publicKey, |
38 |
| - ), |
39 |
| - ).toBe(true); |
| 36 | + validKeyPair.privateKey, |
| 37 | + ); |
| 38 | + const isValid = await verifyKey( |
| 39 | + signedRequest.body, |
| 40 | + signedRequest.signature, |
| 41 | + signedRequest.timestamp, |
| 42 | + validKeyPair.publicKey, |
| 43 | + ); |
| 44 | + expect(isValid).toBe(true); |
40 | 45 | });
|
41 | 46 |
|
42 |
| - it('valid message component', () => { |
| 47 | + it('valid message component', async () => { |
43 | 48 | // Sign and verify a valid message component request
|
44 |
| - const signedRequest = signRequestWithKeyPair( |
| 49 | + const signedRequest = await signRequestWithKeyPair( |
45 | 50 | pingRequestBody,
|
46 |
| - validKeyPair.secretKey, |
47 |
| - ); |
48 |
| - expect( |
49 |
| - verifyKey( |
50 |
| - signedRequest.body, |
51 |
| - signedRequest.signature, |
52 |
| - signedRequest.timestamp, |
53 |
| - validKeyPair.publicKey, |
54 |
| - ), |
55 |
| - ).toBe(true); |
| 51 | + validKeyPair.privateKey, |
| 52 | + ); |
| 53 | + const isValid = await verifyKey( |
| 54 | + signedRequest.body, |
| 55 | + signedRequest.signature, |
| 56 | + signedRequest.timestamp, |
| 57 | + validKeyPair.publicKey, |
| 58 | + ); |
| 59 | + expect(isValid).toBe(true); |
56 | 60 | });
|
57 | 61 |
|
58 |
| - it('valid autocomplete', () => { |
| 62 | + it('valid autocomplete', async () => { |
59 | 63 | // Sign and verify a valid autocomplete request
|
60 |
| - const signedRequest = signRequestWithKeyPair( |
| 64 | + const signedRequest = await signRequestWithKeyPair( |
61 | 65 | pingRequestBody,
|
62 |
| - validKeyPair.secretKey, |
63 |
| - ); |
64 |
| - expect( |
65 |
| - verifyKey( |
66 |
| - signedRequest.body, |
67 |
| - signedRequest.signature, |
68 |
| - signedRequest.timestamp, |
69 |
| - validKeyPair.publicKey, |
70 |
| - ), |
71 |
| - ).toBe(true); |
| 66 | + validKeyPair.privateKey, |
| 67 | + ); |
| 68 | + const isValid = await verifyKey( |
| 69 | + signedRequest.body, |
| 70 | + signedRequest.signature, |
| 71 | + signedRequest.timestamp, |
| 72 | + validKeyPair.publicKey, |
| 73 | + ); |
| 74 | + expect(isValid).toBe(true); |
72 | 75 | });
|
73 | 76 |
|
74 |
| - it('invalid key', () => { |
| 77 | + it('invalid key', async () => { |
75 | 78 | // Sign a request with a different private key and verify with the valid public key
|
76 |
| - const signedRequest = signRequestWithKeyPair( |
| 79 | + const signedRequest = await signRequestWithKeyPair( |
77 | 80 | pingRequestBody,
|
78 |
| - invalidKeyPair.secretKey, |
79 |
| - ); |
80 |
| - expect( |
81 |
| - verifyKey( |
82 |
| - signedRequest.body, |
83 |
| - signedRequest.signature, |
84 |
| - signedRequest.timestamp, |
85 |
| - validKeyPair.publicKey, |
86 |
| - ), |
87 |
| - ).toBe(false); |
| 81 | + invalidKeyPair.privateKey, |
| 82 | + ); |
| 83 | + const isValid = await verifyKey( |
| 84 | + signedRequest.body, |
| 85 | + signedRequest.signature, |
| 86 | + signedRequest.timestamp, |
| 87 | + validKeyPair.publicKey, |
| 88 | + ); |
| 89 | + expect(isValid).toBe(false); |
88 | 90 | });
|
89 | 91 |
|
90 |
| - it('invalid body', () => { |
| 92 | + it('invalid body', async () => { |
91 | 93 | // Sign a valid request and verify with an invalid body
|
92 |
| - const signedRequest = signRequestWithKeyPair( |
| 94 | + const signedRequest = await signRequestWithKeyPair( |
93 | 95 | pingRequestBody,
|
94 |
| - validKeyPair.secretKey, |
95 |
| - ); |
96 |
| - expect( |
97 |
| - verifyKey( |
98 |
| - 'example invalid body', |
99 |
| - signedRequest.signature, |
100 |
| - signedRequest.timestamp, |
101 |
| - validKeyPair.publicKey, |
102 |
| - ), |
103 |
| - ).toBe(false); |
| 96 | + validKeyPair.privateKey, |
| 97 | + ); |
| 98 | + const isValid = await verifyKey( |
| 99 | + 'example invalid body', |
| 100 | + signedRequest.signature, |
| 101 | + signedRequest.timestamp, |
| 102 | + validKeyPair.publicKey, |
| 103 | + ); |
| 104 | + expect(isValid).toBe(false); |
104 | 105 | });
|
105 | 106 |
|
106 |
| - it('invalid signature', () => { |
| 107 | + it('invalid signature', async () => { |
107 | 108 | // Sign a valid request and verify with an invalid signature
|
108 |
| - const signedRequest = signRequestWithKeyPair( |
| 109 | + const signedRequest = await signRequestWithKeyPair( |
109 | 110 | pingRequestBody,
|
110 |
| - validKeyPair.secretKey, |
111 |
| - ); |
112 |
| - expect( |
113 |
| - verifyKey( |
114 |
| - signedRequest.body, |
115 |
| - 'example invalid signature', |
116 |
| - signedRequest.timestamp, |
117 |
| - validKeyPair.publicKey, |
118 |
| - ), |
119 |
| - ).toBe(false); |
| 111 | + validKeyPair.privateKey, |
| 112 | + ); |
| 113 | + const isValid = await verifyKey( |
| 114 | + signedRequest.body, |
| 115 | + 'example invalid signature', |
| 116 | + signedRequest.timestamp, |
| 117 | + validKeyPair.publicKey, |
| 118 | + ); |
| 119 | + expect(isValid).toBe(false); |
120 | 120 | });
|
121 | 121 |
|
122 |
| - it('invalid timestamp', () => { |
| 122 | + it('invalid timestamp', async () => { |
123 | 123 | // Sign a valid request and verify with an invalid timestamp
|
124 |
| - const signedRequest = signRequestWithKeyPair( |
| 124 | + const signedRequest = await signRequestWithKeyPair( |
125 | 125 | pingRequestBody,
|
126 |
| - validKeyPair.secretKey, |
127 |
| - ); |
128 |
| - expect( |
129 |
| - verifyKey( |
130 |
| - 'example invalid body', |
131 |
| - signedRequest.signature, |
132 |
| - String(Math.round(new Date().getTime() / 1000) - 10000), |
133 |
| - validKeyPair.publicKey, |
134 |
| - ), |
135 |
| - ).toBe(false); |
| 126 | + validKeyPair.privateKey, |
| 127 | + ); |
| 128 | + const isValid = await verifyKey( |
| 129 | + 'example invalid body', |
| 130 | + signedRequest.signature, |
| 131 | + String(Math.round(new Date().getTime() / 1000) - 10000), |
| 132 | + validKeyPair.publicKey, |
| 133 | + ); |
| 134 | + expect(isValid).toBe(false); |
136 | 135 | });
|
137 | 136 | });
|
0 commit comments