@btc-vision/btc-runtime 1.3.7 → 1.3.8

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.8",
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);
@@ -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<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,7 +9,7 @@ export class StoredBoolean {
9
9
  public pointer: u16,
10
10
  private defaultValue: bool,
11
11
  ) {
12
- this.u256Pointer = u256.from(this.pointer);
12
+ this.u256Pointer = u256.from(pointer);
13
13
  }
14
14
 
15
15
  private _value: u256 = u256.Zero;
@@ -27,7 +27,6 @@ export class SafeMath {
27
27
  return SafeMath.mod(mul, modulus);
28
28
  }
29
29
 
30
- @inline
31
30
  @unsafe
32
31
  @operator('%')
33
32
  public static mod(a: u256, b: u256): u256 {
@@ -70,6 +69,7 @@ export class SafeMath {
70
69
  if (u256.ne(u256.and(exponent, u256.One), u256.Zero)) {
71
70
  result = SafeMath.mul(result, base);
72
71
  }
72
+
73
73
  base = SafeMath.mul(base, base);
74
74
  exponent = u256.shr(exponent, 1);
75
75
  }
@@ -91,7 +91,6 @@ export class SafeMath {
91
91
  return c;
92
92
  }
93
93
 
94
- @inline
95
94
  @unsafe
96
95
  @operator('/')
97
96
  public static div(a: u256, b: u256): u256 {
@@ -137,7 +136,6 @@ export class SafeMath {
137
136
  return u256.gt(a, b) ? a : b;
138
137
  }
139
138
 
140
- @inline
141
139
  @unsafe
142
140
  public static sqrt(y: u256): u256 {
143
141
  if (u256.gt(y, u256.fromU32(3))) {
@@ -165,7 +163,6 @@ export class SafeMath {
165
163
  }
166
164
  }
167
165
 
168
- @inline
169
166
  @unsafe
170
167
  public static shl(value: u256, shift: i32): u256 {
171
168
  if (shift == 0) {
@@ -223,8 +220,26 @@ export class SafeMath {
223
220
  * @param value The value to increment
224
221
  * @returns The incremented value
225
222
  */
226
- @inline
227
223
  static inc(value: u256): u256 {
228
224
  return value.preInc();
229
225
  }
226
+
227
+ /**
228
+ * Approximates the binary logarithm (log2) of a u256 integer.
229
+ * @param x - The input value for which to calculate log2(x).
230
+ * @returns The approximate log2(x) as u256.
231
+ */
232
+ @unsafe
233
+ public static approximateLog2(x: u256): u256 {
234
+ // Count the position of the highest bit set
235
+ let n: u256 = u256.Zero;
236
+ let value = x;
237
+
238
+ while (u256.gt(value, u256.One)) {
239
+ value = u256.shr(value, 1);
240
+ n = SafeMath.add(n, u256.One);
241
+ }
242
+
243
+ return n;
244
+ }
230
245
  }