22//
33// SPDX-License-Identifier: Apache-2.0
44
5- import { verifyEnvEncryptPublicKey } from '../verify-env-encrypt-public-key'
6- import { describe , it , expect } from 'vitest'
5+ import { verifyEnvEncryptPublicKey , verifyEnvEncryptPublicKeyLegacy } from '../verify-env-encrypt-public-key'
6+ import { describe , it , expect , vi , beforeEach , afterEach } from 'vitest'
77
8- describe ( 'verifySignature ' , ( ) => {
8+ describe ( 'verifyEnvEncryptPublicKeyLegacy ' , ( ) => {
99 it ( 'should verify signature correctly with example data' , ( ) => {
1010 const publicKey = new Uint8Array ( Buffer . from ( 'e33a1832c6562067ff8f844a61e51ad051f1180b66ec2551fb0251735f3ee90a' , 'hex' ) )
1111 const signature = new Uint8Array ( Buffer . from ( '8542c49081fbf4e03f62034f13fbf70630bdf256a53032e38465a27c36fd6bed7a5e7111652004aef37f7fd92fbfc1285212c4ae6a6154203a48f5e16cad2cef00' , 'hex' ) )
1212 const appId = '00' . repeat ( 20 )
13-
14- const result = verifyEnvEncryptPublicKey ( publicKey , signature , appId )
15-
13+
14+ const result = verifyEnvEncryptPublicKeyLegacy ( publicKey , signature , appId )
15+
1616 expect ( result ) . toBe ( '0x0217610d74cbd39b6143842c6d8bc310d79da1d82cc9d17f8876376221eda0c38f' )
1717 } )
1818
1919 it ( 'should handle 0x prefix in app_id' , ( ) => {
2020 const publicKey = new Uint8Array ( Buffer . from ( 'e33a1832c6562067ff8f844a61e51ad051f1180b66ec2551fb0251735f3ee90a' , 'hex' ) )
2121 const signature = new Uint8Array ( Buffer . from ( '8542c49081fbf4e03f62034f13fbf70630bdf256a53032e38465a27c36fd6bed7a5e7111652004aef37f7fd92fbfc1285212c4ae6a6154203a48f5e16cad2cef00' , 'hex' ) )
2222 const appId = '0x' + '00' . repeat ( 20 )
23-
24- const result = verifyEnvEncryptPublicKey ( publicKey , signature , appId )
25-
23+
24+ const result = verifyEnvEncryptPublicKeyLegacy ( publicKey , signature , appId )
25+
2626 expect ( result ) . toBe ( '0x0217610d74cbd39b6143842c6d8bc310d79da1d82cc9d17f8876376221eda0c38f' )
2727 } )
2828
2929 it ( 'should return null for invalid signature length' , ( ) => {
3030 const publicKey = new Uint8Array ( 32 )
3131 const signature = new Uint8Array ( 64 ) // Wrong length
3232 const appId = '00' . repeat ( 20 )
33-
34- const result = verifyEnvEncryptPublicKey ( publicKey , signature , appId )
35-
33+
34+ const result = verifyEnvEncryptPublicKeyLegacy ( publicKey , signature , appId )
35+
3636 expect ( result ) . toBeNull ( )
3737 } )
3838
3939 it ( 'should return null for invalid signature data' , ( ) => {
4040 const publicKey = new Uint8Array ( 32 )
4141 const signature = new Uint8Array ( 65 ) // All zeros
4242 const appId = '00' . repeat ( 20 )
43-
44- const result = verifyEnvEncryptPublicKey ( publicKey , signature , appId )
45-
43+
44+ const result = verifyEnvEncryptPublicKeyLegacy ( publicKey , signature , appId )
45+
46+ expect ( result ) . toBeNull ( )
47+ } )
48+ } )
49+
50+ describe ( 'verifyEnvEncryptPublicKey with timestamp' , ( ) => {
51+ beforeEach ( ( ) => {
52+ // Mock Date.now to return a fixed timestamp for testing
53+ vi . useFakeTimers ( )
54+ vi . setSystemTime ( new Date ( '2024-01-15T12:00:00Z' ) )
55+ } )
56+
57+ afterEach ( ( ) => {
58+ vi . useRealTimers ( )
59+ } )
60+
61+ it ( 'should return null for invalid signature length' , ( ) => {
62+ const publicKey = new Uint8Array ( 32 )
63+ const signature = new Uint8Array ( 64 ) // Wrong length
64+ const appId = '00' . repeat ( 20 )
65+ const timestamp = BigInt ( Math . floor ( Date . now ( ) / 1000 ) )
66+
67+ const result = verifyEnvEncryptPublicKey ( publicKey , signature , appId , timestamp )
68+
69+ expect ( result ) . toBeNull ( )
70+ } )
71+
72+ it ( 'should return null for stale timestamp' , ( ) => {
73+ const publicKey = new Uint8Array ( 32 )
74+ const signature = new Uint8Array ( 65 )
75+ const appId = '00' . repeat ( 20 )
76+ // Timestamp from 10 minutes ago (600 seconds)
77+ const timestamp = BigInt ( Math . floor ( Date . now ( ) / 1000 ) ) - 600n
78+
79+ const result = verifyEnvEncryptPublicKey ( publicKey , signature , appId , timestamp )
80+
81+ expect ( result ) . toBeNull ( )
82+ } )
83+
84+ it ( 'should return null for timestamp too far in the future' , ( ) => {
85+ const publicKey = new Uint8Array ( 32 )
86+ const signature = new Uint8Array ( 65 )
87+ const appId = '00' . repeat ( 20 )
88+ // Timestamp 2 minutes in the future
89+ const timestamp = BigInt ( Math . floor ( Date . now ( ) / 1000 ) ) + 120n
90+
91+ const result = verifyEnvEncryptPublicKey ( publicKey , signature , appId , timestamp )
92+
93+ expect ( result ) . toBeNull ( )
94+ } )
95+
96+ it ( 'should accept timestamp within allowed clock skew (future)' , ( ) => {
97+ const publicKey = new Uint8Array ( 32 )
98+ const signature = new Uint8Array ( 65 ) // Invalid signature, but we're testing timestamp check
99+ const appId = '00' . repeat ( 20 )
100+ // Timestamp 30 seconds in the future (within 60s skew)
101+ const timestamp = BigInt ( Math . floor ( Date . now ( ) / 1000 ) ) + 30n
102+
103+ // Will return null due to invalid signature, not timestamp
104+ const result = verifyEnvEncryptPublicKey ( publicKey , signature , appId , timestamp )
105+
106+ // The function should not reject due to timestamp, but will fail signature verification
107+ expect ( result ) . toBeNull ( )
108+ } )
109+
110+ it ( 'should accept custom maxAgeSeconds option' , ( ) => {
111+ const publicKey = new Uint8Array ( 32 )
112+ const signature = new Uint8Array ( 65 )
113+ const appId = '00' . repeat ( 20 )
114+ // Timestamp from 400 seconds ago (would fail default 300s, but pass 600s)
115+ const timestamp = BigInt ( Math . floor ( Date . now ( ) / 1000 ) ) - 400n
116+
117+ // With default maxAge (300s), this should fail due to stale timestamp
118+ const result1 = verifyEnvEncryptPublicKey ( publicKey , signature , appId , timestamp )
119+ expect ( result1 ) . toBeNull ( )
120+
121+ // With extended maxAge (600s), it would pass timestamp check but fail signature
122+ const result2 = verifyEnvEncryptPublicKey ( publicKey , signature , appId , timestamp , { maxAgeSeconds : 600 } )
123+ // Still null due to invalid signature data, but the timestamp check passed
124+ expect ( result2 ) . toBeNull ( )
125+ } )
126+
127+ it ( 'should accept number timestamp' , ( ) => {
128+ const publicKey = new Uint8Array ( 32 )
129+ const signature = new Uint8Array ( 65 )
130+ const appId = '00' . repeat ( 20 )
131+ const timestamp = Math . floor ( Date . now ( ) / 1000 ) // number instead of bigint
132+
133+ // Will return null due to invalid signature, but should handle number timestamp
134+ const result = verifyEnvEncryptPublicKey ( publicKey , signature , appId , timestamp )
46135 expect ( result ) . toBeNull ( )
47136 } )
48- } )
137+ } )
0 commit comments