diff --git a/src/auth/auth.ts b/src/auth/auth.ts index ac56c22..289eea9 100644 --- a/src/auth/auth.ts +++ b/src/auth/auth.ts @@ -21,18 +21,13 @@ import { IStateStorage, ProofQuery, VerifyOpts, - CircuitId, - ProvingParams, - VerificationHandlerFunc, - VerificationParams, - ZKPPacker, - ZeroKnowledgeProofResponse + ZeroKnowledgeProofResponse, + IPackageManager } from '@0xpolygonid/js-sdk'; import { Resolvable } from 'did-resolver'; import { Options, DocumentLoader } from '@iden3/js-jsonld-merklization'; import path from 'path'; import { DID } from '@iden3/js-iden3-core'; -import { proving } from '@iden3/js-jwz'; /** * createAuthorizationRequest is a function to create protocol authorization request @@ -83,10 +78,10 @@ export function createAuthorizationRequestWithMessage( * options to verify the query */ export type VerifierParams = Options & { - /* resolvers for state of the identities */ - stateResolver: Resolvers; /* state storage for state of the identities */ stateStorage: IStateStorage; + /* package manager */ + packageManager: IPackageManager; /* didDocumentResolver to init default jws packer */ didDocumentResolver?: Resolvable; /* circuitsDir - directory where circuits files are stored (default - 'circuits') */ @@ -100,7 +95,6 @@ export type VerifierParams = Options & { */ export interface VerifierSuiteParams { documentLoader: DocumentLoader; - packageManager: PackageManager; circuitStorage: ICircuitStorage; prover: IZKProver; } @@ -113,11 +107,9 @@ export interface VerifierSuiteParams { */ export class Verifier { private schemaLoader: DocumentLoader; - private stateResolver: Resolvers; - private packageManager: PackageManager; + private packageManager: IPackageManager; private prover: IZKProver; - private circuitStorage: ICircuitStorage; private readonly _pubSignalsVerifier: PubSignalsVerifier; // private readonly _authHandler: AuthHandler; @@ -129,14 +121,12 @@ export class Verifier { * @param {VerifierSuiteParams} params - suite for verification */ private constructor( - stateResolver: Resolvers, stateStorage: IStateStorage, + packageManager: IPackageManager, params: VerifierSuiteParams ) { this.schemaLoader = params.documentLoader; - this.stateResolver = stateResolver; - this.packageManager = params.packageManager; - this.circuitStorage = params.circuitStorage; + this.packageManager = packageManager; this.prover = params.prover; this._pubSignalsVerifier = new PubSignalsVerifier(this.schemaLoader, stateStorage); @@ -156,14 +146,12 @@ export class Verifier { params.suite = { documentLoader, circuitStorage, - prover: new NativeProver(circuitStorage), - packageManager: new PackageManager() + prover: new NativeProver(circuitStorage) }; - const verifier = new Verifier(params.stateResolver, params.stateStorage, params.suite); - await verifier.initPackers(params.didDocumentResolver); + const verifier = new Verifier(params.stateStorage, params.packageManager, params.suite); return verifier; } - return new Verifier(params.stateResolver, params.stateStorage, params.suite); + return new Verifier(params.stateStorage, params.packageManager, params.suite); } // setPackageManager sets the package manager for the Verifier. @@ -176,45 +164,6 @@ export class Verifier { return this.packageManager.registerPackers([packer]); } - // setupAuthV2ZKPPacker sets the custom packer manager for the Verifier. - public async setupAuthV2ZKPPacker(circuitStorage: ICircuitStorage) { - if (!circuitStorage) { - throw new Error('circuit storage is not defined'); - } - const authV2Set = await circuitStorage.loadCircuitData(CircuitId.AuthV2); - - if (!authV2Set.verificationKey) { - throw new Error('verification key is not for authv2 circuit'); - } - const mapKey = proving.provingMethodGroth16AuthV2Instance.methodAlg.toString(); - const provingParamMap: Map = new Map(); - - const stateVerificationFn = async ( - circuitId: string, - pubSignals: Array - ): Promise => { - if (circuitId !== CircuitId.AuthV2) { - throw new Error(`CircuitId is not supported ${circuitId}`); - } - // const verifier = new AuthPubSignalsV2(pubSignals); - // await verifier.verifyStates(this.stateResolver); - - // await this._pubSignalsVerifier.verify(circuitId, { pubSignals, query: {}, sender: '', challenge: 0n }); - return true; - }; - - const verificationFn = new VerificationHandlerFunc(stateVerificationFn); - - const verificationParamMap: Map = new Map(); - verificationParamMap.set(mapKey, { - key: authV2Set.verificationKey, - verificationFn - }); - - const zkpPacker = new ZKPPacker(provingParamMap, verificationParamMap); - return this.setPacker(zkpPacker); - } - // setupJWSPacker sets the JWS packer for the Verifier. public setupJWSPacker(kms: KMS, documentResolver: Resolvable) { const jwsPacker = new JWSPacker(kms, documentResolver); @@ -347,42 +296,6 @@ export class Verifier { } } - // /** - // * verifies jwz token - // * @public - // * @param {string} tokenStr - token string - // * @param {VerifyOpts} opts - verification options - // * - // * @returns `Promise` - // */ - // public async verifyJWZ(tokenStr: string, opts?: VerifyOpts): Promise { - // const token = await Token.parse(tokenStr); - // const key = (await this.circuitStorage.loadCircuitData(token.circuitId as CircuitId)) - // .verificationKey; - // if (!key) { - // throw new Error(`verification key is not found for circuit ${token.circuitId}`); - // } - - // const isValid = await token.verify(key); - // if (!isValid) { - // throw new Error(`zero-knowledge proof of jwz token is not valid`); - // } - - // const CircuitVerifier = Circuits.getCircuitPubSignals(token.circuitId); - - // if (!CircuitVerifier) { - // throw new Error(`circuit ${token.circuitId} is not supported by the library`); - // } - - // // outputs unmarshaller - // const verifier = new CircuitVerifier(token.zkProof.pub_signals); - - // // state verification - // await verifier.verifyStates(this.stateResolver, opts); - - // return token; - // } - /** * perform both verification of jwz / jws token and authorization request message * @public @@ -402,12 +315,4 @@ export class Verifier { await this.verifyAuthResponse(response, request, opts); return response; } - - private async initPackers(didResolver?: Resolvable) { - await this.setupAuthV2ZKPPacker(this.circuitStorage); - // set default jws packer if packageManager is not present in options but did document resolver is. - if (didResolver) { - this.setupJWSPacker(new KMS(), didResolver); - } - } } diff --git a/test/atomicV3.test.ts b/test/atomicV3.test.ts index 742a99e..9fe0f6a 100644 --- a/test/atomicV3.test.ts +++ b/test/atomicV3.test.ts @@ -1,19 +1,64 @@ import { Verifier } from '@lib/auth/auth'; -import { testOpts, resolvers, MOCK_STATE_STORAGE } from './mocks'; +import { + testOpts, + resolvers, + MOCK_STATE_STORAGE, + getPackageMgr, + registerBJJIntoInMemoryKMS, + getInMemoryDataStorage, + schemaLoader +} from './mocks'; import path from 'path'; import { AuthorizationResponseMessage, PROTOCOL_CONSTANTS, AuthorizationRequestMessage, - cacheLoader, - CircuitId + IPackageManager, + CircuitId, + IDataStorage, + IdentityWallet, + CredentialWallet, + ProofService, + CredentialStatusResolverRegistry, + CredentialStatusType, + RHSResolver, + FSCircuitStorage } from '@0xpolygonid/js-sdk'; -import { DocumentLoader } from '@iden3/js-jsonld-merklization'; -const schemaLoader: DocumentLoader = cacheLoader({ - ipfsNodeURL: process.env.IPFS_URL ?? 'https://ipfs.io' -}); describe('atomicV3', () => { + let packageMgr: IPackageManager; + let dataStorage: IDataStorage; + let idWallet: IdentityWallet; + let credWallet: CredentialWallet; + let proofService: ProofService; + + beforeEach(async () => { + const kms = registerBJJIntoInMemoryKMS(); + dataStorage = getInMemoryDataStorage(MOCK_STATE_STORAGE); + const circuitStorage = new FSCircuitStorage({ + dirname: path.join(__dirname, './testdata') + }); + + const resolvers = new CredentialStatusResolverRegistry(); + resolvers.register( + CredentialStatusType.Iden3ReverseSparseMerkleTreeProof, + new RHSResolver(dataStorage.states) + ); + + credWallet = new CredentialWallet(dataStorage, resolvers); + idWallet = new IdentityWallet(kms, dataStorage, credWallet); + + proofService = new ProofService(idWallet, credWallet, circuitStorage, MOCK_STATE_STORAGE, { + documentLoader: schemaLoader + }); + + packageMgr = await getPackageMgr( + await circuitStorage.loadCircuitData(CircuitId.AuthV2), + proofService.generateAuthV2Inputs.bind(proofService), + () => Promise.resolve(true) + ); + }); + it('TestVerifyV3MessageWithSigProof_NonMerklized', async () => { const request: AuthorizationRequestMessage = { id: '28b15cd4-3aa1-4ddc-88a3-c05a0f788065', @@ -172,7 +217,7 @@ describe('atomicV3', () => { }; const authInstance = await Verifier.newVerifier({ - stateResolver: resolvers, + packageManager: packageMgr, stateStorage: MOCK_STATE_STORAGE, circuitsDir: path.join(__dirname, './testdata') }); @@ -214,7 +259,7 @@ describe('atomicV3', () => { ); const authInstance = await Verifier.newVerifier({ - stateResolver: resolvers, + packageManager: packageMgr, stateStorage: MOCK_STATE_STORAGE, circuitsDir: path.join(__dirname, './testdata') }); @@ -279,7 +324,7 @@ describe('atomicV3', () => { }; const verifier = await Verifier.newVerifier({ - stateResolver: resolvers, + packageManager: packageMgr, stateStorage: MOCK_STATE_STORAGE, circuitsDir: path.join(__dirname, './testdata'), documentLoader: schemaLoader diff --git a/test/auth.test.ts b/test/auth.test.ts index a1c7081..22b2016 100644 --- a/test/auth.test.ts +++ b/test/auth.test.ts @@ -7,9 +7,17 @@ import { KMS, NativeProver, PROTOCOL_CONSTANTS, - PackageManager, ZeroKnowledgeProofRequest, - ZeroKnowledgeProofResponse + ZeroKnowledgeProofResponse, + IPackageManager, + CircuitId, + IDataStorage, + IdentityWallet, + CredentialWallet, + ProofService, + CredentialStatusResolverRegistry, + CredentialStatusType, + RHSResolver } from '@0xpolygonid/js-sdk'; import { createAuthorizationRequest, @@ -17,9 +25,50 @@ import { Verifier } from '@lib/auth/auth'; import path from 'path'; -import { MOCK_STATE_STORAGE, resolveDIDDocument, resolvers, schemaLoader, testOpts } from './mocks'; +import { + getInMemoryDataStorage, + getPackageMgr, + MOCK_STATE_STORAGE, + registerBJJIntoInMemoryKMS, + resolveDIDDocument, + resolvers, + schemaLoader, + testOpts +} from './mocks'; describe('auth tests', () => { + let packageMgr: IPackageManager; + let dataStorage: IDataStorage; + let idWallet: IdentityWallet; + let credWallet: CredentialWallet; + let proofService: ProofService; + + beforeEach(async () => { + const kms = registerBJJIntoInMemoryKMS(); + dataStorage = getInMemoryDataStorage(MOCK_STATE_STORAGE); + const circuitStorage = new FSCircuitStorage({ + dirname: path.join(__dirname, './testdata') + }); + + const resolvers = new CredentialStatusResolverRegistry(); + resolvers.register( + CredentialStatusType.Iden3ReverseSparseMerkleTreeProof, + new RHSResolver(dataStorage.states) + ); + + credWallet = new CredentialWallet(dataStorage, resolvers); + idWallet = new IdentityWallet(kms, dataStorage, credWallet); + + proofService = new ProofService(idWallet, credWallet, circuitStorage, MOCK_STATE_STORAGE, { + documentLoader: schemaLoader + }); + + packageMgr = await getPackageMgr( + await circuitStorage.loadCircuitData(CircuitId.AuthV2), + proofService.generateAuthV2Inputs.bind(proofService), + () => Promise.resolve(true) + ); + }); it('createAuthorizationRequest', () => { const sender = '1125GJqgw6YEsKFwj63GY87MMxPL9kwDKxPUiwMLNZ'; const callback = 'https://test.com/callback'; @@ -78,12 +127,11 @@ describe('auth tests', () => { }; const verifier = await Verifier.newVerifier({ - stateResolver: resolvers, stateStorage: MOCK_STATE_STORAGE, + packageManager: packageMgr, suite: { prover: new NativeProver(new FSCircuitStorage({ dirname: '' })), circuitStorage: new FSCircuitStorage({ dirname: '../' }), - packageManager: new PackageManager(), documentLoader: schemaLoader } }); @@ -251,8 +299,8 @@ describe('auth tests', () => { }; const verifier = await Verifier.newVerifier({ - stateResolver: resolvers, stateStorage: MOCK_STATE_STORAGE, + packageManager: packageMgr, circuitsDir: path.join(__dirname, './testdata'), documentLoader: schemaLoader }); @@ -419,7 +467,7 @@ describe('auth tests', () => { }; const verifier = await Verifier.newVerifier({ - stateResolver: resolvers, + packageManager: packageMgr, stateStorage: MOCK_STATE_STORAGE, circuitsDir: path.join(__dirname, './testdata'), documentLoader: schemaLoader @@ -476,7 +524,7 @@ describe('auth tests', () => { expect(request.body.scope.length).toEqual(1); const verifier = await Verifier.newVerifier({ - stateResolver: resolvers, + packageManager: packageMgr, stateStorage: MOCK_STATE_STORAGE, circuitsDir: path.join(__dirname, './testdata') }); @@ -528,7 +576,7 @@ describe('auth tests', () => { request.body.scope.push(proofRequest); const verifier = await Verifier.newVerifier({ - stateResolver: resolvers, + packageManager: packageMgr, stateStorage: MOCK_STATE_STORAGE, documentLoader: schemaLoader, circuitsDir: path.join(__dirname, './testdata'), @@ -694,7 +742,7 @@ describe('auth tests', () => { }; const verifier = await Verifier.newVerifier({ - stateResolver: resolvers, + packageManager: packageMgr, stateStorage: MOCK_STATE_STORAGE, circuitsDir: path.join(__dirname, './testdata'), documentLoader: schemaLoader @@ -807,7 +855,7 @@ describe('auth tests', () => { expect(request.body.scope.length).toEqual(1); const verifier = await Verifier.newVerifier({ - stateResolver: resolvers, + packageManager: packageMgr, stateStorage: MOCK_STATE_STORAGE, circuitsDir: path.join(__dirname, './testdata'), documentLoader: schemaLoader diff --git a/test/linked-proofs.test.ts b/test/linked-proofs.test.ts index e898f53..1539b56 100644 --- a/test/linked-proofs.test.ts +++ b/test/linked-proofs.test.ts @@ -1,9 +1,63 @@ -import { AuthorizationRequestMessage, CircuitId, PROTOCOL_CONSTANTS } from '@0xpolygonid/js-sdk'; +import { + AuthorizationRequestMessage, + PROTOCOL_CONSTANTS, + CircuitId, + IDataStorage, + IdentityWallet, + CredentialWallet, + ProofService, + CredentialStatusResolverRegistry, + IPackageManager, + CredentialStatusType, + RHSResolver, + FSCircuitStorage +} from '@0xpolygonid/js-sdk'; import { Verifier } from '@lib/auth/auth'; -import { MOCK_STATE_STORAGE, resolvers, schemaLoader, testOpts } from './mocks'; +import { + getInMemoryDataStorage, + getPackageMgr, + MOCK_STATE_STORAGE, + registerBJJIntoInMemoryKMS, + resolvers, + schemaLoader, + testOpts +} from './mocks'; import path from 'path'; describe('Linked proofs verification', () => { + let packageMgr: IPackageManager; + let dataStorage: IDataStorage; + let idWallet: IdentityWallet; + let credWallet: CredentialWallet; + let proofService: ProofService; + + beforeEach(async () => { + const kms = registerBJJIntoInMemoryKMS(); + dataStorage = getInMemoryDataStorage(MOCK_STATE_STORAGE); + const circuitStorage = new FSCircuitStorage({ + dirname: path.join(__dirname, './testdata') + }); + + const resolvers = new CredentialStatusResolverRegistry(); + resolvers.register( + CredentialStatusType.Iden3ReverseSparseMerkleTreeProof, + new RHSResolver(dataStorage.states) + ); + + credWallet = new CredentialWallet(dataStorage, resolvers); + idWallet = new IdentityWallet(kms, dataStorage, credWallet); + + proofService = new ProofService(idWallet, credWallet, circuitStorage, MOCK_STATE_STORAGE, { + documentLoader: schemaLoader + }); + + packageMgr = await getPackageMgr( + await circuitStorage.loadCircuitData(CircuitId.AuthV2), + proofService.generateAuthV2Inputs.bind(proofService), + () => Promise.resolve(true) + ); + }); + it('should verification pass', async () => { const authRequest: AuthorizationRequestMessage = { id: 'f5bcdfc9-3819-4052-ad97-c059119e563c', @@ -82,7 +136,7 @@ describe('Linked proofs verification', () => { 'eyJhbGciOiJncm90aDE2IiwiY2lyY3Vpd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oMTYiLCJjdXJ2ZSI6ImJuMTI4In0sInB1Yl9zaWduYWxzIjpbIjIxNTY4MjI1NDY5ODg5NDU4MzA1OTE0ODQxNDkwMTc1MjgwMDkzNTU1MDE1MDcxMzI5Nzg3Mzc1NjQxNDMxMjYyNTA5MjA4MDY1IiwiNTA0MDgzNzU5MDU3MzQwNjA1MjY2Njc2NTQzODAwODQxMjU0MzEyMzEyMzQ4MjY4Nzk0NDkwNDc0NjI5OTkwNTM4MTI5OTA0NTYxNCIsIjAiXX0'; const verifier = await Verifier.newVerifier({ - stateResolver: resolvers, + packageManager: packageMgr, stateStorage: MOCK_STATE_STORAGE, circuitsDir: path.join(__dirname, './testdata'), documentLoader: schemaLoader diff --git a/test/mocks.ts b/test/mocks.ts index bd91140..173a19e 100644 --- a/test/mocks.ts +++ b/test/mocks.ts @@ -4,9 +4,32 @@ import { VerifiableConstants, RootInfo, StateProof, - VerifyOpts + VerifyOpts, + CircuitData, + AuthDataPrepareFunc, + StateVerificationFunc, + IPackageManager, + DataPrepareHandlerFunc, + VerificationHandlerFunc, + VerificationParams, + ProvingParams, + ZKPPacker, + PackageManager, + PlainPacker, + InMemoryPrivateKeyStore, + BjjProvider, + KmsKeyType, + KMS, + CredentialStorage, + InMemoryDataSource, + W3CCredential, + IdentityStorage, + Identity, + Profile, + InMemoryMerkleTreeStorage } from '@0xpolygonid/js-sdk'; import { DocumentLoader } from '@iden3/js-jsonld-merklization'; +import { proving } from '@iden3/js-jwz'; import { IStateResolver, ResolvedState, Resolvers } from '@lib/state/resolver'; import { DIDResolutionResult } from 'did-resolver'; @@ -114,3 +137,67 @@ export const MOCK_STATE_STORAGE: IStateStorage = { }); } }; + +export const getPackageMgr = async ( + circuitData: CircuitData, + prepareFn: AuthDataPrepareFunc, + stateVerificationFn: StateVerificationFunc +): Promise => { + const authInputsHandler = new DataPrepareHandlerFunc(prepareFn); + + const verificationFn = new VerificationHandlerFunc(stateVerificationFn); + const mapKey = proving.provingMethodGroth16AuthV2Instance.methodAlg.toString(); + + if (!circuitData.verificationKey) { + throw new Error(`verification key doesn't exist for ${circuitData.circuitId}`); + } + const verificationParamMap: Map = new Map([ + [ + mapKey, + { + key: circuitData.verificationKey, + verificationFn + } + ] + ]); + + if (!circuitData.provingKey) { + throw new Error(`proving doesn't exist for ${circuitData.circuitId}`); + } + if (!circuitData.wasm) { + throw new Error(`wasm file doesn't exist for ${circuitData.circuitId}`); + } + const provingParamMap: Map = new Map(); + provingParamMap.set(mapKey, { + dataPreparer: authInputsHandler, + provingKey: circuitData.provingKey, + wasm: circuitData.wasm + }); + + const mgr: IPackageManager = new PackageManager(); + const packer = new ZKPPacker(provingParamMap, verificationParamMap); + const plainPacker = new PlainPacker(); + mgr.registerPackers([packer, plainPacker]); + + return mgr; +}; + +export const registerBJJIntoInMemoryKMS = (): KMS => { + const memoryKeyStore = new InMemoryPrivateKeyStore(); + const bjjProvider = new BjjProvider(KmsKeyType.BabyJubJub, memoryKeyStore); + const kms = new KMS(); + kms.registerKeyProvider(KmsKeyType.BabyJubJub, bjjProvider); + return kms; +}; + +export const getInMemoryDataStorage = (states: IStateStorage) => { + return { + credential: new CredentialStorage(new InMemoryDataSource()), + identity: new IdentityStorage( + new InMemoryDataSource(), + new InMemoryDataSource() + ), + mt: new InMemoryMerkleTreeStorage(40), + states + }; +};