@btc-vision/btc-runtime 1.7.3 → 1.8.1

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.7.3",
3
+ "version": "1.8.1",
4
4
  "description": "Bitcoin Smart Contract Runtime",
5
5
  "main": "btc/index.ts",
6
6
  "scripts": {},
@@ -17,8 +17,8 @@
17
17
  "author": "BlobMaster41",
18
18
  "license": "MIT",
19
19
  "devDependencies": {
20
- "@types/node": "^22.15.27",
21
- "assemblyscript": "^0.28.1"
20
+ "@types/node": "^24.0.3",
21
+ "assemblyscript": "^0.28.2"
22
22
  },
23
23
  "repository": {
24
24
  "type": "git",
@@ -35,13 +35,13 @@
35
35
  "test/*.ts"
36
36
  ],
37
37
  "dependencies": {
38
- "@assemblyscript/loader": "^0.28.1",
38
+ "@assemblyscript/loader": "^0.28.2",
39
39
  "@btc-vision/as-bignum": "^0.0.5",
40
- "@btc-vision/opnet-transform": "^0.1.4",
41
- "@eslint/js": "^9.27.0",
40
+ "@btc-vision/opnet-transform": "^0.1.6",
41
+ "@eslint/js": "^9.29.0",
42
42
  "gulplog": "^2.2.0",
43
43
  "ts-node": "^10.9.2",
44
44
  "typescript": "^5.8.3",
45
- "typescript-eslint": "^8.33.0"
45
+ "typescript-eslint": "^8.35.0"
46
46
  }
47
47
  }
@@ -1,22 +1,19 @@
1
- import { DataView } from 'dataview';
2
- import { ArrayBuffer } from 'arraybuffer';
3
1
  import { env_exit } from '../env/global';
4
2
 
