mirror of
https://github.com/selfxyz/self.git
synced 2026-04-05 03:00:53 -04:00
add integration test for mint function
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user