@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
@@ -25,11 +25,10 @@ const elliptic_1 = require("elliptic");
25
25
  const bn_js_1 = __importDefault(require("bn.js"));
26
26
  const constants_1 = require("../constants");
27
27
  const blakejs_1 = __importDefault(require("blakejs"));
28
- const bs58_1 = __importDefault(require("./bs58"));
29
- const djb2_1 = __importDefault(require("./djb2"));
30
- const utils_1 = require("./utils");
28
+ const bs58_1 = __importDefault(require("../utils/bs58"));
29
+ const djb2_1 = __importDefault(require("../utils/djb2"));
30
+ const utils_1 = require("../utils");
31
31
  const lockup_script_codec_1 = require("../codec/lockup-script-codec");
32
- const buffer_1 = require("buffer/");
33
32
  const codec_1 = require("../codec");
34
33
  const ec = new elliptic_1.ec('secp256k1');
35
34
  var AddressType;
@@ -57,7 +56,7 @@ function decodeAndValidateAddress(address) {
57
56
  if (addressType === AddressType.P2MPKH) {
58
57
  let multisig;
59
58
  try {
60
- multisig = lockup_script_codec_1.lockupScriptCodec.decode(buffer_1.Buffer.from(decoded)).script;
59
+ multisig = lockup_script_codec_1.lockupScriptCodec.decode(decoded).script;
61
60
  }
62
61
  catch (_) {
63
62
  throw new Error(`Invalid multisig address: ${address}`);
@@ -163,27 +162,24 @@ exports.publicKeyFromPrivateKey = publicKeyFromPrivateKey;
163
162
  function addressFromPublicKey(publicKey, _keyType) {
164
163
  const keyType = _keyType ?? 'default';
165
164
  if (keyType === 'default') {
166
- const addressType = buffer_1.Buffer.from([AddressType.P2PKH]);
167
- const hash = buffer_1.Buffer.from(blakejs_1.default.blake2b(buffer_1.Buffer.from(publicKey, 'hex'), undefined, 32));
168
- const bytes = buffer_1.Buffer.concat([addressType, hash]);
165
+ const hash = blakejs_1.default.blake2b((0, utils_1.hexToBinUnsafe)(publicKey), undefined, 32);
166
+ const bytes = new Uint8Array([AddressType.P2PKH, ...hash]);
169
167
  return bs58_1.default.encode(bytes);
170
168
  }
171
169
  else {
172
- const lockupScript = buffer_1.Buffer.from(`0101000000000458144020${publicKey}8685`, 'hex');
170
+ const lockupScript = (0, utils_1.hexToBinUnsafe)(`0101000000000458144020${publicKey}8685`);
173
171
  return addressFromScript(lockupScript);
174
172
  }
175
173
  }
176
174
  exports.addressFromPublicKey = addressFromPublicKey;
177
175
  function addressFromScript(script) {
178
176
  const scriptHash = blakejs_1.default.blake2b(script, undefined, 32);
179
- const addressType = buffer_1.Buffer.from([AddressType.P2SH]);
180
- return bs58_1.default.encode(buffer_1.Buffer.concat([addressType, scriptHash]));
177
+ return bs58_1.default.encode(new Uint8Array([AddressType.P2SH, ...scriptHash]));
181
178
  }
182
179
  exports.addressFromScript = addressFromScript;
183
180
  function addressFromContractId(contractId) {
184
- const addressType = buffer_1.Buffer.from([AddressType.P2C]);
185
- const hash = buffer_1.Buffer.from((0, utils_1.hexToBinUnsafe)(contractId));
186
- const bytes = buffer_1.Buffer.concat([addressType, hash]);
181
+ const hash = (0, utils_1.hexToBinUnsafe)(contractId);
182
+ const bytes = new Uint8Array([AddressType.P2C, ...hash]);
187
183
  return bs58_1.default.encode(bytes);
188
184
  }
189
185
  exports.addressFromContractId = addressFromContractId;
@@ -194,7 +190,7 @@ function addressFromTokenId(tokenId) {
194
190
  exports.addressFromTokenId = addressFromTokenId;
195
191
  function contractIdFromTx(txId, outputIndex) {
196
192
  const txIdBin = (0, utils_1.hexToBinUnsafe)(txId);
197
- const data = buffer_1.Buffer.concat([txIdBin, buffer_1.Buffer.from([outputIndex])]);
193
+ const data = new Uint8Array([...txIdBin, outputIndex]);
198
194
  const hash = blakejs_1.default.blake2b(data, undefined, 32);
199
195
  return (0, utils_1.binToHex)(hash);
200
196
  }
@@ -203,10 +199,10 @@ function subContractId(parentContractId, pathInHex, group) {
203
199
  if (group < 0 || group >= constants_1.TOTAL_NUMBER_OF_GROUPS) {
204
200
  throw new Error(`Invalid group ${group}`);
205
201
  }
206
- const data = buffer_1.Buffer.concat([(0, utils_1.hexToBinUnsafe)(parentContractId), (0, utils_1.hexToBinUnsafe)(pathInHex)]);
207
- const bytes = buffer_1.Buffer.concat([
208
- blakejs_1.default.blake2b(blakejs_1.default.blake2b(data, undefined, 32), undefined, 32).slice(0, -1),
209
- buffer_1.Buffer.from([group])
202
+ const data = (0, utils_1.concatBytes)([(0, utils_1.hexToBinUnsafe)(parentContractId), (0, utils_1.hexToBinUnsafe)(pathInHex)]);
203
+ const bytes = new Uint8Array([
204
+ ...blakejs_1.default.blake2b(blakejs_1.default.blake2b(data, undefined, 32), undefined, 32).slice(0, -1),
205
+ group
210
206
  ]);
211
207
  return (0, utils_1.binToHex)(bytes);
212
208
  }
@@ -0,0 +1 @@
1
+ export * from './address';
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
20
+ if (k2 === undefined) k2 = k;
21
+ var desc = Object.getOwnPropertyDescriptor(m, k);
22
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
23
+ desc = { enumerable: true, get: function() { return m[k]; } };
24
+ }
25
+ Object.defineProperty(o, k2, desc);
26
+ }) : (function(o, m, k, k2) {
27
+ if (k2 === undefined) k2 = k;
28
+ o[k2] = m[k];
29
+ }));
30
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
31
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
32
+ };
33
+ Object.defineProperty(exports, "__esModule", { value: true });
34
+ __exportStar(require("./address"), exports);
@@ -21,6 +21,7 @@ exports.tryGetCallResult = exports.NodeProvider = void 0;
21
21
  const types_1 = require("./types");
22
22
  const api_alephium_1 = require("./api-alephium");
23
23
  const utils_1 = require("../utils");
24
+ const address_1 = require("../address");
24
25
  function initializeNodeApi(baseUrl, apiKey, customFetch) {
25
26
  const nodeApi = new api_alephium_1.Api({
26
27
  baseUrl: baseUrl,
@@ -38,8 +39,8 @@ class NodeProvider {
38
39
  };
39
40
  // Only use this when the token follows the fungible token interface, check `guessTokenType` first
40
41
  this.fetchFungibleTokenMetaData = async (tokenId) => {
41
- const address = (0, utils_1.addressFromTokenId)(tokenId);
42
- const group = (0, utils_1.groupOfAddress)(address);
42
+ const address = (0, address_1.addressFromTokenId)(tokenId);
43
+ const group = (0, address_1.groupOfAddress)(address);
43
44
  const calls = Array.from([0, 1, 2, 3], (index) => ({ methodIndex: index, group: group, address: address }));
44
45
  const result = await this.contracts.postContractsMulticallContract({
45
46
  calls: calls
@@ -54,8 +55,8 @@ class NodeProvider {
54
55
  };
55
56
  // Only use this when the token follows the non-fungile token interface, check `guessTokenType` first
56
57
  this.fetchNFTMetaData = async (tokenId) => {
57
- const address = (0, utils_1.addressFromTokenId)(tokenId);
58
- const group = (0, utils_1.groupOfAddress)(address);
58
+ const address = (0, address_1.addressFromTokenId)(tokenId);
59
+ const group = (0, address_1.groupOfAddress)(address);
59
60
  const calls = Array.from([0, 1], (index) => ({ methodIndex: index, group: group, address: address }));
60
61
  const result = await this.contracts.postContractsMulticallContract({
61
62
  calls: calls
@@ -99,8 +100,8 @@ class NodeProvider {
99
100
  };
100
101
  // Only use this when the contract follows the NFT collection interface, check `guessFollowsNFTCollectionStd` first
101
102
  this.fetchNFTCollectionMetaData = async (collectionId) => {
102
- const address = (0, utils_1.addressFromContractId)(collectionId);
103
- const group = (0, utils_1.groupOfAddress)(address);
103
+ const address = (0, address_1.addressFromContractId)(collectionId);
104
+ const group = (0, address_1.groupOfAddress)(address);
104
105
  const calls = Array.from([0, 1], (index) => ({ methodIndex: index, group: group, address: address }));
105
106
  const result = await this.contracts.postContractsMulticallContract({ calls });
106
107
  const callResults = result.results.map((r) => tryGetCallResult(r));
@@ -111,8 +112,8 @@ class NodeProvider {
111
112
  };
112
113
  // Only use this when the contract follows the NFT collection with royalty interface, check `guessFollowsNFTCollectionWithRoyaltyStd` first
113
114
  this.fetchNFTRoyaltyAmount = async (collectionId, tokenId, salePrice) => {
114
- const address = (0, utils_1.addressFromContractId)(collectionId);
115
- const group = (0, utils_1.groupOfAddress)(address);
115
+ const address = (0, address_1.addressFromContractId)(collectionId);
116
+ const group = (0, address_1.groupOfAddress)(address);
116
117
  const apiResult = await this.contracts.postContractsCallContract({
117
118
  address: address,
118
119
  group: group,
@@ -132,7 +133,7 @@ class NodeProvider {
132
133
  return BigInt(result.returns[0].value);
133
134
  };
134
135
  this.guessStdInterfaceId = async (tokenId) => {
135
- const address = (0, utils_1.addressFromTokenId)(tokenId);
136
+ const address = (0, address_1.addressFromTokenId)(tokenId);
136
137
  const rawState = await this.contracts.getContractsAddressState(address);
137
138
  const lastImmField = rawState.immFields.slice(-1).pop()?.value;
138
139
  const interfaceIdPrefix = '414c5048'; // the hex of 'ALPH'
@@ -85,7 +85,7 @@ function toApiByteVec(v) {
85
85
  try {
86
86
  const address = utils_1.bs58.decode(v);
87
87
  if (address.length == 33 && address[0] == 3) {
88
- return Buffer.from(address.slice(1)).toString('hex');
88
+ return (0, utils_1.binToHex)(address.slice(1));
89
89
  }
90
90
  }
91
91
  catch (_) {
@@ -1,4 +1,3 @@
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';
@@ -10,8 +9,8 @@ export declare class ArrayCodec<T> implements Codec<T[]> {
10
9
  private childCodec;
11
10
  parser: Parser;
12
11
  constructor(childCodec: Codec<T>, parser?: Parser);
13
- encode(input: T[]): Buffer;
14
- decode(input: Buffer): T[];
12
+ encode(input: T[]): Uint8Array;
13
+ decode(input: Uint8Array): T[];
15
14
  static arrayParser(parser: Parser): Parser;
16
15
  fromArray(inputs: T[]): DecodedArray<T>;
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
  const compact_int_codec_1 = require("./compact-int-codec");
24
23
  class ArrayCodec {
@@ -31,7 +30,7 @@ class ArrayCodec {
31
30
  for (const element of input) {
32
31
  result.push(...this.childCodec.encode(element));
33
32
  }
34
- return buffer_1.Buffer.from(result);
33
+ return new Uint8Array(result);
35
34
  }
36
35
  decode(input) {
37
36
  const result = this.parser.parse(input);
@@ -51,7 +50,7 @@ class ArrayCodec {
51
50
  }
52
51
  fromArray(inputs) {
53
52
  return {
54
- length: compact_int_codec_1.compactUnsignedIntCodec.fromU32(inputs.length),
53
+ length: compact_int_codec_1.compactSignedIntCodec.fromI32(inputs.length),
55
54
  value: inputs
56
55
  };
57
56
  }
@@ -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';
@@ -10,14 +9,14 @@ import { Token } from './token-codec';
10
9
  export interface AssetOutput {
11
10
  amount: DecodedCompactInt;
12
11
  lockupScript: LockupScript;
13
- lockTime: Buffer;
12
+ lockTime: Uint8Array;
14
13
  tokens: DecodedArray<Token>;
15
14
  additionalData: ByteString;
16
15
  }
17
16
  export declare class AssetOutputCodec implements Codec<AssetOutput> {
18
17
  parser: Parser;
19
- encode(input: AssetOutput): Buffer;
20
- decode(input: Buffer): AssetOutput;
18
+ encode(input: AssetOutput): Uint8Array;
19
+ decode(input: Uint8Array): AssetOutput;
21
20
  static toFixedAssetOutputs(txIdBytes: Uint8Array, outputs: AssetOutput[]): FixedAssetOutput[];
22
21
  static toFixedAssetOutput(txIdBytes: Uint8Array, output: AssetOutput, index: number): FixedAssetOutput;
23
22
  static fromFixedAssetOutputs(fixedOutputs: FixedAssetOutput[]): AssetOutput[];
@@ -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 array_codec_1 = require("./array-codec");
24
23
  const compact_int_codec_1 = require("./compact-int-codec");
@@ -33,7 +32,7 @@ class AssetOutputCodec {
33
32
  constructor() {
34
33
  this.parser = binary_parser_1.Parser.start()
35
34
  .nest('amount', {
36
- type: compact_int_codec_1.compactUnsignedIntCodec.parser
35
+ type: compact_int_codec_1.compactSignedIntCodec.parser
37
36
  })
38
37
  .nest('lockupScript', {
39
38
  type: lockup_script_codec_1.lockupScriptCodec.parser
@@ -49,12 +48,11 @@ class AssetOutputCodec {
49
48
  });
50
49
  }
51
50
  encode(input) {
52
- const amount = buffer_1.Buffer.from(compact_int_codec_1.compactUnsignedIntCodec.encode(input.amount));
51
+ const amount = compact_int_codec_1.compactUnsignedIntCodec.encode(input.amount);
53
52
  const lockupScript = lockup_script_codec_1.lockupScriptCodec.encode(input.lockupScript);
54
- const lockTime = buffer_1.Buffer.from(input.lockTime);
55
- const tokens = buffer_1.Buffer.from(token_codec_1.tokensCodec.encode(input.tokens.value));
56
- const additionalData = buffer_1.Buffer.from(bytestring_codec_1.byteStringCodec.encode(input.additionalData));
57
- return buffer_1.Buffer.concat([amount, lockupScript, lockTime, tokens, additionalData]);
53
+ const tokens = token_codec_1.tokensCodec.encode(input.tokens.value);
54
+ const additionalData = bytestring_codec_1.byteStringCodec.encode(input.additionalData);
55
+ return (0, utils_1.concatBytes)([amount, lockupScript, input.lockTime, tokens, additionalData]);
58
56
  }
59
57
  decode(input) {
60
58
  return this.parser.parse(input);
@@ -67,13 +65,13 @@ class AssetOutputCodec {
67
65
  const lockTime = Number(long_codec_1.longCodec.decode(output.lockTime));
68
66
  const tokens = output.tokens.value.map((token) => {
69
67
  return {
70
- id: token.tokenId.toString('hex'),
68
+ id: (0, utils_1.binToHex)(token.tokenId),
71
69
  amount: compact_int_codec_1.compactUnsignedIntCodec.toU256(token.amount).toString()
72
70
  };
73
71
  });
74
- const message = output.additionalData.value.toString('hex');
72
+ const message = (0, utils_1.binToHex)(output.additionalData.value);
75
73
  const scriptType = output.lockupScript.scriptType;
76
- const key = (0, utils_1.binToHex)((0, hash_1.blakeHash)(buffer_1.Buffer.concat([txIdBytes, signed_int_codec_1.signedIntCodec.encode(index)])));
74
+ const key = (0, utils_1.binToHex)((0, hash_1.blakeHash)((0, utils_1.concatBytes)([txIdBytes, signed_int_codec_1.signedIntCodec.encode(index)])));
77
75
  const outputLockupScript = output.lockupScript.script;
78
76
  const address = utils_1.bs58.encode(lockup_script_codec_1.lockupScriptCodec.encode(output.lockupScript));
79
77
  let hint = undefined;
@@ -105,20 +103,20 @@ class AssetOutputCodec {
105
103
  static fromFixedAssetOutput(fixedOutput) {
106
104
  const amount = compact_int_codec_1.compactUnsignedIntCodec.fromU256(BigInt(fixedOutput.attoAlphAmount));
107
105
  const lockTime = long_codec_1.longCodec.encode(BigInt(fixedOutput.lockTime));
108
- const lockupScript = lockup_script_codec_1.lockupScriptCodec.decode(buffer_1.Buffer.from(utils_1.bs58.decode(fixedOutput.address)));
106
+ const lockupScript = lockup_script_codec_1.lockupScriptCodec.decode(utils_1.bs58.decode(fixedOutput.address));
109
107
  const tokensValue = fixedOutput.tokens.map((token) => {
110
108
  return {
111
- tokenId: buffer_1.Buffer.from(token.id, 'hex'),
109
+ tokenId: (0, utils_1.hexToBinUnsafe)(token.id),
112
110
  amount: compact_int_codec_1.compactUnsignedIntCodec.fromU256(BigInt(token.amount))
113
111
  };
114
112
  });
115
113
  const tokens = {
116
- length: compact_int_codec_1.compactUnsignedIntCodec.fromU32(tokensValue.length),
114
+ length: compact_int_codec_1.compactSignedIntCodec.fromI32(tokensValue.length),
117
115
  value: tokensValue
118
116
  };
119
- const additionalDataValue = buffer_1.Buffer.from(fixedOutput.message, 'hex');
117
+ const additionalDataValue = (0, utils_1.hexToBinUnsafe)(fixedOutput.message);
120
118
  const additionalData = {
121
- length: compact_int_codec_1.compactUnsignedIntCodec.fromU32(additionalDataValue.length),
119
+ length: compact_int_codec_1.compactSignedIntCodec.fromI32(additionalDataValue.length),
122
120
  value: additionalDataValue
123
121
  };
124
122
  return {
@@ -1,5 +1,4 @@
1
- import { Buffer } from 'buffer/';
2
1
  export declare class BigIntCodec {
3
- static encode(value: bigint): Buffer;
4
- static decode(encoded: Buffer, signed: boolean): bigint;
2
+ static encode(value: bigint): Uint8Array;
3
+ static decode(encoded: Uint8Array, signed: boolean): bigint;
5
4
  }
@@ -18,12 +18,11 @@ 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
  class BigIntCodec {
23
22
  static encode(value) {
24
23
  // Special case for zero.
25
24
  if (value === 0n) {
26
- return buffer_1.Buffer.from([0]);
25
+ return new Uint8Array([0]);
27
26
  }
28
27
  const isNegative = value < 0n;
29
28
  let absValue = isNegative ? -value : value;
@@ -60,7 +59,7 @@ class BigIntCodec {
60
59
  }
61
60
  }
62
61
  // The byte array needs to be reversed since we've constructed it in little-endian order.
63
- return buffer_1.Buffer.from(new Uint8Array(bytes.reverse()));
62
+ return new Uint8Array(bytes.reverse());
64
63
  }
65
64
  static decode(encoded, signed) {
66
65
  // Special case for zero.
@@ -1,16 +1,15 @@
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
  export interface ByteString {
6
5
  length: DecodedCompactInt;
7
- value: Buffer;
6
+ value: Uint8Array;
8
7
  }
9
8
  export declare class ByteStringCodec implements Codec<ByteString> {
10
9
  parser: Parser;
11
- encode(input: ByteString): Buffer;
12
- decode(input: Buffer): ByteString;
13
- encodeBuffer(input: Buffer): Buffer;
14
- decodeBuffer(input: Buffer): Buffer;
10
+ encode(input: ByteString): Uint8Array;
11
+ decode(input: Uint8Array): ByteString;
12
+ encodeBytes(input: Uint8Array): Uint8Array;
13
+ decodeBytes(input: Uint8Array): Uint8Array;
15
14
  }
16
15
  export declare const byteStringCodec: ByteStringCodec;
@@ -18,31 +18,31 @@ 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");
23
+ const utils_1 = require("../utils");
24
24
  class ByteStringCodec {
25
25
  constructor() {
26
26
  this.parser = new binary_parser_1.Parser()
27
27
  .nest('length', {
28
- type: compact_int_codec_1.compactUnsignedIntCodec.parser
28
+ type: compact_int_codec_1.compactSignedIntCodec.parser
29
29
  })
30
30
  .buffer('value', {
31
31
  length: function (ctx) {
32
- return compact_int_codec_1.compactUnsignedIntCodec.toU32(this['length']);
32
+ return compact_int_codec_1.compactSignedIntCodec.toI32(this['length']);
33
33
  }
34
34
  });
35
35
  }
36
36
  encode(input) {
37
- return buffer_1.Buffer.from([...compact_int_codec_1.compactUnsignedIntCodec.encode(input.length), ...input.value]);
37
+ return (0, utils_1.concatBytes)([compact_int_codec_1.compactSignedIntCodec.encode(input.length), input.value]);
38
38
  }
39
39
  decode(input) {
40
40
  return this.parser.parse(input);
41
41
  }
42
- encodeBuffer(input) {
43
- return buffer_1.Buffer.from([...compact_int_codec_1.compactUnsignedIntCodec.encodeU32(input.length), ...input]);
42
+ encodeBytes(input) {
43
+ return (0, utils_1.concatBytes)([compact_int_codec_1.compactSignedIntCodec.encodeI32(input.length), input]);
44
44
  }
45
- decodeBuffer(input) {
45
+ decodeBytes(input) {
46
46
  return this.decode(input).value;
47
47
  }
48
48
  }
@@ -1,8 +1,7 @@
1
- import { Buffer } from 'buffer/';
2
1
  import { Parser } from 'binary-parser';
3
2
  export interface Codec<T> {
4
3
  parser: Parser;
5
- encode(input: T): Buffer;
6
- decode(input: Buffer): T;
4
+ encode(input: T): Uint8Array;
5
+ decode(input: Uint8Array): T;
7
6
  }
8
7
  export declare function assert(value: boolean, message: string): void;
@@ -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 declare class CompactInt {
@@ -19,14 +18,11 @@ export declare class CompactUnsignedIntCodec implements Codec<DecodedCompactInt>
19
18
  private twoByteBound;
20
19
  private fourByteBound;
21
20
  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
- fromU32(value: number): DecodedCompactInt;
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;
30
26
  toU256(value: DecodedCompactInt): bigint;
31
27
  fromU256(value: bigint): DecodedCompactInt;
32
28
  }
@@ -37,12 +33,12 @@ export declare class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
37
33
  private twoByteBound;
38
34
  private fourByteBound;
39
35
  parser: Parser;
40
- encode(input: DecodedCompactInt): Buffer;
41
- decode(input: Buffer): DecodedCompactInt;
42
- decodeI32(input: Buffer): number;
43
- encodeI32(value: number): Buffer;
44
- encodeI256(value: bigint): Buffer;
45
- decodeI256(input: Buffer): bigint;
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;
46
42
  toI32(value: DecodedCompactInt): number;
47
43
  fromI32(value: number): DecodedCompactInt;
48
44
  toI256(value: DecodedCompactInt): bigint;