@nori-zk/proof-conversion
Version:
Verifying zkVM proofs inside o1js circuits, to generate Mina compatible proof
54 lines • 2.26 kB
JavaScript
/**
* Implementation of ApiMethod that handles both overloads.
*
* Performs runtime validation when supportsArgs is true to ensure:
* 1. keys parameter is provided
* 2. keys array length matches schema keys length
* 3. keys array elements match schema keys in the same order
*
* Creates a fromArgs function (when supportsArgs is true) that:
* - Takes positional arguments in the order specified by keys
* - Converts them to the typed input object
* - Throws if any argument is undefined
* - Includes a `keys` property with the key order
*
* @internal
*/
export function ApiMethod(schema, supportsArgs, keys) {
// Runtime validation: ensure keys match schema order when supportsArgs is true
if (supportsArgs === true) {
if (!keys) {
throw new Error('keys parameter is required when supportsArgs is true');
}
const schemaKeysOrdered = Object.keys(schema);
const keysArray = Array.from(keys);
// Check length matches
if (keysArray.length !== schemaKeysOrdered.length) {
throw new Error(`Keys length mismatch: keys has ${keysArray.length} elements but schema has ${schemaKeysOrdered.length} keys`);
}
// Check each key matches in order
for (let i = 0; i < keysArray.length; i++) {
if (String(keysArray[i]) !== schemaKeysOrdered[i]) {
throw new Error(`Key order mismatch at index ${i}: expected "${schemaKeysOrdered[i]}" but got "${String(keysArray[i])}". ` +
`Keys must be in the same order as schema keys: [${schemaKeysOrdered.join(', ')}]`);
}
}
}
const schemaKeys = (supportsArgs ? keys : Object.keys(schema));
const fromArgs = (supportsArgs === false
? false
: Object.assign((...args) => {
const input = {};
schemaKeys.forEach((k, i) => {
const v = args[i];
if (v === undefined)
throw new Error(`Argument for "${String(k)}" is undefined`);
input[k] = v;
});
return input;
}, { keys: keys }));
return function (fn) {
return Object.assign(fn, { fromArgs, schema });
};
}
//# sourceMappingURL=ApiMethod.js.map