Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@iden3/js-merkletree

Package Overview
Dependencies
Maintainers
4
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@iden3/js-merkletree - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

2

dist/browser/esm/index.js

@@ -1,2 +0,2 @@

import{Hex as t,poseidon as e}from"@iden3/js-crypto";const i=32,s=0,n=1,r=2,a=65,o=new Uint8Array(65),h="empty",l=32,c=8,u=256,f=2,d=30,w=BigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617"),y=w-BigInt("1");function g(t,e,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(t):s?s.value:e.get(t)}function v(t,e,i,s,n){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!n:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?n.call(t,i):n?n.value=i:e.set(t,i),i}const p=t=>t<w,b=(t,e)=>t.every(((t,i)=>t===e[i])),m=t=>t.slice().reverse(),x=t=>"0b"+t.reduce(((t,e)=>t+e.toString(2).padStart(8,"0")),""),k=(t,e)=>0!=(t[parseInt((e/8).toString())]&1<<e%8),A=(t,e)=>0!=(t[t.length-parseInt(""+e/8)-1]&1<<e%8),I=(t,e)=>{t[t.length-parseInt(""+e/8)-1]|=1<<e%8},R="0123456789abcdef",L=t=>{const e=new Array(2*t.length);let i=0;return t.forEach((t=>{e[i]=R[parseInt((t>>4).toString(10))],e[i+1]=R[parseInt((15&t).toString(10))],i+=2})),e.join("")},N=t=>{if(t.length!==i)throw`Expected 32 bytes, found ${t.length} bytes`;const e=BigInt(x(t));if(!p(e))throw"NewBigIntFromHashBytes: Value not inside the Finite Field";return e},U=t=>new Uint8Array(2*t.length).map(((e,i)=>t.charCodeAt(i))),E=(t,e)=>{const i=new Array(t);for(let s=0;s<t;s+=1)i[s]=k(e,s);return i},K=t=>{const e=new ArrayBuffer(i*t.length),s=new Uint8Array(e);return t.forEach(((t,e)=>{s.set(t.value,e*i)})),s},B=(t,e)=>t.toString(e||10).split("").map((t=>parseInt(t))),S=t=>{const e=BigInt(256),s=new Uint8Array(i);let n=0;for(;t>BigInt(0);)s[31-n]=Number(t%e),t/=e,n+=1;return s};class V{constructor(t){if(t?.length){if(t.length!==i)throw new Error(`Expected 32 bytes, found ${t.length} bytes`);this.bytes=t}else this.bytes=new Uint8Array(i)}get value(){return this.bytes}set value(t){if(t.length!==i)throw`Expected 32 bytes, found ${t.length} bytes`;this.bytes=m(t)}string(){return this.bigInt().toString(10)}hex(){return L(this.bytes)}equals(t){return b(this.value,t.value)}bigInt(){const t=m(this.value);return BigInt(x(t))}}const M=new V,$=t=>{if(!p(t))throw"NewBigIntFromHashBytes: Value not inside the Finite Field";const e=S(t),i=new V;return i.value=e,i},W=e=>{if(!e)return M;const i=new V;return i.value=m(t.decodeString(e)),i},_=t=>{const e=BigInt(t);if(!p(e))throw"NewBigIntFromHashBytes: Value not inside the Finite Field";return $(e)},P=t=>{const i=e.hash(t);return $(i)},F=(t,i)=>{const s=e.hash([...i,t]);return $(s)},T=(t,e)=>{for(let i=t.length;i<e;i+=1)t.push(M);return t};var O,z;class C{constructor(t){O.set(this,void 0),z.set(this,void 0),this.prefix=t,v(this,O,{},"f"),v(this,z,M,"f")}async get(t){const e=new Uint8Array([...this.prefix,...t]);return g(this,O,"f")[e.toString()]?g(this,O,"f")[e.toString()]:void 0}async put(t,e){const i=new Uint8Array([...this.prefix,...t]);g(this,O,"f")[i.toString()]=e}async getRoot(){return g(this,z,"f")}async setRoot(t){v(this,z,t,"f")}}O=new WeakMap,z=new WeakMap;const H=async(t,e)=>F(BigInt(1),[t.bigInt(),e.bigInt()]),j=(t,e,i)=>{const s=new Uint8Array(65),n=S(e.bigInt()),r=S(i.bigInt());s[0]=t;for(let t=1;t<33;t+=1)s[t]=n[t-1];for(let t=33;t<=65;t+=1)s[t]=r[t-33];return s};var q,D,G,J;class Q{constructor(t,e){q.set(this,void 0),this.type=1,this.entry=[t,e],v(this,q,M,"f")}async getKey(){return g(this,q,"f")===M?await H(this.entry[0],this.entry[1]):g(this,q,"f")}get value(){return j(this.type,this.entry[0],this.entry[1])}get string(){return`Leaf I:${this.entry[0]} D:${this.entry[1]}`}}q=new WeakMap;class X{constructor(t,e){D.set(this,void 0),this.type=0,this.childL=t,this.childR=e,v(this,D,M,"f")}async getKey(){return g(this,D,"f")===M?P([this.childL.bigInt(),this.childR.bigInt()]):g(this,D,"f")}get value(){return j(this.type,this.childL,this.childR)}get string(){return`Middle L:${this.childL} R:${this.childR}`}}D=new WeakMap;class Y{constructor(){G.set(this,void 0),this.type=2,v(this,G,M,"f")}async getKey(){return M}get value(){return o}get string(){return h}}G=new WeakMap;class Z{constructor(t){this._prefix=t,J.set(this,void 0);const e=localStorage.getItem(L(t));if(e){const t=JSON.parse(e);v(this,J,new V(Uint8Array.from(t)),"f")}else v(this,J,M,"f")}async get(t){const e=new Uint8Array([...this._prefix,...t]),i=L(e),s=localStorage.getItem(i);if(null===s)return;const n=JSON.parse(s);switch(n.type){case 2:return new Y;case 0:const t=new V(Uint8Array.from(n.childL)),e=new V(Uint8Array.from(n.childR));return new X(t,e);case 1:const i=new V(Uint8Array.from(n.entry[0])),s=new V(Uint8Array.from(n.entry[1]));return new Q(i,s)}throw`error: value found for key ${L(e)} is not of type Node`}async put(t,e){const i=new Uint8Array([...this._prefix,...t]),s=L(i),n={type:e.type};e instanceof X?(n.childL=Array.from(e.childL.bytes),n.childR=Array.from(e.childR.bytes)):e instanceof Q&&(n.entry=[Array.from(e.entry[0].bytes),Array.from(e.entry[1].bytes)]);const r=JSON.stringify(n);localStorage.setItem(s,r)}async getRoot(){return g(this,J,"f")}async setRoot(t){v(this,J,t,"f"),localStorage.setItem(L(this._prefix),JSON.stringify(Array.from(t.bytes)))}}function tt(t){return new Promise(((e,i)=>{t.oncomplete=t.onsuccess=()=>e(t.result),t.onabort=t.onerror=()=>i(t.error)}))}function et(t,e){const i=indexedDB.open(t);i.onupgradeneeded=()=>i.result.createObjectStore(e);const s=tt(i);return(t,i)=>s.then((s=>i(s.transaction(e,t).objectStore(e))))}let it;function st(){return it||(it=et("keyval-store","keyval")),it}function nt(t,e=st()){return e("readonly",(e=>tt(e.get(t))))}function rt(t,e,i=st()){return i("readwrite",(i=>(i.put(e,t),tt(i.transaction))))}var at,ot,ht;J=new WeakMap;class lt{constructor(t,e){this._prefix=t,at.set(this,void 0),v(this,at,M,"f"),this._prefixHash=L(t),this._store=et(`${e??lt.storageName}-db`,lt.storageName)}async get(t){const e=new Uint8Array([...this._prefix,...t]),i=L(e),s=await nt(i,this._store);if(null!=s){if(2===s.type)return new Y;if(0===s.type){const t=new V(Uint8Array.from(s.childL.bytes)),e=new V(Uint8Array.from(s.childR.bytes));return new X(t,e)}if(1===s.type){const t=new V(Uint8Array.from(s.entry[0].bytes)),e=new V(Uint8Array.from(s.entry[1].bytes));return new Q(t,e)}throw new Error(`error: value found for key ${i} is not of type Node`)}}async put(t,e){const i=new Uint8Array([...this._prefix,...t]),s=L(i);await rt(s,e,this._store)}async getRoot(){if(!g(this,at,"f").equals(M))return g(this,at,"f");const t=await nt(this._prefixHash,this._store);return v(this,at,t?new V(t.bytes):M,"f"),g(this,at,"f")}async setRoot(t){await rt(this._prefixHash,t,this._store),v(this,at,t,"f")}}at=new WeakMap,lt.storageName="merkle-tree";class ct{constructor(){ot.set(this,void 0),v(this,ot,new Uint8Array(l),"f")}get value(){return g(this,ot,"f")}set value(t){v(this,ot,t,"f")}bigInt(){return N(m(g(this,ot,"f")))}string(){return`${L(g(this,ot,"f").slice(0,4))}...`}}ot=new WeakMap;class ut{constructor(){ht.set(this,void 0),v(this,ht,new Array(8),"f")}get value(){return g(this,ht,"f")}set value(t){if(8!==t.length)throw`expected bytes length to be 8, got ${t.length}`;v(this,ht,t,"f")}bytes(){const t=new Uint8Array(256);for(let e=0;e<8;e+=1)g(this,ht,"f")[e].value.forEach(((i,s)=>{t[e*l+s]=i}));return t}equal(t){return b(g(this,ht,"f")[0].value,t.value[0].value)&&b(g(this,ht,"f")[1].value,t.value[1].value)&&b(g(this,ht,"f")[2].value,t.value[2].value)&&b(g(this,ht,"f")[3].value,t.value[3].value)}}ht=new WeakMap;const ft=t=>{if(t.length!==u)throw`expected bytes length to be 256, got ${t.length}`;const e=new ut,i=new Array(u);for(let e=0;e<8;e+=1){const s=new ct;s.value=t.slice(e*l,(e+1)*u),i[e]=s}return e.value=i,e};var dt,wt,yt;class gt{constructor(t){dt.set(this,void 0),wt.set(this,void 0),yt.set(this,void 0),v(this,dt,t||new ut,"f"),v(this,wt,M,"f"),v(this,yt,M,"f")}get data(){return g(this,dt,"f")}get index(){return g(this,dt,"f").value.slice(0,4)}get value(){return g(this,dt,"f").value.slice(4,8)}async hIndex(){return g(this,wt,"f")===M?P(vt(this.index)):g(this,wt,"f")}async hValue(){return g(this,yt,"f")===M?P(vt(this.value)):g(this,yt,"f")}hiHv(){return(async()=>({hi:await this.hIndex(),hv:await this.hValue()}))()}bytes(){return g(this,dt,"f").value}equal(t){return g(this,dt,"f").equal(t.data)}clone(){return new gt(g(this,dt,"f"))}}dt=new WeakMap,wt=new WeakMap,yt=new WeakMap;const vt=t=>t.map((t=>t.bigInt())),pt=t=>{const e=vt(t.data.value);let i=!0;return e.forEach((t=>{p(t)||(i=!1)})),i},bt="key already exists",mt="Key not found in the MerkleTree",xt="node data has incorrect size in the DB",kt="reached maximum level of the merkle tree",At="found an invalid node in the DB",It="the serialized proof is invalid",Rt="the value in the DB is invalid",Lt="the entry index already exists in the tree",Nt="Merkle Tree not writable",Ut="key not found";class Et{constructor(t=M,e=[],i=M,s=M,n=!1,r=M,a=M,o=0){this.root=t,this.siblings=e,this.oldKey=i,this.oldValue=s,this.isOld0=n,this.key=r,this.value=a,this.fnc=o}}class Kt{constructor(t=M,e=M,i=[],s=M,n=M,r=M,a=M,o=!1,h=0){this.oldRoot=t,this.newRoot=e,this.siblings=i,this.oldKey=s,this.oldValue=n,this.newKey=r,this.newValue=a,this.isOld0=o,this.fnc=h}}const Bt="non-existence proof being checked against hIndex equal to nodeAux";class St{constructor(){this.existence=!1,this.depth=0,this.siblings=[];const t=new ArrayBuffer(30);this.notEmpties=new Uint8Array(t)}bytes(){let t=2+this.notEmpties.length+l*this.siblings.length;void 0!==this.nodeAux&&(t+=64);const e=new ArrayBuffer(t),i=new Uint8Array(e);this.existence||(i[0]|=1),i[1]=this.depth,i.set(this.notEmpties,2);const s=K(this.siblings);return i.set(s,this.notEmpties.length+2),void 0!==this.nodeAux&&(i[0]|=2,i.set(this.nodeAux.key.value,i.length-64),i.set(this.nodeAux.value.value,i.length-32)),i}allSiblings(){return Vt(this)}}const Vt=t=>{let e=0;const i=[];for(let s=0;s<t.depth;s+=1)A(t.notEmpties,s)?(i.push(t.siblings[e]),e+=1):i.push(M);return i},Mt=async(t,e,i,s)=>{try{const n=await $t(e,i,s);return b(t.value,n.value)}catch(t){if(t===Bt)return!1;throw t}},$t=async(t,e,i)=>{const s=$(e),n=$(i);let r,a=t.siblings.length-1;if(t.existence)r=await H(s,n);else if(void 0===t.nodeAux)r=M;else{const e=t.nodeAux;if(b(s.value,e.key.value))throw Bt;r=await H(e.key,e.value)}const o=E(t.depth,s.value);let h;for(let e=t.depth-1;e>=0;e-=1)A(t.notEmpties,e)?(h=t.siblings[a],a-=1):h=M,r=o[e]?await new X(h,r).getKey():await new X(r,h).getKey();return r};var Wt,_t,Pt,Ft;class Tt{constructor(t,e,i){Wt.set(this,void 0),_t.set(this,void 0),Pt.set(this,void 0),Ft.set(this,void 0),v(this,Wt,t,"f"),v(this,Pt,e,"f"),v(this,Ft,i,"f")}async root(){return g(this,_t,"f")||v(this,_t,await g(this,Wt,"f").getRoot(),"f"),g(this,_t,"f")}get maxLevels(){return g(this,Ft,"f")}async add(t,e){if(!g(this,Pt,"f"))throw Nt;v(this,_t,await this.root(),"f");const i=$(t),s=$(e),n=new Q(i,s),r=E(this.maxLevels,i.value),a=await this.addLeaf(n,g(this,_t,"f"),0,r);v(this,_t,a,"f"),await g(this,Wt,"f").setRoot(g(this,_t,"f"))}async updateNode(t){if(!g(this,Pt,"f"))throw Nt;if(2===t.type)return await t.getKey();const e=await t.getKey();return await g(this,Wt,"f").put(e.value,t),e}async addNode(t){if(!g(this,Pt,"f"))throw Nt;if(2===t.type)return await t.getKey();const e=await t.getKey();return await g(this,Wt,"f").put(e.value,t),e}async addEntry(t){if(!g(this,Pt,"f"))throw Nt;if(!pt(t))throw"elements not inside the finite field over r";v(this,_t,await g(this,Wt,"f").getRoot(),"f");const e=await t.hIndex(),i=await t.hValue(),s=new Q(e,i),n=E(this.maxLevels,e.value),r=await this.addLeaf(s,g(this,_t,"f"),0,n);v(this,_t,r,"f"),await g(this,Wt,"f").setRoot(g(this,_t,"f"))}async pushLeaf(t,e,i,s,n){if(i>g(this,Ft,"f")-2)throw new Error(kt);let r;if(s[i]===n[i]){const a=await this.pushLeaf(t,e,i+1,s,n);return r=s[i]?new X(new V,a):new X(a,new V),await this.addNode(r)}const a=await e.getKey(),o=await t.getKey();return r=s[i]?new X(a,o):new X(o,a),await this.addNode(t),await this.addNode(r)}async addLeaf(t,e,i,s){if(i>g(this,Ft,"f")-1)throw new Error(kt);const n=await this.getNode(e);if(void 0===n)throw Ut;switch(n.type){case 2:return this.addNode(t);case 1:{const e=n.entry[0],r=t.entry[0];if(b(e.value,r.value))throw Lt;const a=E(this.maxLevels,e.value);return this.pushLeaf(t,n,i,s,a)}case 0:{let e;if(s[i]){const r=await this.addLeaf(t,n.childR,i+1,s);e=new X(n.childL,r)}else{const r=await this.addLeaf(t,n.childL,i+1,s);e=new X(r,n.childR)}return this.addNode(e)}default:throw At}}async get(t){const e=$(t),i=E(this.maxLevels,e.value);let s=await this.root();const n=[];for(let t=0;t<this.maxLevels;t++){const e=await this.getNode(s);if(void 0===e)throw mt;switch(e.type){case 2:return{key:BigInt("0"),value:BigInt("0"),siblings:n};case 1:return{key:e.entry[0].bigInt(),value:e.entry[1].bigInt(),siblings:n};case 0:i[t]?(s=e.childR,n.push(e.childL)):(s=e.childL,n.push(e.childR));break;default:throw At}}throw new Error(kt)}async update(t,e){if(!g(this,Pt,"f"))throw Nt;if(!p(t))throw"key not inside the finite field";if(!p(e))throw"key not inside the finite field";const i=$(t),s=$(e),n=E(this.maxLevels,i.value),r=new Kt;r.fnc=1,r.oldRoot=await this.root(),r.oldKey=i,r.newKey=i,r.newValue=s;let a=await this.root();const o=[];for(let t=0;t<this.maxLevels;t+=1){const e=await this.getNode(a);if(void 0===e)throw Ut;switch(e.type){case 2:throw mt;case 1:if(b(i.value,e.entry[0].value)){r.oldValue=e.entry[1],r.siblings=T([...o],this.maxLevels);const t=new Q(i,s);await this.updateNode(t);const a=await this.recalculatePathUntilRoot(n,t,o);return v(this,_t,a,"f"),await g(this,Wt,"f").setRoot(a),r.newRoot=a,r}break;case 0:n[t]?(a=e.childR,o.push(e.childL)):(a=e.childL,o.push(e.childR));break;default:throw At}}throw mt}async getNode(t){return b(t.value,M.value)?new Y:await g(this,Wt,"f").get(t.value)}async recalculatePathUntilRoot(t,e,i){for(let s=i.length-1;s>=0;s-=1){const n=await e.getKey();e=t[s]?new X(i[s],n):new X(n,i[s]),await this.addNode(e)}return await e.getKey()}async delete(t){if(!g(this,Pt,"f"))throw Nt;const e=$(t),i=E(this.maxLevels,e.value);let s=g(this,_t,"f");const n=[];for(let t=0;t<g(this,Ft,"f");t+=1){const r=await this.getNode(s);if(void 0===r)throw Ut;switch(r.type){case 2:throw mt;case 1:if(b(e.bytes,r.entry[0].value))return void await this.rmAndUpload(i,e,n);throw mt;case 0:i[t]?(s=r.childR,n.push(r.childL)):(s=r.childL,n.push(r.childR));break;default:throw At}}throw mt}async rmAndUpload(t,e,i){if(0===i.length)return v(this,_t,M,"f"),void await g(this,Wt,"f").setRoot(g(this,_t,"f"));const s=i[i.length-1];i.length<2&&(v(this,_t,i[0],"f"),await g(this,Wt,"f").setRoot(g(this,_t,"f")));for(let e=i.length-2;e>=0;e-=1){if(!b(i[e].value,M.value)){let n;n=t[e]?new X(i[e],s):new X(s,i[e]),await this.addNode(n);const r=await this.recalculatePathUntilRoot(t,n,i.slice(0,e));v(this,_t,r,"f"),await g(this,Wt,"f").setRoot(g(this,_t,"f"));break}if(0===e){v(this,_t,s,"f"),await g(this,Wt,"f").setRoot(g(this,_t,"f"));break}}}async recWalk(t,e){const i=await this.getNode(t);if(void 0===i)throw Ut;switch(i.type){case 2:case 1:await e(i);break;case 0:await e(i),await this.walk(i.childL,e),await this.walk(i.childR,e);break;default:throw At}}async walk(t,e){b(t.value,M.value)&&(t=await this.root()),await this.walk(t,e)}async generateCircomVerifierProof(t,e){const i=await this.generateSCVerifierProof(t,e);return i.siblings=T(i.siblings,this.maxLevels),i}async generateSCVerifierProof(t,e){b(e.value,M.value)&&(e=await this.root());const{proof:i,value:s}=await this.generateProof(t,e),n=new Et;return n.root=e,n.siblings=i.siblings,void 0!==i.nodeAux?(n.oldKey=i.nodeAux.key,n.oldValue=i.nodeAux.value):(n.oldKey=M,n.oldValue=M),n.key=$(t),n.value=$(s),i.existence?n.fnc=0:n.fnc=1,n}async generateProof(t,e){const i=new St;let s;const n=$(t),r=E(this.maxLevels,n.value);e||(e=await this.root());let a=e;for(i.depth=0;i.depth<this.maxLevels;i.depth+=1){const t=await this.getNode(a);if(void 0===t)throw Ut;switch(t.type){case 2:return{proof:i,value:BigInt("0")};case 1:return b(n.value,t.entry[0].value)?(i.existence=!0,{proof:i,value:t.entry[1].bigInt()}):(i.nodeAux={key:t.entry[0],value:t.entry[1]},{proof:i,value:t.entry[1].bigInt()});case 0:r[i.depth]?(a=t.childR,s=t.childL):(a=t.childL,s=t.childR);break;default:throw At}b(s.value,M.value)||(I(i.notEmpties,i.depth),i.siblings.push(s))}throw mt}async addAndGetCircomProof(t,e){const i=new Kt;i.fnc=2,i.oldRoot=await this.root();let s=BigInt("0"),n=BigInt("0"),r=[];try{const e=await this.get(t);s=e.key,n=e.value,r=e.siblings}catch(t){if(t!==mt)throw t}if(void 0===s||void 0===n)throw"key/value undefined";return i.oldKey=$(s),i.oldValue=$(n),b(i.oldKey.value,M.value)&&(i.isOld0=!0),i.siblings=T(r,this.maxLevels),await this.add(t,e),i.newKey=$(t),i.newValue=$(e),i.newRoot=await this.root(),i}async graphViz(t){let e=0;await this.walk(t,(async t=>{const i=await t.getKey();let s,n;switch(t.type){case 2:break;case 1:console.log(`"${i.string()}" [style=filled]`);break;case 0:s=[t.childL.string(),t.childR.string()],n="",s.forEach(((t,i)=>{"0"===t&&(s[i]=`empty${e}`,n+=`"${s[i]}" [style=dashed,label=0];\n`,e+=1)})),console.log(`"${i.string()}" -> {"${s[1]}"}`),console.log(n)}})),console.log("}\n")}async printGraphViz(t){b(t.value,M.value)&&(t=await this.root()),console.log(`--------\nGraphViz of the MerkleTree with RootKey ${t.bigInt().toString(10)}\n`),await this.graphViz(M),console.log(`End of GraphViz of the MerkleTree with RootKey ${t.bigInt().toString(10)}\n--------\n`)}}Wt=new WeakMap,_t=new WeakMap,Pt=new WeakMap,Ft=new WeakMap;export{Kt as CircomProcessorProof,Et as CircomVerifierProof,c as DATA_LEN,u as DATA_LEN_BYTES,ut as Data,l as ELEM_BYTES_LEN,h as EMPTY_NODE_STRING,o as EMPTY_NODE_VALUE,ct as ElemBytes,gt as Entry,Lt as ErrEntryIndexAlreadyExists,Rt as ErrInvalidDBValue,At as ErrInvalidNodeFound,It as ErrInvalidProofBytes,mt as ErrKeyNotFound,xt as ErrNodeBytesBadSize,bt as ErrNodeKeyAlreadyExists,Ut as ErrNotFound,Nt as ErrNotWritable,kt as ErrReachedMaxLevel,w as FIELD_SIZE,i as HASH_BYTES_LENGTH,V as Hash,C as InMemoryDB,lt as IndexedDBStorage,Z as LocalStorageDB,y as MAX_NUM_IN_FIELD,Tt as Merkletree,r as NODE_TYPE_EMPTY,n as NODE_TYPE_LEAF,s as NODE_TYPE_MIDDLE,a as NODE_VALUE_BYTE_ARR_LENGTH,d as NOT_EMPTIES_LEN,Y as NodeEmpty,Q as NodeLeaf,X as NodeMiddle,f as PROOF_FLAG_LEN,St as Proof,M as ZERO_HASH,S as bigIntToUINT8Array,B as bigint2Array,x as bytes2BinaryString,L as bytes2Hex,b as bytesEqual,p as checkBigIntInField,pt as checkEntryInField,T as circomSiblingsFromSiblings,vt as elemBytesToBigInts,E as getPath,P as hashElems,F as hashElemsKey,N as newBigIntFromBytes,ft as newDataFromBytes,$ as newHashFromBigInt,W as newHashFromHex,_ as newHashFromString,$t as rootFromProof,I as setBitBigEndian,Vt as siblignsFroomProof,K as siblings2Bytes,U as str2Bytes,m as swapEndianness,k as testBit,A as testBitBigEndian,Mt as verifyProof};
import{Hex as t,poseidon as e}from"@iden3/js-crypto";const i=32,s=0,n=1,r=2,a=65,o=new Uint8Array(65),h="empty",l=32,c=8,f=256,u=2,d=30,w=BigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617"),y=w-BigInt("1");function g(t,e,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(t):s?s.value:e.get(t)}function v(t,e,i,s,n){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!n:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?n.call(t,i):n?n.value=i:e.set(t,i),i}const p=t=>t<w,b=(t,e)=>t.every(((t,i)=>t===e[i])),m=t=>t.slice().reverse(),x=t=>"0b"+t.reduce(((t,e)=>t+e.toString(2).padStart(8,"0")),""),I=(t,e)=>0!=(t[parseInt((e/8).toString())]&1<<e%8),A=(t,e)=>0!=(t[t.length-parseInt(""+e/8)-1]&1<<e%8),k=(t,e)=>{t[t.length-parseInt(""+e/8)-1]|=1<<e%8},R="0123456789abcdef",S=t=>{const e=new Array(2*t.length);let i=0;return t.forEach((t=>{e[i]=R[parseInt((t>>4).toString(10))],e[i+1]=R[parseInt((15&t).toString(10))],i+=2})),e.join("")},L=t=>{if(t.length!==i)throw`Expected 32 bytes, found ${t.length} bytes`;const e=BigInt(x(t));if(!p(e))throw"NewBigIntFromHashBytes: Value not inside the Finite Field";return e},B=t=>new Uint8Array(2*t.length).map(((e,i)=>t.charCodeAt(i))),N=(t,e)=>{const i=new Array(t);for(let s=0;s<t;s+=1)i[s]=I(e,s);return i},U=t=>{const e=new ArrayBuffer(i*t.length),s=new Uint8Array(e);return t.forEach(((t,e)=>{s.set(t.value,e*i)})),s},E=(t,e)=>t.toString(e||10).split("").map((t=>parseInt(t))),K=t=>{const e=BigInt(256),s=new Uint8Array(i);let n=0;for(;t>BigInt(0);)s[31-n]=Number(t%e),t/=e,n+=1;return s};class V{constructor(t){if(t?.length){if(t.length!==i)throw new Error(`Expected 32 bytes, found ${t.length} bytes`);this.bytes=t}else this.bytes=new Uint8Array(i)}get value(){return this.bytes}set value(t){if(t.length!==i)throw`Expected 32 bytes, found ${t.length} bytes`;this.bytes=m(t)}string(){return this.bigInt().toString(10)}hex(){return S(this.bytes)}equals(t){return b(this.value,t.value)}bigInt(){const t=m(this.value);return BigInt(x(t))}static fromString(t){try{return V.fromBigInt(BigInt(t))}catch(e){const i=JSON.parse(t),s=Uint8Array.from(Object.values(i.bytes));return new V(s)}}static fromBigInt(t){if(!p(t))throw new Error("NewBigIntFromHashBytes: Value not inside the Finite Field");const e=K(t);return new V(m(e))}static fromHex(e){return e?new V(t.decodeString(e)):M}toJSON(){return this.string()}}const M=new V,W=t=>V.fromBigInt(t),$=t=>V.fromHex(t),O=t=>V.fromString(t),_=t=>{const i=e.hash(t);return V.fromBigInt(i)},J=(t,i)=>{const s=e.hash([...i,t]);return V.fromBigInt(s)},P=(t,e)=>{for(let i=t.length;i<e;i+=1)t.push(M);return t};var T,H;class j{constructor(t){T.set(this,void 0),H.set(this,void 0),this.prefix=t,v(this,T,{},"f"),v(this,H,M,"f")}async get(t){const e=new Uint8Array([...this.prefix,...t]);return g(this,T,"f")[e.toString()]?g(this,T,"f")[e.toString()]:void 0}async put(t,e){const i=new Uint8Array([...this.prefix,...t]);g(this,T,"f")[i.toString()]=e}async getRoot(){return g(this,H,"f")}async setRoot(t){v(this,H,t,"f")}}T=new WeakMap,H=new WeakMap;const z=async(t,e)=>J(BigInt(1),[t.bigInt(),e.bigInt()]),C=(t,e,i)=>{const s=new Uint8Array(65),n=K(e.bigInt()),r=K(i.bigInt());s[0]=t;for(let t=1;t<33;t+=1)s[t]=n[t-1];for(let t=33;t<=65;t+=1)s[t]=r[t-33];return s};var q,F,D,G;class Q{constructor(t,e){q.set(this,void 0),this.type=1,this.entry=[t,e],v(this,q,M,"f")}async getKey(){return g(this,q,"f")===M?await z(this.entry[0],this.entry[1]):g(this,q,"f")}get value(){return C(this.type,this.entry[0],this.entry[1])}get string(){return`Leaf I:${this.entry[0]} D:${this.entry[1]}`}}q=new WeakMap;class X{constructor(t,e){F.set(this,void 0),this.type=0,this.childL=t,this.childR=e,v(this,F,M,"f")}async getKey(){return g(this,F,"f")===M?_([this.childL.bigInt(),this.childR.bigInt()]):g(this,F,"f")}get value(){return C(this.type,this.childL,this.childR)}get string(){return`Middle L:${this.childL} R:${this.childR}`}}F=new WeakMap;class Y{constructor(){D.set(this,void 0),this.type=2,v(this,D,M,"f")}async getKey(){return M}get value(){return o}get string(){return h}}D=new WeakMap;class Z{constructor(t){this._prefix=t,G.set(this,void 0);const e=localStorage.getItem(S(t));if(e){const t=JSON.parse(e);v(this,G,new V(Uint8Array.from(t)),"f")}else v(this,G,M,"f")}async get(t){const e=new Uint8Array([...this._prefix,...t]),i=S(e),s=localStorage.getItem(i);if(null===s)return;const n=JSON.parse(s);switch(n.type){case 2:return new Y;case 0:const t=new V(Uint8Array.from(n.childL)),e=new V(Uint8Array.from(n.childR));return new X(t,e);case 1:const i=new V(Uint8Array.from(n.entry[0])),s=new V(Uint8Array.from(n.entry[1]));return new Q(i,s)}throw`error: value found for key ${S(e)} is not of type Node`}async put(t,e){const i=new Uint8Array([...this._prefix,...t]),s=S(i),n={type:e.type};e instanceof X?(n.childL=Array.from(e.childL.bytes),n.childR=Array.from(e.childR.bytes)):e instanceof Q&&(n.entry=[Array.from(e.entry[0].bytes),Array.from(e.entry[1].bytes)]);const r=JSON.stringify(n);localStorage.setItem(s,r)}async getRoot(){return g(this,G,"f")}async setRoot(t){v(this,G,t,"f"),localStorage.setItem(S(this._prefix),JSON.stringify(Array.from(t.bytes)))}}function tt(t){return new Promise(((e,i)=>{t.oncomplete=t.onsuccess=()=>e(t.result),t.onabort=t.onerror=()=>i(t.error)}))}function et(t,e){const i=indexedDB.open(t);i.onupgradeneeded=()=>i.result.createObjectStore(e);const s=tt(i);return(t,i)=>s.then((s=>i(s.transaction(e,t).objectStore(e))))}let it;function st(){return it||(it=et("keyval-store","keyval")),it}function nt(t,e=st()){return e("readonly",(e=>tt(e.get(t))))}function rt(t,e,i=st()){return i("readwrite",(i=>(i.put(e,t),tt(i.transaction))))}var at,ot,ht;G=new WeakMap;class lt{constructor(t,e){this._prefix=t,at.set(this,void 0),v(this,at,M,"f"),this._prefixHash=S(t),this._store=et(`${e??lt.storageName}-db`,lt.storageName)}async get(t){const e=new Uint8Array([...this._prefix,...t]),i=S(e),s=await nt(i,this._store);if(null!=s){if(2===s.type)return new Y;if(0===s.type){const t=new V(Uint8Array.from(s.childL.bytes)),e=new V(Uint8Array.from(s.childR.bytes));return new X(t,e)}if(1===s.type){const t=new V(Uint8Array.from(s.entry[0].bytes)),e=new V(Uint8Array.from(s.entry[1].bytes));return new Q(t,e)}throw new Error(`error: value found for key ${i} is not of type Node`)}}async put(t,e){const i=new Uint8Array([...this._prefix,...t]),s=S(i);await rt(s,e,this._store)}async getRoot(){if(!g(this,at,"f").equals(M))return g(this,at,"f");const t=await nt(this._prefixHash,this._store);return v(this,at,t?new V(t.bytes):M,"f"),g(this,at,"f")}async setRoot(t){await rt(this._prefixHash,t,this._store),v(this,at,t,"f")}}at=new WeakMap,lt.storageName="merkle-tree";class ct{constructor(){ot.set(this,void 0),v(this,ot,new Uint8Array(l),"f")}get value(){return g(this,ot,"f")}set value(t){v(this,ot,t,"f")}bigInt(){return L(m(g(this,ot,"f")))}string(){return`${S(g(this,ot,"f").slice(0,4))}...`}}ot=new WeakMap;class ft{constructor(){ht.set(this,void 0),v(this,ht,new Array(8),"f")}get value(){return g(this,ht,"f")}set value(t){if(8!==t.length)throw`expected bytes length to be 8, got ${t.length}`;v(this,ht,t,"f")}bytes(){const t=new Uint8Array(256);for(let e=0;e<8;e+=1)g(this,ht,"f")[e].value.forEach(((i,s)=>{t[e*l+s]=i}));return t}equal(t){return b(g(this,ht,"f")[0].value,t.value[0].value)&&b(g(this,ht,"f")[1].value,t.value[1].value)&&b(g(this,ht,"f")[2].value,t.value[2].value)&&b(g(this,ht,"f")[3].value,t.value[3].value)}}ht=new WeakMap;const ut=t=>{if(t.length!==f)throw`expected bytes length to be 256, got ${t.length}`;const e=new ft,i=new Array(f);for(let e=0;e<8;e+=1){const s=new ct;s.value=t.slice(e*l,(e+1)*f),i[e]=s}return e.value=i,e};var dt,wt,yt;class gt{constructor(t){dt.set(this,void 0),wt.set(this,void 0),yt.set(this,void 0),v(this,dt,t||new ft,"f"),v(this,wt,M,"f"),v(this,yt,M,"f")}get data(){return g(this,dt,"f")}get index(){return g(this,dt,"f").value.slice(0,4)}get value(){return g(this,dt,"f").value.slice(4,8)}async hIndex(){return g(this,wt,"f")===M?_(vt(this.index)):g(this,wt,"f")}async hValue(){return g(this,yt,"f")===M?_(vt(this.value)):g(this,yt,"f")}hiHv(){return(async()=>({hi:await this.hIndex(),hv:await this.hValue()}))()}bytes(){return g(this,dt,"f").value}equal(t){return g(this,dt,"f").equal(t.data)}clone(){return new gt(g(this,dt,"f"))}}dt=new WeakMap,wt=new WeakMap,yt=new WeakMap;const vt=t=>t.map((t=>t.bigInt())),pt=t=>{const e=vt(t.data.value);let i=!0;return e.forEach((t=>{p(t)||(i=!1)})),i},bt="key already exists",mt="Key not found in the MerkleTree",xt="node data has incorrect size in the DB",It="reached maximum level of the merkle tree",At="found an invalid node in the DB",kt="the serialized proof is invalid",Rt="the value in the DB is invalid",St="the entry index already exists in the tree",Lt="Merkle Tree not writable",Bt="key not found";class Nt{constructor(t=M,e=[],i=M,s=M,n=!1,r=M,a=M,o=0){this.root=t,this.siblings=e,this.oldKey=i,this.oldValue=s,this.isOld0=n,this.key=r,this.value=a,this.fnc=o}}class Ut{constructor(t=M,e=M,i=[],s=M,n=M,r=M,a=M,o=!1,h=0){this.oldRoot=t,this.newRoot=e,this.siblings=i,this.oldKey=s,this.oldValue=n,this.newKey=r,this.newValue=a,this.isOld0=o,this.fnc=h}}const Et="non-existence proof being checked against hIndex equal to nodeAux";class Kt{constructor(t){this.existence=t?.existence??!1,this.depth=t?.siblings.length??0,this.nodeAux=t?.nodeAux;const{siblings:e,notEmpties:i}=this.reduceSiblings(t?.siblings);this.siblings=e,this.notEmpties=i}bytes(){let t=2+this.notEmpties.length+l*this.siblings.length;void 0!==this.nodeAux&&(t+=64);const e=new ArrayBuffer(t),i=new Uint8Array(e);this.existence||(i[0]|=1),i[1]=this.depth,i.set(this.notEmpties,2);const s=U(this.siblings);return i.set(s,this.notEmpties.length+2),void 0!==this.nodeAux&&(i[0]|=2,i.set(this.nodeAux.key.value,i.length-64),i.set(this.nodeAux.value.value,i.length-32)),i}toJSON(){return{existence:this.existence,siblings:this.allSiblings().map((t=>t.toJSON())),nodeAux:this.nodeAux?{key:this.nodeAux.key.toJSON(),value:this.nodeAux.value.toJSON()}:void 0}}reduceSiblings(t){const e=[],i=new Uint8Array(30);if(!t)return{siblings:e,notEmpties:i};for(let s=0;s<t.length;s++){const n=t[s];JSON.stringify(t[s])!==JSON.stringify(M)&&(k(i,s),e.push(n))}return{notEmpties:i,siblings:e}}static fromJSON(t){let e;t.nodeAux&&(e={key:V.fromString(t.nodeAux.key),value:V.fromString(t.nodeAux.value)});const i=t.existence??!1,s=t.siblings.map((t=>"string"==typeof t?V.fromString(t):new V(t)));return new Kt({existence:i,nodeAux:e,siblings:s})}allSiblings(){let t=0;const e=[];for(let i=0;i<this.depth;i+=1)A(this.notEmpties,i)?(e.push(this.siblings[t]),t+=1):e.push(M);return e}}const Vt=t=>t.allSiblings(),Mt=async(t,e,i,s)=>{try{const n=await Wt(e,i,s);return b(t.value,n.value)}catch(t){if(t===Et)return!1;throw t}},Wt=async(t,e,i)=>{const s=V.fromBigInt(e),n=V.fromBigInt(i);let r;if(t.existence)r=await z(s,n);else if(void 0===t.nodeAux)r=M;else{const e=t.nodeAux;if(b(s.value,e.key.value))throw Et;r=await z(e.key,e.value)}const a=t.allSiblings(),o=N(a.length,s.value);for(let t=a.length-1;t>=0;t-=1)r=o[t]?await new X(a[t],r).getKey():await new X(r,a[t]).getKey();return r};var $t,Ot,_t,Jt;class Pt{constructor(t,e,i){$t.set(this,void 0),Ot.set(this,void 0),_t.set(this,void 0),Jt.set(this,void 0),v(this,$t,t,"f"),v(this,_t,e,"f"),v(this,Jt,i,"f")}async root(){return g(this,Ot,"f")||v(this,Ot,await g(this,$t,"f").getRoot(),"f"),g(this,Ot,"f")}get maxLevels(){return g(this,Jt,"f")}async add(t,e){if(!g(this,_t,"f"))throw Lt;v(this,Ot,await this.root(),"f");const i=V.fromBigInt(t),s=V.fromBigInt(e),n=new Q(i,s),r=N(this.maxLevels,i.value),a=await this.addLeaf(n,g(this,Ot,"f"),0,r);v(this,Ot,a,"f"),await g(this,$t,"f").setRoot(g(this,Ot,"f"))}async updateNode(t){if(!g(this,_t,"f"))throw Lt;if(2===t.type)return await t.getKey();const e=await t.getKey();return await g(this,$t,"f").put(e.value,t),e}async addNode(t){if(!g(this,_t,"f"))throw Lt;if(2===t.type)return await t.getKey();const e=await t.getKey();return await g(this,$t,"f").put(e.value,t),e}async addEntry(t){if(!g(this,_t,"f"))throw Lt;if(!pt(t))throw"elements not inside the finite field over r";v(this,Ot,await g(this,$t,"f").getRoot(),"f");const e=await t.hIndex(),i=await t.hValue(),s=new Q(e,i),n=N(this.maxLevels,e.value),r=await this.addLeaf(s,g(this,Ot,"f"),0,n);v(this,Ot,r,"f"),await g(this,$t,"f").setRoot(g(this,Ot,"f"))}async pushLeaf(t,e,i,s,n){if(i>g(this,Jt,"f")-2)throw new Error(It);let r;if(s[i]===n[i]){const a=await this.pushLeaf(t,e,i+1,s,n);return r=s[i]?new X(new V,a):new X(a,new V),await this.addNode(r)}const a=await e.getKey(),o=await t.getKey();return r=s[i]?new X(a,o):new X(o,a),await this.addNode(t),await this.addNode(r)}async addLeaf(t,e,i,s){if(i>g(this,Jt,"f")-1)throw new Error(It);const n=await this.getNode(e);if(void 0===n)throw Bt;switch(n.type){case 2:return this.addNode(t);case 1:{const e=n.entry[0],r=t.entry[0];if(b(e.value,r.value))throw St;const a=N(this.maxLevels,e.value);return this.pushLeaf(t,n,i,s,a)}case 0:{let e;if(s[i]){const r=await this.addLeaf(t,n.childR,i+1,s);e=new X(n.childL,r)}else{const r=await this.addLeaf(t,n.childL,i+1,s);e=new X(r,n.childR)}return this.addNode(e)}default:throw At}}async get(t){const e=V.fromBigInt(t),i=N(this.maxLevels,e.value);let s=await this.root();const n=[];for(let t=0;t<this.maxLevels;t++){const e=await this.getNode(s);if(void 0===e)throw mt;switch(e.type){case 2:return{key:BigInt("0"),value:BigInt("0"),siblings:n};case 1:return{key:e.entry[0].bigInt(),value:e.entry[1].bigInt(),siblings:n};case 0:i[t]?(s=e.childR,n.push(e.childL)):(s=e.childL,n.push(e.childR));break;default:throw At}}throw new Error(It)}async update(t,e){if(!g(this,_t,"f"))throw Lt;if(!p(t))throw"key not inside the finite field";if(!p(e))throw"key not inside the finite field";const i=V.fromBigInt(t),s=V.fromBigInt(e),n=N(this.maxLevels,i.value),r=new Ut;r.fnc=1,r.oldRoot=await this.root(),r.oldKey=i,r.newKey=i,r.newValue=s;let a=await this.root();const o=[];for(let t=0;t<this.maxLevels;t+=1){const e=await this.getNode(a);if(void 0===e)throw Bt;switch(e.type){case 2:throw mt;case 1:if(b(i.value,e.entry[0].value)){r.oldValue=e.entry[1],r.siblings=P([...o],this.maxLevels);const t=new Q(i,s);await this.updateNode(t);const a=await this.recalculatePathUntilRoot(n,t,o);return v(this,Ot,a,"f"),await g(this,$t,"f").setRoot(a),r.newRoot=a,r}break;case 0:n[t]?(a=e.childR,o.push(e.childL)):(a=e.childL,o.push(e.childR));break;default:throw At}}throw mt}async getNode(t){return b(t.value,M.value)?new Y:await g(this,$t,"f").get(t.value)}async recalculatePathUntilRoot(t,e,i){for(let s=i.length-1;s>=0;s-=1){const n=await e.getKey();e=t[s]?new X(i[s],n):new X(n,i[s]),await this.addNode(e)}return await e.getKey()}async delete(t){if(!g(this,_t,"f"))throw Lt;const e=V.fromBigInt(t),i=N(this.maxLevels,e.value);let s=g(this,Ot,"f");const n=[];for(let t=0;t<g(this,Jt,"f");t+=1){const r=await this.getNode(s);if(void 0===r)throw Bt;switch(r.type){case 2:throw mt;case 1:if(b(e.bytes,r.entry[0].value))return void await this.rmAndUpload(i,e,n);throw mt;case 0:i[t]?(s=r.childR,n.push(r.childL)):(s=r.childL,n.push(r.childR));break;default:throw At}}throw mt}async rmAndUpload(t,e,i){if(0===i.length)return v(this,Ot,M,"f"),void await g(this,$t,"f").setRoot(g(this,Ot,"f"));const s=i[i.length-1];i.length<2&&(v(this,Ot,i[0],"f"),await g(this,$t,"f").setRoot(g(this,Ot,"f")));for(let e=i.length-2;e>=0;e-=1){if(!b(i[e].value,M.value)){let n;n=t[e]?new X(i[e],s):new X(s,i[e]),await this.addNode(n);const r=await this.recalculatePathUntilRoot(t,n,i.slice(0,e));v(this,Ot,r,"f"),await g(this,$t,"f").setRoot(g(this,Ot,"f"));break}if(0===e){v(this,Ot,s,"f"),await g(this,$t,"f").setRoot(g(this,Ot,"f"));break}}}async recWalk(t,e){const i=await this.getNode(t);if(void 0===i)throw Bt;switch(i.type){case 2:case 1:await e(i);break;case 0:await e(i),await this.recWalk(i.childL,e),await this.recWalk(i.childR,e);break;default:throw At}}async walk(t,e){b(t.value,M.value)&&(t=await this.root()),await this.recWalk(t,e)}async generateCircomVerifierProof(t,e){const i=await this.generateSCVerifierProof(t,e);return i.siblings=P(i.siblings,this.maxLevels),i}async generateSCVerifierProof(t,e){b(e.value,M.value)&&(e=await this.root());const{proof:i,value:s}=await this.generateProof(t,e),n=new Nt;return n.root=e,n.siblings=i.allSiblings(),void 0!==i.nodeAux?(n.oldKey=i.nodeAux.key,n.oldValue=i.nodeAux.value):(n.oldKey=M,n.oldValue=M),n.key=V.fromBigInt(t),n.value=V.fromBigInt(s),i.existence?n.fnc=0:n.fnc=1,n}async generateProof(t,e){let i;const s=V.fromBigInt(t),n=N(this.maxLevels,s.value);e||(e=await this.root());let r=e,a=0,o=!1;const h=[];let l;for(a=0;a<this.maxLevels;a+=1){const t=await this.getNode(r);if(void 0===t)throw Bt;switch(t.type){case 2:return{proof:new Kt({existence:o,nodeAux:l,siblings:h}),value:BigInt("0")};case 1:return b(s.value,t.entry[0].value)?(o=!0,{proof:new Kt({existence:o,nodeAux:l,siblings:h}),value:t.entry[1].bigInt()}):(l={key:t.entry[0],value:t.entry[1]},{proof:new Kt({existence:o,nodeAux:l,siblings:h}),value:t.entry[1].bigInt()});case 0:n[a]?(r=t.childR,i=t.childL):(r=t.childL,i=t.childR);break;default:throw At}h.push(i)}throw mt}async addAndGetCircomProof(t,e){const i=new Ut;i.fnc=2,i.oldRoot=await this.root();let s=BigInt("0"),n=BigInt("0"),r=[];try{const e=await this.get(t);s=e.key,n=e.value,r=e.siblings}catch(t){if(t!==mt)throw t}if(void 0===s||void 0===n)throw"key/value undefined";return i.oldKey=V.fromBigInt(s),i.oldValue=V.fromBigInt(n),b(i.oldKey.value,M.value)&&(i.isOld0=!0),i.siblings=P(r,this.maxLevels),await this.add(t,e),i.newKey=V.fromBigInt(t),i.newValue=V.fromBigInt(e),i.newRoot=await this.root(),i}async graphViz(t){let e=0;await this.walk(t,(async t=>{const i=await t.getKey();let s,n;switch(t.type){case 2:break;case 1:console.log(`"${i.string()}" [style=filled]`);break;case 0:s=[t.childL.string(),t.childR.string()],n="",s.forEach(((t,i)=>{"0"===t&&(s[i]=`empty${e}`,n+=`"${s[i]}" [style=dashed,label=0];\n`,e+=1)})),console.log(`"${i.string()}" -> {"${s[1]}"}`),console.log(n)}})),console.log("}\n")}async printGraphViz(t){b(t.value,M.value)&&(t=await this.root()),console.log(`--------\nGraphViz of the MerkleTree with RootKey ${t.bigInt().toString(10)}\n`),await this.graphViz(M),console.log(`End of GraphViz of the MerkleTree with RootKey ${t.bigInt().toString(10)}\n--------\n`)}}$t=new WeakMap,Ot=new WeakMap,_t=new WeakMap,Jt=new WeakMap;export{Ut as CircomProcessorProof,Nt as CircomVerifierProof,c as DATA_LEN,f as DATA_LEN_BYTES,ft as Data,l as ELEM_BYTES_LEN,h as EMPTY_NODE_STRING,o as EMPTY_NODE_VALUE,ct as ElemBytes,gt as Entry,St as ErrEntryIndexAlreadyExists,Rt as ErrInvalidDBValue,At as ErrInvalidNodeFound,kt as ErrInvalidProofBytes,mt as ErrKeyNotFound,xt as ErrNodeBytesBadSize,bt as ErrNodeKeyAlreadyExists,Bt as ErrNotFound,Lt as ErrNotWritable,It as ErrReachedMaxLevel,w as FIELD_SIZE,i as HASH_BYTES_LENGTH,V as Hash,j as InMemoryDB,lt as IndexedDBStorage,Z as LocalStorageDB,y as MAX_NUM_IN_FIELD,Pt as Merkletree,r as NODE_TYPE_EMPTY,n as NODE_TYPE_LEAF,s as NODE_TYPE_MIDDLE,a as NODE_VALUE_BYTE_ARR_LENGTH,d as NOT_EMPTIES_LEN,Y as NodeEmpty,Q as NodeLeaf,X as NodeMiddle,u as PROOF_FLAG_LEN,Kt as Proof,M as ZERO_HASH,K as bigIntToUINT8Array,E as bigint2Array,x as bytes2BinaryString,S as bytes2Hex,b as bytesEqual,p as checkBigIntInField,pt as checkEntryInField,P as circomSiblingsFromSiblings,vt as elemBytesToBigInts,N as getPath,_ as hashElems,J as hashElemsKey,L as newBigIntFromBytes,ut as newDataFromBytes,W as newHashFromBigInt,$ as newHashFromHex,O as newHashFromString,Wt as rootFromProof,k as setBitBigEndian,Vt as siblignsFroomProof,U as siblings2Bytes,B as str2Bytes,m as swapEndianness,I as testBit,A as testBitBigEndian,Mt as verifyProof};
//# sourceMappingURL=index.js.map

