@btc-vision/btc-runtime 1.5.2 → 1.5.4

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/README.md CHANGED
@@ -31,13 +31,13 @@ execution while integrating deeply with Bitcoin's decentralized architecture.
31
31
 
32
32
  ### Features
33
33
 
34
- - **AssemblyScript and WebAssembly:** Efficient and high-performance contract execution using WebAssembly.
35
- - **Bitcoin Integration:** Direct interaction with Bitcoin L1, enabling the creation of decentralized applications that
36
- operate on the Bitcoin network.
37
- - **Comprehensive Storage Management:** Flexible and secure storage management using primary pointers and sub-pointers,
38
- ensuring data integrity through cryptographic proofs.
39
- - **Event Handling:** Sophisticated event system for contract state changes, allowing easy tracking and logging of
40
- contract activities.
34
+ - **AssemblyScript and WebAssembly:** Efficient and high-performance contract execution using WebAssembly.
35
+ - **Bitcoin Integration:** Direct interaction with Bitcoin L1, enabling the creation of decentralized applications that
36
+ operate on the Bitcoin network.
37
+ - **Comprehensive Storage Management:** Flexible and secure storage management using primary pointers and sub-pointers,
38
+ ensuring data integrity through cryptographic proofs.
39
+ - **Event Handling:** Sophisticated event system for contract state changes, allowing easy tracking and logging of
40
+ contract activities.
41
41
 
42
42
  ## Installation
43
43
 
@@ -105,7 +105,7 @@ import {
105
105
  Map,
106
106
  OP20InitParameters,
107
107
  Selector,
108
- AddressMap
108
+ AddressMap,
109
109
  } from '@btc-vision/btc-runtime/runtime';
110
110
  import { u128, u256 } from 'as-bignum/assembly';
111
111
 
@@ -142,7 +142,7 @@ export class MyToken extends DeployableOP_20 {
142
142
  }
143
143
 
