@btc-vision/btc-runtime 1.0.30 → 1.0.31
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/package.json +1 -1
- package/runtime/buffer/BytesReader.ts +200 -200
- package/runtime/buffer/BytesWriter.ts +346 -346
- package/runtime/contracts/OP_20.ts +341 -341
- package/runtime/contracts/OP_NET.ts +73 -73
- package/runtime/contracts/interfaces/IOP_20.ts +21 -21
- package/runtime/env/BTCEnvironment.ts +4 -4
- package/runtime/env/index.ts +3 -3
- package/runtime/events/NetEvent.ts +27 -27
- package/runtime/events/predefined/ApproveEvent.ts +16 -16
- package/runtime/events/predefined/BurnEvent.ts +13 -13
- package/runtime/events/predefined/ClaimEvent.ts +13 -13
- package/runtime/events/predefined/MintEvent.ts +15 -15
- package/runtime/events/predefined/StakeEvent.ts +13 -13
- package/runtime/events/predefined/TransferEvent.ts +16 -16
- package/runtime/events/predefined/UnstakeEvent.ts +13 -13
- package/runtime/events/predefined/index.ts +7 -7
- package/runtime/exports/index.ts +37 -37
- package/runtime/generic/Map.ts +65 -65
- package/runtime/generic/MapU256.ts +57 -57
- package/runtime/index.ts +1 -0
- package/runtime/interfaces/DeployContractResponse.ts +12 -12
- package/runtime/interfaces/IBTC.ts +6 -6
- package/runtime/lang/Definitions.ts +1 -1
- package/runtime/math/abi.ts +37 -37
- package/runtime/math/bytes.ts +34 -34
- package/runtime/math/cyrb53.ts +46 -46
- package/runtime/math/rnd.ts +51 -51
- package/runtime/math/sha256.ts +12 -12
- package/runtime/memory/AddressMemoryMap.ts +44 -44
- package/runtime/memory/KeyMerger.ts +53 -53
- package/runtime/memory/MemorySlot.ts +1 -1
- package/runtime/memory/MemorySlotPointer.ts +3 -3
- package/runtime/memory/MultiAddressMemoryMap.ts +62 -62
- package/runtime/shared-libraries/OP20Utils.ts +21 -0
- package/runtime/shared-libraries/TransferHelper.ts +64 -64
- package/runtime/storage/Serializable.ts +6 -2
- package/runtime/storage/StoredString.ts +145 -145
- package/runtime/storage/StoredU256.ts +246 -246
- package/runtime/types/Address.ts +5 -5
- package/runtime/types/Revert.ts +5 -5
- package/runtime/types/SafeMath.ts +197 -197
- package/runtime/types/index.ts +8 -8
- package/runtime/universal/ABIRegistry.ts +72 -72
|
@@ -1,197 +1,197 @@
|
|
|
1
|
-
import { u256 } from 'as-bignum/assembly';
|
|
2
|
-
|
|
3
|
-
export class SafeMath {
|
|
4
|
-
public static ZERO: u256 = u256.fromU32(0);
|
|
5
|
-
|
|
6
|
-
public static add(a: u256, b: u256): u256 {
|
|
7
|
-
const c: u256 = u256.add(a, b);
|
|
8
|
-
if (c < a) {
|
|
9
|
-
throw new Error('SafeMath: addition overflow');
|
|
10
|
-
}
|
|
11
|
-
return c;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public static sub(a: u256, b: u256): u256 {
|
|
15
|
-
if (a < b) {
|
|
16
|
-
throw new Error('SafeMath: subtraction overflow');
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return u256.sub(a, b);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Computes (a * b) % modulus with full precision
|
|
23
|
-
public static mulmod(a: u256, b: u256, modulus: u256): u256 {
|
|
24
|
-
if (u256.eq(modulus, u256.Zero)) throw new Error('SafeMath: modulo by zero');
|
|
25
|
-
|
|
26
|
-
const mul = SafeMath.mul(a, b);
|
|
27
|
-
return SafeMath.mod(mul, modulus);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
@inline
|
|
31
|
-
@unsafe
|
|
32
|
-
@operator('%')
|
|
33
|
-
public static mod(a: u256, b: u256): u256 {
|
|
34
|
-
if (u256.eq(b, u256.Zero)) {
|
|
35
|
-
throw new Error('SafeMath: modulo by zero');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
let result = a.clone();
|
|
39
|
-
while (u256.ge(result, b)) {
|
|
40
|
-
result = u256.sub(result, b);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return result;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public static mul(a: u256, b: u256): u256 {
|
|
47
|
-
if (a === SafeMath.ZERO || b === SafeMath.ZERO) {
|
|
48
|
-
return SafeMath.ZERO;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const c: u256 = u256.mul(a, b);
|
|
52
|
-
const d: u256 = SafeMath.div(c, a);
|
|
53
|
-
|
|
54
|
-
if (u256.ne(d, b)) {
|
|
55
|
-
throw new Error('SafeMath: multiplication overflow');
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return c;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
@inline
|
|
62
|
-
@unsafe
|
|
63
|
-
@operator('/')
|
|
64
|
-
public static div(a: u256, b: u256): u256 {
|
|
65
|
-
if (b.isZero()) {
|
|
66
|
-
throw new Error('Division by zero');
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (a.isZero()) {
|
|
70
|
-
return new u256();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (u256.lt(a, b)) {
|
|
74
|
-
return new u256(); // Return 0 if a < b
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (u256.eq(a, b)) {
|
|
78
|
-
return new u256(1); // Return 1 if a == b
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
let n = a.clone();
|
|
82
|
-
let d = b.clone();
|
|
83
|
-
let result = new u256();
|
|
84
|
-
|
|
85
|
-
let shift = u256.clz(d) - u256.clz(n);
|
|
86
|
-
d = SafeMath.shl(d, shift); // align d with n by shifting left
|
|
87
|
-
|
|
88
|
-
for (let i = shift; i >= 0; i--) {
|
|
89
|
-
if (u256.ge(n, d)) {
|
|
90
|
-
n = u256.sub(n, d);
|
|
91
|
-
result = u256.or(result, SafeMath.shl(u256.One, i));
|
|
92
|
-
}
|
|
93
|
-
d = u256.shr(d, 1); // restore d to original by shifting right
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return result;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
public static min(a: u256, b: u256): u256 {
|
|
100
|
-
return u256.lt(a, b) ? a : b;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
public static max(a: u256, b: u256): u256 {
|
|
104
|
-
return u256.gt(a, b) ? a : b;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
@inline
|
|
108
|
-
@unsafe
|
|
109
|
-
public static sqrt(y: u256): u256 {
|
|
110
|
-
if (u256.gt(y, u256.fromU32(3))) {
|
|
111
|
-
let z = y;
|
|
112
|
-
|
|
113
|
-
let u246_2 = u256.fromU32(2);
|
|
114
|
-
|
|
115
|
-
let d = SafeMath.div(y, u246_2);
|
|
116
|
-
let x = SafeMath.add(d, u256.One);
|
|
117
|
-
|
|
118
|
-
while (u256.lt(x, z)) {
|
|
119
|
-
z = x;
|
|
120
|
-
|
|
121
|
-
let u = SafeMath.div(y, x);
|
|
122
|
-
let y2 = u256.add(u, x);
|
|
123
|
-
|
|
124
|
-
x = SafeMath.div(y2, u246_2);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return z;
|
|
128
|
-
} else if (!u256.eq(y, u256.Zero)) {
|
|
129
|
-
return u256.One;
|
|
130
|
-
} else {
|
|
131
|
-
return u256.Zero;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
@inline
|
|
136
|
-
@unsafe
|
|
137
|
-
public static shl(value: u256, shift: i32): u256 {
|
|
138
|
-
if (shift == 0) {
|
|
139
|
-
return value.clone();
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
let totalBits = 256;
|
|
143
|
-
let bitsPerSegment = 64;
|
|
144
|
-
|
|
145
|
-
// Normalize shift to be within 0-255 range
|
|
146
|
-
shift &= 255;
|
|
147
|
-
|
|
148
|
-
if (shift >= totalBits) {
|
|
149
|
-
return new u256(); // Shift size larger than width results in zero
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Determine how many full 64-bit segments we are shifting
|
|
153
|
-
let segmentShift = (shift / bitsPerSegment) | 0;
|
|
154
|
-
let bitShift = shift % bitsPerSegment;
|
|
155
|
-
|
|
156
|
-
let segments = [value.lo1, value.lo2, value.hi1, value.hi2];
|
|
157
|
-
|
|
158
|
-
let result = new Array<u64>(4).fill(0);
|
|
159
|
-
|
|
160
|
-
for (let i = 0; i < segments.length; i++) {
|
|
161
|
-
if (i + segmentShift < segments.length) {
|
|
162
|
-
result[i + segmentShift] |= segments[i] << bitShift;
|
|
163
|
-
}
|
|
164
|
-
if (bitShift != 0 && i + segmentShift + 1 < segments.length) {
|
|
165
|
-
result[i + segmentShift + 1] |= segments[i] >>> (bitsPerSegment - bitShift);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return new u256(result[0], result[1], result[2], result[3]);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
public static and(a: u256, b: u256): u256 {
|
|
173
|
-
return u256.and(a, b);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
public static or(a: u256, b: u256): u256 {
|
|
177
|
-
return u256.or(a, b);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
public static xor(a: u256, b: u256): u256 {
|
|
181
|
-
return u256.xor(a, b);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
public static shr(a: u256, b: u32): u256 {
|
|
185
|
-
return u256.shr(a, b);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Increment a u256 value by 1
|
|
190
|
-
* @param value The value to increment
|
|
191
|
-
* @returns The incremented value
|
|
192
|
-
*/
|
|
193
|
-
@inline
|
|
194
|
-
static inc(value: u256): u256 {
|
|
195
|
-
return value.preInc();
|
|
196
|
-
}
|
|
197
|
-
}
|
|
1
|
+
import { u256 } from 'as-bignum/assembly';
|
|
2
|
+
|
|
3
|
+
export class SafeMath {
|
|
4
|
+
public static ZERO: u256 = u256.fromU32(0);
|
|
5
|
+
|
|
6
|
+
public static add(a: u256, b: u256): u256 {
|
|
7
|
+
const c: u256 = u256.add(a, b);
|
|
8
|
+
if (c < a) {
|
|
9
|
+
throw new Error('SafeMath: addition overflow');
|
|
10
|
+
}
|
|
11
|
+
return c;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public static sub(a: u256, b: u256): u256 {
|
|
15
|
+
if (a < b) {
|
|
16
|
+
throw new Error('SafeMath: subtraction overflow');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return u256.sub(a, b);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Computes (a * b) % modulus with full precision
|
|
23
|
+
public static mulmod(a: u256, b: u256, modulus: u256): u256 {
|
|
24
|
+
if (u256.eq(modulus, u256.Zero)) throw new Error('SafeMath: modulo by zero');
|
|
25
|
+
|
|
26
|
+
const mul = SafeMath.mul(a, b);
|
|
27
|
+
return SafeMath.mod(mul, modulus);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@inline
|
|
31
|
+
@unsafe
|
|
32
|
+
@operator('%')
|
|
33
|
+
public static mod(a: u256, b: u256): u256 {
|
|
34
|
+
if (u256.eq(b, u256.Zero)) {
|
|
35
|
+
throw new Error('SafeMath: modulo by zero');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let result = a.clone();
|
|
39
|
+
while (u256.ge(result, b)) {
|
|
40
|
+
result = u256.sub(result, b);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public static mul(a: u256, b: u256): u256 {
|
|
47
|
+
if (a === SafeMath.ZERO || b === SafeMath.ZERO) {
|
|
48
|
+
return SafeMath.ZERO;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const c: u256 = u256.mul(a, b);
|
|
52
|
+
const d: u256 = SafeMath.div(c, a);
|
|
53
|
+
|
|
54
|
+
if (u256.ne(d, b)) {
|
|
55
|
+
throw new Error('SafeMath: multiplication overflow');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return c;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@inline
|
|
62
|
+
@unsafe
|
|
63
|
+
@operator('/')
|
|
64
|
+
public static div(a: u256, b: u256): u256 {
|
|
65
|
+
if (b.isZero()) {
|
|
66
|
+
throw new Error('Division by zero');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (a.isZero()) {
|
|
70
|
+
return new u256();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (u256.lt(a, b)) {
|
|
74
|
+
return new u256(); // Return 0 if a < b
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (u256.eq(a, b)) {
|
|
78
|
+
return new u256(1); // Return 1 if a == b
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let n = a.clone();
|
|
82
|
+
let d = b.clone();
|
|
83
|
+
let result = new u256();
|
|
84
|
+
|
|
85
|
+
let shift = u256.clz(d) - u256.clz(n);
|
|
86
|
+
d = SafeMath.shl(d, shift); // align d with n by shifting left
|
|
87
|
+
|
|
88
|
+
for (let i = shift; i >= 0; i--) {
|
|
89
|
+
if (u256.ge(n, d)) {
|
|
90
|
+
n = u256.sub(n, d);
|
|
91
|
+
result = u256.or(result, SafeMath.shl(u256.One, i));
|
|
92
|
+
}
|
|
93
|
+
d = u256.shr(d, 1); // restore d to original by shifting right
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public static min(a: u256, b: u256): u256 {
|
|
100
|
+
return u256.lt(a, b) ? a : b;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public static max(a: u256, b: u256): u256 {
|
|
104
|
+
return u256.gt(a, b) ? a : b;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
@inline
|
|
108
|
+
@unsafe
|
|
109
|
+
public static sqrt(y: u256): u256 {
|
|
110
|
+
if (u256.gt(y, u256.fromU32(3))) {
|
|
111
|
+
let z = y;
|
|
112
|
+
|
|
113
|
+
let u246_2 = u256.fromU32(2);
|
|
114
|
+
|
|
115
|
+
let d = SafeMath.div(y, u246_2);
|
|
116
|
+
let x = SafeMath.add(d, u256.One);
|
|
117
|
+
|
|
118
|
+
while (u256.lt(x, z)) {
|
|
119
|
+
z = x;
|
|
120
|
+
|
|
121
|
+
let u = SafeMath.div(y, x);
|
|
122
|
+
let y2 = u256.add(u, x);
|
|
123
|
+
|
|
124
|
+
x = SafeMath.div(y2, u246_2);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return z;
|
|
128
|
+
} else if (!u256.eq(y, u256.Zero)) {
|
|
129
|
+
return u256.One;
|
|
130
|
+
} else {
|
|
131
|
+
return u256.Zero;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
@inline
|
|
136
|
+
@unsafe
|
|
137
|
+
public static shl(value: u256, shift: i32): u256 {
|
|
138
|
+
if (shift == 0) {
|
|
139
|
+
return value.clone();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
let totalBits = 256;
|
|
143
|
+
let bitsPerSegment = 64;
|
|
144
|
+
|
|
145
|
+
// Normalize shift to be within 0-255 range
|
|
146
|
+
shift &= 255;
|
|
147
|
+
|
|
148
|
+
if (shift >= totalBits) {
|
|
149
|
+
return new u256(); // Shift size larger than width results in zero
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Determine how many full 64-bit segments we are shifting
|
|
153
|
+
let segmentShift = (shift / bitsPerSegment) | 0;
|
|
154
|
+
let bitShift = shift % bitsPerSegment;
|
|
155
|
+
|
|
156
|
+
let segments = [value.lo1, value.lo2, value.hi1, value.hi2];
|
|
157
|
+
|
|
158
|
+
let result = new Array<u64>(4).fill(0);
|
|
159
|
+
|
|
160
|
+
for (let i = 0; i < segments.length; i++) {
|
|
161
|
+
if (i + segmentShift < segments.length) {
|
|
162
|
+
result[i + segmentShift] |= segments[i] << bitShift;
|
|
163
|
+
}
|
|
164
|
+
if (bitShift != 0 && i + segmentShift + 1 < segments.length) {
|
|
165
|
+
result[i + segmentShift + 1] |= segments[i] >>> (bitsPerSegment - bitShift);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return new u256(result[0], result[1], result[2], result[3]);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public static and(a: u256, b: u256): u256 {
|
|
173
|
+
return u256.and(a, b);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
public static or(a: u256, b: u256): u256 {
|
|
177
|
+
return u256.or(a, b);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
public static xor(a: u256, b: u256): u256 {
|
|
181
|
+
return u256.xor(a, b);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
public static shr(a: u256, b: u32): u256 {
|
|
185
|
+
return u256.shr(a, b);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Increment a u256 value by 1
|
|
190
|
+
* @param value The value to increment
|
|
191
|
+
* @returns The incremented value
|
|
192
|
+
*/
|
|
193
|
+
@inline
|
|
194
|
+
static inc(value: u256): u256 {
|
|
195
|
+
return value.preInc();
|
|
196
|
+
}
|
|
197
|
+
}
|
package/runtime/types/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Map } from '../generic/Map';
|
|
2
|
-
import { MemorySlotPointer } from '../memory/MemorySlotPointer';
|
|
3
|
-
import { MemorySlotData } from '../memory/MemorySlot';
|
|
4
|
-
import { u256 } from 'as-bignum/assembly';
|
|
5
|
-
import { Address } from './Address';
|
|
6
|
-
|
|
7
|
-
export type PointerStorage = Map<MemorySlotPointer, MemorySlotData<u256>>;
|
|
8
|
-
export type BlockchainStorage = Map<Address, PointerStorage>;
|
|
1
|
+
import { Map } from '../generic/Map';
|
|
2
|
+
import { MemorySlotPointer } from '../memory/MemorySlotPointer';
|
|
3
|
+
import { MemorySlotData } from '../memory/MemorySlot';
|
|
4
|
+
import { u256 } from 'as-bignum/assembly';
|
|
5
|
+
import { Address } from './Address';
|
|
6
|
+
|
|
7
|
+
export type PointerStorage = Map<MemorySlotPointer, MemorySlotData<u256>>;
|
|
8
|
+
export type BlockchainStorage = Map<Address, PointerStorage>;
|
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
import { encodeSelector, Selector } from '../math/abi';
|
|
2
|
-
import { BytesReader } from '../buffer/BytesReader';
|
|
3
|
-
import { BytesWriter } from '../buffer/BytesWriter';
|
|
4
|
-
import { Map } from '../generic/Map';
|
|
5
|
-
|
|
6
|
-
export type Calldata = NonNullable<BytesReader>;
|
|
7
|
-
export type SelectorsMap = Map<u32, Uint8Array>;
|
|
8
|
-
|
|
9
|
-
class ABIRegistryBase {
|
|
10
|
-
private methodMap: Selector[] = [];
|
|
11
|
-
private selectors: SelectorsMap = new Map();
|
|
12
|
-
|
|
13
|
-
private viewSelectors: Selector[] = [];
|
|
14
|
-
private allowedWriteMethods: Selector[] = [];
|
|
15
|
-
|
|
16
|
-
// Register properties with their selectors and handlers
|
|
17
|
-
public defineGetterSelector(name: string, canWrite: boolean): void {
|
|
18
|
-
const selector: Selector = encodeSelector(name);
|
|
19
|
-
|
|
20
|
-
const selectorWriter: BytesWriter = new BytesWriter();
|
|
21
|
-
selectorWriter.writeABISelector(name, selector);
|
|
22
|
-
|
|
23
|
-
if (!this.selectors.has(selector)) {
|
|
24
|
-
this.selectors.set(selector, selectorWriter.getBuffer());
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (canWrite) this.addToWriteMethods(selector);
|
|
28
|
-
|
|
29
|
-
if (!this.viewSelectors.includes(selector)) {
|
|
30
|
-
this.viewSelectors.push(selector);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public getViewSelectors(): Uint8Array {
|
|
35
|
-
const writer: BytesWriter = new BytesWriter();
|
|
36
|
-
writer.writeViewSelectorMap(this.selectors);
|
|
37
|
-
|
|
38
|
-
return writer.getBuffer();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public getMethodSelectors(): Uint8Array {
|
|
42
|
-
const writer: BytesWriter = new BytesWriter();
|
|
43
|
-
writer.writeMethodSelectorsMap(this.methodMap);
|
|
44
|
-
|
|
45
|
-
return writer.getBuffer();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
public getWriteMethods(): Uint8Array {
|
|
49
|
-
const writer: BytesWriter = new BytesWriter();
|
|
50
|
-
writer.writeMethodSelectorsMap(this.allowedWriteMethods);
|
|
51
|
-
|
|
52
|
-
return writer.getBuffer();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Register methods with their selectors and handlers
|
|
56
|
-
public defineMethodSelector(name: string, canWrite: boolean): void {
|
|
57
|
-
const selector: u32 = encodeSelector(name);
|
|
58
|
-
if (canWrite) this.addToWriteMethods(selector);
|
|
59
|
-
|
|
60
|
-
if (!this.methodMap.includes(selector)) {
|
|
61
|
-
this.methodMap.push(selector);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
private addToWriteMethods(selector: Selector): void {
|
|
66
|
-
if (!this.allowedWriteMethods.includes(selector)) {
|
|
67
|
-
this.allowedWriteMethods.push(selector);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export const ABIRegistry = new ABIRegistryBase();
|
|
1
|
+
import { encodeSelector, Selector } from '../math/abi';
|
|
2
|
+
import { BytesReader } from '../buffer/BytesReader';
|
|
3
|
+
import { BytesWriter } from '../buffer/BytesWriter';
|
|
4
|
+
import { Map } from '../generic/Map';
|
|
5
|
+
|
|
6
|
+
export type Calldata = NonNullable<BytesReader>;
|
|
7
|
+
export type SelectorsMap = Map<u32, Uint8Array>;
|
|
8
|
+
|
|
9
|
+
class ABIRegistryBase {
|
|
10
|
+
private methodMap: Selector[] = [];
|
|
11
|
+
private selectors: SelectorsMap = new Map();
|
|
12
|
+
|
|
13
|
+
private viewSelectors: Selector[] = [];
|
|
14
|
+
private allowedWriteMethods: Selector[] = [];
|
|
15
|
+
|
|
16
|
+
// Register properties with their selectors and handlers
|
|
17
|
+
public defineGetterSelector(name: string, canWrite: boolean): void {
|
|
18
|
+
const selector: Selector = encodeSelector(name);
|
|
19
|
+
|
|
20
|
+
const selectorWriter: BytesWriter = new BytesWriter();
|
|
21
|
+
selectorWriter.writeABISelector(name, selector);
|
|
22
|
+
|
|
23
|
+
if (!this.selectors.has(selector)) {
|
|
24
|
+
this.selectors.set(selector, selectorWriter.getBuffer());
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (canWrite) this.addToWriteMethods(selector);
|
|
28
|
+
|
|
29
|
+
if (!this.viewSelectors.includes(selector)) {
|
|
30
|
+
this.viewSelectors.push(selector);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public getViewSelectors(): Uint8Array {
|
|
35
|
+
const writer: BytesWriter = new BytesWriter();
|
|
36
|
+
writer.writeViewSelectorMap(this.selectors);
|
|
37
|
+
|
|
38
|
+
return writer.getBuffer();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public getMethodSelectors(): Uint8Array {
|
|
42
|
+
const writer: BytesWriter = new BytesWriter();
|
|
43
|
+
writer.writeMethodSelectorsMap(this.methodMap);
|
|
44
|
+
|
|
45
|
+
return writer.getBuffer();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public getWriteMethods(): Uint8Array {
|
|
49
|
+
const writer: BytesWriter = new BytesWriter();
|
|
50
|
+
writer.writeMethodSelectorsMap(this.allowedWriteMethods);
|
|
51
|
+
|
|
52
|
+
return writer.getBuffer();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Register methods with their selectors and handlers
|
|
56
|
+
public defineMethodSelector(name: string, canWrite: boolean): void {
|
|
57
|
+
const selector: u32 = encodeSelector(name);
|
|
58
|
+
if (canWrite) this.addToWriteMethods(selector);
|
|
59
|
+
|
|
60
|
+
if (!this.methodMap.includes(selector)) {
|
|
61
|
+
this.methodMap.push(selector);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private addToWriteMethods(selector: Selector): void {
|
|
66
|
+
if (!this.allowedWriteMethods.includes(selector)) {
|
|
67
|
+
this.allowedWriteMethods.push(selector);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const ABIRegistry = new ABIRegistryBase();
|