minauth-simple-preimage-plugin
Version:
A very simple Minauth plugin that allows users to authenticate by providing a preimage to a given hash.
60 lines • 2.33 kB
JavaScript
import { Cache } from 'o1js';
import ProvePreimageProgram from './hash-preimage-proof.js';
import z from 'zod';
const RolesSchema = z.record(z.string().min(1), z.string().min(1));
/**
* Somewhat trivial example of a prover.
* The server keeps a fixed set of hashes.
* Each hash is associated with a role in the system.
* You can prove that you have the role by providing the secret
* preimage of the hash.
*
* NOTE. Although you can always generate valid zkproof, its output must
* match the list kept by the server.
*/
export class SimplePreimageProver {
constructor(logger, pluginRoutes) {
this.logger = logger;
this.pluginRoutes = pluginRoutes;
}
/** Build a proof. */
async prove(publicInput, secretInput) {
this.logger.debug('Building proof started.');
const proof = await ProvePreimageProgram.baseCase(publicInput, secretInput);
this.logger.debug('Building proof finished.');
return proof.toJSON();
}
/** Fetches a list of hashes recognized by the server. */
fetchPublicInputs() {
throw 'not implemented, please query the `/roles` endpoint';
}
async getRoles() {
return this.pluginRoutes.get('/admin/roles', RolesSchema);
}
/** Compile the underlying zk circuit */
static async compile() {
// disable cache because of bug in o1js 0.14.1:
// you have a verification key acquired by using cached circuit AND
// not build a proof locally,
// but use a serialized one - it will hang during verification.
return await ProvePreimageProgram.compile({ cache: Cache.None });
}
/** Initialize the prover */
static async initialize(config, { compile = true } = {}) {
const { logger, pluginRoutes } = config;
logger.info('SimplePreimageProver.initialize');
if (compile) {
logger.info('compiling the circuit');
await SimplePreimageProver.compile();
logger.info('compiled');
}
return new SimplePreimageProver(logger, pluginRoutes);
}
}
SimplePreimageProver;
export class DemoSimplePreimageProver extends SimplePreimageProver {
async setRoles(roles) {
return await this.pluginRoutes.post('/admin/roles', RolesSchema, roles);
}
}
//# sourceMappingURL=prover.js.map