@alephium/web3 0.42.0 → 0.44.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 (114) 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/{utils → address}/address.js +15 -19
  4. package/dist/src/address/index.d.ts +1 -0
  5. package/dist/src/address/index.js +34 -0
  6. package/dist/src/api/node-provider.js +10 -9
  7. package/dist/src/api/types.js +1 -1
  8. package/dist/src/codec/array-codec.d.ts +2 -3
  9. package/dist/src/codec/array-codec.js +2 -3
  10. package/dist/src/codec/asset-output-codec.d.ts +3 -4
  11. package/dist/src/codec/asset-output-codec.js +13 -15
  12. package/dist/src/codec/bigint-codec.d.ts +2 -3
  13. package/dist/src/codec/bigint-codec.js +2 -3
  14. package/dist/src/codec/bytestring-codec.d.ts +5 -6
  15. package/dist/src/codec/bytestring-codec.js +7 -7
  16. package/dist/src/codec/codec.d.ts +2 -3
  17. package/dist/src/codec/compact-int-codec.d.ts +11 -15
  18. package/dist/src/codec/compact-int-codec.js +23 -33
  19. package/dist/src/codec/contract-codec.d.ts +5 -6
  20. package/dist/src/codec/contract-codec.js +26 -26
  21. package/dist/src/codec/contract-output-codec.d.ts +2 -3
  22. package/dist/src/codec/contract-output-codec.js +9 -11
  23. package/dist/src/codec/contract-output-ref-codec.d.ts +3 -4
  24. package/dist/src/codec/contract-output-ref-codec.js +2 -2
  25. package/dist/src/codec/either-codec.d.ts +2 -3
  26. package/dist/src/codec/either-codec.js +1 -2
  27. package/dist/src/codec/hash.d.ts +2 -3
  28. package/dist/src/codec/hash.js +17 -0
  29. package/dist/src/codec/input-codec.d.ts +3 -4
  30. package/dist/src/codec/input-codec.js +6 -6
  31. package/dist/src/codec/instr-codec.d.ts +2 -3
  32. package/dist/src/codec/instr-codec.js +1 -2
  33. package/dist/src/codec/lockup-script-codec.d.ts +5 -6
  34. package/dist/src/codec/lockup-script-codec.js +1 -2
  35. package/dist/src/codec/long-codec.d.ts +2 -3
  36. package/dist/src/codec/long-codec.js +1 -2
  37. package/dist/src/codec/method-codec.d.ts +2 -3
  38. package/dist/src/codec/method-codec.js +13 -14
  39. package/dist/src/codec/option-codec.d.ts +3 -4
  40. package/dist/src/codec/option-codec.js +2 -3
  41. package/dist/src/codec/script-codec.d.ts +4 -5
  42. package/dist/src/codec/script-codec.js +2 -4
  43. package/dist/src/codec/signature-codec.d.ts +3 -4
  44. package/dist/src/codec/signature-codec.js +17 -0
  45. package/dist/src/codec/signed-int-codec.d.ts +2 -3
  46. package/dist/src/codec/signed-int-codec.js +1 -2
  47. package/dist/src/codec/token-codec.d.ts +3 -4
  48. package/dist/src/codec/token-codec.js +3 -3
  49. package/dist/src/codec/transaction-codec.d.ts +4 -5
  50. package/dist/src/codec/transaction-codec.js +12 -13
  51. package/dist/src/codec/unlock-script-codec.d.ts +5 -6
  52. package/dist/src/codec/unlock-script-codec.js +14 -15
  53. package/dist/src/codec/unsigned-tx-codec.d.ts +4 -5
  54. package/dist/src/codec/unsigned-tx-codec.js +4 -5
  55. package/dist/src/contract/contract.d.ts +10 -1
  56. package/dist/src/contract/contract.js +197 -21
  57. package/dist/src/contract/ralph.d.ts +2 -1
  58. package/dist/src/contract/ralph.js +25 -11
  59. package/dist/src/{utils → exchange}/exchange.js +5 -5
  60. package/dist/src/exchange/index.d.ts +1 -0
  61. package/dist/src/exchange/index.js +25 -0
  62. package/dist/src/index.d.ts +2 -0
  63. package/dist/src/index.js +2 -0
  64. package/dist/src/signer/signer.js +4 -5
  65. package/dist/src/signer/tx-builder.js +3 -3
  66. package/dist/src/utils/index.d.ts +0 -2
  67. package/dist/src/utils/index.js +0 -7
  68. package/dist/src/utils/sign.js +1 -1
  69. package/dist/src/utils/utils.d.ts +1 -0
  70. package/dist/src/utils/utils.js +30 -11
  71. package/dist/src/utils/webcrypto.js +3 -4
  72. package/package.json +1 -2
  73. package/src/{utils → address}/address.ts +15 -19
  74. package/src/address/index.ts +19 -0
  75. package/src/api/node-provider.ts +2 -9
  76. package/src/api/types.ts +2 -2
  77. package/src/codec/array-codec.ts +5 -6
  78. package/src/codec/asset-output-codec.ts +20 -22
  79. package/src/codec/bigint-codec.ts +4 -5
  80. package/src/codec/bytestring-codec.ts +11 -11
  81. package/src/codec/codec.ts +2 -3
  82. package/src/codec/compact-int-codec.ts +36 -50
  83. package/src/codec/contract-codec.ts +30 -29
  84. package/src/codec/contract-output-codec.ts +13 -15
  85. package/src/codec/contract-output-ref-codec.ts +5 -5
  86. package/src/codec/either-codec.ts +3 -4
  87. package/src/codec/hash.ts +2 -3
  88. package/src/codec/input-codec.ts +10 -10
  89. package/src/codec/instr-codec.ts +3 -4
  90. package/src/codec/lockup-script-codec.ts +8 -9
  91. package/src/codec/long-codec.ts +3 -4
  92. package/src/codec/method-codec.ts +16 -17
  93. package/src/codec/option-codec.ts +4 -5
  94. package/src/codec/script-codec.ts +7 -9
  95. package/src/codec/signature-codec.ts +3 -4
  96. package/src/codec/signed-int-codec.ts +3 -4
  97. package/src/codec/token-codec.ts +6 -6
  98. package/src/codec/transaction-codec.ts +17 -18
  99. package/src/codec/unlock-script-codec.ts +26 -27
  100. package/src/codec/unsigned-tx-codec.ts +10 -11
  101. package/src/contract/contract.ts +274 -9
  102. package/src/contract/ralph.ts +29 -12
  103. package/src/{utils → exchange}/exchange.ts +3 -11
  104. package/src/exchange/index.ts +19 -0
  105. package/src/index.ts +2 -0
  106. package/src/signer/signer.ts +2 -3
  107. package/src/signer/tx-builder.ts +2 -2
  108. package/src/utils/index.ts +0 -2
  109. package/src/utils/sign.ts +1 -1
  110. package/src/utils/utils.ts +29 -10
  111. package/src/utils/webcrypto.ts +3 -4
  112. package/webpack.config.js +1 -5
  113. /package/dist/src/{utils → address}/address.d.ts +0 -0
  114. /package/dist/src/{utils → exchange}/exchange.d.ts +0 -0
