@alephium/web3 1.4.0 → 1.5.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.
Files changed (106) 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/address/address.js +5 -5
  4. package/dist/src/api/api-alephium.d.ts +1 -1
  5. package/dist/src/api/api-alephium.js +1 -1
  6. package/dist/src/codec/array-codec.d.ts +4 -12
  7. package/dist/src/codec/array-codec.js +15 -28
  8. package/dist/src/codec/asset-output-codec.d.ts +6 -11
  9. package/dist/src/codec/asset-output-codec.js +32 -71
  10. package/dist/src/codec/bigint-codec.d.ts +2 -1
  11. package/dist/src/codec/bigint-codec.js +14 -2
  12. package/dist/src/codec/bytestring-codec.d.ts +6 -11
  13. package/dist/src/codec/bytestring-codec.js +9 -23
  14. package/dist/src/codec/codec.d.ts +54 -5
  15. package/dist/src/codec/codec.js +112 -14
  16. package/dist/src/codec/compact-int-codec.d.ts +65 -44
  17. package/dist/src/codec/compact-int-codec.js +222 -204
  18. package/dist/src/codec/contract-codec.d.ts +5 -8
  19. package/dist/src/codec/contract-codec.js +15 -29
  20. package/dist/src/codec/contract-output-codec.d.ts +4 -10
  21. package/dist/src/codec/contract-output-codec.js +20 -40
  22. package/dist/src/codec/contract-output-ref-codec.d.ts +2 -8
  23. package/dist/src/codec/contract-output-ref-codec.js +7 -17
  24. package/dist/src/codec/either-codec.d.ts +8 -15
  25. package/dist/src/codec/either-codec.js +5 -46
  26. package/dist/src/codec/index.d.ts +4 -3
  27. package/dist/src/codec/index.js +7 -4
  28. package/dist/src/codec/input-codec.d.ts +4 -10
  29. package/dist/src/codec/input-codec.js +11 -46
  30. package/dist/src/codec/instr-codec.d.ts +633 -40
  31. package/dist/src/codec/instr-codec.js +1040 -434
  32. package/dist/src/codec/int-as-4bytes-codec.d.ts +7 -0
  33. package/dist/src/codec/{signed-int-codec.js → int-as-4bytes-codec.js} +6 -12
  34. package/dist/src/codec/lockup-script-codec.d.ts +23 -26
  35. package/dist/src/codec/lockup-script-codec.js +12 -58
  36. package/dist/src/codec/method-codec.d.ts +6 -18
  37. package/dist/src/codec/method-codec.js +20 -48
  38. package/dist/src/codec/option-codec.d.ts +8 -13
  39. package/dist/src/codec/option-codec.js +14 -32
  40. package/dist/src/codec/output-codec.d.ts +2 -2
  41. package/dist/src/codec/output-codec.js +1 -1
  42. package/dist/src/codec/reader.d.ts +8 -0
  43. package/dist/src/codec/reader.js +48 -0
  44. package/dist/src/codec/script-codec.d.ts +6 -14
  45. package/dist/src/codec/script-codec.js +6 -22
  46. package/dist/src/codec/signature-codec.d.ts +4 -12
  47. package/dist/src/codec/signature-codec.js +3 -15
  48. package/dist/src/codec/timestamp-codec.d.ts +8 -0
  49. package/dist/src/codec/timestamp-codec.js +39 -0
  50. package/dist/src/codec/token-codec.d.ts +3 -10
  51. package/dist/src/codec/token-codec.js +6 -24
  52. package/dist/src/codec/transaction-codec.d.ts +6 -11
  53. package/dist/src/codec/transaction-codec.js +24 -49
  54. package/dist/src/codec/unlock-script-codec.d.ts +25 -36
  55. package/dist/src/codec/unlock-script-codec.js +26 -147
  56. package/dist/src/codec/unsigned-tx-codec.d.ts +8 -14
  57. package/dist/src/codec/unsigned-tx-codec.js +24 -66
  58. package/dist/src/codec/val.d.ts +27 -0
  59. package/dist/src/codec/val.js +33 -0
  60. package/dist/src/contract/contract.js +6 -6
  61. package/dist/src/contract/index.d.ts +1 -0
  62. package/dist/src/contract/index.js +1 -0
  63. package/dist/src/contract/ralph.d.ts +0 -4
  64. package/dist/src/contract/ralph.js +50 -179
  65. package/dist/src/contract/script-simulator.d.ts +27 -0
  66. package/dist/src/contract/script-simulator.js +757 -0
  67. package/dist/src/exchange/exchange.js +1 -1
  68. package/package.json +3 -4
  69. package/src/address/address.ts +8 -8
  70. package/src/api/api-alephium.ts +1 -1
  71. package/src/codec/array-codec.ts +16 -34
  72. package/src/codec/asset-output-codec.ts +38 -83
  73. package/src/codec/bigint-codec.ts +16 -2
  74. package/src/codec/bytestring-codec.ts +10 -28
  75. package/src/codec/codec.ts +121 -15
  76. package/src/codec/compact-int-codec.ts +230 -207
  77. package/src/codec/contract-codec.ts +20 -33
  78. package/src/codec/contract-output-codec.ts +22 -48
  79. package/src/codec/contract-output-ref-codec.ts +6 -17
  80. package/src/codec/either-codec.ts +4 -53
  81. package/src/codec/index.ts +4 -3
  82. package/src/codec/input-codec.ts +14 -36
  83. package/src/codec/instr-codec.ts +1229 -455
  84. package/src/codec/{signed-int-codec.ts → int-as-4bytes-codec.ts} +6 -10
  85. package/src/codec/lockup-script-codec.ts +28 -76
  86. package/src/codec/method-codec.ts +23 -61
  87. package/src/codec/option-codec.ts +13 -36
  88. package/src/codec/output-codec.ts +2 -2
  89. package/src/codec/reader.ts +56 -0
  90. package/src/codec/script-codec.ts +9 -31
  91. package/src/codec/signature-codec.ts +3 -18
  92. package/src/codec/timestamp-codec.ts +42 -0
  93. package/src/codec/token-codec.ts +7 -26
  94. package/src/codec/transaction-codec.ts +29 -58
  95. package/src/codec/unlock-script-codec.ts +44 -171
  96. package/src/codec/unsigned-tx-codec.ts +34 -63
  97. package/src/codec/val.ts +40 -0
  98. package/src/contract/contract.ts +9 -13
  99. package/src/contract/index.ts +1 -0
  100. package/src/contract/ralph.ts +76 -172
  101. package/src/contract/script-simulator.ts +838 -0
  102. package/src/exchange/exchange.ts +1 -1
  103. package/dist/src/codec/long-codec.d.ts +0 -8
  104. package/dist/src/codec/long-codec.js +0 -55
  105. package/dist/src/codec/signed-int-codec.d.ts +0 -8
  106. package/src/codec/long-codec.ts +0 -58
