|
| 1 | +/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { |
| 4 | + AuthHandler, |
| 5 | + AuthorizationRequestMessage, |
| 6 | + CircuitId, |
| 7 | + CredentialRequest, |
| 8 | + CredentialStatusResolverRegistry, |
| 9 | + CredentialStatusType, |
| 10 | + CredentialWallet, |
| 11 | + IPackageManager, |
| 12 | + IdentityWallet, |
| 13 | + PROTOCOL_CONSTANTS, |
| 14 | + PackageManager, |
| 15 | + PlainPacker, |
| 16 | + ProofService, |
| 17 | + RHSResolver, |
| 18 | + byteEncoder |
| 19 | +} from '../../src'; |
| 20 | +import { MessageHandler } from '../../src/iden3comm/handlers/message-handler'; |
| 21 | +import { DID } from '@iden3/js-iden3-core'; |
| 22 | +import { |
| 23 | + MOCK_STATE_STORAGE, |
| 24 | + RHS_URL, |
| 25 | + SEED_USER, |
| 26 | + createIdentity, |
| 27 | + getInMemoryDataStorage, |
| 28 | + registerBJJIntoInMemoryKMS |
| 29 | +} from '../helpers'; |
| 30 | +import { randomUUID } from 'crypto'; |
| 31 | + |
| 32 | +describe('MessageHandler', () => { |
| 33 | + it('should throw invalid handle messages', async () => { |
| 34 | + const messageHandler = new MessageHandler({ |
| 35 | + messageHandlers: [], |
| 36 | + packageManager: {} as IPackageManager |
| 37 | + }); |
| 38 | + |
| 39 | + expect(messageHandler.handleMessage(new Uint8Array(), { did: new DID() })).to.be.rejectedWith( |
| 40 | + 'Message handler not provided' |
| 41 | + ); |
| 42 | + |
| 43 | + expect( |
| 44 | + messageHandler.handleMessage(byteEncoder.encode('{"type":"other-type"}'), { did: new DID() }) |
| 45 | + ).to.be.rejectedWith('Message handler not provided'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should handle auth req/resp messages', async () => { |
| 49 | + const kms = registerBJJIntoInMemoryKMS(); |
| 50 | + const dataStorage = getInMemoryDataStorage(MOCK_STATE_STORAGE); |
| 51 | + |
| 52 | + const resolvers = new CredentialStatusResolverRegistry(); |
| 53 | + resolvers.register( |
| 54 | + CredentialStatusType.Iden3ReverseSparseMerkleTreeProof, |
| 55 | + new RHSResolver(dataStorage.states) |
| 56 | + ); |
| 57 | + const credWallet = new CredentialWallet(dataStorage, resolvers); |
| 58 | + const idWallet = new IdentityWallet(kms, dataStorage, credWallet); |
| 59 | + |
| 60 | + const proofService = { |
| 61 | + generateProof: async () => { |
| 62 | + return { |
| 63 | + id: 1, |
| 64 | + circuitId: 'credentialAtomicQuerySigV2', |
| 65 | + proof: { |
| 66 | + pi_a: [ |
| 67 | + '6090318505987607359116982693709498160512347822060118450158275035797244829466', |
| 68 | + '10624590653441467100050235454358781645745036858579539447945227388470932932596', |
| 69 | + '1' |
| 70 | + ], |
| 71 | + pi_b: [ |
| 72 | + [ |
| 73 | + '15579599033734000552482480659488902457490303622893675003352684240718764814125', |
| 74 | + '19853179897655798140882806183640277943881334163549025939133177045611827693142' |
| 75 | + ], |
| 76 | + [ |
| 77 | + '17956252054758699988777560724871589999140996895310716217662917525735985669808', |
| 78 | + '4167679433625239489900435862379953507102203954849993146650986670022269249242' |
| 79 | + ], |
| 80 | + ['1', '0'] |
| 81 | + ], |
| 82 | + pi_c: [ |
| 83 | + '15357965579564127675469843845487444552040169552941561049967346379131096661554', |
| 84 | + '6737190440198712400571200710355589533896053379119710658028067695173227485133', |
| 85 | + '1' |
| 86 | + ], |
| 87 | + protocol: 'groth16', |
| 88 | + curve: 'bn128' |
| 89 | + }, |
| 90 | + pub_signals: [ |
| 91 | + '0', |
| 92 | + '21575127216236248869702276246037557119007466180301957762196593786733007617', |
| 93 | + '4487386332479489158003597844990487984925471813907462483907054425759564175341', |
| 94 | + '1', |
| 95 | + '25198543381200665770805816046271594885604002445105767653616878167826895617', |
| 96 | + '1', |
| 97 | + '4487386332479489158003597844990487984925471813907462483907054425759564175341', |
| 98 | + '1712602611', |
| 99 | + '198285726510688200335207273836123338699', |
| 100 | + '1', |
| 101 | + '0', |
| 102 | + '3', |
| 103 | + '1', |
| 104 | + '99', |
| 105 | + '0', |
| 106 | + '0', |
| 107 | + '0', |
| 108 | + '0', |
| 109 | + '0', |
| 110 | + '0', |
| 111 | + '0', |
| 112 | + '0', |
| 113 | + '0', |
| 114 | + '0', |
| 115 | + '0', |
| 116 | + '0', |
| 117 | + '0', |
| 118 | + '0', |
| 119 | + '0', |
| 120 | + '0', |
| 121 | + '0', |
| 122 | + '0', |
| 123 | + '0', |
| 124 | + '0', |
| 125 | + '0', |
| 126 | + '0', |
| 127 | + '0', |
| 128 | + '0', |
| 129 | + '0', |
| 130 | + '0', |
| 131 | + '0', |
| 132 | + '0', |
| 133 | + '0', |
| 134 | + '0', |
| 135 | + '0', |
| 136 | + '0', |
| 137 | + '0', |
| 138 | + '0', |
| 139 | + '0', |
| 140 | + '0', |
| 141 | + '0', |
| 142 | + '0', |
| 143 | + '0', |
| 144 | + '0', |
| 145 | + '0', |
| 146 | + '0', |
| 147 | + '0', |
| 148 | + '0', |
| 149 | + '0', |
| 150 | + '0', |
| 151 | + '0', |
| 152 | + '0', |
| 153 | + '0', |
| 154 | + '0', |
| 155 | + '0', |
| 156 | + '0', |
| 157 | + '0', |
| 158 | + '0', |
| 159 | + '0', |
| 160 | + '0', |
| 161 | + '0', |
| 162 | + '0', |
| 163 | + '0', |
| 164 | + '0', |
| 165 | + '0', |
| 166 | + '0', |
| 167 | + '0' |
| 168 | + ] |
| 169 | + }; |
| 170 | + }, |
| 171 | + verifyZKPResponse: () => Promise.resolve({}) |
| 172 | + } as unknown as ProofService; |
| 173 | + |
| 174 | + const packageMgr = new PackageManager(); |
| 175 | + packageMgr.registerPackers([new PlainPacker()]); |
| 176 | + |
| 177 | + const authHandler = new AuthHandler(packageMgr, proofService); |
| 178 | + |
| 179 | + const { did: userDID, credential: userAuthCredential } = await createIdentity(idWallet, { |
| 180 | + seed: SEED_USER |
| 181 | + }); |
| 182 | + |
| 183 | + expect(userAuthCredential).not.to.be.undefined; |
| 184 | + |
| 185 | + const { did: issuerDID, credential: issuerAuthCredential } = await createIdentity(idWallet); |
| 186 | + expect(issuerAuthCredential).not.to.be.undefined; |
| 187 | + |
| 188 | + const messageHandler = new MessageHandler({ |
| 189 | + messageHandlers: [authHandler], |
| 190 | + packageManager: packageMgr |
| 191 | + }); |
| 192 | + |
| 193 | + const claimReq: CredentialRequest = { |
| 194 | + credentialSchema: |
| 195 | + 'https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json/kyc-nonmerklized.json', |
| 196 | + type: 'KYCAgeCredential', |
| 197 | + credentialSubject: { |
| 198 | + id: userDID.string(), |
| 199 | + birthday: 19960424, |
| 200 | + documentType: 99 |
| 201 | + }, |
| 202 | + expiration: 2793526400, |
| 203 | + revocationOpts: { |
| 204 | + type: CredentialStatusType.Iden3ReverseSparseMerkleTreeProof, |
| 205 | + id: RHS_URL |
| 206 | + } |
| 207 | + }; |
| 208 | + const issuerCred = await idWallet.issueCredential(issuerDID, claimReq); |
| 209 | + |
| 210 | + await credWallet.save(issuerCred); |
| 211 | + |
| 212 | + const id = randomUUID(); |
| 213 | + const authReq: AuthorizationRequestMessage = { |
| 214 | + id, |
| 215 | + typ: PROTOCOL_CONSTANTS.MediaType.PlainMessage, |
| 216 | + type: PROTOCOL_CONSTANTS.PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_REQUEST_MESSAGE_TYPE, |
| 217 | + thid: id, |
| 218 | + body: { |
| 219 | + callbackUrl: 'http://localhost:8080/callback?id=1234442-123123-123123', |
| 220 | + reason: 'reason', |
| 221 | + message: 'mesage', |
| 222 | + did_doc: {}, |
| 223 | + scope: [ |
| 224 | + { |
| 225 | + id: 1, |
| 226 | + circuitId: CircuitId.AtomicQuerySigV2, |
| 227 | + optional: false, |
| 228 | + query: { |
| 229 | + allowedIssuers: ['*'], |
| 230 | + type: claimReq.type, |
| 231 | + context: |
| 232 | + 'https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json-ld/kyc-nonmerklized.jsonld', |
| 233 | + credentialSubject: { |
| 234 | + documentType: { |
| 235 | + $eq: 99 |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + ] |
| 241 | + }, |
| 242 | + from: issuerDID.string() |
| 243 | + }; |
| 244 | + |
| 245 | + const msgBytes = await packageMgr.packMessage( |
| 246 | + PROTOCOL_CONSTANTS.MediaType.PlainMessage, |
| 247 | + authReq, |
| 248 | + {} |
| 249 | + ); |
| 250 | + |
| 251 | + const authResp = await messageHandler.handleMessage(msgBytes, { |
| 252 | + did: issuerDID |
| 253 | + }); |
| 254 | + |
| 255 | + expect(authResp).not.to.be.null; |
| 256 | + |
| 257 | + const { unpackedMessage: authRespMsg } = await packageMgr.unpack(authResp!); |
| 258 | + |
| 259 | + expect(authRespMsg.type).to.be.eq( |
| 260 | + PROTOCOL_CONSTANTS.PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_RESPONSE_MESSAGE_TYPE |
| 261 | + ); |
| 262 | + |
| 263 | + await messageHandler.handleMessage(authResp!, { |
| 264 | + request: authReq |
| 265 | + }); |
| 266 | + }); |
| 267 | +}); |
0 commit comments