@btc-vision/btc-runtime 1.9.16 → 1.10.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.
@@ -0,0 +1,426 @@
1
+ import { Potential } from '../lang/Definitions';
2
+ import { decodeHexArray } from '../utils';
3
+ import { Blockchain } from '../env';
4
+ import { Network } from '../script/Networks';
5
+ import { Address } from './Address';
6
+ import { BitcoinAddresses } from '../script/BitcoinAddresses';
7
+
8
+ /**
9
+ * Extended address implementation for Bitcoin with dual-key support.
10
+ *
11
+ * ExtendedAddress combines both Schnorr (taproot) and ML-DSA public keys to provide
12
+ * a migration path from classical to quantum-resistant cryptography. The address
13
+ * stores the ML-DSA public key hash (inherited from Address) and additionally
14
+ * maintains the tweaked Schnorr public key for Bitcoin taproot compatibility.
15
+ *
16
+ * @remarks
17
+ * This class is marked as @final and cannot be extended. It represents the complete
18
+ * address format for OPNet's quantum-resistant transition, supporting both legacy
19
+ * Schnorr signatures and future ML-DSA signatures within the same address structure.
20
+ *
21
+ * The tweaked public key enables P2TR (pay-to-taproot) address generation while
22
+ * the ML-DSA key hash provides quantum resistance when consensus transitions.
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * // Create from hex strings
27
+ * const addr = ExtendedAddress.fromStringPair(
28
+ * '0x' + '11'.repeat(32), // tweaked Schnorr key
29
+ * '0x' + '22'.repeat(32) // ML-DSA key hash
30
+ * );
31
+ *
32
+ * // Generate P2TR address
33
+ * const bitcoinAddr = addr.p2tr();
34
+ *
35
+ * // Check if address is dead/zero
36
+ * if (addr.isDead()) {
37
+ * // Handle dead address
38
+ * }
39
+ * ```
40
+ */
41
+ @final
42
+ export class ExtendedAddress extends Address {
43
+ /**
44
+ * The 32-byte tweaked Schnorr public key for taproot compatibility.
45
+ * This key is used for P2TR address generation and Schnorr signature verification.
46
+ */
47
+ private readonly tweakedPublicKey: Uint8Array;
48
+
49
+ /**
50
+ * Creates a new ExtendedAddress instance.
51
+ *
52
+ * @param tweakedPublicKey - 32-byte tweaked Schnorr public key for taproot
53
+ * @param publicKey - 32-byte ML-DSA public key hash (SHA256 of full ML-DSA key)
54
+ *
55
+ * @throws {Error} If tweakedPublicKey is not exactly 32 bytes
56
+ * @throws {Error} If publicKey is not exactly 32 bytes (thrown by parent class)
57
+ */
58
+ public constructor(tweakedPublicKey: u8[], publicKey: u8[]) {
59
+ super(publicKey);
60
+
61
+ if (tweakedPublicKey.length !== 32) {
62
+ throw new Error('Tweaked public key must be 32 bytes long');
63
+ }
64
+
65
+ this.tweakedPublicKey = new Uint8Array(32);
66
+ this.tweakedPublicKey.set(tweakedPublicKey);
67
+ }
68
+
69
+ /**
70
+ * Returns the canonical dead address.
71
+ *
72
+ * The dead address (284ae4acdb32a99ba3ebfa66a91ddb41a7b7a1d2fef415399922cd8a04485c02)
73
+ * is generated from the uncompressed public key:
74
+ * 04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f
75
+ *
76
+ * This address is commonly used as a burn address or null recipient in contracts.
77
+ *
78
+ * @returns A clone of the predefined DEAD_ADDRESS constant
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * const burnAddr = ExtendedAddress.dead();
83
+ * if (recipient.isDead()) {
84
+ * // Tokens are being burned
85
+ * }
86
+ * ```
87
+ */
88
+ public static dead(): ExtendedAddress {
89
+ return DEAD_ADDRESS.clone();
90
+ }
91
+
92
+ /**
93
+ * Returns the zero address with all bytes set to 0x00.
94
+ *
95
+ * @returns A clone of the predefined ZERO_BITCOIN_ADDRESS constant
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * const zero = ExtendedAddress.zero();
100
+ * if (addr.isZero()) {
101
+ * // Handle uninitialized address
102
+ * }
103
+ * ```
104
+ */
105
+ public static zero(): ExtendedAddress {
106
+ return ZERO_BITCOIN_ADDRESS.clone();
107
+ }
108
+
109
+ /**
110
+ * Disabled method - use fromStringPair instead.
111
+ *
112
+ * This method is intentionally disabled for ExtendedAddress to prevent
113
+ * accidental creation with only one key. ExtendedAddress requires both
114
+ * the tweaked Schnorr key and ML-DSA key hash for proper functionality.
115
+ *
116
+ * @param _ - Unused parameter
117
+ *
118
+ * @throws {Error} Always throws with instruction to use fromStringPair
119
+ *
120
+ * @deprecated Use {@link fromStringPair} instead
121
+ */
122
+ public static override fromString(_: string): ExtendedAddress {
123
+ ERROR(
124
+ `Use ExtendedAddress.fromStringPair instead. This method is disabled. You must provide both the tweaked public key and the ML-DSA public key.`,
125
+ );
126
+ }
127
+
128
+ /**
129
+ * Creates an ExtendedAddress from hexadecimal string representations of both keys.
130
+ *
131
+ * This is the preferred factory method for creating ExtendedAddress instances when
132
+ * both the Schnorr tweaked public key and ML-DSA public key are available as strings.
133
+ * Unlike the single-parameter fromString method (which maintains backward compatibility
134
+ * with the base Address class), this method properly initializes both key components
135
+ * required for full ExtendedAddress functionality.
136
+ *
137
+ * @param tweakedPubKey - The 32-byte Schnorr tweaked public key as a hexadecimal string.
138
+ * Can be prefixed with '0x' or unprefixed. Must decode to exactly
139
+ * 32 bytes for taproot compatibility.
140
+ * @param mldsaPubKey - The 32-byte ML-DSA public key hash (SHA256 of the full ML-DSA public key)
141
+ * as a hexadecimal string. Can be prefixed with '0x' or unprefixed.
142
+ * Must decode to exactly 32 bytes for address derivation.
143
+ *
144
+ * @returns A new ExtendedAddress instance with both keys properly initialized
145
+ *
146
+ * @throws {Error} If tweakedPubKey doesn't decode to exactly 32 bytes
147
+ * @throws {Error} If mldsaPubKey doesn't decode to exactly 32 bytes
148
+ * @throws {Error} If either string contains invalid hexadecimal characters
149
+ *
150
+ * @example
151
+ * ```typescript
152
+ * // With 0x prefix
153
+ * const addr1 = ExtendedAddress.fromStringPair(
154
+ * '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
155
+ * '0xfedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210'
156
+ * );
157
+ *
158
+ * // Without 0x prefix
159
+ * const addr2 = ExtendedAddress.fromStringPair(
160
+ * '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
161
+ * 'fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210'
162
+ * );
163
+ * ```
164
+ *
165
+ * @remarks
166
+ * For quantum-resistant applications, ensure the mldsaPubKey parameter is the SHA256 hash
167
+ * of a valid ML-DSA-44 (Level 2) public key. The full ML-DSA public key will be loaded
168
+ * from storage when needed for signature verification.
169
+ *
170
+ * @see {@link fromUint8Array} for binary initialization with concatenated keys
171
+ */
172
+ public static fromStringPair(tweakedPubKey: string, mldsaPubKey: string): ExtendedAddress {
173
+ if (tweakedPubKey.startsWith('0x')) {
174
+ tweakedPubKey = tweakedPubKey.slice(2);
175
+ }
176
+
177
+ if (mldsaPubKey.startsWith('0x')) {
178
+ mldsaPubKey = mldsaPubKey.slice(2);
179
+ }
180
+
181
+ return new ExtendedAddress(decodeHexArray(tweakedPubKey), decodeHexArray(mldsaPubKey));
182
+ }
183
+
184
+ /**
185
+ * Creates an ExtendedAddress from a concatenated 64-byte array.
186
+ *
187
+ * The input must contain exactly 64 bytes: the first 32 bytes are the tweaked
188
+ * Schnorr public key, followed by 32 bytes of the ML-DSA public key hash.
189
+ *
190
+ * @param bytes - A 64-byte Uint8Array containing both keys concatenated
191
+ *
192
+ * @returns A new ExtendedAddress instance
193
+ *
194
+ * @throws {Error} If the input is not exactly 64 bytes
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * const combined = new Uint8Array(64);
199
+ * combined.set(tweakedKey, 0);
200
+ * combined.set(mldsaKeyHash, 32);
201
+ * const addr = ExtendedAddress.fromUint8Array(combined);
202
+ * ```
203
+ */
204
+ public static fromUint8Array(bytes: Uint8Array): ExtendedAddress {
205
+ if (bytes.length !== 64) {
206
+ throw new Error('Expected 64 bytes: 32 for tweakedPublicKey, 32 for publicKey');
207
+ }
208
+
209
+ const tweakedPublicKey: u8[] = new Array<u8>(32);
210
+ const publicKey: u8[] = new Array<u8>(32);
211
+
212
+ for (let i = 0; i < 32; i++) {
213
+ tweakedPublicKey[i] = bytes[i];
214
+ publicKey[i] = bytes[32 + i];
215
+ }
216
+
217
+ return new ExtendedAddress(tweakedPublicKey, publicKey);
218
+ }
219
+
220
+ /**
221
+ * Generates a CSV (CheckSequenceVerify) timelocked P2WSH address.
222
+ *
223
+ * Creates a Bitcoin address that requires both the specified number of blocks
224
+ * to pass and the correct signature to spend funds. Used for timelocked
225
+ * liquidity provisions in NativeSwap and similar protocols.
226
+ *
227
+ * @param address - The address bytes to create CSV lock for
228
+ * @param blocks - Number of blocks for the CSV timelock
229
+ *
230
+ * @returns The generated P2WSH address string with CSV timelock
231
+ *
232
+ * @example
233
+ * ```typescript
234
+ * // Create 144-block (approximately 1 day) timelock
235
+ * const timelocked = ExtendedAddress.toCSV(addr.toBytes(), 144);
236
+ * ```
237
+ */
238
+ public static toCSV(address: Uint8Array, blocks: u32): string {
239
+ return BitcoinAddresses.csvP2wshAddress(address, blocks, Network.hrp(Blockchain.network))
240
+ .address;
241
+ }
242
+
243
+ /**
244
+ * Generates a P2WPKH (pay-to-witness-public-key-hash) address.
245
+ *
246
+ * Creates a native SegWit address (bc1...) from the provided address bytes.
247
+ * This is the standard Bitcoin address format for SegWit transactions.
248
+ *
249
+ * @param address - The address bytes to encode
250
+ *
251
+ * @returns The bech32-encoded P2WPKH address string
252
+ *
253
+ * @example
254
+ * ```typescript
255
+ * const segwitAddr = ExtendedAddress.p2wpkh(addr.toBytes());
256
+ * // Returns: "bc1q..." on mainnet or "tb1q..." on testnet
257
+ * ```
258
+ */
259
+ public static p2wpkh(address: Uint8Array): string {
260
+ return BitcoinAddresses.p2wpkh(address, Network.hrp(Blockchain.network));
261
+ }
262
+
263
+ /**
264
+ * Downcasts this ExtendedAddress to a base Address.
265
+ *
266
+ * Returns this instance as the base Address type, maintaining only
267
+ * the ML-DSA public key hash and losing access to the tweaked key.
268
+ *
269
+ * @returns This instance cast as Address
270
+ *
271
+ * @remarks
272
+ * The returned Address still contains the same data but without
273
+ * access to ExtendedAddress-specific methods and the tweaked key.
274
+ */
275
+ public downCast(): Address {
276
+ return this;
277
+ }
278
+
279
+ /**
280
+ * Checks if all bytes of the ML-DSA key hash are zero.
281
+ *
282
+ * @returns `true` if the ML-DSA key hash is all zeros, `false` otherwise
283
+ *
284
+ * @remarks
285
+ * This only checks the ML-DSA key hash portion (inherited from Address),
286
+ * not the tweaked Schnorr key. Use ZERO_BITCOIN_ADDRESS for a fully
287
+ * zero ExtendedAddress.
288
+ */
289
+ public isZero(): bool {
290
+ for (let i = 0; i < this.length; i++) {
291
+ if (this[i] != 0) {
292
+ return false;
293
+ }
294
+ }
295
+
296
+ return true;
297
+ }
298
+
299
+ /**
300
+ * Checks if this address equals the canonical dead address.
301
+ *
302
+ * @returns `true` if this address matches DEAD_ADDRESS, `false` otherwise
303
+ *
304
+ * @example
305
+ * ```typescript
306
+ * if (recipient.isDead()) {
307
+ * // Funds are being burned
308
+ * return;
309
+ * }
310
+ * ```
311
+ */
312
+ public isDead(): bool {
313
+ for (let i = 0; i < this.length; i++) {
314
+ if (this[i] != DEAD_ADDRESS[i]) {
315
+ return false;
316
+ }
317
+ }
318
+ return true;
319
+ }
320
+
321
+ /**
322
+ * Generates the P2TR (pay-to-taproot) address for this ExtendedAddress.
323
+ *
324
+ * Uses the tweaked Schnorr public key to create a taproot address
325
+ * following BIP341 specifications. This is the primary Bitcoin address
326
+ * format for this ExtendedAddress.
327
+ *
328
+ * @returns The bech32m-encoded P2TR address string
329
+ *
330
+ * @example
331
+ * ```typescript
332
+ * const taprootAddr = addr.p2tr();
333
+ * // Returns: "bc1p..." on mainnet or "tb1p..." on testnet
334
+ * ```
335
+ *
336
+ * @remarks
337
+ * The address is generated from the tweaked public key only.
338
+ * The ML-DSA key hash is not used in P2TR address generation.
339
+ */
340
+ public p2tr(): string {
341
+ return BitcoinAddresses.p2trKeyPathAddress(
342
+ this.tweakedPublicKey,
343
+ Network.hrp(Blockchain.network),
344
+ );
345
+ }
346
+
347
+ /**
348
+ * Returns the P2TR address string representation.
349
+ *
350
+ * @returns The P2TR address (delegates to p2tr())
351
+ *
352
+ * @override Overrides the base Address.toString() which returns hex
353
+ */
354
+ public toString(): string {
355
+ return this.p2tr();
356
+ }
357
+
358
+ /**
359
+ * Creates a deep copy of this ExtendedAddress.
360
+ *
361
+ * @returns A new ExtendedAddress instance with identical data
362
+ *
363
+ * @override Overrides Address.clone() to return ExtendedAddress type
364
+ *
365
+ * @remarks
366
+ * Both the tweaked Schnorr key and ML-DSA key hash are cloned.
367
+ * The isDefined flag state is also preserved in the clone.
368
+ */
369
+ public override clone(): ExtendedAddress {
370
+ // Convert Uint8Array to u8[] for the tweakedPublicKey
371
+ const tweakedKeyArray: u8[] = new Array<u8>(this.tweakedPublicKey.length);
372
+ for (let i = 0; i < this.tweakedPublicKey.length; i++) {
373
+ tweakedKeyArray[i] = this.tweakedPublicKey[i];
374
+ }
375
+
376
+ // Convert the address bytes (this.slice(0)) to u8[]
377
+ const addressSlice = this.slice(0);
378
+ const addressArray: u8[] = new Array<u8>(addressSlice.length);
379
+ for (let i = 0; i < addressSlice.length; i++) {
380
+ addressArray[i] = addressSlice[i];
381
+ }
382
+
383
+ const cloned = new ExtendedAddress(tweakedKeyArray, addressArray);
384
+
385
+ // Duplicate the isDefined flag as well:
386
+ cloned.isDefined = this.isDefined;
387
+
388
+ return cloned;
389
+ }
390
+ }
391
+
392
+ /**
393
+ * Pre-initialized zero ExtendedAddress constant.
394
+ * Both the tweaked key and ML-DSA key hash are all zeros.
395
+ */
396
+ export const ZERO_BITCOIN_ADDRESS: ExtendedAddress = new ExtendedAddress(
397
+ [
398
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
399
+ 0,
400
+ ],
401
+ [
402
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
403
+ 0,
404
+ ],
405
+ );
406
+
407
+ /**
408
+ * Pre-initialized dead ExtendedAddress constant.
409
+ * The tweaked key is zero while the ML-DSA key hash represents the canonical dead address.
410
+ * Hash: 284ae4acdb32a99ba3ebfa66a91ddb41a7b7a1d2fef415399922cd8a04485c02
411
+ */
412
+ export const DEAD_ADDRESS: ExtendedAddress = new ExtendedAddress(
413
+ [
414
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
415
+ 0,
416
+ ],
417
+ [
418
+ 40, 74, 228, 172, 219, 50, 169, 155, 163, 235, 250, 102, 169, 29, 219, 65, 167, 183, 161,
419
+ 210, 254, 244, 21, 57, 153, 34, 205, 138, 4, 72, 92, 2,
420
+ ],
421
+ );
422
+
423
+ /**
424
+ * Type alias for nullable ExtendedAddress references.
425
+ */
426
+ export declare type PotentialBitcoinAddress = Potential<ExtendedAddress>;
@@ -1,55 +0,0 @@
1
- import { Blockchain } from '../env';
2
- import { Potential } from '../lang/Definitions';
3
- import { bigEndianAdd } from '../math/bytes';
4
- import { Plugin } from '../plugins/Plugin';
5
-
6
- /**
7
- * A unique key in storage that holds the next free "offset" as a 256-bit counter.
8
- * You can change this to any 32-byte constant you like.
9
- */
10
- const ALLOCATOR_KEY = new Uint8Array(32);
11
- for (let i = 0; i < 32; i++) {
12
- ALLOCATOR_KEY[i] = 0xff;
13
- }
14
-
15
- /**
16
- * PointerManager: ensures we never collide while allocating variable-length data.
17
- * - We store a global "offset" in ALLOCATOR_KEY (as a big-endian u256).
18
- * - Each time we allocate N slots (N * 32 bytes), we do:
19
- * oldOffset = currentGlobalOffset
20
- * newOffset = oldOffset + N
21
- * store newOffset back
22
- * return oldOffset as the base pointer
23
- */
24
- class _PointerManager extends Plugin {
25
- private _cachedOffset: Potential<Uint8Array> = null;
26
-
27
- private get cachedOffset(): Uint8Array {
28
- if (!this._cachedOffset) {
29
- this._cachedOffset = Blockchain.getStorageAt(ALLOCATOR_KEY);
30
- }
31
-
32
- return this._cachedOffset as Uint8Array;
33
- }
34
-
35
- /**
36
- * Allocates `numSlots` (each 32 bytes). Returns a 32-byte pointer (u256 in big-endian).
37
- *
38
- * Each slot is conceptually:
39
- * slot0 = pointer + 0
40
- * slot1 = pointer + 1
41
- * ...
42
- * and so forth in big-endian arithmetic.
43
- */
44
- public allocateSlots(numSlots: u64): Uint8Array {
45
- this._cachedOffset = bigEndianAdd(this.cachedOffset, numSlots);
46
-
47
- const val = this.cachedOffset;
48
- Blockchain.setStorageAt(ALLOCATOR_KEY, val);
49
-
50
- return val;
51
- }
52
- }
53
-
54
- export const PointerManager = new _PointerManager();
55
- Blockchain.registerPlugin(PointerManager);
@@ -1,19 +0,0 @@
1
- import { ICodec } from '../interfaces/ICodec';
2
- import { Address } from '../../types/Address';
3
-
4
- class _AddressCodec implements ICodec<Address> {
5
- public encode(value: Address): Uint8Array {
6
- return value;
7
- }
8
-
9
- public decode(buffer: Uint8Array): Address {
10
- if (buffer.length == 0) {
11
- return Address.zero();
12
- }
13
-
14
- return Address.fromUint8Array(buffer);
15
- }
16
- }
17
-
18
- export const idOfAddressCodec = idof<_AddressCodec>();
19
- export const AddressCodec = new _AddressCodec();
@@ -1,17 +0,0 @@
1
- import { ICodec } from '../interfaces/ICodec';
2
-
3
- class _BooleanCodec implements ICodec<bool> {
4
- public encode(value: bool): Uint8Array {
5
- const out = new Uint8Array(1);
6
- out[0] = value ? 1 : 0;
7
- return out;
8
- }
9
-
10
- public decode(buffer: Uint8Array): bool {
11
- if (buffer.length == 0) return false;
12
- return buffer[0] == 1;
13
- }
14
- }
15
-
16
- export const idOfBoolCodec = idof<_BooleanCodec>();
17
- export const BooleanCodec = new _BooleanCodec();
@@ -1,10 +0,0 @@
1
- import { i128, u128, u256 } from '@btc-vision/as-bignum/assembly';
2
- import { Uint8Array } from 'typedarray';
3
- import { Address } from '../../types/Address';
4
-
5
- export const idOfU256 = idof<u256>();
6
- export const idOfU128 = idof<u128>();
7
- export const idOfI128 = idof<i128>();
8
- export const idOfUint8Array = idof<Uint8Array>();
9
- export const idOfString = idof<string>();
10
- export const idOfAddress = idof<Address>();
@@ -1,58 +0,0 @@
1
- import { i128, u128, u256 } from '@btc-vision/as-bignum/assembly';
2
- import { ICodec } from '../interfaces/ICodec';
3
- import { BytesWriter } from '../../buffer/BytesWriter';
4
- import { BytesReader } from '../../buffer/BytesReader';
5
- import { idOfI128, idOfU128, idOfU256 } from './Ids';
6
-
7
- /**
8
- * A generic NumericCodec<T> that handles:
9
- * - `u256` from @btc-vision/as-bignum (big-endian)
10
- * - Any built-in integer type (i32, u32, i64, etc.) also stored big-endian
11
- */
12
- export class NumericCodec<T> implements ICodec<T> {
13
- public encode(value: T): Uint8Array {
14
- const id = idof<T>();
15
- switch (id) {
16
- case idOfU256: {
17
- // T is `u256`
18
- const val = changetype<u256>(value);
19
- return val.toUint8Array(true); // big-endian
20
- }
21
- case idOfU128: {
22
- // T is `u128`
23
- const val = changetype<u128>(value);
24
- return val.toUint8Array(true); // big-endian
25
- }
26
- case idOfI128: {
27
- // T is `i128`
28
- const val = changetype<i128>(value);
29
- return val.toUint8Array(true); // big-endian
30
- }
31
- default: {
32
- const writer = new BytesWriter(sizeof<T>());
33
- writer.write<T>(value);
34
-
35
- return writer.getBuffer();
36
- }
37
- }
38
- }
39
-
40
- public decode(buffer: Uint8Array): T {
41
- const id = idof<T>();
42
- switch (id) {
43
- case idOfU256:
44
- // T is `u256`
45
- return changetype<T>(u256.fromBytes(buffer, true));
46
- case idOfU128:
47
- // T is `u128`
48
- return changetype<T>(u128.fromBytes(buffer, true));
49
- case idOfI128:
50
- // T is `i128`
51
- return changetype<T>(i128.fromBytes(buffer, true));
52
- default: {
53
- const writer = new BytesReader(buffer);
54
- return writer.read<T>();
55
- }
56
- }
57
- }
58
- }
@@ -1,24 +0,0 @@
1
- import { ICodec } from '../interfaces/ICodec';
2
- import { VariableBytesCodec } from './VariableBytesCodec';
3
-
4
- class IStringCodec implements ICodec<string> {
5
- public encode(value: string): Uint8Array {
6
- // Convert string -> UTF8 bytes
7
- const utf8 = String.UTF8.encode(value, false);
8
-
9
- // Pass to variable-bytes
10
- return VariableBytesCodec.encode(Uint8Array.wrap(utf8));
11
- }
12
-
13
- public decode(buffer: Uint8Array): string {
14
- const raw = VariableBytesCodec.decode(buffer);
15
- if (raw.length == 0) {
16
- return '';
17
- }
18
-
19
- return String.UTF8.decode(raw.buffer, false);
20
- }
21
- }
22
-
23
- export const idOfStringCodec = idof<IStringCodec>();
24
- export const StringCodec = new IStringCodec();
@@ -1,20 +0,0 @@
1
- import { u256 } from '@btc-vision/as-bignum/assembly';
2
- import { ICodec } from '../interfaces/ICodec';
3
-
4
- class _U256Codec implements ICodec<u256> {
5
- public encode(value: u256): Uint8Array {
6
- // big-endian
7
- return value.toUint8Array(true);
8
- }
9
-
10
- public decode(buffer: Uint8Array): u256 {
11
- if (buffer.length == 0) {
12
- return u256.Zero;
13
- }
14
-
15
- return u256.fromUint8ArrayBE(buffer);
16
- }
17
- }
18
-
19
- export const idOfU256Codec = idof<_U256Codec>();
20
- export const U256Codec = new _U256Codec();