@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,145 +1,145 @@
|
|
|
1
|
-
import { u256 } from 'as-bignum/assembly';
|
|
2
|
-
import { Blockchain } from '../env';
|
|
3
|
-
import { SafeMath } from '../types/SafeMath';
|
|
4
|
-
|
|
5
|
-
@final
|
|
6
|
-
export class StoredString {
|
|
7
|
-
constructor(
|
|
8
|
-
public pointer: u16,
|
|
9
|
-
private defaultValue?: string,
|
|
10
|
-
) {}
|
|
11
|
-
|
|
12
|
-
private _value: string = '';
|
|
13
|
-
|
|
14
|
-
@inline
|
|
15
|
-
public get value(): string {
|
|
16
|
-
if (!this._value) {
|
|
17
|
-
this.load();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return this._value;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
@inline
|
|
24
|
-
public set value(value: string) {
|
|
25
|
-
this._value = value;
|
|
26
|
-
this.save();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
private min(a: u32, b: u32): u32 {
|
|
30
|
-
return a < b ? a : b;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
private max(a: u32, b: u32): u32 {
|
|
34
|
-
return a > b ? a : b;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
private save(): void {
|
|
38
|
-
const length: u32 = this._value.length;
|
|
39
|
-
if (length == 0) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (length > 2048) {
|
|
44
|
-
throw new Error('StoredString: value is too long');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Prepare the header with the length of the string in the first 4 bytes
|
|
48
|
-
let header: u256 = u256.fromU32(length);
|
|
49
|
-
header = SafeMath.shl(header, 224);
|
|
50
|
-
|
|
51
|
-
let currentPointer: u256 = u256.Zero;
|
|
52
|
-
let remainingLength: u32 = length;
|
|
53
|
-
let offset: u32 = 0;
|
|
54
|
-
|
|
55
|
-
// Save the initial chunk (first 28 bytes) in the header
|
|
56
|
-
let bytesToWrite: u32 = this.min(remainingLength, 28);
|
|
57
|
-
header = this.saveChunk(header, this._value, offset, bytesToWrite, 4);
|
|
58
|
-
Blockchain.setStorageAt(this.pointer, currentPointer, header);
|
|
59
|
-
|
|
60
|
-
remainingLength -= bytesToWrite;
|
|
61
|
-
offset += bytesToWrite;
|
|
62
|
-
|
|
63
|
-
// Save the remaining chunks in subsequent storage slots
|
|
64
|
-
while (remainingLength > 0) {
|
|
65
|
-
bytesToWrite = this.min(remainingLength, 32);
|
|
66
|
-
let storageValue: u256 = this.saveChunk(
|
|
67
|
-
u256.Zero,
|
|
68
|
-
this._value,
|
|
69
|
-
offset,
|
|
70
|
-
bytesToWrite,
|
|
71
|
-
0,
|
|
72
|
-
);
|
|
73
|
-
currentPointer = u256.add(currentPointer, u256.One);
|
|
74
|
-
Blockchain.setStorageAt(this.pointer, currentPointer, storageValue);
|
|
75
|
-
|
|
76
|
-
remainingLength -= bytesToWrite;
|
|
77
|
-
offset += bytesToWrite;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Helper method to save a chunk of the string into the storage slot
|
|
82
|
-
private saveChunk(
|
|
83
|
-
storage: u256,
|
|
84
|
-
value: string,
|
|
85
|
-
offset: u32,
|
|
86
|
-
length: u32,
|
|
87
|
-
storageOffset: u32,
|
|
88
|
-
): u256 {
|
|
89
|
-
let bytes = storage.toBytes(true);
|
|
90
|
-
for (let i: u32 = 0; i < length; i++) {
|
|
91
|
-
let index: i32 = i32(offset + i);
|
|
92
|
-
bytes[i + storageOffset] = u8(value.charCodeAt(index));
|
|
93
|
-
}
|
|
94
|
-
return u256.fromBytes(bytes, true);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
private load(): void {
|
|
98
|
-
const header: u256 = Blockchain.getStorageAt(this.pointer, u256.Zero, u256.Zero);
|
|
99
|
-
if (u256.eq(header, u256.Zero)) {
|
|
100
|
-
if (this.defaultValue) {
|
|
101
|
-
this.value = this.defaultValue;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// the length of the string is stored in the first 4 bytes of the header
|
|
108
|
-
const bits: u256 = u256.shr(header, 224);
|
|
109
|
-
const length: u32 = bits.toU32();
|
|
110
|
-
|
|
111
|
-
// the rest contains the string itself
|
|
112
|
-
let currentPointer: u256 = u256.Zero;
|
|
113
|
-
let remainingLength: u32 = length;
|
|
114
|
-
let currentStorage: u256 = header;
|
|
115
|
-
|
|
116
|
-
let bytesToRead: u32 = this.min(remainingLength, 28);
|
|
117
|
-
let str: string = this.loadChunk(currentStorage, 4, bytesToRead);
|
|
118
|
-
remainingLength -= bytesToRead;
|
|
119
|
-
|
|
120
|
-
while (remainingLength > 0) {
|
|
121
|
-
// Move to the next storage slot
|
|
122
|
-
currentPointer = u256.add(currentPointer, u256.One);
|
|
123
|
-
currentStorage = Blockchain.getStorageAt(this.pointer, currentPointer, u256.Zero);
|
|
124
|
-
|
|
125
|
-
// Extract the relevant portion of the string from the current storage slot
|
|
126
|
-
let bytesToRead: u32 = this.min(remainingLength, 32);
|
|
127
|
-
str += this.loadChunk(currentStorage, 0, bytesToRead);
|
|
128
|
-
|
|
129
|
-
remainingLength -= bytesToRead;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
this._value = str;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
private loadChunk(value: u256, offset: u32, length: u32): string {
|
|
136
|
-
const bytes = value.toBytes(true);
|
|
137
|
-
|
|
138
|
-
let str: string = '';
|
|
139
|
-
for (let i: u32 = 0; i < length; i++) {
|
|
140
|
-
str += String.fromCharCode(bytes[i + offset]);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return str;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
1
|
+
import { u256 } from 'as-bignum/assembly';
|
|
2
|
+
import { Blockchain } from '../env';
|
|
3
|
+
import { SafeMath } from '../types/SafeMath';
|
|
4
|
+
|
|
5
|
+
@final
|
|
6
|
+
export class StoredString {
|
|
7
|
+
constructor(
|
|
8
|
+
public pointer: u16,
|
|
9
|
+
private defaultValue?: string,
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
private _value: string = '';
|
|
13
|
+
|
|
14
|
+
@inline
|
|
15
|
+
public get value(): string {
|
|
16
|
+
if (!this._value) {
|
|
17
|
+
this.load();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return this._value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@inline
|
|
24
|
+
public set value(value: string) {
|
|
25
|
+
this._value = value;
|
|
26
|
+
this.save();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private min(a: u32, b: u32): u32 {
|
|
30
|
+
return a < b ? a : b;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private max(a: u32, b: u32): u32 {
|
|
34
|
+
return a > b ? a : b;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private save(): void {
|
|
38
|
+
const length: u32 = this._value.length;
|
|
39
|
+
if (length == 0) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (length > 2048) {
|
|
44
|
+
throw new Error('StoredString: value is too long');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Prepare the header with the length of the string in the first 4 bytes
|
|
48
|
+
let header: u256 = u256.fromU32(length);
|
|
49
|
+
header = SafeMath.shl(header, 224);
|
|
50
|
+
|
|
51
|
+
let currentPointer: u256 = u256.Zero;
|
|
52
|
+
let remainingLength: u32 = length;
|
|
53
|
+
let offset: u32 = 0;
|
|
54
|
+
|
|
55
|
+
// Save the initial chunk (first 28 bytes) in the header
|
|
56
|
+
let bytesToWrite: u32 = this.min(remainingLength, 28);
|
|
57
|
+
header = this.saveChunk(header, this._value, offset, bytesToWrite, 4);
|
|
58
|
+
Blockchain.setStorageAt(this.pointer, currentPointer, header);
|
|
59
|
+
|
|
60
|
+
remainingLength -= bytesToWrite;
|
|
61
|
+
offset += bytesToWrite;
|
|
62
|
+
|
|
63
|
+
// Save the remaining chunks in subsequent storage slots
|
|
64
|
+
while (remainingLength > 0) {
|
|
65
|
+
bytesToWrite = this.min(remainingLength, 32);
|
|
66
|
+
let storageValue: u256 = this.saveChunk(
|
|
67
|
+
u256.Zero,
|
|
68
|
+
this._value,
|
|
69
|
+
offset,
|
|
70
|
+
bytesToWrite,
|
|
71
|
+
0,
|
|
72
|
+
);
|
|
73
|
+
currentPointer = u256.add(currentPointer, u256.One);
|
|
74
|
+
Blockchain.setStorageAt(this.pointer, currentPointer, storageValue);
|
|
75
|
+
|
|
76
|
+
remainingLength -= bytesToWrite;
|
|
77
|
+
offset += bytesToWrite;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Helper method to save a chunk of the string into the storage slot
|
|
82
|
+
private saveChunk(
|
|
83
|
+
storage: u256,
|
|
84
|
+
value: string,
|
|
85
|
+
offset: u32,
|
|
86
|
+
length: u32,
|
|
87
|
+
storageOffset: u32,
|
|
88
|
+
): u256 {
|
|
89
|
+
let bytes = storage.toBytes(true);
|
|
90
|
+
for (let i: u32 = 0; i < length; i++) {
|
|
91
|
+
let index: i32 = i32(offset + i);
|
|
92
|
+
bytes[i + storageOffset] = u8(value.charCodeAt(index));
|
|
93
|
+
}
|
|
94
|
+
return u256.fromBytes(bytes, true);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private load(): void {
|
|
98
|
+
const header: u256 = Blockchain.getStorageAt(this.pointer, u256.Zero, u256.Zero);
|
|
99
|
+
if (u256.eq(header, u256.Zero)) {
|
|
100
|
+
if (this.defaultValue) {
|
|
101
|
+
this.value = this.defaultValue;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// the length of the string is stored in the first 4 bytes of the header
|
|
108
|
+
const bits: u256 = u256.shr(header, 224);
|
|
109
|
+
const length: u32 = bits.toU32();
|
|
110
|
+
|
|
111
|
+
// the rest contains the string itself
|
|
112
|
+
let currentPointer: u256 = u256.Zero;
|
|
113
|
+
let remainingLength: u32 = length;
|
|
114
|
+
let currentStorage: u256 = header;
|
|
115
|
+
|
|
116
|
+
let bytesToRead: u32 = this.min(remainingLength, 28);
|
|
117
|
+
let str: string = this.loadChunk(currentStorage, 4, bytesToRead);
|
|
118
|
+
remainingLength -= bytesToRead;
|
|
119
|
+
|
|
120
|
+
while (remainingLength > 0) {
|
|
121
|
+
// Move to the next storage slot
|
|
122
|
+
currentPointer = u256.add(currentPointer, u256.One);
|
|
123
|
+
currentStorage = Blockchain.getStorageAt(this.pointer, currentPointer, u256.Zero);
|
|
124
|
+
|
|
125
|
+
// Extract the relevant portion of the string from the current storage slot
|
|
126
|
+
let bytesToRead: u32 = this.min(remainingLength, 32);
|
|
127
|
+
str += this.loadChunk(currentStorage, 0, bytesToRead);
|
|
128
|
+
|
|
129
|
+
remainingLength -= bytesToRead;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
this._value = str;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private loadChunk(value: u256, offset: u32, length: u32): string {
|
|
136
|
+
const bytes = value.toBytes(true);
|
|
137
|
+
|
|
138
|
+
let str: string = '';
|
|
139
|
+
for (let i: u32 = 0; i < length; i++) {
|
|
140
|
+
str += String.fromCharCode(bytes[i + offset]);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return str;
|
|
144
|
+
}
|
|
145
|
+
}
|