diff --git a/packages/smt/src/smt.ts b/packages/smt/src/smt.ts index 0427b930..4d367ef9 100644 --- a/packages/smt/src/smt.ts +++ b/packages/smt/src/smt.ts @@ -273,22 +273,48 @@ export default class SMT { return false } + /** + * It enables the conversion of the full tree structure into a JSON string, + * facilitating future imports of the tree. This approach is beneficial for + * sharing across networks, as it saves time by storing root and the map of + * hashed nodes directly instead of recomputing them + * @returns The stringified JSON of the tree. + */ export(): string { const obj: { [key: string]: string[] } = {} + obj.root = [this.root.toString()] this.nodes.forEach((value, key) => { obj[key.toString()] = value.map((v) => v.toString()) }) return JSON.stringify(obj, null, 2) } - import(json: string): void { + + /** + * It imports an entire tree by initializing the nodes and root without calculating + * any hashes. Note that it is crucial to ensure the integrity of the tree + * before or after importing it. + * The tree must be empty before importing. + * @param nodes The stringified JSON of the tree. + */ + import(json: string) { const obj = JSON.parse(json) const map = new Map() + for (const [key, value] of Object.entries(obj)) { - const bigintKey = BigInt(key) - const bigintArray = (value as string[]).map((v) => BigInt(v)) - map.set(bigintKey, bigintArray) + if (key === "root") { + this.root = this.bigNumbers ? BigInt((value as string[])[0]) : (value as string[])[0] + } else { + const Key = this.bigNumbers ? BigInt(key) : key + if (this.bigNumbers) { + const Children = (value as string[]).map((v) => BigInt(v)) + map.set(Key, Children) + } else { + map.set(Key, value as string[]) + } + } } + this.nodes = map } diff --git a/yarn.lock b/yarn.lock index 37ee5dfc..a68b7486 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1964,6 +1964,43 @@ __metadata: languageName: node linkType: hard +"@openpassport/zk-kit@workspace:.": + version: 0.0.0-use.local + resolution: "@openpassport/zk-kit@workspace:." + dependencies: + "@commitlint/cli": "npm:^18.6.1" + "@commitlint/config-conventional": "npm:^18.6.2" + "@types/glob": "npm:^7.2.0" + "@types/jest": "npm:^29.5.12" + "@types/node": "npm:^20" + "@typescript-eslint/eslint-plugin": "npm:^7.0.2" + "@typescript-eslint/parser": "npm:^7.0.2" + benny: "npm:^3.7.1" + changelogithub: "patch:changelogithub@npm%3A0.13.3#~/.yarn/patches/changelogithub-npm-0.13.3-1783949906.patch" + czg: "npm:^1.9.1" + eslint: "npm:^8.56.0" + eslint-config-airbnb-base: "npm:^15.0.0" + eslint-config-airbnb-typescript: "npm:^17.1.0" + eslint-config-prettier: "npm:^9.1.0" + eslint-plugin-import: "npm:^2.29.1" + eslint-plugin-jest: "npm:^27.9.0" + husky: "npm:^9.0.11" + jest: "npm:^29.7.0" + jest-config: "npm:^29.7.0" + lint-staged: "npm:^15.2.2" + prettier: "npm:^3.2.5" + rimraf: "npm:^5.0.5" + rollup-plugin-dts: "npm:^6.1.1" + ts-jest: "npm:^29.1.2" + ts-node: "npm:^10.9.2" + tslib: "npm:^2.6.2" + turbo: "npm:^1.13.2" + typedoc: "npm:^0.25.8" + typescript: "npm:^5.3.3" + winston: "npm:^3.14.2" + languageName: unknown + linkType: soft + "@openzeppelin/contracts@npm:^4.0.0": version: 4.9.6 resolution: "@openzeppelin/contracts@npm:4.9.6" @@ -16783,40 +16820,3 @@ __metadata: checksum: 10/f77b3d8d00310def622123df93d4ee654fc6a0096182af8bd60679ddcdfb3474c56c6c7190817c84a2785648cdee9d721c0154eb45698c62176c322fb46fc700 languageName: node linkType: hard - -"zk-kit@workspace:.": - version: 0.0.0-use.local - resolution: "zk-kit@workspace:." - dependencies: - "@commitlint/cli": "npm:^18.6.1" - "@commitlint/config-conventional": "npm:^18.6.2" - "@types/glob": "npm:^7.2.0" - "@types/jest": "npm:^29.5.12" - "@types/node": "npm:^20" - "@typescript-eslint/eslint-plugin": "npm:^7.0.2" - "@typescript-eslint/parser": "npm:^7.0.2" - benny: "npm:^3.7.1" - changelogithub: "patch:changelogithub@npm%3A0.13.3#~/.yarn/patches/changelogithub-npm-0.13.3-1783949906.patch" - czg: "npm:^1.9.1" - eslint: "npm:^8.56.0" - eslint-config-airbnb-base: "npm:^15.0.0" - eslint-config-airbnb-typescript: "npm:^17.1.0" - eslint-config-prettier: "npm:^9.1.0" - eslint-plugin-import: "npm:^2.29.1" - eslint-plugin-jest: "npm:^27.9.0" - husky: "npm:^9.0.11" - jest: "npm:^29.7.0" - jest-config: "npm:^29.7.0" - lint-staged: "npm:^15.2.2" - prettier: "npm:^3.2.5" - rimraf: "npm:^5.0.5" - rollup-plugin-dts: "npm:^6.1.1" - ts-jest: "npm:^29.1.2" - ts-node: "npm:^10.9.2" - tslib: "npm:^2.6.2" - turbo: "npm:^1.13.2" - typedoc: "npm:^0.25.8" - typescript: "npm:^5.3.3" - winston: "npm:^3.14.2" - languageName: unknown - linkType: soft