@btc-vision/btc-runtime 1.3.8 → 1.3.10

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.8",
3
+ "version": "1.3.10",
4
4
  "description": "Bitcoin Smart Contract Runtime",
5
5
  "main": "btc/index.ts",
6
6
  "scripts": {
@@ -191,6 +191,17 @@ export class BytesReader {
191
191
  return result;
192
192
  }
193
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
+
194
205
  public readAddressValueTuple(): AddressMap<u256> {
195
206
  const length: u16 = this.readU16();
196
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
 
@@ -284,7 +284,6 @@ export abstract class DeployableOP_20 extends OP_NET implements IOP_20 {
284
284
  protected _transfer(to: Address, value: u256): boolean {
285
285
  const sender = Blockchain.tx.sender;
286
286
 
287
- if (!this.balanceOfMap.has(sender)) throw new Revert();
288
287
  if (this.isSelf(sender)) throw new Revert('Can not transfer from self account');
289
288
 
290
289
  if (u256.eq(value, 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
 
@@ -19,6 +19,7 @@ import {
19
19
  nextPointerGreaterThan,
20
20
  storePointer,
21
21
  validateBitcoinAddress,
22
+ verifySchnorrSignature,
22
23
  } from './global';
23
24
  import { DeployContractResponse } from '../interfaces/DeployContractResponse';
24
25
  import { MapU256 } from '../generic/MapU256';
@@ -248,6 +249,22 @@ export class BlockchainEnvironment {
248
249
  return reader.readU256();
249
250
  }
250
251
 
252
+ public verifySchnorrSignature(
253
+ publicKey: Address,
254
+ signature: Uint8Array,
255
+ hash: Uint8Array,
256
+ ): boolean {
257
+ const writer = new BytesWriter(128);
258
+ writer.writeBytes(publicKey);
259
+ writer.writeBytes(signature);
260
+ writer.writeBytes(hash);
261
+
262
+ const result: Uint8Array = verifySchnorrSignature(writer.getBuffer());
263
+
264
+ const reader = new BytesReader(result);
265
+ return reader.readBoolean();
266
+ }
267
+
251
268
  public hasStorageAt(pointerHash: MemorySlotPointer): bool {
252
269
  // We mark zero as the default value for the storage, if something is 0, the storage slot get deleted or is non-existent
253
270
  const val: u256 = this.getStorageAt(pointerHash, u256.Zero);
@@ -53,3 +53,7 @@ export declare function inputs(): Uint8Array;
53
53
  // @ts-ignore
54
54
  @external('env', 'outputs')
55
55
  export declare function outputs(): Uint8Array;
56
+
57
+ // @ts-ignore
58
+ @external('env', 'verifySchnorrSignature')
59
+ export declare function verifySchnorrSignature(data: Uint8Array): Uint8Array;
@@ -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
  }