All files node-zk-config-provider.ts

94.11% Statements 16/17
80% Branches 4/5
100% Functions 5/5
94.11% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                                                1x       1x       1x       1x       1x                   6x 6x                     19x 19x 19x 19x 19x     8x             14x             3x             2x      
/*
 * This file is part of midnight-js.
 * Copyright (C) 2025-2026 Midnight Foundation
 * SPDX-License-Identifier: Apache-2.0
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import type { ProverKey, VerifierKey, ZKIR } from '@midnight-ntwrk/midnight-js-types';
import { createProverKey, createVerifierKey, createZKIR, ZKConfigProvider } from '@midnight-ntwrk/midnight-js-types';
import { assertSafeName } from '@midnight-ntwrk/midnight-js-utils';
import * as fs from 'fs/promises';
import * as path from 'path';
 
/**
 * The name of the directory containing proving and verifying keys.
 */
const KEY_DIR = 'keys';
/**
 * File extension for proving keys.
 */
const PROVER_EXT = '.prover';
/**
 * File extension for verifying keys.
 */
const VERIFIER_EXT = '.verifier';
/**
 * The name of the directory containing zkIRs.
 */
const ZKIR_DIR = 'zkir';
/**
 * File extension for zkIRs.
 */
const ZKIR_EXT = '.bzkir';
 
/**
 * Implementation of {@link ZKConfigProvider} that reads the keys and zkIR from the local filesystem.
 * @typeParam K - The type of the circuit ID used by the provider.
 */
export class NodeZkConfigProvider<K extends string> extends ZKConfigProvider<K> {
  /**
   * @param directory The path to the base directory containing the key and ZKIR subdirectories.
   */
  constructor(readonly directory: string) {
    super();
  }
 
  /**
   * Reads a file from the local filesystem.
   * @param subDir The subdirectory of the base-directory to read from.
   * @param circuitId The circuit ID corresponding to the file to read.
   * @param ext The file extension of the file to read.
   * @private
   */
  private async readFile(subDir: string, circuitId: K, ext: string): Promise<Buffer> {
    assertSafeName(circuitId, 'circuitId');
    const baseDir = path.resolve(this.directory, subDir);
    const target = path.resolve(baseDir, circuitId + ext);
    const rel = path.relative(baseDir, target);
    Iif (rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) {
      throw new Error(`Invalid circuitId: ${JSON.stringify(circuitId)}`);
    }
    return fs.readFile(target);
  }
 
  /**
   * {@link ZKConfigProvider.getProverKey}
   */
  getProverKey(circuitId: K): Promise<ProverKey> {
    return this.readFile(KEY_DIR, circuitId, PROVER_EXT).then(createProverKey);
  }
 
  /**
   * {@link ZKConfigProvider.getVerifierKey}
   */
  getVerifierKey(circuitId: K): Promise<VerifierKey> {
    return this.readFile(KEY_DIR, circuitId, VERIFIER_EXT).then(createVerifierKey);
  }
 
  /**
   * {@link ZKConfigProvider.getZKIR}
   */
  getZKIR(circuitId: K): Promise<ZKIR> {
    return this.readFile(ZKIR_DIR, circuitId, ZKIR_EXT).then(createZKIR);
  }
}