@iden3/js-iden3-auth
Version:
iden3-auth implementation in JavaScript
38 lines (37 loc) • 1.72 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';
const defaultAuthVerifyOpts = 5 * 60 * 1000; // 5 minutes
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);
let acceptedStateTransitionDelay = defaultAuthVerifyOpts;
if (opts?.acceptedStateTransitionDelay) {
acceptedStateTransitionDelay = opts.acceptedStateTransitionDelay;
}
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);
}
}