144
144
  private airdrop(calldata: Calldata): BytesWriter {
145
- this.onlyOwner(Blockchain.tx.sender);
145
+ this.onlyDeployer(Blockchain.tx.sender);
146
146
 
147
147
  const drops: AddressMap<u256> = calldata.readAddressValueTuple();
148
148
 
@@ -169,7 +169,7 @@ export class MyToken extends DeployableOP_20 {
169
169
  }
170
170
 
171
171
  private airdropWithAmount(calldata: Calldata): BytesWriter {
172
- this.onlyOwner(Blockchain.tx.sender);
172
+ this.onlyDeployer(Blockchain.tx.sender);
173
173
 
174
174
  const amount: u256 = calldata.readU256();
175
175
  const addresses: Address[] = calldata.readAddressArray();
@@ -211,11 +211,11 @@ class ComplexData extends Serializable {
211
211
 
212
212
  For more detailed explanations on specific topics, refer to the individual documentation files:
213
213
 
214
- - [Blockchain.md](docs/Blockchain.md)
215
- - [Contract.md](docs/Contract.md)
216
- - [Events.md](docs/Events.md)
217
- - [Pointers.md](docs/Pointers.md)
218
- - [Storage.md](docs/Storage.md)
214
+ - [Blockchain.md](docs/Blockchain.md)
215
+ - [Contract.md](docs/Contract.md)
216
+ - [Events.md](docs/Events.md)
217
+ - [Pointers.md](docs/Pointers.md)
218
+ - [Storage.md](docs/Storage.md)
219
219
 
220
220
  ## License
221
221
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@btc-vision/btc-runtime",
3
- "version": "1.5.2",
3
+ "version": "1.5.4",
4
4
  "description": "Bitcoin Smart Contract Runtime",
5
5
  "main": "btc/index.ts",
6
6
  "scripts": {},
@@ -329,12 +329,12 @@ export class BytesReader {
329
329
  }
330
330
 
331
331
  public readTransactionInputs(): TransactionInput[] {
332
- const length = this.readU8();
332
+ const length = this.readU16();
333
333
  const result = new Array<TransactionInput>(length);
334
334
 
335
335
  for (let i: u16 = 0; i < length; i++) {
336
336
  const txId = this.readBytes(32);
337
- const outputIndex = this.readU8();
337
+ const outputIndex = this.readU16();
338
338
  const scriptSig = this.readBytesWithLength();
339
339
  result[i] = new TransactionInput(txId, outputIndex, scriptSig);
340
340
  }
@@ -343,11 +343,11 @@ export class BytesReader {
343
343
  }
344
344
 
345
345
  public readTransactionOutputs(): TransactionOutput[] {
346
- const length = this.readU8();
346
+ const length = this.readU16();
347
347
  const result = new Array<TransactionOutput>(length);
348
348
 
349
349
  for (let i: u16 = 0; i < length; i++) {
350
- const index = this.readU8();
350
+ const index = this.readU16();
351
351
  const scriptPubKey = this.readStringWithLength();
352
352
  const value = this.readU64();
353
353
  result[i] = new TransactionOutput(index, scriptPubKey, value);
@@ -9,16 +9,16 @@ import { Address } from '../types/Address';
9
9
  import { Revert } from '../types/Revert';
10
10
  import { SafeMath } from '../types/SafeMath';
11
11
 
12
+ import { sha256 } from '../env/global';
13
+ import { EMPTY_POINTER } from '../math/bytes';
14
+ import { AddressMemoryMap } from '../memory/AddressMemoryMap';
15
+ import { MapOfMap } from '../memory/MapOfMap';
12
16
  import { ApproveStr, TransferFromStr, TransferStr } from '../shared-libraries/TransferHelper';
13
17
  import { Calldata } from '../types';
14
18
  import { ADDRESS_BYTE_LENGTH, BOOLEAN_BYTE_LENGTH, U256_BYTE_LENGTH } from '../utils';
15
19
  import { IOP_20 } from './interfaces/IOP_20';
16
20
  import { OP20InitParameters } from './interfaces/OP20InitParameters';
17
21
  import { OP_NET } from './OP_NET';
18
- import { sha256 } from '../env/global';
19
- import { EMPTY_POINTER } from '../math/bytes';
20
- import { MapOfMap } from '../memory/MapOfMap';
21
- import { AddressMemoryMap } from '../memory/AddressMemoryMap';
22
22
 
23
23
  const nonceMapPointer: u16 = Blockchain.nextPointer;
24
24
  const maxSupplyPointer: u16 = Blockchain.nextPointer;
@@ -84,12 +84,15 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
84
84
  return this._symbol.value;
85
85
  }
86
86
 
87
- public instantiate(params: OP20InitParameters, skipOwnerVerification: boolean = false): void {
87
+ public instantiate(
88
+ params: OP20InitParameters,
89
+ skipDeployerVerification: boolean = false,
90
+ ): void {
88
91
  if (!this._maxSupply.value.isZero()) {
89
92
  throw new Revert('Already initialized');
90
93
  }
91
94
 
92
- if (!skipOwnerVerification) this.onlyDeployer(Blockchain.tx.sender);
95
+ if (!skipDeployerVerification) this.onlyDeployer(Blockchain.tx.sender);
93
96
 
94
97
  if (params.decimals > 32) {
95
98
  throw new Revert('Decimals can not be more than 32');
@@ -446,8 +449,8 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
446
449
  this.emitEvent(approveEvent);
447
450
  }
448
451
 
449
- protected createMintEvent(owner: Address, value: u256): void {
450
- const mintEvent = new MintEvent(owner, value);
452
+ protected createMintEvent(recipient: Address, value: u256): void {
453
+ const mintEvent = new MintEvent(recipient, value);
451
454
  this.emitEvent(mintEvent);
452
455
  }
453
456
 
@@ -17,14 +17,11 @@ export class OP_NET implements IBTC {
17
17
  return Blockchain.contractDeployer;
18
18
  }
19
19
 
20
- public onDeployment(_calldata: Calldata): void {
21
- }
20
+ public onDeployment(_calldata: Calldata): void {}
22
21
 
23
- public onExecutionStarted(): void {
24
- }
22
+ public onExecutionStarted(): void {}
25
23
 
26
- public onExecutionCompleted(): void {
27
- }
24
+ public onExecutionCompleted(): void {}
28
25
 
29
26
  public execute(method: Selector, _calldata: Calldata): BytesWriter {
30
27
  let response: BytesWriter;
@@ -55,7 +52,7 @@ export class OP_NET implements IBTC {
55
52
 
56
53
  protected onlyDeployer(caller: Address): void {
57
54
  if (this.contractDeployer !== caller) {
58
- throw new Revert('Only owner can call this method');
55
+ throw new Revert('Only deployer can call this method');
59
56
  }
60
57
  }
61
58
  }
@@ -2,7 +2,7 @@
2
2
  export class TransactionInput {
3
3
  public constructor(
4
4
  public readonly txId: Uint8Array,
5
- public readonly outputIndex: u8,
5
+ public readonly outputIndex: u16,
6
6
  public readonly scriptSig: Uint8Array,
7
7
  ) {
8
8
  }
@@ -11,7 +11,7 @@ export class TransactionInput {
11
11
  @final
12
12
  export class TransactionOutput {
13
13
  public constructor(
14
- public readonly index: u8,
14
+ public readonly index: u16,
15
15
  public readonly to: string,
16
16
  public readonly value: u64,
17
17
  ) {
@@ -1,7 +1,6 @@
1
- import { BytesReader } from '../../buffer/BytesReader';
2
1
  import { BytesWriter } from '../../buffer/BytesWriter';
3
2
  import { Blockchain } from '../../env';
4
- import { addUint8ArraysBE, u64ToBE32Bytes } from '../../math/bytes';
3
+ import { addUint8ArraysBE, encodeBasePointer, readLengthAndStartIndex, u64ToBE32Bytes } from '../../math/bytes';
5
4
  import { Address } from '../../types/Address';
6
5
  import { Revert } from '../../types/Revert';
7
6
 
@@ -38,25 +37,15 @@ export class StoredAddressArray {
38
37
  `You must pass a 30 bytes sub-pointer. (AddressArray, got ${subPointer.length})`,
39
38
  );
40
39
 
41
- // Construct base pointer as a 32-byte array
42
- const writer = new BytesWriter(32);
43
- writer.writeU16(pointer);
44
- writer.writeBytes(subPointer);
40
+ const basePointer = encodeBasePointer(pointer, subPointer);
41
+ this.lengthPointer = Uint8Array.wrap(basePointer.buffer);
42
+ this.baseU256Pointer = basePointer;
45
43
 
46
- // We'll reuse the same bytes as the "base pointer" for offsets
47
- const baseU256Pointer = writer.getBuffer(); // 32 bytes
48
- // For length+startIndex, we'll use the same pointer
49
- const lengthPointer = Uint8Array.wrap(baseU256Pointer.buffer);
44
+ const storedLenStart = Blockchain.getStorageAt(basePointer);
45
+ const data = readLengthAndStartIndex(storedLenStart);
50
46
 
51
- // Load length + startIndex from storage (16 bytes: 8 for length, 8 for startIndex).
52
- const storedLengthAndStartIndex: Uint8Array = Blockchain.getStorageAt(lengthPointer);
53
-
54
- const reader = new BytesReader(storedLengthAndStartIndex);
55
- this._length = reader.readU64();
56
- this._startIndex = reader.readU64();
57
-
58
- this.lengthPointer = lengthPointer;
59
- this.baseU256Pointer = baseU256Pointer;
47
+ this._length = data[0];
48
+ this._startIndex = data[1];
60
49
  }
61
50
 
62
51
  /** Get an element by its global index. */
@@ -1,8 +1,15 @@
1
1
  import { BytesWriter } from '../../buffer/BytesWriter';
2
- import { BytesReader } from '../../buffer/BytesReader';
3
2
  import { Blockchain } from '../../env';
4
3
  import { Revert } from '../../types/Revert';
5
- import { addUint8ArraysBE, GET_EMPTY_BUFFER, getBit, setBit, u64ToBE32Bytes } from '../../math/bytes';
4
+ import {
5
+ addUint8ArraysBE,
6
+ encodeBasePointer,
7
+ GET_EMPTY_BUFFER,
8
+ getBit,
9
+ readLengthAndStartIndex,
10
+ setBit,
11
+ u64ToBE32Bytes,
12
+ } from '../../math/bytes';
6
13
 
7
14
  /**
8
15
  * @class StoredBooleanArray
@@ -42,22 +49,15 @@ export class StoredBooleanArray {
42
49
  ) {
43
50
  assert(subPtr.length <= 30, `You must pass a 30 bytes sub-pointer. (StoredBooleanArray, got ${subPtr.length})`);
44
51
 
45
- const writer = new BytesWriter(32);
46
- writer.writeU16(pointer);
47
- writer.writeBytes(subPtr);
48
-
49
- const basePtr = writer.getBuffer();
52
+ const basePointer = encodeBasePointer(pointer, subPtr);
53
+ this.lengthPointer = Uint8Array.wrap(basePointer.buffer);
54
+ this.basePointer = basePointer;
50
55
 
51
- this.basePointer = basePtr;
52
- this.lengthPointer = basePtr;
56
+ const storedLenStart = Blockchain.getStorageAt(basePointer);
57
+ const data = readLengthAndStartIndex(storedLenStart);
53
58
 
54
- const storedLenStart: Uint8Array = Blockchain.getStorageAt(
55
- this.lengthPointer,
56
- );
57
-
58
- const r = new BytesReader(storedLenStart);
59
- this._length = r.readU64();
60
- this._startIndex = r.readU64();
59
+ this._length = data[0];
60
+ this._startIndex = data[1];
61
61
  }
62
62
 
63
63
  // -------------- Public Accessors -------------- //
@@ -187,12 +187,11 @@ export class StoredBooleanArray {
187
187
 
188
188
  // 2) If length or startIndex changed, store them
189
189
  if (this._isChangedLength || this._isChangedStartIndex) {
190
- const w = new BytesWriter(16);
190
+ const w = new BytesWriter(32);
191
191
  w.writeU64(this._length);
192
192
  w.writeU64(this._startIndex);
193
193
 
194
- const data = w.getBuffer(); // 16 bytes
195
- // You could store 32 bytes if you prefer; the leftover bytes can be 0
194
+ const data = w.getBuffer();
196
195
  Blockchain.setStorageAt(this.lengthPointer, data);
197
196
 
198
197
  this._isChangedLength = false;
@@ -215,9 +214,7 @@ export class StoredBooleanArray {
215
214
  }
216
215
 
217
216
  // also reset length + startIndex in storage
218
- const writer = new BytesWriter(16);
219
- writer.writeU64(0);
220
- writer.writeU64(0);
217
+ const writer = new BytesWriter(32);
221
218
  Blockchain.setStorageAt(this.lengthPointer, writer.getBuffer());
222
219
 
223
220
  // reset in memory