@btc-vision/btc-runtime 1.3.7 → 1.3.9
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 +16 -6
- package/runtime/buffer/BytesWriter.ts +17 -2
- package/runtime/contracts/DeployableOP_20.ts +0 -2
- package/runtime/contracts/OP_NET.ts +2 -0
- package/runtime/env/classes/Transaction.ts +16 -11
- package/runtime/env/classes/UTXO.ts +1 -1
- package/runtime/exports/index.ts +2 -0
- package/runtime/index.ts +5 -0
- package/runtime/math/abi.ts +1 -1
- package/runtime/math/bytes.ts +10 -1
- package/runtime/math/u160.ts +779 -0
- package/runtime/storage/Serializable.ts +32 -12
- package/runtime/storage/StoredBoolean.ts +1 -1
- package/runtime/storage/StoredU128Array.ts +271 -0
- package/runtime/storage/StoredU16Array.ts +281 -0
- package/runtime/storage/StoredU256Array.ts +300 -0
- package/runtime/storage/StoredU64.ts +179 -0
- package/runtime/types/SafeMath.ts +139 -6
package/package.json
CHANGED
|
@@ -3,7 +3,6 @@ import { Selector } from '../math/abi';
|
|
|
3
3
|
import { i128, u128, u256 } from 'as-bignum/assembly';
|
|
4
4
|
import { Revert } from '../types/Revert';
|
|
5
5
|
import { TransactionInput, TransactionOutput } from '../env/classes/UTXO';
|
|
6
|
-
import { StaticArray } from 'staticarray';
|
|
7
6
|
import { i256 } from '../math/i256';
|
|
8
7
|
import { AddressMap } from '../generic/AddressMap';
|
|
9
8
|
|
|
@@ -143,9 +142,9 @@ export class BytesReader {
|
|
|
143
142
|
return String.UTF8.decode(bytes.buffer);
|
|
144
143
|
}
|
|
145
144
|
|
|
146
|
-
public readTransactionInputs():
|
|
145
|
+
public readTransactionInputs(): TransactionInput[] {
|
|
147
146
|
const length = this.readU8();
|
|
148
|
-
const result = new
|
|
147
|
+
const result = new Array<TransactionInput>(length);
|
|
149
148
|
|
|
150
149
|
for (let i: u16 = 0; i < length; i++) {
|
|
151
150
|
const txId = this.readBytes(32);
|
|
@@ -158,13 +157,13 @@ export class BytesReader {
|
|
|
158
157
|
return result;
|
|
159
158
|
}
|
|
160
159
|
|
|
161
|
-
public readTransactionOutputs():
|
|
160
|
+
public readTransactionOutputs(): TransactionOutput[] {
|
|
162
161
|
const length = this.readU8();
|
|
163
|
-
const result = new
|
|
162
|
+
const result = new Array<TransactionOutput>(length);
|
|
164
163
|
|
|
165
164
|
for (let i: u16 = 0; i < length; i++) {
|
|
166
165
|
const index = this.readU8();
|
|
167
|
-
const scriptPubKey = this.
|
|
166
|
+
const scriptPubKey = this.readStringWithLength();
|
|
168
167
|
const value = this.readU64();
|
|
169
168
|
|
|
170
169
|
result[i] = new TransactionOutput(index, scriptPubKey, value);
|
|
@@ -192,6 +191,17 @@ export class BytesReader {
|
|
|
192
191
|
return result;
|
|
193
192
|
}
|
|
194
193
|
|
|
194
|
+
public readU128Array(): u128[] {
|
|
195
|
+
const length = this.readU16();
|
|
196
|
+
const result: u128[] = new Array<u128>(length);
|
|
197
|
+
|
|
198
|
+
for (let i: u16 = 0; i < length; i++) {
|
|
199
|
+
result[i] = this.readU128();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
|
|
195
205
|
public readAddressValueTuple(): AddressMap<u256> {
|
|
196
206
|
const length: u16 = this.readU16();
|
|
197
207
|
const result = new AddressMap<u256>();
|
|
@@ -73,6 +73,10 @@ export class BytesWriter {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
public writeU8At(value: u8, offset: u32): void {
|
|
77
|
+
this.buffer.setUint8(offset, value);
|
|
78
|
+
}
|
|
79
|
+
|
|
76
80
|
public writeU256(value: u256): void {
|
|
77
81
|
this.allocSafe(32);
|
|
78
82
|
|
|
@@ -92,10 +96,10 @@ export class BytesWriter {
|
|
|
92
96
|
}
|
|
93
97
|
|
|
94
98
|
public writeU128(value: u128): void {
|
|
95
|
-
this.allocSafe(
|
|
99
|
+
this.allocSafe(16);
|
|
96
100
|
|
|
97
101
|
const bytes = value.toUint8Array(true);
|
|
98
|
-
for (let i: i32 = 0; i <
|
|
102
|
+
for (let i: i32 = 0; i < 16; i++) {
|
|
99
103
|
this.writeU8(bytes[i] || 0);
|
|
100
104
|
}
|
|
101
105
|
}
|
|
@@ -109,6 +113,17 @@ export class BytesWriter {
|
|
|
109
113
|
}
|
|
110
114
|
}
|
|
111
115
|
|
|
116
|
+
public writeU128Array(value: u128[]): void {
|
|
117
|
+
if (value.length > 65535) throw new Revert('Array size is too large');
|
|
118
|
+
|
|
119
|
+
this.allocSafe(2 + value.length * 16);
|
|
120
|
+
this.writeU32(u16(value.length));
|
|
121
|
+
|
|
122
|
+
for (let i = 0; i < value.length; i++) {
|
|
123
|
+
this.writeU128(value[i]);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
112
127
|
public writeBytes(value: Uint8Array): void {
|
|
113
128
|
this.allocSafe(value.length);
|
|
114
129
|
|
|
@@ -37,9 +37,7 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
|
|
|
37
37
|
super();
|
|
38
38
|
|
|
39
39
|
this.allowanceMap = new MultiAddressMemoryMap<u256>(allowanceMapPointer, u256.Zero);
|
|
40
|
-
|
|
41
40
|
this.balanceOfMap = new AddressMemoryMap<u256>(balanceOfMapPointer, u256.Zero);
|
|
42
|
-
|
|
43
41
|
this._totalSupply = new StoredU256(totalSupplyPointer, u256.Zero, u256.Zero);
|
|
44
42
|
|
|
45
43
|
this._maxSupply = new StoredU256(maxSupplyPointer, u256.Zero, u256.Zero);
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Address } from '../../types/Address';
|
|
2
2
|
import { TransactionInput, TransactionOutput } from './UTXO';
|
|
3
3
|
import { Potential } from '../../lang/Definitions';
|
|
4
|
-
import { StaticArray } from 'staticarray';
|
|
5
4
|
import { BytesReader } from '../../buffer/BytesReader';
|
|
6
5
|
import { inputs, outputs } from '../global';
|
|
7
6
|
|
|
@@ -13,33 +12,39 @@ export class Transaction {
|
|
|
13
12
|
public readonly id: Uint8Array,
|
|
14
13
|
) {}
|
|
15
14
|
|
|
16
|
-
private _inputs: Potential<
|
|
15
|
+
private _inputs: Potential<TransactionInput[]> = null;
|
|
17
16
|
|
|
18
|
-
public get inputs():
|
|
17
|
+
public get inputs(): TransactionInput[] {
|
|
19
18
|
if (!this._inputs) {
|
|
20
|
-
|
|
19
|
+
const inputs = this.loadInputs();
|
|
20
|
+
this._inputs = inputs;
|
|
21
|
+
|
|
22
|
+
return inputs;
|
|
21
23
|
}
|
|
22
24
|
|
|
23
|
-
return this._inputs;
|
|
25
|
+
return this._inputs as TransactionInput[];
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
private _outputs: Potential<
|
|
28
|
+
private _outputs: Potential<TransactionOutput[]> = null;
|
|
27
29
|
|
|
28
|
-
public get outputs():
|
|
30
|
+
public get outputs(): TransactionOutput[] {
|
|
29
31
|
if (!this._outputs) {
|
|
30
|
-
|
|
32
|
+
const outputs = this.loadOutputs();
|
|
33
|
+
this._outputs = outputs;
|
|
34
|
+
|
|
35
|
+
return outputs;
|
|
31
36
|
}
|
|
32
37
|
|
|
33
|
-
return this._outputs;
|
|
38
|
+
return this._outputs as TransactionOutput[];
|
|
34
39
|
}
|
|
35
40
|
|
|
36
|
-
private loadInputs():
|
|
41
|
+
private loadInputs(): TransactionInput[] {
|
|
37
42
|
const buffer = new BytesReader(inputs());
|
|
38
43
|
|
|
39
44
|
return buffer.readTransactionInputs();
|
|
40
45
|
}
|
|
41
46
|
|
|
42
|
-
private loadOutputs():
|
|
47
|
+
private loadOutputs(): TransactionOutput[] {
|
|
43
48
|
const buffer = new BytesReader(outputs());
|
|
44
49
|
|
|
45
50
|
return buffer.readTransactionOutputs();
|
package/runtime/exports/index.ts
CHANGED
|
@@ -9,6 +9,8 @@ export function execute(data: Uint8Array): Uint8Array {
|
|
|
9
9
|
const selector: Selector = calldata.readSelector();
|
|
10
10
|
const result: BytesWriter = Blockchain.contract.execute(selector, calldata);
|
|
11
11
|
|
|
12
|
+
Blockchain.contract.onExecutionCompleted();
|
|
13
|
+
|
|
12
14
|
return result.getBuffer();
|
|
13
15
|
}
|
|
14
16
|
|
package/runtime/index.ts
CHANGED
|
@@ -46,6 +46,7 @@ export * from './math/cyrb53';
|
|
|
46
46
|
export * from './math/sha256';
|
|
47
47
|
export * from './math/rnd';
|
|
48
48
|
export * from './math/i256';
|
|
49
|
+
//export * from './math/u160'; Broken at the moment
|
|
49
50
|
export * from './secp256k1/ECPoint';
|
|
50
51
|
|
|
51
52
|
/** Memory */
|
|
@@ -60,6 +61,10 @@ export * from './memory/Uint8ArrayMerger';
|
|
|
60
61
|
|
|
61
62
|
/** Storage */
|
|
62
63
|
export * from './storage/StoredU256';
|
|
64
|
+
export * from './storage/StoredU64';
|
|
65
|
+
export * from './storage/StoredU16Array';
|
|
66
|
+
export * from './storage/StoredU128Array';
|
|
67
|
+
export * from './storage/StoredU256Array';
|
|
63
68
|
export * from './storage/StoredString';
|
|
64
69
|
export * from './storage/StoredAddress';
|
|
65
70
|
export * from './storage/StoredBoolean';
|
package/runtime/math/abi.ts
CHANGED
|
@@ -13,7 +13,7 @@ export function encodeSelector(name: string): Selector {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export function encodePointer(uniqueIdentifier: u16, typed: Uint8Array): MemorySlotPointer {
|
|
16
|
-
const hash = Sha256.hash(typed);
|
|
16
|
+
const hash: Uint8Array = typed.length !== 32 ? Sha256.hash(typed) : typed;
|
|
17
17
|
|
|
18
18
|
const finalPointer = new Uint8Array(32);
|
|
19
19
|
finalPointer[0] = uniqueIdentifier & 0xff;
|
package/runtime/math/bytes.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { u256 } from 'as-bignum/assembly';
|
|
1
|
+
import { u128, u256 } from 'as-bignum/assembly';
|
|
2
2
|
|
|
3
3
|
export function bytes(number: u256[]): Uint8Array {
|
|
4
4
|
const result = new Uint8Array(32 * number.length);
|
|
@@ -29,6 +29,15 @@ export function bytes8(number: Uint8Array): u64 {
|
|
|
29
29
|
);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
export function bytes16(buffer: Uint8Array): u128 {
|
|
33
|
+
// Make sure that the buffer is 16 bytes long.
|
|
34
|
+
if (buffer.length !== 16) {
|
|
35
|
+
buffer = buffer.slice(0, 16);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return u128.fromBytes(buffer);
|
|
39
|
+
}
|
|
40
|
+
|
|
32
41
|
export function bytes32(number: Uint8Array): u256 {
|
|
33
42
|
return u256.fromBytes(number);
|
|
34
43
|
}
|