@@ -1,47 +1,68 @@
1
- import { Parser } from 'binary-parser';
2
1
  import { Codec } from './codec';
3
- export declare class CompactInt {
4
- static readonly oneBytePrefix = 0;
5
- static readonly oneByteNegPrefix = 192;
6
- static readonly twoBytePrefix = 64;
7
- static readonly twoByteNegPrefix = 128;
8
- static readonly fourBytePrefix = 128;
9
- static readonly fourByteNegPrefix = 64;
10
- static readonly multiBytePrefix = 192;
2
+ import { Reader } from './reader';
3
+ type FixedWidthMode = {
4
+ type: 'SingleByte' | 'TwoByte' | 'FourByte';
5
+ prefix: number;
6
+ negPrefix: number;
7
+ };
8
+ type MultiByteMode = {
9
+ type: 'MultiByte';
10
+ prefix: number;
11
+ };
12
+ type Mode = FixedWidthMode | MultiByteMode;
13
+ export declare class UnSigned {
14
+ static readonly oneByteBound: bigint;
15
+ static readonly twoByteBound: bigint;
16
+ static readonly fourByteBound: bigint;
17
+ static readonly u256UpperBound: bigint;
18
+ static readonly u32UpperBound: number;
19
+ static encodeU32(value: number): Uint8Array;
20
+ static encodeU256(value: bigint): Uint8Array;
21
+ static decodeInt(mode: FixedWidthMode, body: Uint8Array): number;
22
+ static decodeU32(mode: Mode, body: Uint8Array): number;
23
+ static decodeU256(mode: Mode, body: Uint8Array): bigint;
11
24
  }
12
- export interface DecodedCompactInt {
13
- mode: number;
14
- rest: Uint8Array;
25
+ export declare const u256Codec: {
26
+ encode(input: bigint): Uint8Array;
27
+ _decode(input: Reader): bigint;
28
+ decode(input: Uint8Array): bigint;
29
+ bimap<R>(from: (v: bigint) => R, to: (v: R) => bigint): Codec<R>;
30
+ };
31
+ export declare const u32Codec: {
32
+ encode(input: number): Uint8Array;
33
+ _decode(input: Reader): number;
34
+ decode(input: Uint8Array): number;
35
+ bimap<R>(from: (v: number) => R, to: (v: R) => number): Codec<R>;
36
+ };
37
+ export declare class Signed {
38
+ static readonly signFlag = 32;
39
+ static readonly oneByteBound: bigint;
40
+ static readonly twoByteBound: bigint;
41
+ static readonly fourByteBound: bigint;
42
+ static readonly i256UpperBound: bigint;
43
+ static readonly i256LowerBound: bigint;
44
+ static readonly i32UpperBound: number;
45
+ static readonly i32LowerBound: number;
46
+ static encodeI32(value: number): Uint8Array;
47
+ static encodePositiveI32(value: number): Uint8Array;
48
+ static encodeNegativeI32(value: number): Uint8Array;
49
+ static encodeI256(value: bigint): Uint8Array;
50
+ static decodeInt(mode: FixedWidthMode, body: Uint8Array): number;
51
+ static decodePositiveInt(mode: FixedWidthMode, body: Uint8Array): number;
52
+ static decodeNegativeInt(mode: FixedWidthMode, body: Uint8Array): number;
53
+ static decodeI32(mode: Mode, body: Uint8Array): number;
54
+ static decodeI256(mode: Mode, body: Uint8Array): bigint;
15
55
  }
16
- export declare class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
17
- private oneByteBound;
18
- private twoByteBound;
19
- private fourByteBound;
20
- parser: Parser;
21
- encode(input: DecodedCompactInt): Uint8Array;
22
- encodeU32(value: number): Uint8Array;
23
- encodeU256(value: bigint): Uint8Array;
24
- decodeU256(input: Uint8Array): bigint;
25
- decode(input: Uint8Array): DecodedCompactInt;
26
- toU256(value: DecodedCompactInt): bigint;
27
- fromU256(value: bigint): DecodedCompactInt;
28
- }
29
- export declare const compactUnsignedIntCodec: CompactUnsignedIntCodec;
30
- export declare class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
31
- private signFlag;
32
- private oneByteBound;
33
- private twoByteBound;
34
- private fourByteBound;
35
- parser: Parser;
36
- encode(input: DecodedCompactInt): Uint8Array;
37
- decode(input: Uint8Array): DecodedCompactInt;
38
- decodeI32(input: Uint8Array): number;
39
- encodeI32(value: number): Uint8Array;
40
- encodeI256(value: bigint): Uint8Array;
41
- decodeI256(input: Uint8Array): bigint;
42
- toI32(value: DecodedCompactInt): number;
43
- fromI32(value: number): DecodedCompactInt;
44
- toI256(value: DecodedCompactInt): bigint;
45
- fromI256(value: bigint): DecodedCompactInt;
46
- }
47
- export declare const compactSignedIntCodec: CompactSignedIntCodec;
56
+ export declare const i256Codec: {
57
+ encode(input: bigint): Uint8Array;
58
+ _decode(input: Reader): bigint;
59
+ decode(input: Uint8Array): bigint;
60
+ bimap<R>(from: (v: bigint) => R, to: (v: R) => bigint): Codec<R>;
61
+ };
62
+ export declare const i32Codec: {
63
+ encode(input: number): Uint8Array;
64
+ _decode(input: Reader): number;
65
+ decode(input: Uint8Array): number;
66
+ bimap<R>(from: (v: number) => R, to: (v: R) => number): Codec<R>;
67
+ };
68
+ export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compactSignedIntCodec = exports.CompactSignedIntCodec = exports.compactUnsignedIntCodec = exports.CompactUnsignedIntCodec = exports.CompactInt = void 0;
3
+ exports.i32Codec = exports.i256Codec = exports.Signed = exports.u32Codec = exports.u256Codec = exports.UnSigned = void 0;
4
4
  /*
5
5
  Copyright 2018 - 2022 The Alephium Authors
6
6
  This file is part of the alephium project.
@@ -18,60 +18,42 @@ GNU Lesser General Public License for more details.
18
18
  You should have received a copy of the GNU Lesser General Public License
19
19
  along with the library. If not, see <http://www.gnu.org/licenses/>.
20
20
  */
