@@ -40,13 +40,13 @@ export class MerkleRawProofParser {
4040
4141 const byteFlags = withoutHeader . slice ( offset , offset + 2 * byteFlagsCount ) ;
4242
43- this . flagPath = processFlags ( byteFlags ) ;
43+ this . flagPath = this . processFlags ( byteFlags ) ;
4444 this . maxDepth = Math . ceil ( Math . log2 ( this . txCountInBlock ) ) ;
45- this . nodeCountPerLevel = getNodeCountPerLevel ( this . txCountInBlock , this . maxDepth ) ;
45+ this . nodeCountPerLevel = this . getNodeCountPerLevel ( this . txCountInBlock , this . maxDepth ) ;
4646
4747 [ this . txIndex , this . sortedHashes ] = this . processTree ( 0 , 0 , 0 , 0 , 0 , [ ] ) ;
4848
49- this . directions = processDirections ( this . txIndex , this . txCountInBlock ) ;
49+ this . directions = this . processDirections ( this . txIndex , this . txCountInBlock ) ;
5050 }
5151
5252 getTxidReversed ( ) : string {
@@ -65,7 +65,49 @@ export class MerkleRawProofParser {
6565 return this . directions ;
6666 }
6767
68- processTree (
68+ private processFlags ( flagBytes : string ) : string {
69+ let directions = "" ;
70+
71+ for ( let i = 0 ; i < flagBytes . length ; i += 2 ) {
72+ directions += reverseByte ( flagBytes . substring ( i , i + 2 ) ) ;
73+ }
74+
75+ return directions ;
76+ }
77+
78+ private getNodeCountPerLevel ( txCount : number , depth : number ) : number [ ] {
79+ let result : number [ ] = [ ] ;
80+ let levelSize = txCount ;
81+
82+ for ( let i = depth ; i >= 0 ; i -- ) {
83+ result [ depth ] = levelSize ;
84+
85+ levelSize = Math . ceil ( levelSize / 2 ) ;
86+ depth -- ;
87+ }
88+
89+ return result ;
90+ }
91+
92+ private processDirections ( txIndex : number , totalTransactions : number ) {
93+ let directions : number [ ] = [ ] ;
94+ let curIndex = txIndex ;
95+ let levelSize = totalTransactions ;
96+
97+ while ( levelSize > 1 ) {
98+ if ( curIndex % 2 == 0 ) {
99+ if ( levelSize % 2 == 1 && levelSize - 1 == curIndex ) directions . push ( 2 ) ;
100+ else directions . push ( 0 ) ;
101+ } else directions . push ( 1 ) ;
102+
103+ curIndex = Math . floor ( curIndex / 2 ) ;
104+ levelSize = Math . ceil ( levelSize / 2 ) ;
105+ }
106+
107+ return directions ;
108+ }
109+
110+ private processTree (
69111 depth : number ,
70112 currentFlag : number ,
71113 txIndex : number ,
@@ -123,19 +165,19 @@ export class MerkleRawProofParser {
123165 ) ;
124166 }
125167
126- nodesCountIsUneven ( level : number ) : boolean {
168+ private nodesCountIsUneven ( level : number ) : boolean {
127169 return this . nodeCountPerLevel [ level ] ! % 2 == 1 ;
128170 }
129171
130- isNodeWithoutPair ( depth : number , nodePosition : number ) : boolean {
172+ private isNodeWithoutPair ( depth : number , nodePosition : number ) : boolean {
131173 return depth != 0 && this . nodesCountIsUneven ( depth ) && nodePosition + 1 == this . nodeCountPerLevel [ depth ] ;
132174 }
133175
134- isLeftNode ( depth : number , nodePosition : number ) : boolean {
176+ private isLeftNode ( depth : number , nodePosition : number ) : boolean {
135177 return depth != 0 && nodePosition % 2 == 0 ;
136178 }
137179
138- isLastLeaf ( nodePosition : number , currentHash : number ) : boolean {
180+ private isLastLeaf ( nodePosition : number , currentHash : number ) : boolean {
139181 return nodePosition % 2 == 0 && currentHash < this . hashes . length ;
140182 }
141183}
@@ -146,59 +188,17 @@ function reverseBytes(str: string) {
146188 return "0x" + Buffer . from ( str , "hex" ) . reverse ( ) . toString ( "hex" ) ;
147189}
148190
149- function parseCuint ( data : string , offset : number ) : [ number , number ] {
150- const firstByte = parseInt ( data . slice ( offset , offset + 2 ) , 16 ) ;
151-
152- if ( firstByte < 0xfd ) return [ parseInt ( data . slice ( offset , offset + 2 ) , 16 ) , 2 ] ;
153- if ( firstByte == 0xfd ) return [ parseInt ( data . slice ( offset + 2 , offset + 6 ) , 16 ) , 6 ] ;
154- if ( firstByte == 0xfe ) return [ parseInt ( data . slice ( offset + 2 , offset + 10 ) , 16 ) , 10 ] ;
155- return [ parseInt ( data . slice ( offset + 2 , offset + 18 ) , 16 ) , 18 ] ;
156- }
157-
158- function processFlags ( flagBytes : string ) : string {
159- let directions = "" ;
160-
161- for ( let i = 0 ; i < flagBytes . length ; i += 2 ) {
162- directions += reverseByte ( flagBytes . substring ( i , i + 2 ) ) ;
163- }
164-
165- return directions ;
166- }
167-
168191function reverseByte ( byte : string ) : string {
169192 const binary = parseInt ( byte , 16 ) . toString ( 2 ) ;
170193 const padded = binary . padStart ( 8 , "0" ) ;
171194 return padded . split ( "" ) . reverse ( ) . join ( "" ) ;
172195}
173196
174- function getNodeCountPerLevel ( txCount : number , depth : number ) : number [ ] {
175- let result : number [ ] = [ ] ;
176- let levelSize = txCount ;
177-
178- for ( let i = depth ; i >= 0 ; i -- ) {
179- result [ depth ] = levelSize ;
180-
181- levelSize = Math . ceil ( levelSize / 2 ) ;
182- depth -- ;
183- }
184-
185- return result ;
186- }
187-
188- function processDirections ( txIndex : number , totalTransactions : number ) {
189- let directions : number [ ] = [ ] ;
190- let curIndex = txIndex ;
191- let levelSize = totalTransactions ;
192-
193- while ( levelSize > 1 ) {
194- if ( curIndex % 2 == 0 ) {
195- if ( levelSize % 2 == 1 && levelSize - 1 == curIndex ) directions . push ( 2 ) ;
196- else directions . push ( 0 ) ;
197- } else directions . push ( 1 ) ;
198-
199- curIndex = Math . floor ( curIndex / 2 ) ;
200- levelSize = Math . ceil ( levelSize / 2 ) ;
201- }
197+ function parseCuint ( data : string , offset : number ) : [ number , number ] {
198+ const firstByte = parseInt ( data . slice ( offset , offset + 2 ) , 16 ) ;
202199
203- return directions ;
200+ if ( firstByte < 0xfd ) return [ parseInt ( data . slice ( offset , offset + 2 ) , 16 ) , 2 ] ;
201+ if ( firstByte == 0xfd ) return [ parseInt ( data . slice ( offset + 2 , offset + 6 ) , 16 ) , 6 ] ;
202+ if ( firstByte == 0xfe ) return [ parseInt ( data . slice ( offset + 2 , offset + 10 ) , 16 ) , 10 ] ;
203+ return [ parseInt ( data . slice ( offset + 2 , offset + 18 ) , 16 ) , 18 ] ;
204204}
0 commit comments