@btc-vision/btc-runtime 1.3.6 → 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.6",
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 {
@@ -66,15 +65,14 @@ export class SafeMath {
66
65
 
67
66
  public static pow(base: u256, exponent: u256): u256 {
68
67
  let result: u256 = u256.One;
69
- while (exponent > u256.Zero) {
70
- if (u256.and(exponent, u256.One)) {
68
+ while (u256.gt(exponent, u256.Zero)) {
69
+ if (u256.ne(u256.and(exponent, u256.One), u256.Zero)) {
71
70
  result = SafeMath.mul(result, base);
72
71
  }
73
72
 
74
73
  base = SafeMath.mul(base, base);
75
74
  exponent = u256.shr(exponent, 1);
76
75
  }
77
-
78
76
  return result;
79
77
  }
80
78
 
@@ -93,7 +91,6 @@ export class SafeMath {
93
91
  return c;
94
92
  }
95
93
 
96
- @inline
97
94
  @unsafe
98
95
  @operator('/')
99
96
  public static div(a: u256, b: u256): u256 {
@@ -139,7 +136,6 @@ export class SafeMath {
139
136
  return u256.gt(a, b) ? a : b;
140
137
  }
141
138
 
142
- @inline
143
139
  @unsafe
144
140
  public static sqrt(y: u256): u256 {
145
141
  if (u256.gt(y, u256.fromU32(3))) {
@@ -167,7 +163,6 @@ export class SafeMath {
167
163
  }
168
164
  }
169
165
 
170
- @inline
171
166
  @unsafe
172
167
  public static shl(value: u256, shift: i32): u256 {
173
168
  if (shift == 0) {
@@ -225,8 +220,26 @@ export class SafeMath {
225
220
  * @param value The value to increment
226
221
  * @returns The incremented value
227
222
  */
228
- @inline
229
223
  static inc(value: u256): u256 {
230
224
  return value.preInc();
231
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
+ }
232
245
  }