@@ -4,7 +4,13 @@ import { NodeLeaf, NodeMiddle } from '../src/lib/node/node';
4
4
import { InMemoryDB , LocalStorageDB , IndexedDBStorage } from '../src/lib/db' ;
5
5
import { bigIntToUINT8Array , bytes2Hex , bytesEqual , str2Bytes } from '../src/lib/utils' ;
6
6
import { Hash , ZERO_HASH } from '../src/lib/hash/hash' ;
7
- import { Merkletree , Proof , siblignsFroomProof , verifyProof } from '../src/lib/merkletree' ;
7
+ import {
8
+ Merkletree ,
9
+ Proof ,
10
+ ProofJSON ,
11
+ siblignsFroomProof ,
12
+ verifyProof
13
+ } from '../src/lib/merkletree' ;
8
14
import { ErrEntryIndexAlreadyExists , ErrKeyNotFound , ErrReachedMaxLevel } from '../src/lib/errors' ;
9
15
10
16
import { expect } from 'chai' ;
@@ -1097,7 +1103,31 @@ for (let index = 0; index < storages.length; index++) {
1097
1103
await tree . walk ( await tree . root ( ) , ( node : Node ) => f ( node ) ) ;
1098
1104
} ) ;
1099
1105
1100
- it ( 'proof stringify' , async ( ) => {
1106
+ it ( 'proof stringify (old format for node aux)' , async ( ) => {
1107
+ const tree = new Merkletree ( new InMemoryDB ( str2Bytes ( '' ) ) , true , 40 ) ;
1108
+
1109
+ for ( let i = 0 ; i < 5 ; i ++ ) {
1110
+ await tree . add ( BigInt ( i ) , BigInt ( i ) ) ;
1111
+ }
1112
+
1113
+ const { proof, value } = await tree . generateProof ( BigInt ( 9 ) ) ;
1114
+
1115
+ const proofModel = JSON . stringify ( proof ) ;
1116
+ const p = JSON . parse ( proofModel ) as ProofJSON ;
1117
+
1118
+ p . nodeAux = p . node_aux ;
1119
+ p . node_aux = undefined ;
1120
+
1121
+ const proofFromJSON = Proof . fromJSON ( JSON . parse ( proofModel ) ) ;
1122
+
1123
+ expect ( JSON . stringify ( proof . allSiblings ( ) ) ) . to . equal (
1124
+ JSON . stringify ( proofFromJSON . allSiblings ( ) )
1125
+ ) ;
1126
+ expect ( proof . existence ) . to . eq ( proofFromJSON . existence ) ;
1127
+ expect ( proof . existence ) . to . eq ( false ) ;
1128
+ expect ( JSON . stringify ( proof . nodeAux ) ) . to . eq ( JSON . stringify ( proofFromJSON . nodeAux ) ) ;
1129
+ } ) ;
1130
+ it ( 'proof stringify (new format for node aux)' , async ( ) => {
1101
1131
const tree = new Merkletree ( new InMemoryDB ( str2Bytes ( '' ) ) , true , 40 ) ;
1102
1132
1103
1133
for ( let i = 0 ; i < 5 ; i ++ ) {
@@ -1183,6 +1213,52 @@ for (let index = 0; index < storages.length; index++) {
1183
1213
expect ( cvp . value . string ( ) ) . to . be . equal ( '22' ) ;
1184
1214
expect ( cvp . fnc ) . to . be . equal ( 0 ) ;
1185
1215
} ) ;
1216
+ it ( 'calculate depth for mtp' , async ( ) => {
1217
+ const storage = getTreeStorage ( 'calculatedepth' ) ;
1218
+ const mt = new Merkletree ( storage , true , 40 ) ;
1219
+
1220
+ await mt . add ( BigInt ( '1' ) , BigInt ( '2' ) ) ;
1221
+ await mt . add ( BigInt ( '3' ) , BigInt ( '8' ) ) ;
1222
+ await mt . add ( BigInt ( '7' ) , BigInt ( '8' ) ) ;
1223
+ await mt . add ( BigInt ( '9' ) , BigInt ( '8' ) ) ;
1224
+
1225
+ const { proof } : { proof : Proof } = await mt . generateProof ( BigInt ( '11' ) , await mt . root ( ) ) ;
1226
+
1227
+ const given = `{ "existence": false, "siblings": [ "0", "12166698708103333637493481507263348370172773813051235807348785759284762677336", "7750564177398573185975752951631372712868228752107043582052272719841058100111", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" ], "node_aux": { "key": "3", "value": "8" }}` ;
1228
+ const p = Proof . fromJSON ( JSON . parse ( given ) ) ;
1229
+
1230
+ expect ( proof . allSiblings ( ) ) . to . deep . equal ( p . allSiblings ( ) ) ;
1231
+ expect ( proof . nodeAux ) . to . deep . equal ( p . nodeAux ) ;
1232
+ expect ( proof . existence ) . to . equal ( p . existence ) ;
1233
+
1234
+ let isValid = await verifyProof ( await mt . root ( ) , proof , BigInt ( '11' ) , BigInt ( '0' ) ) ;
1235
+ expect ( isValid ) . to . be . true ;
1236
+ isValid = await verifyProof ( await mt . root ( ) , p , BigInt ( '11' ) , BigInt ( '0' ) ) ;
1237
+ expect ( isValid ) . to . be . true ;
1238
+ } ) ;
1239
+ it ( 'calculate depth for mtp (old format)' , async ( ) => {
1240
+ const storage = getTreeStorage ( 'calculatedepth' ) ;
1241
+ const mt = new Merkletree ( storage , true , 40 ) ;
1242
+
1243
+ await mt . add ( BigInt ( '1' ) , BigInt ( '2' ) ) ;
1244
+ await mt . add ( BigInt ( '3' ) , BigInt ( '8' ) ) ;
1245
+ await mt . add ( BigInt ( '7' ) , BigInt ( '8' ) ) ;
1246
+ await mt . add ( BigInt ( '9' ) , BigInt ( '8' ) ) ;
1247
+
1248
+ const { proof } : { proof : Proof } = await mt . generateProof ( BigInt ( '11' ) , await mt . root ( ) ) ;
1249
+
1250
+ const given = `{ "existence": false, "siblings": [ "0", "12166698708103333637493481507263348370172773813051235807348785759284762677336", "7750564177398573185975752951631372712868228752107043582052272719841058100111", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" ], "nodeAux": { "key": "3", "value": "8" }}` ;
1251
+ const p = Proof . fromJSON ( JSON . parse ( given ) ) ;
1252
+
1253
+ expect ( proof . allSiblings ( ) ) . to . deep . equal ( p . allSiblings ( ) ) ;
1254
+ expect ( proof . nodeAux ) . to . deep . equal ( p . nodeAux ) ;
1255
+ expect ( proof . existence ) . to . equal ( p . existence ) ;
1256
+
1257
+ let isValid = await verifyProof ( await mt . root ( ) , proof , BigInt ( '11' ) , BigInt ( '0' ) ) ;
1258
+ expect ( isValid ) . to . be . true ;
1259
+ isValid = await verifyProof ( await mt . root ( ) , p , BigInt ( '11' ) , BigInt ( '0' ) ) ;
1260
+ expect ( isValid ) . to . be . true ;
1261
+ } ) ;
1186
1262
} ) ;
1187
1263
}
1188
1264
0 commit comments