@@ -2,6 +2,11 @@ import assert from 'assert'
22import { isHexBytes } from '../validations'
33import { add0x } from '../utils'
44
5+ enum Side {
6+ Front ,
7+ Back
8+ }
9+
510/**
611 * Class to iterate through bytes string by parsing individual bytes
712 *
@@ -12,6 +17,8 @@ import {add0x} from '../utils'
1217 * iter.nextBytes(2) == BigInt(0xbeef)
1318 */
1419export class BytesIter < T > {
20+ public static SIDE = Side
21+
1522 private bytes : string
1623
1724 private constructor (
@@ -42,11 +49,11 @@ export class BytesIter<T> {
4249 return this . bytes . length === 0
4350 }
4451
45- public nextByte ( ) : T {
46- return this . nextBytes ( 1 )
52+ public nextByte ( side = Side . Front ) : T {
53+ return this . nextBytes ( 1 , side )
4754 }
4855
49- public nextBytes ( n : number ) : T {
56+ public nextBytes ( n : number , side = Side . Front ) : T {
5057 const cnt = n * 2
5158
5259 if ( this . bytes . length < cnt ) {
@@ -55,38 +62,42 @@ export class BytesIter<T> {
5562 )
5663 }
5764
58- const bytes = this . bytes . slice ( 0 , cnt )
65+ const isFront = side === Side . Front
66+
67+ const bytes = isFront
68+ ? this . bytes . slice ( 0 , cnt )
69+ : this . bytes . slice ( - cnt )
5970
60- this . bytes = this . bytes . slice ( cnt )
71+ this . bytes = isFront ? this . bytes . slice ( cnt ) : this . bytes . slice ( 0 , - cnt )
6172
6273 return this . ResultType ( add0x ( bytes ) )
6374 }
6475
65- public nextUint8 ( ) : T {
66- return this . nextByte ( )
76+ public nextUint8 ( side = Side . Front ) : T {
77+ return this . nextByte ( side )
6778 }
6879
69- public nextUint16 ( ) : T {
70- return this . nextBytes ( 2 )
80+ public nextUint16 ( side = Side . Front ) : T {
81+ return this . nextBytes ( 2 , side )
7182 }
7283
73- public nextUint24 ( ) : T {
74- return this . nextBytes ( 3 )
84+ public nextUint24 ( side = Side . Front ) : T {
85+ return this . nextBytes ( 3 , side )
7586 }
7687
77- public nextUint32 ( ) : T {
78- return this . nextBytes ( 4 )
88+ public nextUint32 ( side = Side . Front ) : T {
89+ return this . nextBytes ( 4 , side )
7990 }
8091
81- public nextUint128 ( ) : T {
82- return this . nextBytes ( 16 )
92+ public nextUint128 ( side = Side . Front ) : T {
93+ return this . nextBytes ( 16 , side )
8394 }
8495
85- public nextUint160 ( ) : T {
86- return this . nextBytes ( 20 )
96+ public nextUint160 ( side = Side . Front ) : T {
97+ return this . nextBytes ( 20 , side )
8798 }
8899
89- public nextUint256 ( ) : T {
90- return this . nextBytes ( 32 )
100+ public nextUint256 ( side = Side . Front ) : T {
101+ return this . nextBytes ( 32 , side )
91102 }
92103}
0 commit comments