@iden3/js-iden3-auth
Version:
iden3-auth implementation in JavaScript
35 lines (34 loc) • 1.63 kB
JavaScript
import { getDateFromUnixTimestamp } from '@iden3/js-iden3-core';
import { IDOwnershipPubSignals } from '../circuits/ownershipVerifier.js';
import { checkGlobalState, getResolverByID } from '../circuits/common.js';
import { AuthV2PubSignals, byteEncoder } from '@0xpolygonid/js-sdk';
import { CONSTANTS } from '../constants.js';
export class AuthPubSignalsV2 extends IDOwnershipPubSignals {
constructor(pubSignals) {
super();
this.pubSignals = new AuthV2PubSignals();
this.pubSignals = this.pubSignals.pubSignalsUnmarshal(byteEncoder.encode(JSON.stringify(pubSignals)));
this.userId = this.pubSignals.userID;
this.challenge = this.pubSignals.challenge;
}
verifyQuery() {
throw new Error(`authV2 circuit doesn't support queries`);
}
async verifyStates(resolvers, opts) {
const resolver = getResolverByID(resolvers, this.userId);
if (!resolver) {
throw new Error(`resolver not found for id ${this.userId.string()}`);
}
const gist = await checkGlobalState(resolver, this.pubSignals.GISTRoot);
const acceptedStateTransitionDelay = opts?.acceptedStateTransitionDelay ?? CONSTANTS.AUTH_ACCEPTED_STATE_TRANSITION_DELAY;
if (!gist.latest) {
const timeDiff = Date.now() - getDateFromUnixTimestamp(Number(gist.transitionTimestamp)).getTime();
if (timeDiff > acceptedStateTransitionDelay) {
throw new Error('global state is outdated');
}
}
}
verifyIdOwnership(sender, challenge) {
return super.verifyIdOwnership(sender, challenge);
}
}