Skip to content

Commit

Permalink
add packageManager
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Mar 7, 2024
1 parent 44e4ae2 commit 0160eaa
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 130 deletions.
115 changes: 10 additions & 105 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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') */
Expand All @@ -100,7 +95,6 @@ export type VerifierParams = Options & {
*/
export interface VerifierSuiteParams {
documentLoader: DocumentLoader;
packageManager: PackageManager;
circuitStorage: ICircuitStorage;
prover: IZKProver;
}
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -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<string, ProvingParams> = new Map();

const stateVerificationFn = async (
circuitId: string,
pubSignals: Array<string>
): Promise<boolean> => {
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<string, VerificationParams> = 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);
Expand Down Expand Up @@ -347,42 +296,6 @@ export class Verifier {
}
}

// /**
// * verifies jwz token
// * @public
// * @param {string} tokenStr - token string
// * @param {VerifyOpts} opts - verification options
// *
// * @returns `Promise<Token>`
// */
// public async verifyJWZ(tokenStr: string, opts?: VerifyOpts): Promise<Token> {
// 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
Expand All @@ -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);
}
}
}
65 changes: 55 additions & 10 deletions test/atomicV3.test.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -172,7 +217,7 @@ describe('atomicV3', () => {
};

const authInstance = await Verifier.newVerifier({
stateResolver: resolvers,
packageManager: packageMgr,
stateStorage: MOCK_STATE_STORAGE,
circuitsDir: path.join(__dirname, './testdata')
});
Expand Down Expand Up @@ -214,7 +259,7 @@ describe('atomicV3', () => {
);

const authInstance = await Verifier.newVerifier({
stateResolver: resolvers,
packageManager: packageMgr,
stateStorage: MOCK_STATE_STORAGE,
circuitsDir: path.join(__dirname, './testdata')
});
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 0160eaa

Please sign in to comment.