add integration test for mint function

This commit is contained in:
motemotech
2024-10-10 18:03:25 +09:00
parent f9d37568b6
commit 2822070005
9 changed files with 363 additions and 118 deletions

View File

@@ -1,6 +1,8 @@
import { poseidon9, poseidon3, poseidon2, poseidon6, poseidon13 } from "poseidon-lite"
import { stringToAsciiBigIntArray } from "./utils";
import { ChildNodes,SMT } from "@ashpect/smt"
import { hash, stringToAsciiBigIntArray } from "./utils";
import { ChildNodes,SMT } from "@ashpect/smt";
import fs from 'fs';
import path from 'path';
// SMT trees for 3 levels :
// 1. Passport tree : level 3 (Absolute Match)
@@ -47,6 +49,38 @@ export function buildSMT(field :any[], treetype:string): [number, number, SMT]{
return [count, performance.now() - startTime, tree]
}
export function exportSMTToJsonFile(count: number, time: number, smt: SMT, outputPath?: string) {
const serializedSMT = smt.export();
const data = {
count: count,
time: time,
smt: serializedSMT
};
const jsonString = JSON.stringify(data, null, 2);
const defaultPath = path.join(process.cwd(), 'smt.json');
const finalPath = outputPath ? path.resolve(process.cwd(), outputPath) : defaultPath;
fs.writeFileSync(finalPath, jsonString, 'utf8');
}
export function importSMTFromJsonFile(filePath?: string): SMT | null {
try {
const jsonString = fs.readFileSync(path.resolve(process.cwd(), filePath), 'utf8');
const data = JSON.parse(jsonString);
const hash2 = (childNodes: ChildNodes) => (childNodes.length === 2 ? poseidon2(childNodes) : poseidon3(childNodes));
const smt = new SMT(hash2, true);
smt.import(data.smt);
console.log('Successfully imported SMT from JSON file');
return smt;
} catch (error) {
console.error('Failed to import SMT from JSON file:', error);
return null;
}
}
function processPassport(passno : string, index: number): bigint {
if (passno.length > 9) {
console.log('passport length is greater than 9:', index, passno)

View File

@@ -5,6 +5,8 @@ import { sha384, sha512_256 } from 'js-sha512';
import { SMT } from '@ashpect/smt';
import forge from 'node-forge';
import { n_dsc, k_dsc, n_dsc_ecdsa, k_dsc_ecdsa, n_csca, k_csca, attributeToPosition } from '../constants/constants';
import * as fs from 'fs';
import * as path from 'path';
export function formatMrz(mrz: string) {
const mrzCharcodes = [...mrz].map((char) => char.charCodeAt(0));
@@ -351,6 +353,24 @@ export function generateSMTProof(smt: SMT, leaf: bigint) {
};
}
// I don't know if we need this function
// export function saveSMTToJsonFile(count: number, time: number, smt: SMT, outputPath?: string): void {
// const serializedSMT = smt.export();
// const data = {
// count: count,
// time: time,
// smt: serializedSMT
// };
// const jsonString = JSON.stringify(data, null, 2);
// const defaultPath = path.join(__dirname, 'smtData.json');
// const finalPath = outputPath ? path.resolve(__dirname, outputPath) : defaultPath;
// fs.writeFileSync(finalPath, jsonString, 'utf8');
// console.log(`Saved json file here: ${finalPath}`);
// }
export function generateMerkleProof(imt: LeanIMT, _index: number, maxDepth: number) {
const { siblings: merkleProofSiblings, index } = imt.generateProof(_index);
const depthForThisOne = merkleProofSiblings.length;