@@ -19,39 +19,49 @@ import {
1919 privateVerif ,
2020 publicVerif ,
2121} from '../src/index.js' ;
22- const { Client : Type1Client } = privateVerif ;
23- const { BlindRSAMode, Client : Type2Client } = publicVerif ;
2422
2523import { keysFromVector as type2KeysFromVector } from './pub_verif_token.js' ;
26- import { hexToUint8 , testSerialize , uint8ToHex } from './util.js' ;
24+ import { hexToUint8 , uint8ToHex } from './util.js' ;
2725
2826// https://github.com/cloudflare/pat-go/blob/main/tokens/batched/batched-issuance-test-vectors.json
2927import vectorsGo from './test_data/generic_batched_tokens_v6_go.json' ;
3028// https://raw.githubusercontent.com/raphaelrobert/privacypass/0600835c039c4b89f2137be3f5b1ecbeffe05417/tests/kat_vectors/generic_rs.json
3129import vectorsRust from './test_data/generic_batched_tokens_v6_rs.json' ;
3230
3331const vectors = [ ...vectorsGo , ...vectorsRust ] ;
32+ console . log ( 'NUMBEROFVECTORS' , vectors . length ) ;
3433type Vectors = ( typeof vectors ) [ number ] ;
3534
3635const SUPPORTED_TYPES = [ TOKEN_TYPES . VOPRF . value , TOKEN_TYPES . BLIND_RSA . value ] . map ( ( t ) =>
3736 t . toString ( ) . padStart ( 4 , '0' ) ,
3837) ;
3938
39+ const token_type = ( i : unknown ) : string => {
40+ if ( typeof i !== 'object' || i === null ) {
41+ throw new Error ( 'unsupported' ) ;
42+ }
43+ if ( 'type' in i && typeof i . type === 'string' ) {
44+ return i . type ;
45+ } else if ( 'token_type' in i && typeof i . token_type === 'string' ) {
46+ return i . token_type ;
47+ } else {
48+ throw new Error ( 'unsupported' ) ;
49+ }
50+ } ;
51+
4052describe . each ( vectors ) ( 'GenericBatched-Vector-%#' , ( v : Vectors ) => {
4153 const params = [ [ ] , [ { supportsRSARAW : true } ] ] ;
4254
4355 test . each ( params ) ( 'GenericBatched-Vector-%#-Issuer-Params-%#' , async ( ...params ) => {
4456 // if the test vector contains an unsupported type, skip the test
45- if ( v . issuance . find ( ( i ) => ! SUPPORTED_TYPES . includes ( i . type ) ) !== undefined ) {
46- expect ( true ) . toBe ( true ) ;
47- return ;
48- }
57+ console . log ( v . issuance . map ( token_type ) ) ;
58+ expect ( v . issuance . every ( ( i ) => SUPPORTED_TYPES . includes ( token_type ( i ) ) ) ) . toBe ( true ) ;
4959 const tokenRequests = new Array < TokenRequest > ( v . issuance . length ) ;
5060 const issuers = new Array < privateVerif . Issuer | publicVerif . Issuer > ( v . issuance . length ) ;
5161 const clients = new Array < privateVerif . Client | publicVerif . Client > ( v . issuance . length ) ;
5262 for ( let i = 0 ; i < v . issuance . length ; i += 1 ) {
5363 const issuance = v . issuance [ i ] ;
54- const type = Number . parseInt ( issuance . type ) ;
64+ const type = Number . parseInt ( token_type ( issuance ) ) ;
5565
5666 const nonce = hexToUint8 ( issuance . nonce ) ;
5767 const blind = hexToUint8 ( issuance . blind ) ;
@@ -68,7 +78,7 @@ describe.each(vectors)('GenericBatched-Vector-%#', (v: Vectors) => {
6878 Promise . resolve ( TOKEN_TYPES . VOPRF . group . desScalar ( blind ) ) ,
6979 ) ;
7080
71- const client = new Type1Client ( ) ;
81+ const client = new privateVerif . Client ( ) ;
7282 clients [ i ] = client ;
7383 const tokReq = await client . createTokenRequest ( tokChl , publicKey ) ;
7484 tokenRequests [ i ] = new TokenRequest ( tokReq ) ;
@@ -82,21 +92,21 @@ describe.each(vectors)('GenericBatched-Vector-%#', (v: Vectors) => {
8292 break ;
8393 }
8494 case TOKEN_TYPES . BLIND_RSA . value : {
85- if ( issuance . salt === undefined ) {
95+ if ( issuance . salt === undefined || issuance . salt === null ) {
8696 throw new Error ( 'invalid test vector' ) ;
8797 }
8898 const salt = hexToUint8 ( issuance . salt ) ;
8999 const mode =
90- salt . length == ( BlindRSAMode . PSS as number )
91- ? BlindRSAMode . PSS
92- : BlindRSAMode . PSSZero ;
100+ salt . length == ( publicVerif . BlindRSAMode . PSS as number )
101+ ? publicVerif . BlindRSAMode . PSS
102+ : publicVerif . BlindRSAMode . PSSZero ;
93103
94104 vi . spyOn ( crypto , 'getRandomValues' )
95105 . mockReturnValueOnce ( nonce )
96106 . mockReturnValueOnce ( salt )
97107 . mockReturnValueOnce ( blind ) ;
98108
99- const client = new Type2Client ( mode ) ;
109+ const client = new publicVerif . Client ( mode ) ;
100110 clients [ i ] = client ;
101111 const [ { privateKey, publicKey } , publicKeyEnc ] =
102112 await type2KeysFromVector ( issuance ) ;
@@ -124,34 +134,40 @@ describe.each(vectors)('GenericBatched-Vector-%#', (v: Vectors) => {
124134
125135 const issuer = new Issuer ( ...issuers ) ;
126136
137+ console . log ( tokReq ) ;
127138 const tokRes = await issuer . issue ( tokReq ) ;
128- testSerialize ( GenericBatchTokenResponse , tokRes ) ;
139+ const bytes = tokRes . serialize ( ) ;
140+ const got = GenericBatchTokenResponse . deserialize (
141+ bytes ,
142+ tokReq . tokenRequests . map ( ( r ) => r . tokenType ) as ( 1 | 2 ) [ ] ,
143+ ) ;
144+ expect ( got ) . toStrictEqual ( tokRes ) ;
145+ console . log ( tokRes ) ;
129146
130147 for ( let i = 0 ; i < v . issuance . length ; i += 1 ) {
131148 const issuance = v . issuance [ i ] ;
132149 const res = tokRes . tokenResponses [ i ] ;
133- const type = Number . parseInt ( issuance . type ) ;
150+ const type = Number . parseInt ( token_type ( issuance ) ) ;
134151
135152 let token : Token ;
136153 switch ( type ) {
137154 case TOKEN_TYPES . VOPRF . value : {
138155 const client = clients [ i ] as privateVerif . Client ;
139- const rawTokenResponse = res . tokenResponse ;
140- if ( rawTokenResponse === null ) {
156+ const tokenResponse = res . tokenResponse as privateVerif . TokenResponse | null ;
157+ if ( tokenResponse === null ) {
141158 throw new Error ( 'should not be null' ) ;
142159 }
143- const tokenResponse = privateVerif . TokenResponse . deserialize ( rawTokenResponse ) ;
144160 token = await client . finalize ( tokenResponse ) ;
145161 break ;
146162 }
147163 case TOKEN_TYPES . BLIND_RSA . value : {
148164 const client = clients [ i ] as publicVerif . Client ;
149- const rawTokenResponse = res . tokenResponse ;
150- if ( rawTokenResponse === null ) {
165+ const tokenResponse = res . tokenResponse ;
166+ if ( tokenResponse === null ) {
151167 throw new Error ( 'should not be null' ) ;
152168 }
153- const tokenResponse = publicVerif . TokenResponse . deserialize ( rawTokenResponse ) ;
154- token = await client . finalize ( tokenResponse ) ;
169+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
170+ token = await client . finalize ( tokenResponse as publicVerif . TokenResponse ) ;
155171 break ;
156172 }
157173 default :
0 commit comments