@subsquid/evm-codec 0.3.0 → 1.0.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.
Files changed (79) hide show
  1. package/lib/codec.d.ts +57 -12
  2. package/lib/codec.d.ts.map +1 -1
  3. package/lib/codec.js.map +1 -1
  4. package/lib/codecs/array.d.ts +1 -3
  5. package/lib/codecs/array.d.ts.map +1 -1
  6. package/lib/codecs/array.js +13 -9
  7. package/lib/codecs/array.js.map +1 -1
  8. package/lib/codecs/primitives.d.ts +2 -17
  9. package/lib/codecs/primitives.d.ts.map +1 -1
  10. package/lib/codecs/primitives.js +46 -152
  11. package/lib/codecs/primitives.js.map +1 -1
  12. package/lib/codecs/struct.d.ts +16 -9
  13. package/lib/codecs/struct.d.ts.map +1 -1
  14. package/lib/codecs/struct.js +53 -40
  15. package/lib/codecs/struct.js.map +1 -1
  16. package/lib/dsl.d.ts +11 -0
  17. package/lib/dsl.d.ts.map +1 -0
  18. package/lib/dsl.js +33 -0
  19. package/lib/dsl.js.map +1 -0
  20. package/lib/index.d.ts +7 -4
  21. package/lib/index.d.ts.map +1 -1
  22. package/lib/index.js +7 -6
  23. package/lib/index.js.map +1 -1
  24. package/lib/sink/bounds.d.ts +21 -0
  25. package/lib/sink/bounds.d.ts.map +1 -0
  26. package/lib/sink/bounds.js +24 -0
  27. package/lib/sink/bounds.js.map +1 -0
  28. package/lib/sink/bytes.d.ts +41 -0
  29. package/lib/sink/bytes.d.ts.map +1 -0
  30. package/lib/sink/bytes.js +261 -0
  31. package/lib/sink/bytes.js.map +1 -0
  32. package/lib/sink/hex.d.ts +33 -0
  33. package/lib/sink/hex.d.ts.map +1 -0
  34. package/lib/sink/hex.js +289 -0
  35. package/lib/sink/hex.js.map +1 -0
  36. package/lib/{src.d.ts → src/bytes.d.ts} +7 -5
  37. package/lib/src/bytes.d.ts.map +1 -0
  38. package/lib/src/bytes.js +161 -0
  39. package/lib/src/bytes.js.map +1 -0
  40. package/lib/src/hex.d.ts +33 -0
  41. package/lib/src/hex.d.ts.map +1 -0
  42. package/lib/src/hex.js +164 -0
  43. package/lib/src/hex.js.map +1 -0
  44. package/lib/util.d.ts +6 -0
  45. package/lib/util.d.ts.map +1 -0
  46. package/lib/util.js +20 -0
  47. package/lib/util.js.map +1 -0
  48. package/package.json +6 -8
  49. package/src/codec.ts +65 -19
  50. package/src/codecs/array.test.ts +87 -0
  51. package/src/codecs/array.ts +6 -11
  52. package/src/codecs/primitives.test.ts +27 -0
  53. package/src/codecs/primitives.ts +87 -208
  54. package/src/codecs/struct.test.ts +69 -0
  55. package/src/codecs/struct.ts +80 -61
  56. package/src/dsl.ts +16 -0
  57. package/src/index.ts +7 -4
  58. package/src/sink/bounds.ts +26 -0
  59. package/src/sink/bytes.test.ts +92 -0
  60. package/src/sink/bytes.ts +290 -0
  61. package/src/sink/hex.ts +311 -0
  62. package/src/src/bytes.test.ts +114 -0
  63. package/src/src/bytes.ts +187 -0
  64. package/src/src/hex.ts +191 -0
  65. package/src/util.ts +19 -0
  66. package/lib/safeToNumber.d.ts +0 -2
  67. package/lib/safeToNumber.d.ts.map +0 -1
  68. package/lib/safeToNumber.js +0 -11
  69. package/lib/safeToNumber.js.map +0 -1
  70. package/lib/sink.d.ts +0 -43
  71. package/lib/sink.d.ts.map +0 -1
  72. package/lib/sink.js +0 -215
  73. package/lib/sink.js.map +0 -1
  74. package/lib/src.d.ts.map +0 -1
  75. package/lib/src.js +0 -141
  76. package/lib/src.js.map +0 -1
  77. package/src/safeToNumber.ts +0 -6
  78. package/src/sink.ts +0 -241
  79. package/src/src.ts +0 -158