21
- const binary_parser_1 = require("binary-parser");
22
21
  const codec_1 = require("./codec");
23
22
  const bigint_codec_1 = require("./bigint-codec");
24
- const utils_1 = require("../utils");
25
- class CompactInt {
26
- }
27
- exports.CompactInt = CompactInt;
28
- CompactInt.oneBytePrefix = 0x00;
29
- CompactInt.oneByteNegPrefix = 0xc0;
30
- CompactInt.twoBytePrefix = 0x40;
31
- CompactInt.twoByteNegPrefix = 0x80;
32
- CompactInt.fourBytePrefix = 0x80;
33
- CompactInt.fourByteNegPrefix = 0x40;
34
- CompactInt.multiBytePrefix = 0xc0;
35
23
  const maskRest = 0xc0;
36
24
  const maskMode = 0x3f;
37
25
  const maskModeNeg = 0xffffffc0;
38
- const signFlag = 0x20; // 0b00100000
39
- const compactIntParser = new binary_parser_1.Parser().uint8('mode').buffer('rest', {
40
- length: function (ctx) {
41
- const rawMode = this['mode'];
42
- const mode = rawMode & maskRest;
43
- switch (mode) {
44
- case CompactInt.oneBytePrefix:
45
- return 0;
46
- case CompactInt.twoBytePrefix:
47
- return 1;
48
- case CompactInt.fourBytePrefix:
49
- return 3;
50
- default:
51
- return (rawMode & maskMode) + 4;
26
+ const SingleByteMode = { type: 'SingleByte', prefix: 0x00, negPrefix: 0xc0 };
27
+ const TwoByteMode = { type: 'TwoByte', prefix: 0x40, negPrefix: 0x80 };
28
+ const FourByteMode = { type: 'FourByte', prefix: 0x80, negPrefix: 0x40 };
29
+ const MultiByte = { type: 'MultiByte', prefix: 0xc0 };
30
+ function decodeMode(input) {
31
+ const byte = input.consumeByte();
32
+ switch (byte & maskRest) {
33
+ case SingleByteMode.prefix:
34
+ return { mode: SingleByteMode, body: new Uint8Array([byte]) };
35
+ case TwoByteMode.prefix:
36
+ return { mode: TwoByteMode, body: new Uint8Array([byte, ...input.consumeBytes(1)]) };
37
+ case FourByteMode.prefix:
38
+ return { mode: FourByteMode, body: new Uint8Array([byte, ...input.consumeBytes(3)]) };
39
+ default: {
40
+ const length = (byte & maskMode) + 4;
41
+ return { mode: MultiByte, body: new Uint8Array([byte, ...input.consumeBytes(length)]) };
52
42
  }
53
43
  }
54
- });
55
- class CompactUnsignedIntCodec {
56
- constructor() {
57
- this.oneByteBound = 0x40;
58
- this.twoByteBound = this.oneByteBound << 8;
59
- this.fourByteBound = this.oneByteBound << (8 * 3);
60
- this.parser = compactIntParser;
61
- }
62
- encode(input) {
63
- return new Uint8Array([input.mode, ...input.rest]);
64
- }
65
- encodeU32(value) {
66
- if (value < this.oneByteBound) {
67
- return new Uint8Array([(CompactInt.oneBytePrefix + value) & 0xff]);
44
+ }
45
+ class UnSigned {
46
+ static encodeU32(value) {
47
+ (0, codec_1.assert)(value >= 0 && value < UnSigned.u32UpperBound, `Invalid u32 value: ${value}`);
48
+ if (value < UnSigned.oneByteBound) {
49
+ return new Uint8Array([(SingleByteMode.prefix + value) & 0xff]);
68
50
  }
69
- else if (value < this.twoByteBound) {
70
- return new Uint8Array([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff]);
51
+ else if (value < UnSigned.twoByteBound) {
52
+ return new Uint8Array([(TwoByteMode.prefix + (value >> 8)) & 0xff, value & 0xff]);
71
53
  }
72
- else if (value < this.fourByteBound) {
54
+ else if (value < UnSigned.fourByteBound) {
73
55
  return new Uint8Array([
74
- (CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
56
+ (FourByteMode.prefix + (value >> 24)) & 0xff,
75
57
  (value >> 16) & 0xff,
76
58
  (value >> 8) & 0xff,
77
59
  value & 0xff
@@ -79,7 +61,7 @@ class CompactUnsignedIntCodec {
79
61
  }
80
62
  else {
81
63
  return new Uint8Array([
82
- CompactInt.multiBytePrefix,
64
+ MultiByte.prefix,
83
65
  (value >> 24) & 0xff,
84
66
  (value >> 16) & 0xff,
85
67
  (value >> 8) & 0xff,
@@ -87,204 +69,240 @@ class CompactUnsignedIntCodec {
87
69
  ]);
88
70
  }
89
71
  }
90
- encodeU256(value) {
91
- (0, codec_1.assert)(value >= 0n, 'Value should be positive');
92
- if (value < this.fourByteBound) {
93
- return this.encodeU32(Number(value));
72
+ static encodeU256(value) {
73
+ (0, codec_1.assert)(value >= 0n && value < UnSigned.u256UpperBound, `Invalid u256 value: ${value}`);
74
+ if (value < UnSigned.fourByteBound) {
75
+ return UnSigned.encodeU32(Number(value));
94
76
  }
95
77
  else {
96
78
  let bytes = bigint_codec_1.BigIntCodec.encode(value);
97
79
  if (bytes[0] === 0) {
98
80
  bytes = bytes.slice(1);
99
81
  }
100
- (0, codec_1.assert)(bytes.length <= 32, 'Expect <= 32 bytes for U256');
101
- const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff;
82
+ const header = (bytes.length - 4 + MultiByte.prefix) & 0xff;
102
83
  return new Uint8Array([header, ...bytes]);
103
84
  }
104
85
  }
105
- decodeU256(input) {
106
- const decoded = this.decode(input);
107
- return this.toU256(decoded);
108
- }
109
- decode(input) {
110
- return this.parser.parse(input);
111
- }
112
- toU256(value) {
113
- const mode = value.mode & maskRest;
114
- if (fixedSize(mode)) {
115
- const body = new Uint8Array([value.mode, ...value.rest]);
116
- return BigInt(decodePositiveInt(value.mode, body));
86
+ static decodeInt(mode, body) {
87
+ switch (mode.type) {
88
+ case 'SingleByte':
89
+ (0, codec_1.assert)(body.length === 1, 'Length should be 2');
90
+ return body[0];
91
+ case 'TwoByte':
92
+ (0, codec_1.assert)(body.length === 2, 'Length should be 2');
93
+ return ((body[0] & maskMode) << 8) | (body[1] & 0xff);
94
+ case 'FourByte':
95
+ (0, codec_1.assert)(body.length === 4, 'Length should be 4');
96
+ return ((((body[0] & maskMode) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff)) >>> 0);
117
97
  }
118
- else {
119
- (0, codec_1.assert)(value.rest.length <= 32, 'Expect <= 32 bytes for U256');
120
- return bigint_codec_1.BigIntCodec.decode(value.rest, false);
98
+ }
99
+ static decodeU32(mode, body) {
100
+ switch (mode.type) {
101
+ case 'SingleByte':
102
+ case 'TwoByte':
103
+ case 'FourByte':
104
+ return UnSigned.decodeInt(mode, body);
105
+ case 'MultiByte':
106
+ (0, codec_1.assert)(body.length >= 5, 'Length should be greater than 5');
107
+ if (body.length === 5) {
108
+ return ((body[1] << 24) | ((body[2] & 0xff) << 16) | ((body[3] & 0xff) << 8) | (body[4] & 0xff)) >>> 0;
109
+ }
110
+ else {
111
+ throw new Error(`Expect 4 bytes int, but get ${body.length - 1} bytes int`);
112
+ }
121
113
  }
122
114
  }
123
- fromU256(value) {
124
- return this.decode(this.encodeU256(value));
115
+ static decodeU256(mode, body) {
116
+ switch (mode.type) {
117
+ case 'SingleByte':
118
+ case 'TwoByte':
119
+ case 'FourByte':
120
+ return BigInt(UnSigned.decodeInt(mode, body));
121
+ case 'MultiByte':
122
+ return bigint_codec_1.BigIntCodec.decodeUnsigned(body.slice(1, body.length));
123
+ }
125
124
  }
126
125
  }
127
- exports.CompactUnsignedIntCodec = CompactUnsignedIntCodec;
128
- exports.compactUnsignedIntCodec = new CompactUnsignedIntCodec();
129
- class CompactSignedIntCodec {
130
- constructor() {
131
- this.signFlag = 0x20; // 0b00100000
132
- this.oneByteBound = 0x20; // 0b00100000
133
- this.twoByteBound = this.oneByteBound << 8;
134
- this.fourByteBound = this.oneByteBound << (8 * 3);
135
- this.parser = compactIntParser;
136
- }
126
+ exports.UnSigned = UnSigned;
127
+ UnSigned.oneByteBound = BigInt(0x40);
128
+ UnSigned.twoByteBound = UnSigned.oneByteBound << BigInt(8);
129
+ UnSigned.fourByteBound = UnSigned.oneByteBound << BigInt(8 * 3);
130
+ UnSigned.u256UpperBound = BigInt(1) << BigInt(256);
131
+ UnSigned.u32UpperBound = 2 ** 32;
132
+ exports.u256Codec = new (class extends codec_1.Codec {
137
133
  encode(input) {
138
- return new Uint8Array([input.mode, ...input.rest]);
134
+ return UnSigned.encodeU256(input);
135
+ }
136
+ _decode(input) {
137
+ const { mode, body } = decodeMode(input);
138
+ return UnSigned.decodeU256(mode, body);
139
139
  }
140
- decode(input) {
141
- return this.parser.parse(input);
140
+ })();
141
+ exports.u32Codec = new (class extends codec_1.Codec {
142
+ encode(input) {
143
+ return UnSigned.encodeU32(input);
142
144
  }
143
- decodeI32(input) {
144
- const decoded = this.decode(input);
145
- return this.toI32(decoded);
145
+ _decode(input) {
146
+ const { mode, body } = decodeMode(input);
147
+ return UnSigned.decodeU32(mode, body);
146
148
  }
147
- encodeI32(value) {
149
+ })();
150
+ class Signed {
151
+ static encodeI32(value) {
152
+ (0, codec_1.assert)(value >= Signed.i32LowerBound && value < Signed.i32UpperBound, `Invalid i32 value: ${value}`);
148
153
  if (value >= 0) {
149
- if (value < this.oneByteBound) {
150
- return new Uint8Array([(CompactInt.oneBytePrefix + value) & 0xff]);
151
- }
152
- else if (value < this.twoByteBound) {
153
- return new Uint8Array([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff]);
154
- }
155
- else if (value < this.fourByteBound) {
156
- return new Uint8Array([
157
- (CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
158
- (value >> 16) & 0xff,
159
- (value >> 8) & 0xff,
160
- value & 0xff
161
- ]);
162
- }
163
- else {
164
- return new Uint8Array([
165
- CompactInt.multiBytePrefix,
166
- (value >> 24) & 0xff,
167
- (value >> 16) & 0xff,
168
- (value >> 8) & 0xff,
169
- value & 0xff
170
- ]);
171
- }
154
+ return Signed.encodePositiveI32(value);
172
155
  }
173
156
  else {
174
- if (value >= -this.oneByteBound) {
175
- return new Uint8Array([(value ^ CompactInt.oneByteNegPrefix) & 0xff]);
176
- }
177
- else if (value >= -this.twoByteBound) {
178
- return new Uint8Array([((value >> 8) ^ CompactInt.twoByteNegPrefix) & 0xff, value & 0xff]);
179
- }
180
- else if (value >= -this.fourByteBound) {
181
- return new Uint8Array([
182
- ((value >> 24) ^ CompactInt.fourByteNegPrefix) & 0xff,
183
- (value >> 16) & 0xff,
184
- (value >> 8) & 0xff,
185
- value & 0xff
186
- ]);
187
- }
188
- else {
189
- return new Uint8Array([
190
- CompactInt.multiBytePrefix,
191
- (value >> 24) & 0xff,
192
- (value >> 16) & 0xff,
193
- (value >> 8) & 0xff,
194
- value & 0xff
195
- ]);
196
- }
157
+ return Signed.encodeNegativeI32(value);
197
158
  }
198
159
  }
199
- encodeI256(value) {
160
+ static encodePositiveI32(value) {
161
+ if (value < this.oneByteBound) {
162
+ return new Uint8Array([(SingleByteMode.prefix + value) & 0xff]);
163
+ }
164
+ else if (value < this.twoByteBound) {
165
+ return new Uint8Array([(TwoByteMode.prefix + (value >> 8)) & 0xff, value & 0xff]);
166
+ }
167
+ else if (value < this.fourByteBound) {
168
+ return new Uint8Array([
169
+ (FourByteMode.prefix + (value >> 24)) & 0xff,
170
+ (value >> 16) & 0xff,
171
+ (value >> 8) & 0xff,
172
+ value & 0xff
173
+ ]);
174
+ }
175
+ else {
176
+ return new Uint8Array([
177
+ MultiByte.prefix,
178
+ (value >> 24) & 0xff,
179
+ (value >> 16) & 0xff,
180
+ (value >> 8) & 0xff,
181
+ value & 0xff
182
+ ]);
183
+ }
184
+ }
185
+ static encodeNegativeI32(value) {
186
+ if (value >= -this.oneByteBound) {
187
+ return new Uint8Array([(value ^ SingleByteMode.negPrefix) & 0xff]);
188
+ }
189
+ else if (value >= -this.twoByteBound) {
190
+ return new Uint8Array([((value >> 8) ^ TwoByteMode.negPrefix) & 0xff, value & 0xff]);
191
+ }
192
+ else if (value >= -this.fourByteBound) {
193
+ return new Uint8Array([
194
+ ((value >> 24) ^ FourByteMode.negPrefix) & 0xff,
195
+ (value >> 16) & 0xff,
196
+ (value >> 8) & 0xff,
197
+ value & 0xff
198
+ ]);
199
+ }
200
+ else {
201
+ return new Uint8Array([
202
+ MultiByte.prefix,
203
+ (value >> 24) & 0xff,
204
+ (value >> 16) & 0xff,
205
+ (value >> 8) & 0xff,
206
+ value & 0xff
207
+ ]);
208
+ }
209
+ }
210
+ static encodeI256(value) {
211
+ (0, codec_1.assert)(value >= Signed.i256LowerBound && value < Signed.i256UpperBound, `Invalid i256 value: ${value}`);
200
212
  if (value >= -0x20000000 && value < 0x20000000) {
201
213
  return this.encodeI32(Number(value));
202
214
  }
203
215
  else {
204
216
  const bytes = bigint_codec_1.BigIntCodec.encode(value);
205
- const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff;
217
+ const header = (bytes.length - 4 + MultiByte.prefix) & 0xff;
206
218
  return new Uint8Array([header, ...bytes]);
207
219
  }
208
220
  }
209
- decodeI256(input) {
210
- const decoded = this.decode(input);
211
- return this.toI256(decoded);
212
- }
213
- toI32(value) {
214
- const body = new Uint8Array([value.mode, ...value.rest]);
215
- const mode = value.mode & maskRest;
216
- if (fixedSize(mode)) {
217
- const isPositive = (value.mode & signFlag) == 0;
218
- if (isPositive) {
219
- return decodePositiveInt(value.mode, body);
220
- }
221
- else {
222
- return decodeNegativeInt(value.mode, body);
223
- }
221
+ static decodeInt(mode, body) {
222
+ const isPositive = (body[0] & Signed.signFlag) === 0;
223
+ if (isPositive) {
224
+ return Signed.decodePositiveInt(mode, body);
224
225
  }
225
226
  else {
226
- if (body.length === 5) {
227
- return ((body[1] & 0xff) << 24) | ((body[2] & 0xff) << 16) | ((body[3] & 0xff) << 8) | (body[4] & 0xff);
228
- }
229
- else {
230
- throw new Error(`Expect 4 bytes int, but get ${body.length - 1} bytes int`);
231
- }
227
+ return Signed.decodeNegativeInt(mode, body);
232
228
  }
233
229
  }
234
- fromI32(value) {
235
- return this.decode(this.encodeI32(value));
230
+ static decodePositiveInt(mode, body) {
231
+ switch (mode.type) {
232
+ case 'SingleByte':
233
+ return body[0];
234
+ case 'TwoByte':
235
+ (0, codec_1.assert)(body.length === 2, 'Length should be 2');
236
+ return ((body[0] & maskMode) << 8) | (body[1] & 0xff);
237
+ case 'FourByte':
238
+ (0, codec_1.assert)(body.length === 4, 'Length should be 4');
239
+ return ((body[0] & maskMode) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff);
240
+ }
236
241
  }
237
- toI256(value) {
238
- const mode = value.mode & maskRest;
239
- if (fixedSize(mode)) {
240
- return BigInt(this.toI32(value));
242
+ static decodeNegativeInt(mode, body) {
243
+ switch (mode.type) {
244
+ case 'SingleByte':
245
+ return body[0] | maskModeNeg;
246
+ case 'TwoByte':
247
+ (0, codec_1.assert)(body.length === 2, 'Length should be 2');
248
+ return ((body[0] | maskModeNeg) << 8) | (body[1] & 0xff);
249
+ case 'FourByte':
250
+ (0, codec_1.assert)(body.length === 4, 'Length should be 4');
251
+ return ((body[0] | maskModeNeg) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff);
241
252
  }
242
- else {
243
- (0, codec_1.assert)(value.rest.length <= 32, 'Expect <= 32 bytes for I256');
244
- return bigint_codec_1.BigIntCodec.decode(value.rest, true);
253
+ }
254
+ static decodeI32(mode, body) {
255
+ switch (mode.type) {
256
+ case 'SingleByte':
257
+ case 'TwoByte':
258
+ case 'FourByte':
259
+ return Signed.decodeInt(mode, body);
260
+ case 'MultiByte':
261
+ if (body.length === 5) {
262
+ return (body[1] << 24) | ((body[2] & 0xff) << 16) | ((body[3] & 0xff) << 8) | (body[4] & 0xff);
263
+ }
264
+ else {
265
+ throw new Error(`Expect 4 bytes int, but get ${body.length - 1} bytes int`);
266
+ }
245
267
  }
246
268
  }
247
- fromI256(value) {
248
- return this.decode(this.encodeI256(value));
269
+ static decodeI256(mode, body) {
270
+ switch (mode.type) {
271
+ case 'SingleByte':
272
+ case 'TwoByte':
273
+ case 'FourByte':
274
+ return BigInt(Signed.decodeInt(mode, body));
275
+ case 'MultiByte':
276
+ const bytes = body.slice(1, body.length);
277
+ (0, codec_1.assert)(bytes.length <= 32, 'Expect <= 32 bytes for I256');
278
+ return bigint_codec_1.BigIntCodec.decodeSigned(bytes);
279
+ }
249
280
  }
250
281
  }
251
- exports.CompactSignedIntCodec = CompactSignedIntCodec;
252
- exports.compactSignedIntCodec = new CompactSignedIntCodec();
253
- function decodePositiveInt(rawMode, body) {
254
- const mode = rawMode & maskRest;
255
- switch (mode) {
256
- case CompactInt.oneBytePrefix:
257
- return rawMode;
258
- case CompactInt.twoBytePrefix:
259
- (0, codec_1.assert)(body.length === 2, 'Length should be 2');
260
- return ((body[0] & maskMode) << 8) | (body[1] & 0xff);
261
- case CompactInt.fourBytePrefix:
262
- (0, codec_1.assert)(body.length === 4, 'Length should be 4');
263
- return ((body[0] & maskMode) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff);
264
- default:
265
- if (body.length === 5) {
266
- return Number(BigInt('0x' + (0, utils_1.binToHex)(body.slice(1))));
267
- }
268
- else {
269
- throw new Error(`decodePositiveInt: Expect 4 bytes int, but get ${body.length - 1} bytes int`);
270
- }
282
+ exports.Signed = Signed;
283
+ Signed.signFlag = 0x20; // 0b00100000
284
+ Signed.oneByteBound = BigInt(0x20);
285
+ Signed.twoByteBound = Signed.oneByteBound << BigInt(8);
286
+ Signed.fourByteBound = Signed.oneByteBound << BigInt(8 * 3);
287
+ Signed.i256UpperBound = BigInt(1) << BigInt(255);
288
+ Signed.i256LowerBound = -Signed.i256UpperBound;
289
+ Signed.i32UpperBound = 2 ** 31;
290
+ Signed.i32LowerBound = -Signed.i32UpperBound;
291
+ exports.i256Codec = new (class extends codec_1.Codec {
292
+ encode(input) {
293
+ return Signed.encodeI256(input);
271
294
  }
272
- }
273
- function decodeNegativeInt(rawMode, body) {
274
- const mode = rawMode & maskRest;
275
- switch (mode) {
276
- case CompactInt.oneBytePrefix:
277
- return rawMode | maskModeNeg;
278
- case CompactInt.twoBytePrefix:
279
- (0, codec_1.assert)(body.length === 2, 'Length should be 2');
280
- return ((body[0] | maskModeNeg) << 8) | (body[1] & 0xff);
281
- case CompactInt.fourBytePrefix:
282
- (0, codec_1.assert)(body.length === 4, 'Length should be 4');
283
- return ((body[0] | maskModeNeg) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff);
284
- default:
285
- throw new Error(`decodeNegativeInt: Expect 4 bytes int, but get ${body.length - 1} bytes int`);
295
+ _decode(input) {
296
+ const { mode, body } = decodeMode(input);
297
+ return Signed.decodeI256(mode, body);
286
298
  }
287
- }
288
- function fixedSize(mode) {
289
- return mode === CompactInt.oneBytePrefix || mode === CompactInt.twoBytePrefix || mode === CompactInt.fourBytePrefix;
290
- }
299
+ })();
300
+ exports.i32Codec = new (class extends codec_1.Codec {
301
+ encode(input) {
302
+ return Signed.encodeI32(input);
303
+ }
304
+ _decode(input) {
305
+ const { mode, body } = decodeMode(input);
306
+ return Signed.decodeI32(mode, body);
307
+ }
308
+ })();
@@ -1,21 +1,18 @@
1
- import { Parser } from 'binary-parser';
2
- import { DecodedArray } from './array-codec';
3
1
  import { Codec } from './codec';
4
- import { DecodedCompactInt } from './compact-int-codec';
5
2
  import { Method } from './method-codec';
3
+ import { Reader } from './reader';
6
4
  export interface HalfDecodedContract {
7
- fieldLength: DecodedCompactInt;
8
- methodIndexes: DecodedArray<DecodedCompactInt>;
5
+ fieldLength: number;
6
+ methodIndexes: number[];
9
7
  methods: Uint8Array;
10
8
  }
11
9
  export interface Contract {
12
10
  fieldLength: number;
13
11
  methods: Method[];
14
12
  }
15
- export declare class ContractCodec implements Codec<HalfDecodedContract> {
16
- parser: Parser;
13
+ export declare class ContractCodec extends Codec<HalfDecodedContract> {
17
14
  encode(input: HalfDecodedContract): Uint8Array;
18
- decode(input: Uint8Array): HalfDecodedContract;
15
+ _decode(input: Reader): HalfDecodedContract;
19
16
  decodeContract(input: Uint8Array): Contract;
20
17
  encodeContract(contract: Contract): Uint8Array;
21
18
  }