@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@btc-vision/btc-runtime",
3
- "version": "1.3.7",
3
+ "version": "1.3.9",
4
4
  "description": "Bitcoin Smart Contract Runtime",
5
5
  "main": "btc/index.ts",
6
6
  "scripts": {
@@ -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(): StaticArray<TransactionInput> {
145
+ public readTransactionInputs(): TransactionInput[] {
147
146
  const length = this.readU8();
148
- const result = new StaticArray<TransactionInput>(length);
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(): StaticArray<TransactionOutput> {
160
+ public readTransactionOutputs(): TransactionOutput[] {
162
161
  const length = this.readU8();
163
- const result = new StaticArray<TransactionOutput>(length);
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.readBytesWithLength();
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(32);
99
+ this.allocSafe(16);
96
100
 
97
101
  const bytes = value.toUint8Array(true);
98
- for (let i: i32 = 0; i < 32; 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);
@@ -18,6 +18,8 @@ export class OP_NET implements IBTC {
18
18
 
19
19
  public onDeployment(_calldata: Calldata): void {}
20
20
 
21
+ public onExecutionCompleted(): void {}
22
+
21
23
  public execute(method: Selector, _calldata: Calldata): BytesWriter {
22
24
  let response: BytesWriter;
23
25
 
@@ -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<StaticArray<TransactionInput>> = null;
15
+ private _inputs: Potential<TransactionInput[]> = null;
17
16
 
18
- public get inputs(): StaticArray<TransactionInput> {
17
+ public get inputs(): TransactionInput[] {
19
18
  if (!this._inputs) {
20
- this._inputs = this.loadInputs();
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<StaticArray<TransactionOutput>> = null;
28
+ private _outputs: Potential<TransactionOutput[]> = null;
27
29
 
28
- public get outputs(): StaticArray<TransactionOutput> {
30
+ public get outputs(): TransactionOutput[] {
29
31
  if (!this._outputs) {
30
- this._outputs = this.loadOutputs();
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(): StaticArray<TransactionInput> {
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(): StaticArray<TransactionOutput> {
47
+ private loadOutputs(): TransactionOutput[] {
43
48
  const buffer = new BytesReader(outputs());
44
49
 
45
50
  return buffer.readTransactionOutputs();
@@ -11,7 +11,7 @@ export class TransactionInput {
11
11
  export class TransactionOutput {
12
12
  public constructor(
13
13
  public readonly index: u8,
14
- public readonly scriptPubKey: Uint8Array,
14
+ public readonly to: string,
15
15
  public readonly value: u64,
16
16
  ) {}
17
17
  }
@@ -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';
@@ -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;
@@ -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
  }