minauth
Version:
A TypeScript library for building authentication systems on top of the Mina blockchain and other zero-knowledge proofs solutions.
91 lines • 3.86 kB
JavaScript
import { Cache } from 'o1js';
import { outputInvalid, outputValid } from '../../plugin/plugintype.js';
import ProvePreimageProgram from './hash-preimage-proof.js';
import { Router } from 'express';
import { z } from 'zod';
import * as fs from 'fs/promises';
import { wrapZodDec, combineEncDec, noOpEncoder } from 'minauth/dist/plugin/encodedecoder.js';
export class DummyPlugin {
/**
* Verify a proof and return the role.
*/
async verifyAndGetOutput(_, serializedProof) {
}
/**
* Check if produced output is still valid. If the roles dictionary was edited
* it may become invalid. Notice that the proof and output consumer must not
* allow output forgery as this will accept forged outputs without verification.
* To prevent it the plugin could take the reponsibility by having a cache of outputs
* with unique identifiers.
*/
async checkOutputValidity(output) {
this.logger.debug('Checking validity of ', output);
if (!this.roles.hasOwnProperty(output.provedHash)) {
this.logger.debug('Proved hash no longer exists.');
return Promise.resolve(outputInvalid('Proved hash is no longer valid.'));
}
if (this.roles[output.provedHash] !== output.role) {
this.logger.debug('Proved hash no longer exists.');
return Promise.resolve(outputInvalid('The role assigned to the hash is no longer valid.'));
}
return Promise.resolve(outputValid);
}
/**
* This ctor is meant ot be called by the `initialize` function.
*/
constructor(verificationKey, roles, logger) {
/**
* This plugin uses an idiomatic Typescript interface
*/
this.__interface_tag = 'ts';
/**
* Trivial - no public inputs.
*/
this.publicInputArgsSchema = z.any();
/**
* Provide an endpoint returning a list of roles recognized by the plugin.
* Additionally, provide an endpoint to update the roles
* NOTE. the setRoles endpoint should not be used by the client
* but rather by the plugin admin and that it is not persisted.
*/
this.customRoutes = Router()
.post('/admin/roles', (req, res) => {
try {
// Assuming the new roles are sent in the request body
this.roles = rolesSchema.parse(req.body);
res.status(200).json({ message: 'Roles updated successfully' });
}
catch (error) {
// Handle errors, such as invalid input
res.status(400).json({ message: 'Error updating roles' });
}
})
.get('/admin/roles', (_, res) => res.status(200).json(this.roles));
this.verificationKey = verificationKey;
this.roles = roles;
this.logger = logger;
}
/**
* Initialize the plugin with a configuration.
*/
static async initialize(configuration, logger) {
const { verificationKey } = await ProvePreimageProgram.compile({
cache: Cache.None
});
const roles = 'roles' in configuration
? configuration.roles
: await fs
.readFile(configuration.loadRolesFrom, 'utf-8')
.then(JSON.parse);
return new SimplePreimagePlugin(verificationKey, roles, logger);
}
}
DummyPlugin.__interface_tag = 'ts';
DummyPlugin.configurationDec = wrapZodDec('ts', configurationSchema);
DummyPlugin.publicInputArgsDec = wrapZodDec('ts', z.unknown());
/** output parsing and serialization */
DummyPlugin.outputEncDec = combineEncDec(noOpEncoder('ts'), wrapZodDec('ts', z.object({ provedHash: z.string(), role: z.string() })));
// sanity check
SimplePreimagePlugin;
export default SimplePreimagePlugin;
//# sourceMappingURL=plugin.js.map