@@ -43,33 +43,53 @@ "use strict";

}
static fromString(s) {
try {
return Hash.fromBigInt(BigInt(s));
}
catch (e) {
const deserializedHash = JSON.parse(s);
const bytes = Uint8Array.from(Object.values(deserializedHash.bytes));
return new Hash(bytes);
}
}
static fromBigInt(i) {
if (!(0, utils_1.checkBigIntInField)(i)) {
throw new Error('NewBigIntFromHashBytes: Value not inside the Finite Field');
}
const bytes = (0, utils_1.bigIntToUINT8Array)(i);
return new Hash((0, utils_1.swapEndianness)(bytes));
}
static fromHex(h) {
if (!h) {
return exports.ZERO_HASH;
}
return new Hash(js_crypto_1.Hex.decodeString(h));
}
toJSON() {
return this.string();
}
}
exports.Hash = Hash;
exports.ZERO_HASH = new Hash();
// returned bytes endianess will be big-endian
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use Hash.fromBigInt instead
*/
const newHashFromBigInt = (bigNum) => {
if (!(0, utils_1.checkBigIntInField)(bigNum)) {
throw 'NewBigIntFromHashBytes: Value not inside the Finite Field';
}
const bytes = (0, utils_1.bigIntToUINT8Array)(bigNum);
const hash = new Hash();
hash.value = bytes;
return hash;
return Hash.fromBigInt(bigNum);
};
exports.newHashFromBigInt = newHashFromBigInt;
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use Hash.fromBigInt instead
*/
const newHashFromHex = (h) => {
if (!h) {
return exports.ZERO_HASH;
}
// TODO: add in field check
const hash = new Hash();
hash.value = (0, utils_1.swapEndianness)(js_crypto_1.Hex.decodeString(h));
return hash;
return Hash.fromHex(h);
};
exports.newHashFromHex = newHashFromHex;
// return object of class Hash from a decimal string
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use Hash.fromBigString instead
*/
const newHashFromString = (decimalString) => {
const bigNum = BigInt(decimalString);
if (!(0, utils_1.checkBigIntInField)(bigNum)) {
throw 'NewBigIntFromHashBytes: Value not inside the Finite Field';
}
return (0, exports.newHashFromBigInt)(bigNum);
return Hash.fromString(decimalString);
};