package/src/src.ts DELETED
@@ -1,158 +0,0 @@
1
- import { WORD_SIZE } from './codec'
2
-
3
- export class Src {
4
- private view: DataView
5
- private pos = 0
6
- private oldPos = 0
7
- constructor(private buf: Uint8Array) {
8
- this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength)
9
- }
10
-
11
- slice(start: number, end?: number): Src {
12
- return new Src(this.buf.subarray(start, end))
13
- }
14
-
15
- u8(): number {
16
- this.pos += WORD_SIZE - 1
17
- let val = this.view.getUint8(this.pos)
18
- this.pos += 1
19
- return val
20
- }
21
-
22
- i8(): number {
23
- return Number(this.i256())
24
- }
25
-
26
- u16(): number {
27
- this.pos += WORD_SIZE - 2
28
- let val = this.view.getUint16(this.pos, false)
29
- this.pos += 2
30
- return val
31
- }
32
-
33
- i16(): number {
34
- return Number(this.i256())
35
- }
36
-
37
- u32(): number {
38
- this.pos += WORD_SIZE - 4
39
- let val = this.view.getUint32(this.pos, false)
40
- this.pos += 4
41
- return val
42
- }
43
-
44
- i32(): number {
45
- return Number(this.i256())
46
- }
47
-
48
- u64(): bigint {
49
- this.pos += WORD_SIZE - 8
50
- return this.#u64()
51
- }
52
-
53
- #u64(): bigint {
54
- let val = this.view.getBigUint64(this.pos, false)
55
- this.pos += 8
56
- return val
57
- }
58
-
59
- i64(): bigint {
60
- this.pos += WORD_SIZE - 8
61
- return this.#i64()
62
- }
63
-
64
- #i64(): bigint {
65
- let val = this.view.getBigInt64(this.pos, false)
66
- this.pos += 8
67
- return val
68
- }
69
-
70
- u128(): bigint {
71
- this.pos += WORD_SIZE - 16
72
- return this.#u128()
73
- }
74
-
75
- #u128(): bigint {
76
- let hi = this.#u64()
77
- let lo = this.#u64()
78
- return lo + (hi << 64n)
79
- }
80
-
81
- i128(): bigint {
82
- this.pos += WORD_SIZE - 16
83
- return this.#i128()
84
- }
85
-
86
- #i128(): bigint {
87
- let hi = this.#i64()
88
- let lo = this.#u64()
89
- return lo + (hi << 64n)
90
- }
91
-
92
- u256(): bigint {
93
- let hi = this.#u128()
94
- let lo = this.#u128()
95
- return lo + (hi << 128n)
96
- }
97
-
98
- i256(): bigint {
99
- let hi = this.#i128()
100
- let lo = this.#u128()
101
- return lo + (hi << 128n)
102
- }
103
-
104
- address(): string {
105
- return '0x' + this.u256().toString(16).padStart(40, '0')
106
- }
107
-
108
- bytes(): Uint8Array {
109
- const ptr = this.u32()
110
- this.safeJump(ptr, 'bytes')
111
- const len = Number(this.u256())
112
- this.assertLength(len, 'bytes')
113
- const val = this.buf.subarray(this.pos, this.pos + len)
114
- this.jumpBack()
115
- return val
116
- }
117
-
118
- staticBytes(len: number): Uint8Array {
119
- if (len > 32) {
120
- throw new Error(`bytes${len} is not a valid type`)
121
- }
122
- const val = this.buf.subarray(this.pos, this.pos + len)
123
- this.pos += WORD_SIZE
124
- return val
125
- }
126
-
127
- string(): string {
128
- const ptr = this.u32()
129
- this.safeJump(ptr, 'string')
130
- const len = Number(this.u256())
131
- this.assertLength(len, 'string')
132
- const val = Buffer.from(this.buf.buffer, this.buf.byteOffset + this.pos, len).toString('utf-8')
133
- this.jumpBack()
134
- return val
135
- }
136
-
137
- bool(): boolean {
138
- return !!this.u8()
139
- }
140
-
141
- private assertLength(len: number, typeName: string): void {
142
- if (this.buf.length - this.pos < len) {
143
- throw new RangeError(`Unexpected end of input. Attempting to read ${typeName} of length ${len} from 0x${Buffer.from(this.buf).toString('hex')}`)
144
- }
145
- }
146
-
147
- public safeJump(pos: number, typeName: string): void {
148
- if (pos < 0 || pos >= this.buf.length) {
149
- throw new RangeError(`Unexpected pointer location: 0x${pos.toString(16)}. Attempting to read ${typeName} from 0x${Buffer.from(this.buf).toString('hex')}`)
150
- }
151
- this.oldPos = this.pos
152
- this.pos = pos
153
- }
154
-
155
- public jumpBack(): void {
156
- this.pos = this.oldPos
157
- }
158
- }