@subsquid/evm-codec 0.0.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/sink.ts CHANGED
@@ -43,7 +43,14 @@ export class Sink {
43
43
  this.view = new DataView(this.buf.buffer, this.buf.byteOffset, this.buf.byteLength)
44
44
  }
45
45
 
46
+ private checkNumberBounds(val: bigint | number, min: bigint, max: bigint, typeName: string) {
47
+ if (val < min || val > max) {
48
+ throw new Error(`${val} is out of bounds for ${typeName}[${min}, ${max}]`)
49
+ }
50
+ }
51
+
46
52
  u8(val: number) {
53
+ this.checkNumberBounds(val, 0n, 255n, 'uint8')
47
54
  this.reserve(WORD_SIZE)
48
55
  this.pos += WORD_SIZE - 1
49
56
  this.view.setUint8(this.pos, val)
@@ -51,10 +58,12 @@ export class Sink {
51
58
  }
52
59
 
53
60
  i8(val: number) {
54
- this.i256(BigInt(val))
61
+ this.checkNumberBounds(val, -128n, 127n, 'int8')
62
+ this.#i256(BigInt(val))
55
63
  }
56
64
 
57
65
  u16(val: number) {
66
+ this.checkNumberBounds(val, 0n, 65535n, 'uint16')
58
67
  this.reserve(WORD_SIZE)
59
68
  this.pos += WORD_SIZE - 2
60
69
  this.view.setUint16(this.pos, val, false)
@@ -62,10 +71,12 @@ export class Sink {
62
71
  }
63
72
 
64
73
  i16(val: number) {
65
- this.i256(BigInt(val))
74
+ this.checkNumberBounds(val, -32768n, 32767n, 'int16')
75
+ this.#i256(BigInt(val))
66
76
  }
67
77
 
68
78
  u32(val: number) {
79
+ this.checkNumberBounds(val, 0n, 4294967295n, 'uint32')
69
80
  this.reserve(WORD_SIZE)
70
81
  this.pos += WORD_SIZE - 4
71
82
  this.view.setUint32(this.pos, val, false)
@@ -73,10 +84,12 @@ export class Sink {
73
84
  }
74
85
 
75
86
  i32(val: number) {
76
- this.i256(BigInt(val))
87
+ this.checkNumberBounds(val, -2147483648n, 2147483647n, 'int32')
88
+ this.#i256(BigInt(val))
77
89
  }
78
90
 
79
91
  u64(val: bigint) {
92
+ this.checkNumberBounds(val, 0n, 18446744073709551615n, 'uint64')
80
93
  this.reserve(WORD_SIZE)
81
94
  this.pos += WORD_SIZE - 8
82
95
  this.view.setBigUint64(this.pos, val, false)
@@ -84,7 +97,8 @@ export class Sink {
84
97
  }
85
98
 
86
99
  i64(val: bigint) {
87
- this.i256(val)
100
+ this.checkNumberBounds(val, -9223372036854775808n, 9223372036854775807n, 'int64')
101
+ this.#i256(val)
88
102
  }
89
103
 
90
104
  #u64(val: bigint) {
@@ -93,6 +107,7 @@ export class Sink {
93
107
  }
94
108
 
95
109
  u128(val: bigint) {
110
+ this.checkNumberBounds(val, 0n, 340282366920938463463374607431768211455n, 'uint128')
96
111
  this.reserve(WORD_SIZE)
97
112
  this.pos += WORD_SIZE - 16
98
113
  this.#u64(val & 0xffffffffffffffffn)
@@ -100,7 +115,8 @@ export class Sink {
100
115
  }
101
116
 
102
117
  i128(val: bigint) {
103
- this.i256(BigInt(val))
118
+ this.checkNumberBounds(val, -170141183460469231731687303715884105728n, 170141183460469231731687303715884105727n, 'int128')
119
+ this.#i256(val)
104
120
  }
105
121
 
106
122
  #u128(val: bigint) {
@@ -110,15 +126,27 @@ export class Sink {
110
126
  }
111
127
 
112
128
  u256(val: bigint) {
129
+ this.checkNumberBounds(val, 0n, 115792089237316195423570985008687907853269984665640564039457584007913129639935n, 'uint256')
113
130
  this.reserve(WORD_SIZE)
114
131
  this.#u128(val >> 128n)
115
132
  this.#u128(val & (2n ** 128n - 1n))
116
133
  }
117
134
 
118
135
  i256(val: bigint) {
136
+ this.checkNumberBounds(val,
137
+ -57896044618658097711785492504343953926634992332820282019728792003956564819968n,
138
+ 57896044618658097711785492504343953926634992332820282019728792003956564819967n,
139
+ 'int256'
140
+ )
141
+ this.#i256(val)
142
+ }
143
+
144
+ #i256(val: bigint) {
119
145
  let base = 2n ** 256n
120
- val = (val + base) % base
121
- this.u256(val)
146
+ const uval = (val + base) % base
147
+ this.reserve(WORD_SIZE)
148
+ this.#u128(uval >> 128n)
149
+ this.#u128(uval & (2n ** 128n - 1n))
122
150
  }
123
151
 
124
152
  bytes(val: Uint8Array) {