@@ -79,3 +99,3 @@ exports.newHashFromString = newHashFromString;

const hashBigInt = js_crypto_1.poseidon.hash(e);
return (0, exports.newHashFromBigInt)(hashBigInt);
return Hash.fromBigInt(hashBigInt);
};

@@ -85,3 +105,3 @@ exports.hashElems = hashElems;

const hashBigInt = js_crypto_1.poseidon.hash([...e, k]);
return (0, exports.newHashFromBigInt)(hashBigInt);
return Hash.fromBigInt(hashBigInt);
};

@@ -88,0 +108,0 @@ exports.hashElemsKey = hashElemsKey;

@@ -49,4 +49,4 @@ "use strict";

__classPrivateFieldSet(this, _Merkletree_root, await this.root(), "f");
const kHash = (0, hash_1.newHashFromBigInt)(k);
const vHash = (0, hash_1.newHashFromBigInt)(v);
const kHash = hash_1.Hash.fromBigInt(k);
const vHash = hash_1.Hash.fromBigInt(v);
const newNodeLeaf = new node_1.NodeLeaf(kHash, vHash);

@@ -164,3 +164,3 @@ const path = (0, utils_1.getPath)(this.maxLevels, kHash.value);

async get(k) {
const kHash = (0, hash_1.newHashFromBigInt)(k);
const kHash = hash_1.Hash.fromBigInt(k);
const path = (0, utils_1.getPath)(this.maxLevels, kHash.value);

@@ -220,4 +220,4 @@ let nextKey = await this.root();

}
const kHash = (0, hash_1.newHashFromBigInt)(k);
const vHash = (0, hash_1.newHashFromBigInt)(v);
const kHash = hash_1.Hash.fromBigInt(k);
const vHash = hash_1.Hash.fromBigInt(v);
const path = (0, utils_1.getPath)(this.maxLevels, kHash.value);