@@ -18,10 +18,10 @@ 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 buffer_1 = require("buffer/");
22
21
  const binary_parser_1 = require("binary-parser");
23
22
  const codec_1 = require("./codec");
24
23
  const bigint_codec_1 = require("./bigint-codec");
24
+ const utils_1 = require("../utils");
25
25
  class CompactInt {
26
26
  }
27
27
  exports.CompactInt = CompactInt;
@@ -60,17 +60,17 @@ class CompactUnsignedIntCodec {
60
60
  this.parser = compactIntParser;
61
61
  }
62
62
  encode(input) {
63
- return buffer_1.Buffer.from([input.mode, ...input.rest]);
63
+ return new Uint8Array([input.mode, ...input.rest]);
64
64
  }
65
65
  encodeU32(value) {
66
66
  if (value < this.oneByteBound) {
67
- return buffer_1.Buffer.from([(CompactInt.oneBytePrefix + value) & 0xff]);
67
+ return new Uint8Array([(CompactInt.oneBytePrefix + value) & 0xff]);
68
68
  }
69
69
  else if (value < this.twoByteBound) {
70
- return buffer_1.Buffer.from([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff]);
70
+ return new Uint8Array([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff]);
71
71
  }
72
72
  else if (value < this.fourByteBound) {
73
- return buffer_1.Buffer.from([
73
+ return new Uint8Array([
74
74
  (CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
75
75
  (value >> 16) & 0xff,
76
76
  (value >> 8) & 0xff,
@@ -78,7 +78,7 @@ class CompactUnsignedIntCodec {
78
78
  ]);
79
79
  }
80
80
  else {
81
- return buffer_1.Buffer.from([
81
+ return new Uint8Array([
82
82
  CompactInt.multiBytePrefix,
83
83
  (value >> 24) & 0xff,
84
84
  (value >> 16) & 0xff,
@@ -99,13 +99,9 @@ class CompactUnsignedIntCodec {
99
99
  }
100
100
  (0, codec_1.assert)(bytes.length <= 32, 'Expect <= 32 bytes for U256');
101
101
  const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff;
102
- return buffer_1.Buffer.concat([buffer_1.Buffer.from([header]), bytes]);
102
+ return new Uint8Array([header, ...bytes]);
103
103
  }
104
104
  }
105
- decodeU32(input) {
106
- const decoded = this.decode(input);
107
- return this.toU32(decoded);
108
- }
109
105
  decodeU256(input) {
110
106
  const decoded = this.decode(input);
111
107
  return this.toU256(decoded);
@@ -113,21 +109,15 @@ class CompactUnsignedIntCodec {
113
109
  decode(input) {
114
110
  return this.parser.parse(input);
115
111
  }
116
- toU32(value) {
117
- const body = buffer_1.Buffer.from([value.mode, ...value.rest]);
118
- return decodePositiveInt(value.mode, body);
119
- }
120
- fromU32(value) {
121
- return this.decode(this.encodeU32(value));
122
- }
123
112
  toU256(value) {
124
113
  const mode = value.mode & maskRest;
125
114
  if (fixedSize(mode)) {
126
- return BigInt(this.toU32(value));
115
+ const body = new Uint8Array([value.mode, ...value.rest]);
116
+ return BigInt(decodePositiveInt(value.mode, body));
127
117
  }
128
118
  else {
129
119
  (0, codec_1.assert)(value.rest.length <= 32, 'Expect <= 32 bytes for U256');
130
- return bigint_codec_1.BigIntCodec.decode(buffer_1.Buffer.from(value.rest), false);
120
+ return bigint_codec_1.BigIntCodec.decode(value.rest, false);
131
121
  }
132
122
  }
133
123
  fromU256(value) {
@@ -145,7 +135,7 @@ class CompactSignedIntCodec {
145
135
  this.parser = compactIntParser;
146
136
  }
147
137
  encode(input) {
148
- return buffer_1.Buffer.from([input.mode, ...input.rest]);
138
+ return new Uint8Array([input.mode, ...input.rest]);
149
139
  }
150
140
  decode(input) {
151
141
  return this.parser.parse(input);
@@ -157,13 +147,13 @@ class CompactSignedIntCodec {
157
147
  encodeI32(value) {
158
148
  if (value >= 0) {
159
149
  if (value < this.oneByteBound) {
160
- return buffer_1.Buffer.from([(CompactInt.oneBytePrefix + value) & 0xff]);
150
+ return new Uint8Array([(CompactInt.oneBytePrefix + value) & 0xff]);
161
151
  }
162
152
  else if (value < this.twoByteBound) {
163
- return buffer_1.Buffer.from([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff]);
153
+ return new Uint8Array([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff]);
164
154
  }
165
155
  else if (value < this.fourByteBound) {
166
- return buffer_1.Buffer.from([
156
+ return new Uint8Array([
167
157
  (CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
168
158
  (value >> 16) & 0xff,
169
159
  (value >> 8) & 0xff,
@@ -171,7 +161,7 @@ class CompactSignedIntCodec {
171
161
  ]);
172
162
  }
173
163
  else {
174
- return buffer_1.Buffer.from([
164
+ return new Uint8Array([
175
165
  CompactInt.multiBytePrefix,
176
166
  (value >> 24) & 0xff,
177
167
  (value >> 16) & 0xff,
@@ -182,13 +172,13 @@ class CompactSignedIntCodec {
182
172
  }
183
173
  else {
184
174
  if (value >= -this.oneByteBound) {
185
- return buffer_1.Buffer.from([(value ^ CompactInt.oneByteNegPrefix) & 0xff]);
175
+ return new Uint8Array([(value ^ CompactInt.oneByteNegPrefix) & 0xff]);
186
176
  }
187
177
  else if (value >= -this.twoByteBound) {
188
- return buffer_1.Buffer.from([((value >> 8) ^ CompactInt.twoByteNegPrefix) & 0xff, value & 0xff]);
178
+ return new Uint8Array([((value >> 8) ^ CompactInt.twoByteNegPrefix) & 0xff, value & 0xff]);
189
179
  }
190
180
  else if (value >= -this.fourByteBound) {
191
- return buffer_1.Buffer.from([
181
+ return new Uint8Array([
192
182
  ((value >> 24) ^ CompactInt.fourByteNegPrefix) & 0xff,
193
183
  (value >> 16) & 0xff,
194
184
  (value >> 8) & 0xff,
@@ -196,7 +186,7 @@ class CompactSignedIntCodec {
196
186
  ]);
197
187
  }
198
188
  else {
199
- return buffer_1.Buffer.from([
189
+ return new Uint8Array([
200
190
  CompactInt.multiBytePrefix,
201
191
  (value >> 24) & 0xff,
202
192
  (value >> 16) & 0xff,
@@ -213,7 +203,7 @@ class CompactSignedIntCodec {
213
203
  else {
214
204
  const bytes = bigint_codec_1.BigIntCodec.encode(value);
215
205
  const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff;
216
- return buffer_1.Buffer.concat([buffer_1.Buffer.from([header]), bytes]);
206
+ return new Uint8Array([header, ...bytes]);
217
207
  }
218
208
  }
219
209
  decodeI256(input) {
@@ -221,7 +211,7 @@ class CompactSignedIntCodec {
221
211
  return this.toI256(decoded);
222
212
  }
223
213
  toI32(value) {
224
- const body = buffer_1.Buffer.from([value.mode, ...value.rest]);
214
+ const body = new Uint8Array([value.mode, ...value.rest]);
225
215
  const mode = value.mode & maskRest;
226
216
  if (fixedSize(mode)) {
227
217
  const isPositive = (value.mode & signFlag) == 0;
@@ -251,7 +241,7 @@ class CompactSignedIntCodec {
251
241
  }
252
242
  else {
253
243
  (0, codec_1.assert)(value.rest.length <= 32, 'Expect <= 32 bytes for I256');
254
- return bigint_codec_1.BigIntCodec.decode(buffer_1.Buffer.from(value.rest), true);
244
+ return bigint_codec_1.BigIntCodec.decode(value.rest, true);
255
245
  }
256
246
  }
257
247
  fromI256(value) {
@@ -273,7 +263,7 @@ function decodePositiveInt(rawMode, body) {
273
263
  return ((body[0] & maskMode) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff);
274
264
  default:
275
265
  if (body.length === 5) {
276
- return Number(BigInt('0x' + body.slice(1).toString('hex')));
266
+ return Number(BigInt('0x' + (0, utils_1.binToHex)(body.slice(1))));
277
267
  }
278
268
  else {
279
269
  throw new Error(`decodePositiveInt: Expect 4 bytes int, but get ${body.length - 1} bytes int`);
@@ -1,4 +1,3 @@
1
- import { Buffer } from 'buffer/';
2
1
  import { Parser } from 'binary-parser';
3
2
  import { DecodedArray } from './array-codec';
4
3
  import { Codec } from './codec';
@@ -7,17 +6,17 @@ import { Method } from './method-codec';
7
6
  export interface HalfDecodedContract {
8
7
  fieldLength: DecodedCompactInt;
9
8
  methodIndexes: DecodedArray<DecodedCompactInt>;
10
- methods: Buffer;
9
+ methods: Uint8Array;
11
10
  }
12
11
  export interface Contract {
13
12
  fieldLength: number;
14
13
  methods: Method[];
15
14
  }
16
- export declare function toHalfDecoded(contract: Contract): HalfDecodedContract;
17
15
  export declare class ContractCodec implements Codec<HalfDecodedContract> {
18
16
  parser: Parser;
19
- encode(input: HalfDecodedContract): Buffer;
20
- decode(input: Buffer): HalfDecodedContract;
21
- decodeContract(input: Buffer): Contract;
17
+ encode(input: HalfDecodedContract): Uint8Array;
18
+ decode(input: Uint8Array): HalfDecodedContract;
19
+ decodeContract(input: Uint8Array): Contract;
20
+ encodeContract(contract: Contract): Uint8Array;
22
21
  }
23
22
  export declare const contractCodec: ContractCodec;
@@ -17,31 +17,13 @@ You should have received a copy of the GNU Lesser General Public License
17
17
  along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.contractCodec = exports.ContractCodec = exports.toHalfDecoded = void 0;
21
- const buffer_1 = require("buffer/");
20
+ exports.contractCodec = exports.ContractCodec = void 0;
22
21
  const binary_parser_1 = require("binary-parser");
23
22
  const array_codec_1 = require("./array-codec");
24
23
  const compact_int_codec_1 = require("./compact-int-codec");
25
24
  const method_codec_1 = require("./method-codec");
25
+ const utils_1 = require("../utils");
26
26
  const compactSignedIntsCodec = new array_codec_1.ArrayCodec(compact_int_codec_1.compactSignedIntCodec);
27
- function toHalfDecoded(contract) {
28
- const fieldLength = compact_int_codec_1.compactSignedIntCodec.fromI32(contract.fieldLength);
29
- const methods = contract.methods.map((m) => method_codec_1.methodCodec.encode(method_codec_1.MethodCodec.fromMethod(m)));
30
- let count = 0;
31
- const methodIndexes = Array.from(Array(methods.length).keys()).map((index) => {
32
- count += methods[`${index}`].length;
33
- return count;
34
- });
35
- return {
36
- fieldLength,
37
- methodIndexes: {
38
- length: compact_int_codec_1.compactSignedIntCodec.fromI32(methodIndexes.length),
39
- value: methodIndexes.map((value) => compact_int_codec_1.compactSignedIntCodec.fromI32(value))
40
- },
41
- methods: methods.reduce((acc, buffer) => buffer_1.Buffer.concat([acc, buffer]))
42
- };
43
- }
44
- exports.toHalfDecoded = toHalfDecoded;
45
27
  class ContractCodec {
46
28
  constructor() {
47
29
  this.parser = binary_parser_1.Parser.start()
@@ -54,10 +36,10 @@ class ContractCodec {
54
36
  .buffer('methods', { readUntil: 'eof' });
55
37
  }
56
38
  encode(input) {
57
- return buffer_1.Buffer.from([
58
- ...compact_int_codec_1.compactSignedIntCodec.encode(input.fieldLength),
59
- ...compactSignedIntsCodec.encode(input.methodIndexes.value),
60
- ...input.methods
39
+ return (0, utils_1.concatBytes)([
40
+ compact_int_codec_1.compactSignedIntCodec.encode(input.fieldLength),
41
+ compactSignedIntsCodec.encode(input.methodIndexes.value),
42
+ input.methods
61
43
  ]);
62
44
  }
63
45
  decode(input) {
@@ -65,8 +47,8 @@ class ContractCodec {
65
47
  }
66
48
  decodeContract(input) {
67
49
  const halfDecoded = this.decode(input);
68
- const fieldLength = compact_int_codec_1.compactUnsignedIntCodec.toU32(halfDecoded.fieldLength);
69
- const methodIndexes = halfDecoded.methodIndexes.value.map((v) => compact_int_codec_1.compactUnsignedIntCodec.toU32(v));
50
+ const fieldLength = compact_int_codec_1.compactSignedIntCodec.toI32(halfDecoded.fieldLength);
51
+ const methodIndexes = halfDecoded.methodIndexes.value.map((v) => compact_int_codec_1.compactSignedIntCodec.toI32(v));
70
52
  const methods = [];
71
53
  for (let i = 0, start = 0; i < methodIndexes.length; i++) {
72
54
  const end = methodIndexes[i];
@@ -76,6 +58,24 @@ class ContractCodec {
76
58
  }
77
59
  return { fieldLength, methods };
78
60
  }
61
+ encodeContract(contract) {
62
+ const fieldLength = compact_int_codec_1.compactSignedIntCodec.fromI32(contract.fieldLength);
63
+ const methods = contract.methods.map((m) => method_codec_1.methodCodec.encode(method_codec_1.MethodCodec.fromMethod(m)));
64
+ let count = 0;
65
+ const methodIndexes = Array.from(Array(methods.length).keys()).map((index) => {
66
+ count += methods[`${index}`].length;
67
+ return count;
68
+ });
69
+ const halfDecoded = {
70
+ fieldLength,
71
+ methodIndexes: {
72
+ length: compact_int_codec_1.compactSignedIntCodec.fromI32(methodIndexes.length),
73
+ value: methodIndexes.map((value) => compact_int_codec_1.compactSignedIntCodec.fromI32(value))
74
+ },
75
+ methods: (0, utils_1.concatBytes)(methods)
76
+ };
77
+ return this.encode(halfDecoded);
78
+ }
79
79
  }
80
80
  exports.ContractCodec = ContractCodec;
81
81
  exports.contractCodec = new ContractCodec();
@@ -1,4 +1,3 @@
1
- import { Buffer } from 'buffer/';
2
1
  import { Parser } from 'binary-parser';
3
2
  import { DecodedArray } from './array-codec';
4
3
  import { DecodedCompactInt } from './compact-int-codec';
@@ -13,8 +12,8 @@ export interface ContractOutput {
13
12
  }
14
13
  export declare class ContractOutputCodec implements Codec<ContractOutput> {
15
14
  parser: Parser;
16
- encode(input: ContractOutput): Buffer;
17
- decode(input: Buffer): ContractOutput;
15
+ encode(input: ContractOutput): Uint8Array;
16
+ decode(input: Uint8Array): ContractOutput;
18
17
  static convertToApiContractOutput(txIdBytes: Uint8Array, output: ContractOutput, index: number): ApiContractOutput;
19
18
  static convertToOutput(apiContractOutput: ApiContractOutput): ContractOutput;
20
19
  }
@@ -18,7 +18,6 @@ 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 buffer_1 = require("buffer/");
22
21
  const binary_parser_1 = require("binary-parser");
23
22
  const compact_int_codec_1 = require("./compact-int-codec");
24
23
  const token_codec_1 = require("./token-codec");
@@ -40,22 +39,22 @@ class ContractOutputCodec {
40
39
  });
41
40
  }
42
41
  encode(input) {
43
- const amount = buffer_1.Buffer.from(compact_int_codec_1.compactUnsignedIntCodec.encode(input.amount));
42
+ const amount = compact_int_codec_1.compactUnsignedIntCodec.encode(input.amount);
44
43
  const lockupScript = input.lockupScript.contractId;
45
- const tokens = buffer_1.Buffer.from(token_codec_1.tokensCodec.encode(input.tokens.value));
46
- return buffer_1.Buffer.concat([amount, lockupScript, tokens]);
44
+ const tokens = token_codec_1.tokensCodec.encode(input.tokens.value);
45
+ return (0, utils_1.concatBytes)([amount, lockupScript, tokens]);
47
46
  }
48
47
  decode(input) {
49
48
  return this.parser.parse(input);
50
49
  }
51
50
  static convertToApiContractOutput(txIdBytes, output, index) {
52
51
  const hint = (0, hash_1.createHint)(output.lockupScript.contractId);
53
- const key = (0, utils_1.binToHex)((0, hash_1.blakeHash)(buffer_1.Buffer.concat([txIdBytes, signed_int_codec_1.signedIntCodec.encode(index)])));
52
+ const key = (0, utils_1.binToHex)((0, hash_1.blakeHash)((0, utils_1.concatBytes)([txIdBytes, signed_int_codec_1.signedIntCodec.encode(index)])));
54
53
  const attoAlphAmount = compact_int_codec_1.compactUnsignedIntCodec.toU256(output.amount).toString();
55
- const address = utils_1.bs58.encode(buffer_1.Buffer.concat([buffer_1.Buffer.from([0x03]), output.lockupScript.contractId]));
54
+ const address = utils_1.bs58.encode(new Uint8Array([0x03, ...output.lockupScript.contractId]));
56
55
  const tokens = output.tokens.value.map((token) => {
57
56
  return {
58
- id: token.tokenId.toString('hex'),
57
+ id: (0, utils_1.binToHex)(token.tokenId),
59
58
  amount: compact_int_codec_1.compactUnsignedIntCodec.toU256(token.amount).toString()
60
59
  };
61
60
  });
@@ -63,16 +62,15 @@ class ContractOutputCodec {
63
62
  }
64
63
  static convertToOutput(apiContractOutput) {
65
64
  const amount = compact_int_codec_1.compactUnsignedIntCodec.fromU256(BigInt(apiContractOutput.attoAlphAmount));
66
- const lockupScript = lockup_script_codec_1.lockupScriptCodec.decode(buffer_1.Buffer.from(utils_1.bs58.decode(apiContractOutput.address)))
67
- .script;
65
+ const lockupScript = lockup_script_codec_1.lockupScriptCodec.decode(utils_1.bs58.decode(apiContractOutput.address)).script;
68
66
  const tokensValue = apiContractOutput.tokens.map((token) => {
69
67
  return {
70
- tokenId: buffer_1.Buffer.from(token.id, 'hex'),
68
+ tokenId: (0, utils_1.hexToBinUnsafe)(token.id),
71
69
  amount: compact_int_codec_1.compactUnsignedIntCodec.fromU256(BigInt(token.amount))
72
70
  };
73
71
  });
74
72
  const tokens = {
75
- length: compact_int_codec_1.compactUnsignedIntCodec.fromU32(tokensValue.length),
73
+ length: compact_int_codec_1.compactSignedIntCodec.fromI32(tokensValue.length),
76
74
  value: tokensValue
77
75
  };
78
76
  return { amount, lockupScript, tokens };
@@ -1,15 +1,14 @@
1
- import { Buffer } from 'buffer/';
2
1
  import { Parser } from 'binary-parser';
3
2
  import { ArrayCodec } from './array-codec';
4
3
  import { Codec } from './codec';
5
4
  export interface ContractOutputRef {
6
5
  hint: number;
7
- key: Buffer;
6
+ key: Uint8Array;
8
7
  }
9
8
  export declare class ContractOutputRefCodec implements Codec<ContractOutputRef> {
10
9
  parser: Parser;
11
- encode(input: ContractOutputRef): Buffer;
12
- decode(input: Buffer): ContractOutputRef;
10
+ encode(input: ContractOutputRef): Uint8Array;
11
+ decode(input: Uint8Array): ContractOutputRef;
13
12
  }
14
13
  export declare const contractOutputRefCodec: ContractOutputRefCodec;
15
14
  export declare const contractOutputRefsCodec: ArrayCodec<ContractOutputRef>;
@@ -18,16 +18,16 @@ 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 buffer_1 = require("buffer/");
22
21
  const binary_parser_1 = require("binary-parser");
23
22
  const array_codec_1 = require("./array-codec");
24
23
  const signed_int_codec_1 = require("./signed-int-codec");
24
+ const utils_1 = require("../utils");
25
25
  class ContractOutputRefCodec {
26
26
  constructor() {
27
27
  this.parser = binary_parser_1.Parser.start().int32('hint').buffer('key', { length: 32 });
28
28
  }
29
29
  encode(input) {
30
- return buffer_1.Buffer.concat([buffer_1.Buffer.from([...signed_int_codec_1.signedIntCodec.encode(input.hint), ...input.key])]);
30
+ return (0, utils_1.concatBytes)([signed_int_codec_1.signedIntCodec.encode(input.hint), input.key]);
31
31
  }
32
32
  decode(input) {
33
33
  return this.parser.parse(input);
@@ -1,4 +1,3 @@
1
- import { Buffer } from 'buffer/';
2
1
  import { Parser } from 'binary-parser';
3
2
  import { Codec } from './codec';
4
3
  export interface Either<L, R> {
@@ -10,8 +9,8 @@ export declare class EitherCodec<L, R> implements Codec<Either<L, R>> {
10
9
  private rightCodec;
11
10
  parser: Parser;
12
11
  constructor(leftCodec: Codec<L>, rightCodec: Codec<R>, parser?: Parser);
13
- encode(input: Either<L, R>): Buffer;
14
- decode(input: Buffer): Either<L, R>;
12
+ encode(input: Either<L, R>): Uint8Array;
13
+ decode(input: Uint8Array): Either<L, R>;
15
14
  fromLeft(left: L): Either<L, R>;
16
15
  fromRight(right: R): Either<L, R>;
17
16
  }
@@ -18,7 +18,6 @@ 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 buffer_1 = require("buffer/");
22
21
  const binary_parser_1 = require("binary-parser");
23
22
  class EitherCodec {
24
23
  constructor(leftCodec, rightCodec, parser = binary_parser_1.Parser.start()
@@ -42,7 +41,7 @@ class EitherCodec {
42
41
  else {
43
42
  result.push(...this.rightCodec.encode(input.value));
44
43
  }
45
- return buffer_1.Buffer.from(result);
44
+ return new Uint8Array(result);
46
45
  }
47
46
  decode(input) {
48
47
  const result = this.parser.parse(input);
@@ -1,4 +1,3 @@
1
- import { Buffer } from 'buffer/';
2
1
  export declare function blakeHash(raw: Uint8Array): Uint8Array;
3
- export declare function djbIntHash(bytes: Buffer): number;
4
- export declare function createHint(input: Buffer): number;
2
+ export declare function djbIntHash(bytes: Uint8Array): number;
3
+ export declare function createHint(input: Uint8Array): number;
@@ -4,6 +4,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.createHint = exports.djbIntHash = exports.blakeHash = void 0;
7
+ /*
8
+ Copyright 2018 - 2022 The Alephium Authors
9
+ This file is part of the alephium project.
10
+
11
+ The library is free software: you can redistribute it and/or modify
12
+ it under the terms of the GNU Lesser General Public License as published by
13
+ the Free Software Foundation, either version 3 of the License, or
14
+ (at your option) any later version.
15
+
16
+ The library is distributed in the hope that it will be useful,
17
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ GNU Lesser General Public License for more details.
20
+
21
+ You should have received a copy of the GNU Lesser General Public License
22
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
23
+ */
7
24
  const blakejs_1 = __importDefault(require("blakejs"));
8
25
  function blakeHash(raw) {
9
26
  return blakejs_1.default.blake2b(raw, undefined, 32);
@@ -1,4 +1,3 @@
1
- import { Buffer } from 'buffer/';
2
1
  import { Parser } from 'binary-parser';
3
2
  import { AssetInput } from '../api/api-alephium';
4
3
  import { UnlockScript } from './unlock-script-codec';
@@ -7,14 +6,14 @@ import { ArrayCodec } from './array-codec';
7
6
  export interface Input {
8
7
  outputRef: {
9
8
  hint: number;
10
- key: Buffer;
9
+ key: Uint8Array;
11
10
  };
12
11
  unlockScript: UnlockScript;
13
12
  }
14
13
  export declare class InputCodec implements Codec<Input> {
15
14
  parser: Parser;
16
- encode(input: Input): Buffer;
17
- decode(input: Buffer): Input;
15
+ encode(input: Input): Uint8Array;
16
+ decode(input: Uint8Array): Input;
18
17
  static toAssetInputs(inputs: Input[]): AssetInput[];
19
18
  static fromAssetInputs(inputs: AssetInput[]): Input[];
20
19
  }
@@ -18,7 +18,6 @@ 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 buffer_1 = require("buffer/");
22
21
  const binary_parser_1 = require("binary-parser");
23
22
  const utils_1 = require("../utils");
24
23
  const unlock_script_codec_1 = require("./unlock-script-codec");
@@ -35,8 +34,9 @@ class InputCodec {
35
34
  });
36
35
  }
37
36
  encode(input) {
38
- return buffer_1.Buffer.concat([
39
- buffer_1.Buffer.from([...signed_int_codec_1.signedIntCodec.encode(input.outputRef.hint), ...input.outputRef.key]),
37
+ return (0, utils_1.concatBytes)([
38
+ signed_int_codec_1.signedIntCodec.encode(input.outputRef.hint),
39
+ input.outputRef.key,
40
40
  unlock_script_codec_1.unlockScriptCodec.encode(input.unlockScript)
41
41
  ]);
42
42
  }
@@ -50,15 +50,15 @@ class InputCodec {
50
50
  const unlockScript = unlock_script_codec_1.unlockScriptCodec.encode(input.unlockScript);
51
51
  return {
52
52
  outputRef: { hint, key },
53
- unlockScript: unlockScript.toString('hex')
53
+ unlockScript: (0, utils_1.binToHex)(unlockScript)
54
54
  };
55
55
  });
56
56
  }
57
57
  static fromAssetInputs(inputs) {
58
58
  return inputs.map((input) => {
59
59
  const hint = input.outputRef.hint;
60
- const key = buffer_1.Buffer.from(input.outputRef.key, 'hex');
61
- const unlockScript = unlock_script_codec_1.unlockScriptCodec.decode(buffer_1.Buffer.from(input.unlockScript, 'hex'));
60
+ const key = (0, utils_1.hexToBinUnsafe)(input.outputRef.key);
61
+ const unlockScript = unlock_script_codec_1.unlockScriptCodec.decode((0, utils_1.hexToBinUnsafe)(input.unlockScript));
62
62
  return {
63
63
  outputRef: { hint, key },
64
64
  unlockScript
@@ -1,4 +1,3 @@
1
- import { Buffer } from 'buffer/';
2
1
  import { Parser } from 'binary-parser';
3
2
  import { ArrayCodec, DecodedArray } from './array-codec';
4
3
  import { DecodedCompactInt } from './compact-int-codec';
@@ -226,8 +225,8 @@ export declare const MethodSelector: (selector: number) => Instr;
226
225
  export declare const CallExternalBySelector: (selector: number) => Instr;
227
226
  export declare class InstrCodec implements Codec<Instr> {
228
227
  parser: Parser;
229
- encode(instr: Instr): Buffer;
230
- decode(input: Buffer): Instr;
228
+ encode(instr: Instr): Uint8Array;
229
+ decode(input: Uint8Array): Instr;
231
230
  }
232
231
  export declare const instrCodec: InstrCodec;
233
232
  export declare const instrsCodec: ArrayCodec<Instr>;
@@ -21,7 +21,6 @@ GNU Lesser General Public License for more details.
21
21
  You should have received a copy of the GNU Lesser General Public License
22
22
  along with the library. If not, see <http://www.gnu.org/licenses/>.
23
23
  */
24
- const buffer_1 = require("buffer/");
25
24
  const binary_parser_1 = require("binary-parser");
26
25
  const array_codec_1 = require("./array-codec");
27
26
  const compact_int_codec_1 = require("./compact-int-codec");
@@ -476,7 +475,7 @@ class InstrCodec {
476
475
  else if (instr.code === 0xd3 || instr.code === 0xd4) {
477
476
  result.push(...signed_int_codec_1.signedIntCodec.encode(instrValue.index));
478
477
  }
479
- return buffer_1.Buffer.from(result);
478
+ return new Uint8Array(result);
480
479
  }
481
480
  decode(input) {
482
481
  return this.parser.parse(input);
@@ -1,20 +1,19 @@
1
- import { Buffer } from 'buffer/';
2
1
  import { Parser } from 'binary-parser';
3
2
  import { DecodedCompactInt } from './compact-int-codec';
4
3
  import { Codec } from './codec';
5
4
  import { DecodedArray } from './array-codec';
6
5
  export interface PublicKeyHash {
7
- publicKeyHash: Buffer;
6
+ publicKeyHash: Uint8Array;
8
7
  }
9
8
  export interface MultiSig {
10
9
  publicKeyHashes: DecodedArray<PublicKeyHash>;
11
10
  m: DecodedCompactInt;
12
11
  }
13
12
  export interface P2SH {
14
- scriptHash: Buffer;
13
+ scriptHash: Uint8Array;
15
14
  }
16
15
  export interface P2C {
17
- contractId: Buffer;
16
+ contractId: Uint8Array;
18
17
  }
19
18
  export interface LockupScript {
20
19
  scriptType: number;
@@ -22,7 +21,7 @@ export interface LockupScript {
22
21
  }
23
22
  export declare class LockupScriptCodec implements Codec<LockupScript> {
24
23
  parser: Parser;
25
- encode(input: LockupScript): Buffer;
26
- decode(input: Buffer): LockupScript;
24
+ encode(input: LockupScript): Uint8Array;
25
+ decode(input: Uint8Array): LockupScript;
27
26
  }
28
27
  export declare const lockupScriptCodec: LockupScriptCodec;
@@ -18,7 +18,6 @@ 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 buffer_1 = require("buffer/");
22
21
  const binary_parser_1 = require("binary-parser");
23
22
  const compact_int_codec_1 = require("./compact-int-codec");
24
23
  const array_codec_1 = require("./array-codec");
@@ -70,7 +69,7 @@ class LockupScriptCodec {
70
69
  else {
71
70
  throw new Error(`Unsupported script type: ${input.scriptType}`);
72
71
  }
73
- return buffer_1.Buffer.from(result);
72
+ return new Uint8Array(result);
74
73
  }
75
74
  decode(input) {
76
75
  return this.parser.parse(input);
@@ -1,9 +1,8 @@
1
- import { Buffer } from 'buffer/';
2
1
  import { Parser } from 'binary-parser';
3
2
  import { Codec } from './codec';
4
3
  export declare class LongCodec implements Codec<bigint> {
5
4
  parser: Parser;
6
- encode(input: bigint): Buffer;
7
- decode(bytes: Buffer): bigint;
5
+ encode(input: bigint): Uint8Array;
6
+ decode(bytes: Uint8Array): bigint;
8
7
  }
9
8
  export declare const longCodec: LongCodec;
@@ -18,7 +18,6 @@ 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 buffer_1 = require("buffer/");
22
21
  const binary_parser_1 = require("binary-parser");
23
22
  const codec_1 = require("./codec");
24
23
  class LongCodec {
@@ -35,7 +34,7 @@ class LongCodec {
35
34
  byteArray[byteArray.length - index - 1] = Number(byte);
36
35
  input >>= BigInt(8);
37
36
  }
38
- return buffer_1.Buffer.from(byteArray);
37
+ return byteArray;
39
38
  }
40
39
  decode(bytes) {
41
40
  (0, codec_1.assert)(bytes.length == 8, 'Length should be 8');