@btc-vision/btc-runtime 1.0.30 → 1.1.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/package.json +1 -1
- package/runtime/buffer/BytesReader.ts +200 -200
- package/runtime/buffer/BytesWriter.ts +346 -346
- package/runtime/contracts/DeployableOP_20.ts +409 -0
- package/runtime/contracts/OP_20.ts +8 -341
- package/runtime/contracts/OP_NET.ts +73 -73
- package/runtime/contracts/interfaces/IOP_20.ts +21 -21
- package/runtime/env/BTCEnvironment.ts +319 -319
- 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 +56 -53
- 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/StoredBoolean.ts +48 -0
- package/runtime/storage/StoredString.ts +145 -145
- package/runtime/storage/StoredU256.ts +248 -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
package/package.json
CHANGED
|
@@ -1,200 +1,200 @@
|
|
|
1
|
-
import { Address, ADDRESS_BYTE_LENGTH } from '../types/Address';
|
|
2
|
-
import { Selector } from '../math/abi';
|
|
3
|
-
import { u256 } from 'as-bignum/assembly';
|
|
4
|
-
import { Revert } from '../types/Revert';
|
|
5
|
-
import { Map } from '../generic/Map';
|
|
6
|
-
|
|
7
|
-
@final
|
|
8
|
-
export class BytesReader {
|
|
9
|
-
private readonly buffer: DataView;
|
|
10
|
-
|
|
11
|
-
private currentOffset: i32 = 0;
|
|
12
|
-
|
|
13
|
-
constructor(bytes: Uint8Array) {
|
|
14
|
-
this.buffer = new DataView(bytes.buffer);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public readU8(): u8 {
|
|
18
|
-
this.verifyEnd(this.currentOffset + 1);
|
|
19
|
-
|
|
20
|
-
return this.buffer.getUint8(this.currentOffset++);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public readU16(): u16 {
|
|
24
|
-
this.verifyEnd(this.currentOffset + 2);
|
|
25
|
-
|
|
26
|
-
const value = this.buffer.getUint16(this.currentOffset, true);
|
|
27
|
-
this.currentOffset += 2;
|
|
28
|
-
|
|
29
|
-
return value;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public readU32(le: boolean = true): u32 {
|
|
33
|
-
this.verifyEnd(this.currentOffset + 4);
|
|
34
|
-
|
|
35
|
-
const value = this.buffer.getUint32(this.currentOffset, le);
|
|
36
|
-
this.currentOffset += 4;
|
|
37
|
-
return value;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public readU64(): u64 {
|
|
41
|
-
this.verifyEnd(this.currentOffset + 8);
|
|
42
|
-
|
|
43
|
-
const value = this.buffer.getUint64(this.currentOffset, true);
|
|
44
|
-
this.currentOffset += 8;
|
|
45
|
-
|
|
46
|
-
return value;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
public readU256(): u256 {
|
|
50
|
-
const next32Bytes: u8[] = [];
|
|
51
|
-
for (let i = 0; i < 32; i++) {
|
|
52
|
-
next32Bytes[i] = this.readU8();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return u256.fromBytesBE(next32Bytes);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public readBytes(length: u32, zeroStop: boolean = false): Uint8Array {
|
|
59
|
-
let bytes: Uint8Array = new Uint8Array(length);
|
|
60
|
-
for (let i: u32 = 0; i < length; i++) {
|
|
61
|
-
const byte: u8 = this.readU8();
|
|
62
|
-
if (zeroStop && byte === 0) {
|
|
63
|
-
bytes = bytes.slice(0, i);
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
bytes[i] = byte;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return bytes;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
public readMultiBytesAddressMap(): Map<Address, Uint8Array[]> {
|
|
74
|
-
const map: Map<Address, Uint8Array[]> = new Map<Address, Uint8Array[]>();
|
|
75
|
-
const size: u8 = this.readU8();
|
|
76
|
-
|
|
77
|
-
if (size > 8) throw new Revert('Too many contract called.');
|
|
78
|
-
|
|
79
|
-
for (let i: u8 = 0; i < size; i++) {
|
|
80
|
-
const address: Address = this.readAddress();
|
|
81
|
-
const responseSize: u8 = this.readU8();
|
|
82
|
-
|
|
83
|
-
if (responseSize > 10) throw new Revert('Too many calls.');
|
|
84
|
-
|
|
85
|
-
const calls: Uint8Array[] = [];
|
|
86
|
-
for (let j: u8 = 0; j < responseSize; j++) {
|
|
87
|
-
const response: Uint8Array = this.readBytesWithLength();
|
|
88
|
-
calls.push(response);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
map.set(address, calls);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return map;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public readBytesWithLength(): Uint8Array {
|
|
98
|
-
const length = this.readU32();
|
|
99
|
-
|
|
100
|
-
return this.readBytes(length);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
public readString(length: u16): string {
|
|
104
|
-
const bytes = this.readBytes(length, true);
|
|
105
|
-
|
|
106
|
-
return String.UTF8.decode(bytes.buffer);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
public readTuple(): u256[] {
|
|
110
|
-
const length = this.readU32();
|
|
111
|
-
const result: u256[] = new Array<u256>(length);
|
|
112
|
-
|
|
113
|
-
for (let i = 0; i < length; i++) {
|
|
114
|
-
result[i] = this.readU256();
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return result;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
public readAddressValueTuple(): Map<Address, u256> {
|
|
121
|
-
const length: u16 = this.readU16();
|
|
122
|
-
const result = new Map<Address, u256>();
|
|
123
|
-
|
|
124
|
-
for (let i: u16 = 0; i < length; i++) {
|
|
125
|
-
const address = this.readAddress();
|
|
126
|
-
const value = this.readU256();
|
|
127
|
-
|
|
128
|
-
if (result.has(address)) throw new Revert('Duplicate address found in map');
|
|
129
|
-
|
|
130
|
-
result.set(address, value);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return result;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
public readSelector(): Selector {
|
|
137
|
-
return this.readU32(false);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
public readStringWithLength(): string {
|
|
141
|
-
const length = this.readU16();
|
|
142
|
-
|
|
143
|
-
return this.readString(length);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
public readBoolean(): boolean {
|
|
147
|
-
return this.readU8() !== 0;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
public readFloat(): f32 {
|
|
151
|
-
const value = this.buffer.getFloat32(this.currentOffset, true);
|
|
152
|
-
this.currentOffset += 4;
|
|
153
|
-
|
|
154
|
-
return value;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
public readAddress(): Address {
|
|
158
|
-
return this.readString(ADDRESS_BYTE_LENGTH);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
public getOffset(): i32 {
|
|
162
|
-
return this.currentOffset;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
public setOffset(offset: i32): void {
|
|
166
|
-
this.currentOffset = offset;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
public verifyEnd(size: i32): void {
|
|
170
|
-
if (this.currentOffset > this.buffer.byteLength) {
|
|
171
|
-
throw new Error(`Expected to read ${size} bytes but read ${this.currentOffset} bytes`);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
public readAddressArray(): Address[] {
|
|
176
|
-
const length = this.readU16();
|
|
177
|
-
const result = new Array<Address>(length);
|
|
178
|
-
|
|
179
|
-
for (let i: u16 = 0; i < length; i++) {
|
|
180
|
-
result[i] = this.readAddress();
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return result;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
private verifyChecksum(): void {
|
|
187
|
-
const writtenChecksum = this.readU32();
|
|
188
|
-
|
|
189
|
-
let checksum: u32 = 0;
|
|
190
|
-
for (let i = 0; i < this.buffer.byteLength; i++) {
|
|
191
|
-
checksum += this.buffer.getUint8(i);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
checksum = checksum % 2 ** 32;
|
|
195
|
-
|
|
196
|
-
if (checksum !== writtenChecksum) {
|
|
197
|
-
throw new Error('Invalid checksum for buffer');
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
1
|
+
import { Address, ADDRESS_BYTE_LENGTH } from '../types/Address';
|
|
2
|
+
import { Selector } from '../math/abi';
|
|
3
|
+
import { u256 } from 'as-bignum/assembly';
|
|
4
|
+
import { Revert } from '../types/Revert';
|
|
5
|
+
import { Map } from '../generic/Map';
|
|
6
|
+
|
|
7
|
+
@final
|
|
8
|
+
export class BytesReader {
|
|
9
|
+
private readonly buffer: DataView;
|
|
10
|
+
|
|
11
|
+
private currentOffset: i32 = 0;
|
|
12
|
+
|
|
13
|
+
constructor(bytes: Uint8Array) {
|
|
14
|
+
this.buffer = new DataView(bytes.buffer);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public readU8(): u8 {
|
|
18
|
+
this.verifyEnd(this.currentOffset + 1);
|
|
19
|
+
|
|
20
|
+
return this.buffer.getUint8(this.currentOffset++);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public readU16(): u16 {
|
|
24
|
+
this.verifyEnd(this.currentOffset + 2);
|
|
25
|
+
|
|
26
|
+
const value = this.buffer.getUint16(this.currentOffset, true);
|
|
27
|
+
this.currentOffset += 2;
|
|
28
|
+
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public readU32(le: boolean = true): u32 {
|
|
33
|
+
this.verifyEnd(this.currentOffset + 4);
|
|
34
|
+
|
|
35
|
+
const value = this.buffer.getUint32(this.currentOffset, le);
|
|
36
|
+
this.currentOffset += 4;
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public readU64(): u64 {
|
|
41
|
+
this.verifyEnd(this.currentOffset + 8);
|
|
42
|
+
|
|
43
|
+
const value = this.buffer.getUint64(this.currentOffset, true);
|
|
44
|
+
this.currentOffset += 8;
|
|
45
|
+
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public readU256(): u256 {
|
|
50
|
+
const next32Bytes: u8[] = [];
|
|
51
|
+
for (let i = 0; i < 32; i++) {
|
|
52
|
+
next32Bytes[i] = this.readU8();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return u256.fromBytesBE(next32Bytes);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public readBytes(length: u32, zeroStop: boolean = false): Uint8Array {
|
|
59
|
+
let bytes: Uint8Array = new Uint8Array(length);
|
|
60
|
+
for (let i: u32 = 0; i < length; i++) {
|
|
61
|
+
const byte: u8 = this.readU8();
|
|
62
|
+
if (zeroStop && byte === 0) {
|
|
63
|
+
bytes = bytes.slice(0, i);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
bytes[i] = byte;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return bytes;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public readMultiBytesAddressMap(): Map<Address, Uint8Array[]> {
|
|
74
|
+
const map: Map<Address, Uint8Array[]> = new Map<Address, Uint8Array[]>();
|
|
75
|
+
const size: u8 = this.readU8();
|
|
76
|
+
|
|
77
|
+
if (size > 8) throw new Revert('Too many contract called.');
|
|
78
|
+
|
|
79
|
+
for (let i: u8 = 0; i < size; i++) {
|
|
80
|
+
const address: Address = this.readAddress();
|
|
81
|
+
const responseSize: u8 = this.readU8();
|
|
82
|
+
|
|
83
|
+
if (responseSize > 10) throw new Revert('Too many calls.');
|
|
84
|
+
|
|
85
|
+
const calls: Uint8Array[] = [];
|
|
86
|
+
for (let j: u8 = 0; j < responseSize; j++) {
|
|
87
|
+
const response: Uint8Array = this.readBytesWithLength();
|
|
88
|
+
calls.push(response);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
map.set(address, calls);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return map;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public readBytesWithLength(): Uint8Array {
|
|
98
|
+
const length = this.readU32();
|
|
99
|
+
|
|
100
|
+
return this.readBytes(length);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public readString(length: u16): string {
|
|
104
|
+
const bytes = this.readBytes(length, true);
|
|
105
|
+
|
|
106
|
+
return String.UTF8.decode(bytes.buffer);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public readTuple(): u256[] {
|
|
110
|
+
const length = this.readU32();
|
|
111
|
+
const result: u256[] = new Array<u256>(length);
|
|
112
|
+
|
|
113
|
+
for (let i = 0; i < length; i++) {
|
|
114
|
+
result[i] = this.readU256();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
public readAddressValueTuple(): Map<Address, u256> {
|
|
121
|
+
const length: u16 = this.readU16();
|
|
122
|
+
const result = new Map<Address, u256>();
|
|
123
|
+
|
|
124
|
+
for (let i: u16 = 0; i < length; i++) {
|
|
125
|
+
const address = this.readAddress();
|
|
126
|
+
const value = this.readU256();
|
|
127
|
+
|
|
128
|
+
if (result.has(address)) throw new Revert('Duplicate address found in map');
|
|
129
|
+
|
|
130
|
+
result.set(address, value);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public readSelector(): Selector {
|
|
137
|
+
return this.readU32(false);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public readStringWithLength(): string {
|
|
141
|
+
const length = this.readU16();
|
|
142
|
+
|
|
143
|
+
return this.readString(length);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
public readBoolean(): boolean {
|
|
147
|
+
return this.readU8() !== 0;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
public readFloat(): f32 {
|
|
151
|
+
const value = this.buffer.getFloat32(this.currentOffset, true);
|
|
152
|
+
this.currentOffset += 4;
|
|
153
|
+
|
|
154
|
+
return value;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
public readAddress(): Address {
|
|
158
|
+
return this.readString(ADDRESS_BYTE_LENGTH);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
public getOffset(): i32 {
|
|
162
|
+
return this.currentOffset;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public setOffset(offset: i32): void {
|
|
166
|
+
this.currentOffset = offset;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
public verifyEnd(size: i32): void {
|
|
170
|
+
if (this.currentOffset > this.buffer.byteLength) {
|
|
171
|
+
throw new Error(`Expected to read ${size} bytes but read ${this.currentOffset} bytes`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
public readAddressArray(): Address[] {
|
|
176
|
+
const length = this.readU16();
|
|
177
|
+
const result = new Array<Address>(length);
|
|
178
|
+
|
|
179
|
+
for (let i: u16 = 0; i < length; i++) {
|
|
180
|
+
result[i] = this.readAddress();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private verifyChecksum(): void {
|
|
187
|
+
const writtenChecksum = this.readU32();
|
|
188
|
+
|
|
189
|
+
let checksum: u32 = 0;
|
|
190
|
+
for (let i = 0; i < this.buffer.byteLength; i++) {
|
|
191
|
+
checksum += this.buffer.getUint8(i);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
checksum = checksum % 2 ** 32;
|
|
195
|
+
|
|
196
|
+
if (checksum !== writtenChecksum) {
|
|
197
|
+
throw new Error('Invalid checksum for buffer');
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|