@@ -303,3 +303,3 @@ const cp = new circom_1.CircomProcessorProof();

}
const kHash = (0, hash_1.newHashFromBigInt)(k);
const kHash = hash_1.Hash.fromBigInt(k);
const path = (0, utils_1.getPath)(this.maxLevels, kHash.value);

@@ -385,4 +385,4 @@ let nextKey = __classPrivateFieldGet(this, _Merkletree_root, "f");

await f(n);
await this.walk(n.childL, f);
await this.walk(n.childR, f);
await this.recWalk(n.childL, f);
await this.recWalk(n.childR, f);
break;

@@ -397,3 +397,3 @@ default:

}
await this.walk(rootKey, f);
await this.recWalk(rootKey, f);
}

@@ -412,3 +412,3 @@ async generateCircomVerifierProof(k, rootKey) {

cp.root = rootKey;
cp.siblings = proof.siblings;
cp.siblings = proof.allSiblings();
if (typeof proof.nodeAux !== 'undefined') {

@@ -422,4 +422,4 @@ cp.oldKey = proof.nodeAux.key;

}
cp.key = (0, hash_1.newHashFromBigInt)(k);
cp.value = (0, hash_1.newHashFromBigInt)(value);
cp.key = hash_1.Hash.fromBigInt(k);
cp.value = hash_1.Hash.fromBigInt(value);
if (proof.existence) {

@@ -434,5 +434,4 @@ cp.fnc = 0;

async generateProof(k, rootKey) {
const p = new proof_1.Proof();
let siblingKey;
const kHash = (0, hash_1.newHashFromBigInt)(k);
const kHash = hash_1.Hash.fromBigInt(k);
const path = (0, utils_1.getPath)(this.maxLevels, kHash.value);

@@ -443,3 +442,7 @@ if (!rootKey) {

let nextKey = rootKey;
for (p.depth = 0; p.depth < this.maxLevels; p.depth += 1) {
let depth = 0;
let existence = false;
const siblings = [];
let nodeAux;
for (depth = 0; depth < this.maxLevels; depth += 1) {
const n = await this.getNode(nextKey);

@@ -451,15 +454,36 @@ if (typeof n === 'undefined') {

case constants_1.NODE_TYPE_EMPTY:
return { proof: p, value: BigInt('0') };
return {
proof: new proof_1.Proof({
existence,
nodeAux,
siblings
}),
value: BigInt('0')
};
case constants_1.NODE_TYPE_LEAF:
if ((0, utils_1.bytesEqual)(kHash.value, n.entry[0].value)) {
p.existence = true;
return { proof: p, value: n.entry[1].bigInt() };
existence = true;
return {
proof: new proof_1.Proof({
existence,
nodeAux,
siblings
}),
value: n.entry[1].bigInt()
};
}
p.nodeAux = {
nodeAux = {
key: n.entry[0],
value: n.entry[1]
};
return { proof: p, value: n.entry[1].bigInt() };
return {
proof: new proof_1.Proof({
existence,
nodeAux,
siblings
}),
value: n.entry[1].bigInt()
};
case constants_1.NODE_TYPE_MIDDLE:
if (path[p.depth]) {
if (path[depth]) {
nextKey = n.childR;

@@ -476,6 +500,3 @@ siblingKey = n.childL;

}
if (!(0, utils_1.bytesEqual)(siblingKey.value, hash_1.ZERO_HASH.value)) {
(0, utils_1.setBitBigEndian)(p.notEmpties, p.depth);
p.siblings.push(siblingKey);
}
siblings.push(siblingKey);
}

@@ -505,4 +526,4 @@ throw errors_1.ErrKeyNotFound;

}
cp.oldKey = (0, hash_1.newHashFromBigInt)(key);
cp.oldValue = (0, hash_1.newHashFromBigInt)(value);
cp.oldKey = hash_1.Hash.fromBigInt(key);
cp.oldValue = hash_1.Hash.fromBigInt(value);
if ((0, utils_1.bytesEqual)(cp.oldKey.value, hash_1.ZERO_HASH.value)) {

@@ -513,4 +534,4 @@ cp.isOld0 = true;

await this.add(k, v);
cp.newKey = (0, hash_1.newHashFromBigInt)(k);
cp.newValue = (0, hash_1.newHashFromBigInt)(v);
cp.newKey = hash_1.Hash.fromBigInt(k);
cp.newValue = hash_1.Hash.fromBigInt(v);
cp.newRoot = await this.root();

@@ -517,0 +538,0 @@ return cp;

@@ -11,8 +11,9 @@ "use strict";

class Proof {
constructor() {
this.existence = false;
this.depth = 0;
this.siblings = [];
const arrBuff = new ArrayBuffer(constants_1.NOT_EMPTIES_LEN);
this.notEmpties = new Uint8Array(arrBuff);
constructor(obj) {
this.existence = obj?.existence ?? false;
this.depth = obj?.siblings.length ?? 0;
this.nodeAux = obj?.nodeAux;
const { siblings, notEmpties } = this.reduceSiblings(obj?.siblings);
this.siblings = siblings;
this.notEmpties = notEmpties;
}

@@ -40,20 +41,63 @@ bytes() {

}
toJSON() {
return {
existence: this.existence,
siblings: this.allSiblings().map((s) => s.toJSON()),
nodeAux: this.nodeAux
? {
key: this.nodeAux.key.toJSON(),
value: this.nodeAux.value.toJSON()
}
: undefined
};
}
reduceSiblings(siblings) {
const reducedSiblings = [];
const notEmpties = new Uint8Array(constants_1.NOT_EMPTIES_LEN);
if (!siblings) {
return { siblings: reducedSiblings, notEmpties };
}
for (let i = 0; i < siblings.length; i++) {
const sibling = siblings[i];
if (JSON.stringify(siblings[i]) !== JSON.stringify(hash_1.ZERO_HASH)) {
(0, utils_1.setBitBigEndian)(notEmpties, i);
reducedSiblings.push(sibling);
}
}
return { notEmpties, siblings: reducedSiblings };
}
static fromJSON(obj) {
let nodeAux = undefined;
if (obj.nodeAux) {
nodeAux = {
key: hash_1.Hash.fromString(obj.nodeAux.key),
value: hash_1.Hash.fromString(obj.nodeAux.value)
};
}
const existence = obj.existence ?? false;
const siblings = obj.siblings.map((s) => typeof s === 'string' ? hash_1.Hash.fromString(s) : new hash_1.Hash(s));
return new Proof({ existence, nodeAux, siblings });
}
allSiblings() {
return (0, exports.siblignsFroomProof)(this);
let sibIdx = 0;
const siblings = [];
for (let i = 0; i < this.depth; i += 1) {
if ((0, utils_1.testBitBigEndian)(this.notEmpties, i)) {
siblings.push(this.siblings[sibIdx]);
sibIdx += 1;
}
else {
siblings.push(hash_1.ZERO_HASH);
}
}
return siblings;
}
}
exports.Proof = Proof;
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use proof.allSiblings instead
*/
const siblignsFroomProof = (proof) => {
let sibIdx = 0;
const siblings = [];
for (let i = 0; i < proof.depth; i += 1) {
if ((0, utils_1.testBitBigEndian)(proof.notEmpties, i)) {
siblings.push(proof.siblings[sibIdx]);
sibIdx += 1;
}
else {
siblings.push(hash_1.ZERO_HASH);
}
}
return siblings;
return proof.allSiblings();
};

@@ -75,5 +119,4 @@ exports.siblignsFroomProof = siblignsFroomProof;

const rootFromProof = async (proof, k, v) => {
const kHash = (0, hash_1.newHashFromBigInt)(k);
const vHash = (0, hash_1.newHashFromBigInt)(v);
let sibIdx = proof.siblings.length - 1;
const kHash = hash_1.Hash.fromBigInt(k);
const vHash = hash_1.Hash.fromBigInt(v);
let midKey;

@@ -95,17 +138,10 @@ if (proof.existence) {

}
const path = (0, utils_1.getPath)(proof.depth, kHash.value);
let siblingKey;
for (let i = proof.depth - 1; i >= 0; i -= 1) {
if ((0, utils_1.testBitBigEndian)(proof.notEmpties, i)) {
siblingKey = proof.siblings[sibIdx];
sibIdx -= 1;
}
else {
siblingKey = hash_1.ZERO_HASH;
}
const siblings = proof.allSiblings();
const path = (0, utils_1.getPath)(siblings.length, kHash.value);
for (let i = siblings.length - 1; i >= 0; i -= 1) {
if (path[i]) {
midKey = await new node_1.NodeMiddle(siblingKey, midKey).getKey();
midKey = await new node_1.NodeMiddle(siblings[i], midKey).getKey();
}
else {
midKey = await new node_1.NodeMiddle(midKey, siblingKey).getKey();
midKey = await new node_1.NodeMiddle(midKey, siblings[i]).getKey();
}

@@ -112,0 +148,0 @@ }

@@ -40,38 +40,58 @@ import { HASH_BYTES_LENGTH } from '../../constants';

}
static fromString(s) {
try {
return Hash.fromBigInt(BigInt(s));
}
catch (e) {
const deserializedHash = JSON.parse(s);
const bytes = Uint8Array.from(Object.values(deserializedHash.bytes));
return new Hash(bytes);
}
}
static fromBigInt(i) {
if (!checkBigIntInField(i)) {
throw new Error('NewBigIntFromHashBytes: Value not inside the Finite Field');
}
const bytes = bigIntToUINT8Array(i);
return new Hash(swapEndianness(bytes));
}
static fromHex(h) {
if (!h) {
return ZERO_HASH;
}
return new Hash(Hex.decodeString(h));
}
toJSON() {
return this.string();
}
}
export const ZERO_HASH = new Hash();
// returned bytes endianess will be big-endian
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use Hash.fromBigInt instead
*/
export const newHashFromBigInt = (bigNum) => {
if (!checkBigIntInField(bigNum)) {
throw 'NewBigIntFromHashBytes: Value not inside the Finite Field';
}
const bytes = bigIntToUINT8Array(bigNum);
const hash = new Hash();
hash.value = bytes;
return hash;
return Hash.fromBigInt(bigNum);
};
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use Hash.fromBigInt instead
*/
export const newHashFromHex = (h) => {
if (!h) {
return ZERO_HASH;
}
// TODO: add in field check
const hash = new Hash();
hash.value = swapEndianness(Hex.decodeString(h));
return hash;
return Hash.fromHex(h);
};
// return object of class Hash from a decimal string
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use Hash.fromBigString instead
*/
export const newHashFromString = (decimalString) => {
const bigNum = BigInt(decimalString);
if (!checkBigIntInField(bigNum)) {
throw 'NewBigIntFromHashBytes: Value not inside the Finite Field';
}
return newHashFromBigInt(bigNum);
return Hash.fromString(decimalString);
};
export const hashElems = (e) => {
const hashBigInt = poseidon.hash(e);
return newHashFromBigInt(hashBigInt);
return Hash.fromBigInt(hashBigInt);
};
export const hashElemsKey = (k, e) => {
const hashBigInt = poseidon.hash([...e, k]);
return newHashFromBigInt(hashBigInt);
return Hash.fromBigInt(hashBigInt);
};

@@ -78,0 +98,0 @@ export const circomSiblingsFromSiblings = (siblings, levels) => {

@@ -13,6 +13,6 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {

var _Merkletree_db, _Merkletree_root, _Merkletree_writable, _Merkletree_maxLevel;
import { Hash, ZERO_HASH, circomSiblingsFromSiblings, newHashFromBigInt } from '../hash/hash';
import { Hash, ZERO_HASH, circomSiblingsFromSiblings } from '../hash/hash';
import { NODE_TYPE_EMPTY, NODE_TYPE_LEAF, NODE_TYPE_MIDDLE } from '../../constants';
import { NodeEmpty, NodeLeaf, NodeMiddle } from '../node/node';
import { bytesEqual, getPath, setBitBigEndian } from '../utils';
import { bytesEqual, getPath } from '../utils';
import { checkBigIntInField } from '../utils/crypto';

@@ -47,4 +47,4 @@ import { CircomProcessorProof, CircomVerifierProof } from './circom';

__classPrivateFieldSet(this, _Merkletree_root, await this.root(), "f");
const kHash = newHashFromBigInt(k);
const vHash = newHashFromBigInt(v);
const kHash = Hash.fromBigInt(k);
const vHash = Hash.fromBigInt(v);
const newNodeLeaf = new NodeLeaf(kHash, vHash);

@@ -162,3 +162,3 @@ const path = getPath(this.maxLevels, kHash.value);

async get(k) {
const kHash = newHashFromBigInt(k);
const kHash = Hash.fromBigInt(k);
const path = getPath(this.maxLevels, kHash.value);

@@ -218,4 +218,4 @@ let nextKey = await this.root();

}
const kHash = newHashFromBigInt(k);
const vHash = newHashFromBigInt(v);
const kHash = Hash.fromBigInt(k);
const vHash = Hash.fromBigInt(v);
const path = getPath(this.maxLevels, kHash.value);

@@ -301,3 +301,3 @@ const cp = new CircomProcessorProof();

}
const kHash = newHashFromBigInt(k);
const kHash = Hash.fromBigInt(k);
const path = getPath(this.maxLevels, kHash.value);

@@ -383,4 +383,4 @@ let nextKey = __classPrivateFieldGet(this, _Merkletree_root, "f");

await f(n);
await this.walk(n.childL, f);
await this.walk(n.childR, f);
await this.recWalk(n.childL, f);
await this.recWalk(n.childR, f);
break;

@@ -395,3 +395,3 @@ default:

}
await this.walk(rootKey, f);
await this.recWalk(rootKey, f);
}

@@ -410,3 +410,3 @@ async generateCircomVerifierProof(k, rootKey) {

cp.root = rootKey;
cp.siblings = proof.siblings;
cp.siblings = proof.allSiblings();
if (typeof proof.nodeAux !== 'undefined') {

@@ -420,4 +420,4 @@ cp.oldKey = proof.nodeAux.key;

}
cp.key = newHashFromBigInt(k);
cp.value = newHashFromBigInt(value);
cp.key = Hash.fromBigInt(k);
cp.value = Hash.fromBigInt(value);
if (proof.existence) {

@@ -432,5 +432,4 @@ cp.fnc = 0;

async generateProof(k, rootKey) {
const p = new Proof();
let siblingKey;
const kHash = newHashFromBigInt(k);
const kHash = Hash.fromBigInt(k);
const path = getPath(this.maxLevels, kHash.value);

@@ -441,3 +440,7 @@ if (!rootKey) {

let nextKey = rootKey;
for (p.depth = 0; p.depth < this.maxLevels; p.depth += 1) {
let depth = 0;
let existence = false;
const siblings = [];
let nodeAux;
for (depth = 0; depth < this.maxLevels; depth += 1) {
const n = await this.getNode(nextKey);

@@ -449,15 +452,36 @@ if (typeof n === 'undefined') {

case NODE_TYPE_EMPTY:
return { proof: p, value: BigInt('0') };
return {
proof: new Proof({
existence,
nodeAux,
siblings
}),
value: BigInt('0')
};
case NODE_TYPE_LEAF:
if (bytesEqual(kHash.value, n.entry[0].value)) {
p.existence = true;
return { proof: p, value: n.entry[1].bigInt() };
existence = true;
return {
proof: new Proof({
existence,
nodeAux,
siblings
}),
value: n.entry[1].bigInt()
};
}
p.nodeAux = {
nodeAux = {
key: n.entry[0],
value: n.entry[1]
};
return { proof: p, value: n.entry[1].bigInt() };
return {
proof: new Proof({
existence,
nodeAux,
siblings
}),
value: n.entry[1].bigInt()
};
case NODE_TYPE_MIDDLE:
if (path[p.depth]) {
if (path[depth]) {
nextKey = n.childR;

@@ -474,6 +498,3 @@ siblingKey = n.childL;

}
if (!bytesEqual(siblingKey.value, ZERO_HASH.value)) {
setBitBigEndian(p.notEmpties, p.depth);
p.siblings.push(siblingKey);
}
siblings.push(siblingKey);
}

@@ -503,4 +524,4 @@ throw ErrKeyNotFound;

}
cp.oldKey = newHashFromBigInt(key);
cp.oldValue = newHashFromBigInt(value);
cp.oldKey = Hash.fromBigInt(key);
cp.oldValue = Hash.fromBigInt(value);
if (bytesEqual(cp.oldKey.value, ZERO_HASH.value)) {

@@ -511,4 +532,4 @@ cp.isOld0 = true;

await this.add(k, v);
cp.newKey = newHashFromBigInt(k);
cp.newValue = newHashFromBigInt(v);
cp.newKey = Hash.fromBigInt(k);
cp.newValue = Hash.fromBigInt(v);
cp.newRoot = await this.root();

@@ -515,0 +536,0 @@ return cp;

import { ELEM_BYTES_LEN, NOT_EMPTIES_LEN, PROOF_FLAG_LEN } from '../../constants';
import { bytesEqual, getPath, siblings2Bytes, testBitBigEndian } from '../utils';
import { ZERO_HASH, newHashFromBigInt } from '../hash/hash';
import { bytesEqual, getPath, setBitBigEndian, siblings2Bytes, testBitBigEndian } from '../utils';
import { Hash, ZERO_HASH } from '../hash/hash';
import { NodeMiddle } from '../node/node';

@@ -8,8 +8,9 @@ import { leafKey } from '../utils/node';

export class Proof {
constructor() {
this.existence = false;
this.depth = 0;
this.siblings = [];
const arrBuff = new ArrayBuffer(NOT_EMPTIES_LEN);
this.notEmpties = new Uint8Array(arrBuff);
constructor(obj) {
this.existence = obj?.existence ?? false;
this.depth = obj?.siblings.length ?? 0;
this.nodeAux = obj?.nodeAux;
const { siblings, notEmpties } = this.reduceSiblings(obj?.siblings);
this.siblings = siblings;
this.notEmpties = notEmpties;
}

@@ -37,19 +38,62 @@ bytes() {

}
toJSON() {
return {
existence: this.existence,
siblings: this.allSiblings().map((s) => s.toJSON()),
nodeAux: this.nodeAux
? {
key: this.nodeAux.key.toJSON(),
value: this.nodeAux.value.toJSON()
}
: undefined
};
}
reduceSiblings(siblings) {
const reducedSiblings = [];
const notEmpties = new Uint8Array(NOT_EMPTIES_LEN);
if (!siblings) {
return { siblings: reducedSiblings, notEmpties };
}
for (let i = 0; i < siblings.length; i++) {
const sibling = siblings[i];
if (JSON.stringify(siblings[i]) !== JSON.stringify(ZERO_HASH)) {
setBitBigEndian(notEmpties, i);
reducedSiblings.push(sibling);
}
}
return { notEmpties, siblings: reducedSiblings };
}
static fromJSON(obj) {
let nodeAux = undefined;
if (obj.nodeAux) {
nodeAux = {
key: Hash.fromString(obj.nodeAux.key),
value: Hash.fromString(obj.nodeAux.value)
};
}
const existence = obj.existence ?? false;
const siblings = obj.siblings.map((s) => typeof s === 'string' ? Hash.fromString(s) : new Hash(s));
return new Proof({ existence, nodeAux, siblings });
}
allSiblings() {
return siblignsFroomProof(this);
let sibIdx = 0;
const siblings = [];
for (let i = 0; i < this.depth; i += 1) {
if (testBitBigEndian(this.notEmpties, i)) {
siblings.push(this.siblings[sibIdx]);
sibIdx += 1;
}
else {
siblings.push(ZERO_HASH);
}
}
return siblings;
}
}
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use proof.allSiblings instead
*/
export const siblignsFroomProof = (proof) => {
let sibIdx = 0;
const siblings = [];
for (let i = 0; i < proof.depth; i += 1) {
if (testBitBigEndian(proof.notEmpties, i)) {
siblings.push(proof.siblings[sibIdx]);
sibIdx += 1;
}
else {
siblings.push(ZERO_HASH);
}
}
return siblings;
return proof.allSiblings();
};

@@ -69,5 +113,4 @@ export const verifyProof = async (rootKey, proof, k, v) => {

export const rootFromProof = async (proof, k, v) => {
const kHash = newHashFromBigInt(k);
const vHash = newHashFromBigInt(v);
let sibIdx = proof.siblings.length - 1;
const kHash = Hash.fromBigInt(k);
const vHash = Hash.fromBigInt(v);
let midKey;

@@ -89,17 +132,10 @@ if (proof.existence) {

}
const path = getPath(proof.depth, kHash.value);
let siblingKey;
for (let i = proof.depth - 1; i >= 0; i -= 1) {
if (testBitBigEndian(proof.notEmpties, i)) {
siblingKey = proof.siblings[sibIdx];
sibIdx -= 1;
}
else {
siblingKey = ZERO_HASH;
}
const siblings = proof.allSiblings();
const path = getPath(siblings.length, kHash.value);
for (let i = siblings.length - 1; i >= 0; i -= 1) {
if (path[i]) {
midKey = await new NodeMiddle(siblingKey, midKey).getKey();
midKey = await new NodeMiddle(siblings[i], midKey).getKey();
}
else {
midKey = await new NodeMiddle(midKey, siblingKey).getKey();
midKey = await new NodeMiddle(midKey, siblings[i]).getKey();
}

@@ -106,0 +142,0 @@ }

@@ -11,6 +11,22 @@ import { Bytes, IHash, Siblings } from '../../types';

bigInt(): bigint;
static fromString(s: string): Hash;
static fromBigInt(i: bigint): Hash;
static fromHex(h: string | undefined): Hash;
toJSON(): string;
}
export declare const ZERO_HASH: Hash;
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use Hash.fromBigInt instead
*/
export declare const newHashFromBigInt: (bigNum: bigint) => Hash;
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use Hash.fromBigInt instead
*/
export declare const newHashFromHex: (h: string) => Hash;
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use Hash.fromBigString instead
*/
export declare const newHashFromString: (decimalString: string) => Hash;

@@ -17,0 +33,0 @@ export declare const hashElems: (e: Array<bigint>) => Hash;

import { NodeAux, Siblings } from '../../types/merkletree';
import { Hash } from '../hash/hash';
import { Bytes } from '../../types';
export interface ProofJSON {
existence: boolean;
siblings: string[];
nodeAux: NodeAuxJSON | undefined;
}
export interface NodeAuxJSON {
key: string;
value: string;
}
export declare class Proof {
existence: boolean;
depth: number;
notEmpties: Bytes;
siblings: Siblings;
private depth;
private notEmpties;
private siblings;
nodeAux: NodeAux | undefined;
constructor();
constructor(obj?: {
siblings: Siblings;
nodeAux: NodeAux | undefined;
existence: boolean;
});
bytes(): Bytes;
toJSON(): {
existence: boolean;
siblings: string[];
nodeAux: {
key: string;
value: string;
} | undefined;
};
private reduceSiblings;
static fromJSON(obj: ProofJSON): Proof;
allSiblings(): Siblings;
}
/**
* @deprecated The method should not be used and will be removed in the next major version,
* please use proof.allSiblings instead
*/
export declare const siblignsFroomProof: (proof: Proof) => Siblings;
export declare const verifyProof: (rootKey: Hash, proof: Proof, k: bigint, v: bigint) => Promise<boolean>;
export declare const rootFromProof: (proof: Proof, k: bigint, v: bigint) => Promise<Hash>;
{
"name": "@iden3/js-merkletree",
"version": "1.0.2",
"version": "1.1.0",
"description": "javascript sparse merkle tree library",

@@ -5,0 +5,0 @@ "typings": "dist/types/index.d.ts",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc