@alephium/web3 0.30.0-beta.1 → 0.30.0

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.
Files changed (87) hide show
  1. package/dist/alephium-web3.min.js +1 -1
  2. package/dist/alephium-web3.min.js.map +1 -1
  3. package/dist/src/api/api-alephium.d.ts +5 -1
  4. package/dist/src/api/api-alephium.js +1 -1
  5. package/dist/src/index.d.ts +0 -1
  6. package/dist/src/index.js +1 -2
  7. package/package.json +3 -4
  8. package/src/api/api-alephium.ts +5 -1
  9. package/src/index.ts +1 -1
  10. package/dist/src/codec/array-codec.d.ts +0 -17
  11. package/dist/src/codec/array-codec.js +0 -58
  12. package/dist/src/codec/asset-output-codec.d.ts +0 -27
  13. package/dist/src/codec/asset-output-codec.js +0 -135
  14. package/dist/src/codec/bigint-codec.d.ts +0 -5
  15. package/dist/src/codec/bigint-codec.js +0 -85
  16. package/dist/src/codec/bytestring-codec.d.ts +0 -16
  17. package/dist/src/codec/bytestring-codec.js +0 -49
  18. package/dist/src/codec/codec.d.ts +0 -8
  19. package/dist/src/codec/codec.js +0 -9
  20. package/dist/src/codec/compact-int-codec.d.ts +0 -47
  21. package/dist/src/codec/compact-int-codec.js +0 -287
  22. package/dist/src/codec/contract-codec.d.ts +0 -22
  23. package/dist/src/codec/contract-codec.js +0 -62
  24. package/dist/src/codec/contract-output-codec.d.ts +0 -21
  25. package/dist/src/codec/contract-output-codec.js +0 -81
  26. package/dist/src/codec/contract-output-ref-codec.d.ts +0 -15
  27. package/dist/src/codec/contract-output-ref-codec.js +0 -37
  28. package/dist/src/codec/either-codec.d.ts +0 -17
  29. package/dist/src/codec/either-codec.js +0 -66
  30. package/dist/src/codec/hash.d.ts +0 -4
  31. package/dist/src/codec/hash.js +0 -40
  32. package/dist/src/codec/index.d.ts +0 -23
  33. package/dist/src/codec/index.js +0 -69
  34. package/dist/src/codec/input-codec.d.ts +0 -22
  35. package/dist/src/codec/input-codec.js +0 -70
  36. package/dist/src/codec/instr-codec.d.ts +0 -223
  37. package/dist/src/codec/instr-codec.js +0 -459
  38. package/dist/src/codec/lockup-script-codec.d.ts +0 -28
  39. package/dist/src/codec/lockup-script-codec.js +0 -79
  40. package/dist/src/codec/long-codec.d.ts +0 -9
  41. package/dist/src/codec/long-codec.js +0 -55
  42. package/dist/src/codec/method-codec.d.ts +0 -30
  43. package/dist/src/codec/method-codec.js +0 -67
  44. package/dist/src/codec/option-codec.d.ts +0 -15
  45. package/dist/src/codec/option-codec.js +0 -54
  46. package/dist/src/codec/output-codec.d.ts +0 -7
  47. package/dist/src/codec/output-codec.js +0 -26
  48. package/dist/src/codec/script-codec.d.ts +0 -16
  49. package/dist/src/codec/script-codec.js +0 -40
  50. package/dist/src/codec/signature-codec.d.ts +0 -14
  51. package/dist/src/codec/signature-codec.js +0 -36
  52. package/dist/src/codec/signed-int-codec.d.ts +0 -9
  53. package/dist/src/codec/signed-int-codec.js +0 -38
  54. package/dist/src/codec/token-codec.d.ts +0 -16
  55. package/dist/src/codec/token-codec.js +0 -45
  56. package/dist/src/codec/transaction-codec.d.ts +0 -27
  57. package/dist/src/codec/transaction-codec.js +0 -127
  58. package/dist/src/codec/unlock-script-codec.d.ts +0 -40
  59. package/dist/src/codec/unlock-script-codec.js +0 -169
  60. package/dist/src/codec/unsigned-tx-codec.d.ts +0 -30
  61. package/dist/src/codec/unsigned-tx-codec.js +0 -102
  62. package/src/codec/array-codec.ts +0 -62
  63. package/src/codec/asset-output-codec.ts +0 -150
  64. package/src/codec/bigint-codec.ts +0 -91
  65. package/src/codec/bytestring-codec.ts +0 -55
  66. package/src/codec/codec.ts +0 -30
  67. package/src/codec/compact-int-codec.ts +0 -299
  68. package/src/codec/contract-codec.ts +0 -76
  69. package/src/codec/contract-output-codec.ts +0 -97
  70. package/src/codec/contract-output-ref-codec.ts +0 -41
  71. package/src/codec/either-codec.ts +0 -73
  72. package/src/codec/hash.ts +0 -34
  73. package/src/codec/index.ts +0 -41
  74. package/src/codec/input-codec.ts +0 -80
  75. package/src/codec/instr-codec.ts +0 -462
  76. package/src/codec/lockup-script-codec.ts +0 -98
  77. package/src/codec/long-codec.ts +0 -58
  78. package/src/codec/method-codec.ts +0 -85
  79. package/src/codec/option-codec.ts +0 -59
  80. package/src/codec/output-codec.ts +0 -26
  81. package/src/codec/script-codec.ts +0 -44
  82. package/src/codec/signature-codec.ts +0 -39
  83. package/src/codec/signed-int-codec.ts +0 -36
  84. package/src/codec/token-codec.ts +0 -49
  85. package/src/codec/transaction-codec.ts +0 -146
  86. package/src/codec/unlock-script-codec.ts +0 -193
  87. package/src/codec/unsigned-tx-codec.ts +0 -123
@@ -319,6 +319,8 @@ export interface CallContract {
319
319
  txId?: string;
320
320
  /** @format address */
321
321
  address: string;
322
+ /** @format address */
323
+ callerAddress?: string;
322
324
  /** @format int32 */
323
325
  methodIndex: number;
324
326
  args?: Val[];
@@ -741,6 +743,8 @@ export interface TestContract {
741
743
  txId?: string;
742
744
  /** @format address */
743
745
  address?: string;
746
+ /** @format address */
747
+ callerAddress?: string;
744
748
  /** @format contract */
745
749
  bytecode: string;
746
750
  initialImmFields?: Val[];
@@ -983,7 +987,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
983
987
  }
984
988
  /**
985
989
  * @title Alephium API
986
- * @version 2.8.3
990
+ * @version 2.8.4
987
991
  * @baseUrl ../
988
992
  */
989
993
  export declare class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
@@ -151,7 +151,7 @@ class HttpClient {
151
151
  exports.HttpClient = HttpClient;
152
152
  /**
153
153
  * @title Alephium API
154
- * @version 2.8.3
154
+ * @version 2.8.4
155
155
  * @baseUrl ../
156
156
  */
157
157
  class Api extends HttpClient {
@@ -6,6 +6,5 @@ export * from './transaction';
6
6
  export * from './token';
7
7
  export * from './constants';
8
8
  export * as web3 from './global';
9
- export * as codec from './codec';
10
9
  export * as utils from './utils';
11
10
  export * from './debug';
package/dist/src/index.js CHANGED
@@ -43,7 +43,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
43
43
  return result;
44
44
  };
45
45
  Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.utils = exports.codec = exports.web3 = void 0;
46
+ exports.utils = exports.web3 = void 0;
47
47
  BigInt.prototype['toJSON'] = function () {
48
48
  return this.toString();
49
49
  };
@@ -55,6 +55,5 @@ __exportStar(require("./transaction"), exports);
55
55
  __exportStar(require("./token"), exports);
56
56
  __exportStar(require("./constants"), exports);
57
57
  exports.web3 = __importStar(require("./global"));
58
- exports.codec = __importStar(require("./codec"));
59
58
  exports.utils = __importStar(require("./utils"));
60
59
  __exportStar(require("./debug"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.30.0-beta.1",
3
+ "version": "0.30.0",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "author": "Alephium dev <dev@alephium.org>",
29
29
  "config": {
30
- "alephium_version": "2.8.3",
30
+ "alephium_version": "2.8.4",
31
31
  "explorer_backend_version": "1.16.1"
32
32
  },
33
33
  "type": "commonjs",
@@ -43,8 +43,7 @@
43
43
  "elliptic": "6.5.4",
44
44
  "eventemitter3": "^4.0.7",
45
45
  "path-browserify": "^1.0.1",
46
- "stream-browserify": "^3.0.0",
47
- "binary-parser": "2.2.1"
46
+ "stream-browserify": "^3.0.0"
48
47
  },
49
48
  "devDependencies": {
50
49
  "@babel/eslint-parser": "^7.21.3",
@@ -361,6 +361,8 @@ export interface CallContract {
361
361
  txId?: string
362
362
  /** @format address */
363
363
  address: string
364
+ /** @format address */
365
+ callerAddress?: string
364
366
  /** @format int32 */
365
367
  methodIndex: number
366
368
  args?: Val[]
@@ -851,6 +853,8 @@ export interface TestContract {
851
853
  txId?: string
852
854
  /** @format address */
853
855
  address?: string
856
+ /** @format address */
857
+ callerAddress?: string
854
858
  /** @format contract */
855
859
  bytecode: string
856
860
  initialImmFields?: Val[]
@@ -1278,7 +1282,7 @@ export class HttpClient<SecurityDataType = unknown> {
1278
1282
 
1279
1283
  /**
1280
1284
  * @title Alephium API
1281
- * @version 2.8.3
1285
+ * @version 2.8.4
1282
1286
  * @baseUrl ../
1283
1287
  */
1284
1288
  export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
package/src/index.ts CHANGED
@@ -29,6 +29,6 @@ export * from './token'
29
29
 
30
30
  export * from './constants'
31
31
  export * as web3 from './global'
32
- export * as codec from './codec'
32
+
33
33
  export * as utils from './utils'
34
34
  export * from './debug'
@@ -1,17 +0,0 @@
1
- /// <reference types="node" />
2
- import { Parser } from 'binary-parser';
3
- import { DecodedCompactInt } from './compact-int-codec';
4
- import { Codec } from './codec';
5
- export interface DecodedArray<T> {
6
- length: DecodedCompactInt;
7
- value: T[];
8
- }
9
- export declare class ArrayCodec<T> implements Codec<T[]> {
10
- private childCodec;
11
- parser: Parser;
12
- constructor(childCodec: Codec<T>, parser?: Parser);
13
- encode(input: T[]): Buffer;
14
- decode(input: Buffer): T[];
15
- static arrayParser(parser: Parser): Parser;
16
- fromArray(inputs: T[]): DecodedArray<T>;
17
- }
@@ -1,58 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ArrayCodec = void 0;
4
- /*
5
- Copyright 2018 - 2022 The Alephium Authors
6
- This file is part of the alephium project.
7
-
8
- The library is free software: you can redistribute it and/or modify
9
- it under the terms of the GNU Lesser General Public License as published by
10
- the Free Software Foundation, either version 3 of the License, or
11
- (at your option) any later version.
12
-
13
- The library is distributed in the hope that it will be useful,
14
- but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- GNU Lesser General Public License for more details.
17
-
18
- You should have received a copy of the GNU Lesser General Public License
19
- along with the library. If not, see <http://www.gnu.org/licenses/>.
20
- */
21
- const binary_parser_1 = require("binary-parser");
22
- const compact_int_codec_1 = require("./compact-int-codec");
23
- class ArrayCodec {
24
- constructor(childCodec, parser = ArrayCodec.arrayParser(childCodec.parser)) {
25
- this.childCodec = childCodec;
26
- this.parser = parser;
27
- }
28
- encode(input) {
29
- const result = [...compact_int_codec_1.compactSignedIntCodec.encodeI256(BigInt(input.length))];
30
- for (const element of input) {
31
- result.push(...this.childCodec.encode(element));
32
- }
33
- return Buffer.from(result);
34
- }
35
- decode(input) {
36
- const result = this.parser.parse(input);
37
- return result.value.map((v) => this.childCodec.decode(v.value));
38
- }
39
- static arrayParser(parser) {
40
- return new binary_parser_1.Parser()
41
- .nest('length', {
42
- type: compact_int_codec_1.compactSignedIntCodec.parser
43
- })
44
- .array('value', {
45
- length: function (ctx) {
46
- return compact_int_codec_1.compactSignedIntCodec.toI32(this['length']);
47
- },
48
- type: parser
49
- });
50
- }
51
- fromArray(inputs) {
52
- return {
53
- length: compact_int_codec_1.compactUnsignedIntCodec.decode(compact_int_codec_1.compactUnsignedIntCodec.encodeU32(inputs.length)),
54
- value: inputs
55
- };
56
- }
57
- }
58
- exports.ArrayCodec = ArrayCodec;
@@ -1,27 +0,0 @@
1
- /// <reference types="node" />
2
- import { Parser } from 'binary-parser';
3
- import { ArrayCodec, DecodedArray } from './array-codec';
4
- import { DecodedCompactInt } from './compact-int-codec';
5
- import { ByteString } from './bytestring-codec';
6
- import { LockupScript } from './lockup-script-codec';
7
- import { FixedAssetOutput } from '../api/api-alephium';
8
- import { Codec } from './codec';
9
- import { Token } from './token-codec';
10
- export interface AssetOutput {
11
- amount: DecodedCompactInt;
12
- lockupScript: LockupScript;
13
- lockTime: Buffer;
14
- tokens: DecodedArray<Token>;
15
- additionalData: ByteString;
16
- }
17
- export declare class AssetOutputCodec implements Codec<AssetOutput> {
18
- parser: Parser;
19
- encode(input: AssetOutput): Buffer;
20
- decode(input: Buffer): AssetOutput;
21
- static toFixedAssetOutputs(txIdBytes: Uint8Array, outputs: AssetOutput[]): FixedAssetOutput[];
22
- static toFixedAssetOutput(txIdBytes: Uint8Array, output: AssetOutput, index: number): FixedAssetOutput;
23
- static fromFixedAssetOutputs(fixedOutputs: FixedAssetOutput[]): AssetOutput[];
24
- static fromFixedAssetOutput(fixedOutput: FixedAssetOutput): AssetOutput;
25
- }
26
- export declare const assetOutputCodec: AssetOutputCodec;
27
- export declare const assetOutputsCodec: ArrayCodec<AssetOutput>;
@@ -1,135 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assetOutputsCodec = exports.assetOutputCodec = exports.AssetOutputCodec = void 0;
4
- /*
5
- Copyright 2018 - 2022 The Alephium Authors
6
- This file is part of the alephium project.
7
-
8
- The library is free software: you can redistribute it and/or modify
9
- it under the terms of the GNU Lesser General Public License as published by
10
- the Free Software Foundation, either version 3 of the License, or
11
- (at your option) any later version.
12
-
13
- The library is distributed in the hope that it will be useful,
14
- but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- GNU Lesser General Public License for more details.
17
-
18
- You should have received a copy of the GNU Lesser General Public License
19
- along with the library. If not, see <http://www.gnu.org/licenses/>.
20
- */
21
- const binary_parser_1 = require("binary-parser");
22
- const array_codec_1 = require("./array-codec");
23
- const compact_int_codec_1 = require("./compact-int-codec");
24
- const signed_int_codec_1 = require("./signed-int-codec");
25
- const long_codec_1 = require("./long-codec");
26
- const bytestring_codec_1 = require("./bytestring-codec");
27
- const lockup_script_codec_1 = require("./lockup-script-codec");
28
- const hash_1 = require("./hash");
29
- const utils_1 = require("../utils");
30
- const token_codec_1 = require("./token-codec");
31
- class AssetOutputCodec {
32
- constructor() {
33
- this.parser = binary_parser_1.Parser.start()
34
- .nest('amount', {
35
- type: compact_int_codec_1.compactUnsignedIntCodec.parser
36
- })
37
- .nest('lockupScript', {
38
- type: lockup_script_codec_1.lockupScriptCodec.parser
39
- })
40
- .buffer('lockTime', {
41
- length: 8
42
- })
43
- .nest('tokens', {
44
- type: token_codec_1.tokensCodec.parser
45
- })
46
- .nest('additionalData', {
47
- type: bytestring_codec_1.byteStringCodec.parser
48
- });
49
- }
50
- encode(input) {
51
- const amount = Buffer.from(compact_int_codec_1.compactUnsignedIntCodec.encode(input.amount));
52
- const lockupScript = lockup_script_codec_1.lockupScriptCodec.encode(input.lockupScript);
53
- const lockTime = Buffer.from(input.lockTime);
54
- const tokens = Buffer.from(token_codec_1.tokensCodec.encode(input.tokens.value));
55
- const additionalData = Buffer.from(bytestring_codec_1.byteStringCodec.encode(input.additionalData));
56
- return Buffer.concat([amount, lockupScript, lockTime, tokens, additionalData]);
57
- }
58
- decode(input) {
59
- return this.parser.parse(input);
60
- }
61
- static toFixedAssetOutputs(txIdBytes, outputs) {
62
- return outputs.map((output, index) => AssetOutputCodec.toFixedAssetOutput(txIdBytes, output, index));
63
- }
64
- static toFixedAssetOutput(txIdBytes, output, index) {
65
- const attoAlphAmount = compact_int_codec_1.compactUnsignedIntCodec.toU256(output.amount).toString();
66
- const lockTime = Number(long_codec_1.longCodec.decode(output.lockTime));
67
- const tokens = output.tokens.value.map((token) => {
68
- return {
69
- id: token.tokenId.toString('hex'),
70
- amount: compact_int_codec_1.compactUnsignedIntCodec.toU256(token.amount).toString()
71
- };
72
- });
73
- const message = output.additionalData.value.toString('hex');
74
- const scriptType = output.lockupScript.scriptType;
75
- const key = (0, utils_1.binToHex)((0, hash_1.blakeHash)(Buffer.concat([txIdBytes, signed_int_codec_1.signedIntCodec.encode(index)])));
76
- const outputLockupScript = output.lockupScript.script;
77
- const address = utils_1.bs58.encode(lockup_script_codec_1.lockupScriptCodec.encode(output.lockupScript));
78
- let hint = undefined;
79
- if (scriptType === 0) {
80
- // P2PKH
81
- hint = (0, hash_1.createHint)(outputLockupScript.publicKeyHash);
82
- }
83
- else if (scriptType === 1) {
84
- // P2MPKH
85
- hint = (0, hash_1.createHint)(outputLockupScript.publicKeyHashes.value[0].publicKeyHash);
86
- }
87
- else if (scriptType === 2) {
88
- // P2SH
89
- hint = (0, hash_1.createHint)(outputLockupScript.scriptHash);
90
- }
91
- else if (scriptType === 3) {
92
- // P2C
93
- hint = (0, hash_1.createHint)(outputLockupScript.contractId);
94
- }
95
- else {
96
- throw new Error(`TODO: decode output script type: ${scriptType}`);
97
- }
98
- return { hint, key, attoAlphAmount, lockTime, tokens, address, message };
99
- }
100
- static fromFixedAssetOutputs(fixedOutputs) {
101
- return fixedOutputs.map((output) => {
102
- return AssetOutputCodec.fromFixedAssetOutput(output);
103
- });
104
- }
105
- static fromFixedAssetOutput(fixedOutput) {
106
- const amount = compact_int_codec_1.compactUnsignedIntCodec.decode(compact_int_codec_1.compactUnsignedIntCodec.encodeU256(BigInt(fixedOutput.attoAlphAmount)));
107
- const lockTime = long_codec_1.longCodec.encode(BigInt(fixedOutput.lockTime));
108
- const lockupScript = lockup_script_codec_1.lockupScriptCodec.decode(Buffer.from(utils_1.bs58.decode(fixedOutput.address)));
109
- const tokensValue = fixedOutput.tokens.map((token) => {
110
- return {
111
- tokenId: Buffer.from(token.id, 'hex'),
112
- amount: compact_int_codec_1.compactUnsignedIntCodec.decode(compact_int_codec_1.compactUnsignedIntCodec.encodeU256(BigInt(token.amount)))
113
- };
114
- });
115
- const tokens = {
116
- length: compact_int_codec_1.compactUnsignedIntCodec.decode(compact_int_codec_1.compactUnsignedIntCodec.encodeU32(tokensValue.length)),
117
- value: tokensValue
118
- };
119
- const additionalDataValue = Buffer.from(fixedOutput.message, 'hex');
120
- const additionalData = {
121
- length: compact_int_codec_1.compactUnsignedIntCodec.decode(compact_int_codec_1.compactUnsignedIntCodec.encodeU32(additionalDataValue.length)),
122
- value: additionalDataValue
123
- };
124
- return {
125
- amount,
126
- lockupScript,
127
- lockTime,
128
- tokens,
129
- additionalData
130
- };
131
- }
132
- }
133
- exports.AssetOutputCodec = AssetOutputCodec;
134
- exports.assetOutputCodec = new AssetOutputCodec();
135
- exports.assetOutputsCodec = new array_codec_1.ArrayCodec(exports.assetOutputCodec);
@@ -1,5 +0,0 @@
1
- /// <reference types="node" />
2
- export declare class BigIntCodec {
3
- static encode(value: bigint): Buffer;
4
- static decode(encoded: Buffer, signed: boolean): bigint;
5
- }
@@ -1,85 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BigIntCodec = void 0;
4
- /*
5
- Copyright 2018 - 2022 The Alephium Authors
6
- This file is part of the alephium project.
7
-
8
- The library is free software: you can redistribute it and/or modify
9
- it under the terms of the GNU Lesser General Public License as published by
10
- the Free Software Foundation, either version 3 of the License, or
11
- (at your option) any later version.
12
-
13
- The library is distributed in the hope that it will be useful,
14
- but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- GNU Lesser General Public License for more details.
17
-
18
- You should have received a copy of the GNU Lesser General Public License
19
- along with the library. If not, see <http://www.gnu.org/licenses/>.
20
- */
21
- class BigIntCodec {
22
- static encode(value) {
23
- // Special case for zero.
24
- if (value === 0n) {
25
- return Buffer.from([0]);
26
- }
27
- const isNegative = value < 0n;
28
- let absValue = isNegative ? -value : value;
29
- const bytes = [];
30
- // Extract bytes from absolute value.
31
- while (absValue > 0n) {
32
- bytes.push(Number(absValue & 0xffn));
33
- absValue >>= 8n;
34
- }
35
- // If the bigint is positive and the most significant byte has its high bit set,
36
- // prefix the byte array with a zero byte to signify positive value.
37
- if (!isNegative && (bytes[bytes.length - 1] & 0x80) !== 0) {
38
- bytes.push(0);
39
- }
40
- // If the bigint is negative, compute the two's complement of the byte array.
41
- if (isNegative) {
42
- let carry = true;
43
- for (let i = 0; i < bytes.length; i++) {
44
- bytes[i] = ~bytes[i] & 0xff; // Invert the bits of the byte.
45
- if (carry) {
46
- if (bytes[i] === 0xff) {
47
- bytes[i] = 0;
48
- }
49
- else {
50
- bytes[i] += 1;
51
- carry = false;
52
- }
53
- }
54
- }
55
- // If there's still a carry, and the most significant byte is not 0xFF (to store the negative sign bit),
56
- // or if no bytes were set (which means the value was -1), append a 0xFF byte to hold the carry.
57
- if (carry || bytes.length === 0 || (bytes[bytes.length - 1] & 0x80) === 0) {
58
- bytes.push(0xff);
59
- }
60
- }
61
- // The byte array needs to be reversed since we've constructed it in little-endian order.
62
- return Buffer.from(new Uint8Array(bytes.reverse()));
63
- }
64
- static decode(encoded, signed) {
65
- // Special case for zero.
66
- if (encoded.length === 1 && encoded[0] === 0) {
67
- return 0n;
68
- }
69
- // Determine if the number is negative by checking the most significant byte (MSB)
70
- const isNegative = signed ? encoded[0] === 0xff : signed;
71
- // Convert the byte array to a bigint
72
- let value = 0n;
73
- for (const byte of encoded) {
74
- value = (value << 8n) | BigInt(byte);
75
- }
76
- // If the number is negative, convert from two's complement
77
- if (isNegative) {
78
- // Create a mask for the value's bit length
79
- const mask = (1n << (8n * BigInt(encoded.length))) - 1n;
80
- value = -(~value & mask) - 1n;
81
- }
82
- return value;
83
- }
84
- }
85
- exports.BigIntCodec = BigIntCodec;
@@ -1,16 +0,0 @@
1
- /// <reference types="node" />
2
- import { Parser } from 'binary-parser';
3
- import { DecodedCompactInt } from './compact-int-codec';
4
- import { Codec } from './codec';
5
- export interface ByteString {
6
- length: DecodedCompactInt;
7
- value: Buffer;
8
- }
9
- export declare class ByteStringCodec implements Codec<ByteString> {
10
- parser: Parser;
11
- encode(input: ByteString): Buffer;
12
- decode(input: Buffer): ByteString;
13
- encodeBuffer(input: Buffer): Buffer;
14
- decodeBuffer(input: Buffer): Buffer;
15
- }
16
- export declare const byteStringCodec: ByteStringCodec;
@@ -1,49 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.byteStringCodec = exports.ByteStringCodec = void 0;
4
- /*
5
- Copyright 2018 - 2022 The Alephium Authors
6
- This file is part of the alephium project.
7
-
8
- The library is free software: you can redistribute it and/or modify
9
- it under the terms of the GNU Lesser General Public License as published by
10
- the Free Software Foundation, either version 3 of the License, or
11
- (at your option) any later version.
12
-
13
- The library is distributed in the hope that it will be useful,
14
- but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- GNU Lesser General Public License for more details.
17
-
18
- You should have received a copy of the GNU Lesser General Public License
19
- along with the library. If not, see <http://www.gnu.org/licenses/>.
20
- */
21
- const binary_parser_1 = require("binary-parser");
22
- const compact_int_codec_1 = require("./compact-int-codec");
23
- class ByteStringCodec {
24
- constructor() {
25
- this.parser = new binary_parser_1.Parser()
26
- .nest('length', {
27
- type: compact_int_codec_1.compactUnsignedIntCodec.parser
28
- })
29
- .buffer('value', {
30
- length: function (ctx) {
31
- return compact_int_codec_1.compactUnsignedIntCodec.toU32(this['length']);
32
- }
33
- });
34
- }
35
- encode(input) {
36
- return Buffer.from([...compact_int_codec_1.compactUnsignedIntCodec.encode(input.length), ...input.value]);
37
- }
38
- decode(input) {
39
- return this.parser.parse(input);
40
- }
41
- encodeBuffer(input) {
42
- return Buffer.from([...compact_int_codec_1.compactUnsignedIntCodec.encodeU32(input.length), ...input]);
43
- }
44
- decodeBuffer(input) {
45
- return this.decode(input).value;
46
- }
47
- }
48
- exports.ByteStringCodec = ByteStringCodec;
49
- exports.byteStringCodec = new ByteStringCodec();
@@ -1,8 +0,0 @@
1
- /// <reference types="node" />
2
- import { Parser } from 'binary-parser';
3
- export interface Codec<T> {
4
- parser: Parser;
5
- encode(input: T): Buffer;
6
- decode(input: Buffer): T;
7
- }
8
- export declare function assert(value: boolean, message: string): void;
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assert = void 0;
4
- function assert(value, message) {
5
- if (!value) {
6
- throw new Error(message);
7
- }
8
- }
9
- exports.assert = assert;
@@ -1,47 +0,0 @@
1
- /// <reference types="node" />
2
- import { Parser } from 'binary-parser';
3
- import { Codec } from './codec';
4
- export declare class CompactInt {
5
- static readonly oneBytePrefix = 0;
6
- static readonly oneByteNegPrefix = 192;
7
- static readonly twoBytePrefix = 64;
8
- static readonly twoByteNegPrefix = 128;
9
- static readonly fourBytePrefix = 128;
10
- static readonly fourByteNegPrefix = 64;
11
- static readonly multiBytePrefix = 192;
12
- }
13
- export interface DecodedCompactInt {
14
- mode: number;
15
- rest: Uint8Array;
16
- }
17
- export declare class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
18
- private oneByteBound;
19
- private twoByteBound;
20
- private fourByteBound;
21
- parser: Parser;
22
- encode(input: DecodedCompactInt): Buffer;
23
- encodeU32(value: number): Buffer;
24
- encodeU256(value: bigint): Buffer;
25
- decodeU32(input: Buffer): number;
26
- decodeU256(input: Buffer): bigint;
27
- decode(input: Buffer): DecodedCompactInt;
28
- toU32(value: DecodedCompactInt): number;
29
- toU256(value: DecodedCompactInt): bigint;
30
- }
31
- export declare const compactUnsignedIntCodec: CompactUnsignedIntCodec;
32
- export declare class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
33
- private signFlag;
34
- private oneByteBound;
35
- private twoByteBound;
36
- private fourByteBound;
37
- parser: Parser;
38
- encode(input: DecodedCompactInt): Buffer;
39
- decode(input: Buffer): DecodedCompactInt;
40
- decodeI32(input: Buffer): number;
41
- encodeI32(value: number): Buffer;
42
- encodeI256(value: bigint): Buffer;
43
- decodeI256(input: Buffer): bigint;
44
- toI32(value: DecodedCompactInt): number;
45
- toI256(value: DecodedCompactInt): bigint;
46
- }
47
- export declare const compactSignedIntCodec: CompactSignedIntCodec;