5
3
  export function revertOnError(message: string, fileName: string, line: u32, column: u32): void {
6
4
  const selector = 0x63739d5c; // Error(string)
7
5
 
8
6
  const revertMessage = `${message} at ${fileName}:${line}:${column}`;
7
+ const revertMessageBytes = Uint8Array.wrap(String.UTF8.encode(revertMessage));
9
8
 
10
- const length = revertMessage.length;
11
-
12
- const arrayBuffer = new ArrayBuffer(4 + length + 4);
9
+ const arrayBuffer = new ArrayBuffer(4 + revertMessageBytes.length + 4);
13
10
  const writer = new DataView(arrayBuffer);
14
11
 
15
12
  writer.setUint32(0, selector, false);
16
- writer.setUint32(4, length, false);
13
+ writer.setUint32(4, revertMessageBytes.length, false);
17
14
 
18
- for (let i = 0; i < length; i++) {
19
- writer.setUint8(8 + i, <u8>revertMessage.charCodeAt(i));
15
+ for (let i = 0; i < revertMessageBytes.length; i++) {
16
+ writer.setUint8(8 + i, revertMessageBytes[i]);
20
17
  }
21
18
 
22
19
  env_exit(1, arrayBuffer, arrayBuffer.byteLength);
@@ -253,7 +253,6 @@ export class BytesWriter {
253
253
  }
254
254
  }
255
255
 
256
- @inline
257
256
  public writeBytesU8Array(value: u8[]): void {
258
257
  this.allocSafe(value.length);
259
258
  for (let i = 0; i < value.length; i++) {
@@ -275,9 +274,14 @@ export class BytesWriter {
275
274
  }
276
275
 
277
276
  public writeString(value: string): void {
278
- for (let i: i32 = 0; i < value.length; i++) {
279
- this.writeU8(u8(value.charCodeAt(i)));
280
- }
277
+ const bytes = String.UTF8.encode(value);
278
+ this.writeBytes(Uint8Array.wrap(bytes));
279
+ }
280
+
281
+ public writeStringWithLength(value: string): void {
282
+ const bytes = String.UTF8.encode(value);
283
+ this.writeU32(bytes.byteLength);
284
+ this.writeBytes(Uint8Array.wrap(bytes));
281
285
  }
282
286
 
283
287
  public writeAddress(value: Address): void {
@@ -285,11 +289,6 @@ export class BytesWriter {
285
289
  this.writeBytes(bytes);
286
290
  }
287
291
 
288
- public writeStringWithLength(value: string): void {
289
- this.writeU32(u32(value.length));
290
- this.writeString(value);
291
- }
292
-
293
292
  /**
294
293
  * Equivalent to TS’s writeAddressValueTuple, except specialized for u256 values.
295
294
  */
@@ -106,7 +106,7 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
106
106
  @method('name')
107
107
  @returns({ name: 'name', type: ABIDataTypes.STRING })
108
108
  public fn_name(_: Calldata): BytesWriter {
109
- const w = new BytesWriter(this.name.length + 4);
109
+ const w = new BytesWriter(String.UTF8.byteLength(this.name) + 4);
110
110
  w.writeStringWithLength(this.name);
111
111
  return w;
112
112
  }
@@ -114,7 +114,7 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
114
114
  @method('symbol')
115
115
  @returns({ name: 'symbol', type: ABIDataTypes.STRING })
116
116
  public fn_symbol(_: Calldata): BytesWriter {
117
- const w = new BytesWriter(this.symbol.length + 4);
117
+ const w = new BytesWriter(String.UTF8.byteLength(this.symbol) + 4);
118
118
  w.writeStringWithLength(this.symbol);
119
119
  return w;
120
120
  }
@@ -108,6 +108,26 @@ export class BlockchainEnvironment {
108
108
  return this._contractAddress as Address;
109
109
  }
110
110
 
111
+ public _chainId: Potential<Uint8Array> = null;
112
+
113
+ public get chainId(): Uint8Array {
114
+ if (!this._chainId) {
115
+ throw new Revert('Chain id is required');
116
+ }
117
+
118
+ return this._chainId as Uint8Array;
119
+ }
120
+
121
+ public _protocolId: Potential<Uint8Array> = null;
122
+
123
+ public get protocolId(): Uint8Array {
124
+ if (!this._protocolId) {
125
+ throw new Revert('Protocol id is required');
126
+ }
127
+
128
+ return this._protocolId as Uint8Array;
129
+ }
130
+
111
131
  public registerPlugin(plugin: Plugin): void {
112
132
  this._plugins.push(plugin);
113
133
  }
@@ -154,11 +174,15 @@ export class BlockchainEnvironment {
154
174
  const contractDeployer = reader.readAddress();
155
175
  const caller = reader.readAddress();
156
176
  const origin = reader.readAddress();
177
+ const chainId = reader.readBytes(32);
178
+ const protocolId = reader.readBytes(32);
157
179
 
158
180
  this._tx = new Transaction(caller, origin, txId, txHash);
159
181
 
160
182
  this._contractDeployer = contractDeployer;
161
183
  this._contractAddress = contractAddress;
184
+ this._chainId = chainId;
185
+ this._protocolId = protocolId;
162
186
 
163
187
  this._block = new Block(blockHash, blockNumber, blockMedianTime);
164
188
 
@@ -191,7 +215,7 @@ export class BlockchainEnvironment {
191
215
  }
192
216
 
193
217
  public log(data: string): void {
194
- const writer = new BytesWriter(data.length + 2);
218
+ const writer = new BytesWriter(String.UTF8.byteLength(data));
195
219
  writer.writeString(data);
196
220
 
197
221
  const buffer = writer.getBuffer();
@@ -200,7 +224,7 @@ export class BlockchainEnvironment {
200
224
 
201
225
  public emit(event: NetEvent): void {
202
226
  const data = event.getEventData();
203
- const writer = new BytesWriter(event.eventType.length + 8 + data.byteLength);
227
+ const writer = new BytesWriter(String.UTF8.byteLength(event.eventType) + 8 + data.byteLength);
204
228
 
205
229
  writer.writeStringWithLength(event.eventType);
206
230
  writer.writeBytesWithLength(data);
@@ -209,7 +233,7 @@ export class BlockchainEnvironment {
209
233
  }
210
234
 
211
235
  public validateBitcoinAddress(address: string): bool {
212
- const writer = new BytesWriter(address.length);
236
+ const writer = new BytesWriter(String.UTF8.byteLength(address));
213
237
  writer.writeString(address);
214
238
 
215
239
  const result = validateBitcoinAddress(writer.getBuffer().buffer, address.length);
@@ -14,7 +14,8 @@ export class Transaction {
14
14
  public readonly origin: Address, // "leftmost thing in the call chain"
15
15
  public readonly txId: Uint8Array,
16
16
  public readonly hash: Uint8Array,
17
- ) {}
17
+ ) {
18
+ }
18
19
 
19
20
  private _inputs: Potential<TransactionInput[]> = null;
20
21
 
@@ -44,7 +45,7 @@ export class Transaction {
44
45
 
45
46
  private loadInputs(): TransactionInput[] {
46
47
  const inputsSize = getInputsSize();
47
- let resultBuffer = new ArrayBuffer(inputsSize);
48
+ const resultBuffer = new ArrayBuffer(inputsSize);
48
49
  inputs(resultBuffer);
49
50
 
50
51
  const reader = new BytesReader(Uint8Array.wrap(resultBuffer));
@@ -53,7 +54,7 @@ export class Transaction {
53
54
 
54
55
  private loadOutputs(): TransactionOutput[] {
55
56
  const outputsSize = getOutputsSize();
56
- let resultBuffer = new ArrayBuffer(outputsSize);
57
+ const resultBuffer = new ArrayBuffer(outputsSize);
57
58
  outputs(resultBuffer);
58
59
 
59
60
  const reader = new BytesReader(Uint8Array.wrap(resultBuffer));
@@ -5,7 +5,7 @@ import { Selector } from '../math/abi';
5
5
  import { Calldata } from '../types';
6
6
  import { env_exit, getCalldata, getEnvironmentVariables } from '../env/global';
7
7
 
8
- const ENVIRONMENT_VARIABLES_BYTE_LENGTH: u32 = 240;
8
+ const ENVIRONMENT_VARIABLES_BYTE_LENGTH: u32 = 304;
9
9
 
10
10
  export function execute(calldataLength: u32): u32 {
11
11
  const environmentVariablesBuffer = new ArrayBuffer(ENVIRONMENT_VARIABLES_BYTE_LENGTH);