@alephium/web3 0.41.0 → 0.43.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 (123) 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 +1 -2
  10. package/dist/src/codec/asset-output-codec.d.ts +3 -4
  11. package/dist/src/codec/asset-output-codec.js +10 -12
  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 +5 -5
  16. package/dist/src/codec/codec.d.ts +2 -3
  17. package/dist/src/codec/compact-int-codec.d.ts +12 -13
  18. package/dist/src/codec/compact-int-codec.js +22 -22
  19. package/dist/src/codec/contract-codec.d.ts +5 -6
  20. package/dist/src/codec/contract-codec.js +24 -24
  21. package/dist/src/codec/contract-output-codec.d.ts +2 -3
  22. package/dist/src/codec/contract-output-codec.js +8 -10
  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 +1 -2
  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 +1 -3
  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 +8 -95
  56. package/dist/src/contract/contract.js +31 -564
  57. package/dist/src/contract/ralph.d.ts +1 -1
  58. package/dist/src/contract/ralph.js +9 -10
  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/transaction/index.d.ts +1 -0
  67. package/dist/src/transaction/index.js +1 -0
  68. package/dist/src/transaction/utils.d.ts +2 -0
  69. package/dist/src/transaction/utils.js +34 -0
  70. package/dist/src/utils/index.d.ts +0 -2
  71. package/dist/src/utils/index.js +0 -7
  72. package/dist/src/utils/sign.js +1 -1
  73. package/dist/src/utils/utils.d.ts +1 -0
  74. package/dist/src/utils/utils.js +30 -11
  75. package/dist/src/utils/webcrypto.js +3 -4
  76. package/package.json +1 -2
  77. package/src/{utils → address}/address.ts +15 -19
  78. package/src/address/index.ts +19 -0
  79. package/src/api/node-provider.ts +2 -9
  80. package/src/api/types.ts +2 -2
  81. package/src/codec/array-codec.ts +3 -4
  82. package/src/codec/asset-output-codec.ts +16 -18
  83. package/src/codec/bigint-codec.ts +4 -5
  84. package/src/codec/bytestring-codec.ts +8 -8
  85. package/src/codec/codec.ts +2 -3
  86. package/src/codec/compact-int-codec.ts +36 -37
  87. package/src/codec/contract-codec.ts +28 -27
  88. package/src/codec/contract-output-codec.ts +11 -13
  89. package/src/codec/contract-output-ref-codec.ts +5 -5
  90. package/src/codec/either-codec.ts +3 -4
  91. package/src/codec/hash.ts +2 -3
  92. package/src/codec/input-codec.ts +10 -10
  93. package/src/codec/instr-codec.ts +3 -4
  94. package/src/codec/lockup-script-codec.ts +8 -9
  95. package/src/codec/long-codec.ts +3 -4
  96. package/src/codec/method-codec.ts +3 -4
  97. package/src/codec/option-codec.ts +4 -5
  98. package/src/codec/script-codec.ts +5 -7
  99. package/src/codec/signature-codec.ts +3 -4
  100. package/src/codec/signed-int-codec.ts +3 -4
  101. package/src/codec/token-codec.ts +6 -6
  102. package/src/codec/transaction-codec.ts +17 -18
  103. package/src/codec/unlock-script-codec.ts +26 -27
  104. package/src/codec/unsigned-tx-codec.ts +10 -11
  105. package/src/contract/contract.ts +18 -790
  106. package/src/contract/ralph.ts +10 -11
  107. package/src/{utils → exchange}/exchange.ts +3 -11
  108. package/src/exchange/index.ts +19 -0
  109. package/src/index.ts +2 -0
  110. package/src/signer/signer.ts +2 -3
  111. package/src/signer/tx-builder.ts +2 -2
  112. package/src/transaction/index.ts +1 -0
  113. package/src/transaction/utils.ts +38 -0
  114. package/src/utils/index.ts +0 -2
  115. package/src/utils/sign.ts +1 -1
  116. package/src/utils/utils.ts +29 -10
  117. package/src/utils/webcrypto.ts +3 -4
  118. package/webpack.config.js +1 -6
  119. package/dist/src/utils/error.d.ts +0 -15
  120. package/dist/src/utils/error.js +0 -66
  121. package/src/utils/error.ts +0 -77
  122. /package/dist/src/{utils → address}/address.d.ts +0 -0
  123. /package/dist/src/{utils → exchange}/exchange.d.ts +0 -0
@@ -5,7 +5,7 @@ export declare function encodeBool(bool: boolean): Uint8Array;
5
5
  export declare function decodeBool(bytes: Uint8Array): boolean;
6
6
  export declare function encodeI256(i256: bigint): Uint8Array;
7
7
  export declare function encodeU256(u256: bigint): Uint8Array;
8
- export declare function encodeByteVec(bytes: string): Uint8Array;
8
+ export declare function encodeByteVec(hex: string): Uint8Array;
9
9
  export declare function encodeAddress(address: string): Uint8Array;
10
10
  export declare enum VmValType {
11
11
  Bool = 0,
@@ -19,7 +19,6 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
19
19
  var _a;
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
21
  exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.encodeContractFields = exports.buildScriptByteCode = exports.flattenFields = exports.primitiveToByteVec = exports.decodePrimitive = exports.tryDecodeMapDebugLog = exports.calcFieldSize = exports.encodeMapPrefix = exports.parseMapType = exports.splitFields = exports.encodeScriptField = exports.encodeScriptFieldAsString = exports.encodePrimitiveValues = exports.addressVal = exports.byteVecVal = exports.u256Val = exports.i256Val = exports.boolVal = exports.encodeVmAddress = exports.encodeVmByteVec = exports.encodeVmU256 = exports.encodeVmI256 = exports.encodeVmBool = exports.VmValType = exports.encodeAddress = exports.encodeByteVec = exports.encodeU256 = exports.encodeI256 = exports.decodeBool = exports.encodeBool = void 0;
22
- const buffer_1 = require("buffer/");
23
22
  const api_1 = require("../api");
24
23
  const utils_1 = require("../utils");
25
24
  const codec_1 = require("../codec");
@@ -159,13 +158,13 @@ function encodeU256(u256) {
159
158
  }
160
159
  }
161
160
  exports.encodeU256 = encodeU256;
162
- function encodeByteVec(bytes) {
163
- if (!(0, utils_1.isHexString)(bytes)) {
164
- throw Error(`Given value ${bytes} is not a valid hex string`);
161
+ function encodeByteVec(hex) {
162
+ if (!(0, utils_1.isHexString)(hex)) {
163
+ throw Error(`Given value ${hex} is not a valid hex string`);
165
164
  }
166
- const buffer0 = buffer_1.Buffer.from(bytes, 'hex');
167
- const buffer1 = buffer_1.Buffer.from(encodeI256(BigInt(buffer0.length)));
168
- return buffer_1.Buffer.concat([buffer1, buffer0]);
165
+ const bytes0 = (0, utils_1.hexToBinUnsafe)(hex);
166
+ const bytes1 = encodeI256(BigInt(bytes0.length));
167
+ return (0, utils_1.concatBytes)([bytes1, bytes0]);
169
168
  }
170
169
  exports.encodeByteVec = encodeByteVec;
171
170
  function encodeAddress(address) {
@@ -258,7 +257,7 @@ function encodeScriptFieldU256(value) {
258
257
  return new Uint8Array([Instruction.u256Const, ...encodeU256(value)]);
259
258
  }
260
259
  function encodeScriptFieldAsString(tpe, value) {
261
- return buffer_1.Buffer.from(encodeScriptField(tpe, value)).toString('hex');
260
+ return (0, utils_1.binToHex)(encodeScriptField(tpe, value));
262
261
  }
263
262
  exports.encodeScriptFieldAsString = encodeScriptFieldAsString;
264
263
  function encodeScriptField(tpe, value) {
@@ -369,9 +368,9 @@ function decodePrimitive(value, type) {
369
368
  case 'Bool':
370
369
  return decodeBool(value);
371
370
  case 'I256':
372
- return codec_1.compactSignedIntCodec.decodeI256(buffer_1.Buffer.from(value));
371
+ return codec_1.compactSignedIntCodec.decodeI256(value);
373
372
  case 'U256':
374
- return codec_1.compactUnsignedIntCodec.decodeU256(buffer_1.Buffer.from(value));
373
+ return codec_1.compactUnsignedIntCodec.decodeU256(value);
375
374
  case 'ByteVec':
376
375
  return (0, utils_1.binToHex)(value);
377
376
  case 'Address':
@@ -18,9 +18,9 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.getAddressFromUnlockScript = exports.getSenderAddress = exports.getALPHDepositInfo = exports.isALPHTransferTx = exports.validateExchangeAddress = void 0;
21
+ const address_1 = require("../address");
21
22
  const utils_1 = require("../utils");
22
23
  const unlock_script_codec_1 = require("../codec/unlock-script-codec");
23
- const buffer_1 = require("buffer/");
24
24
  const script_codec_1 = require("../codec/script-codec");
25
25
  function validateExchangeAddress(address) {
26
26
  let decoded;
@@ -33,7 +33,7 @@ function validateExchangeAddress(address) {
33
33
  if (decoded.length === 0)
34
34
  throw new Error('Address is empty');
35
35
  const addressType = decoded[0];
36
- if (addressType !== utils_1.AddressType.P2PKH && addressType !== utils_1.AddressType.P2SH) {
36
+ if (addressType !== address_1.AddressType.P2PKH && addressType !== address_1.AddressType.P2SH) {
37
37
  throw new Error('Invalid address type');
38
38
  }
39
39
  if (decoded.length !== 33) {
@@ -98,7 +98,7 @@ function getAddressFromUnlockScript(unlockScript) {
98
98
  if (unlockScriptBody.length !== 33) {
99
99
  throw new Error(`Invalid p2pkh unlock script: ${unlockScript}`);
100
100
  }
101
- return (0, utils_1.addressFromPublicKey)((0, utils_1.binToHex)(unlockScriptBody));
101
+ return (0, address_1.addressFromPublicKey)((0, utils_1.binToHex)(unlockScriptBody));
102
102
  }
103
103
  if (unlockScriptType === UnlockScriptType.P2MPKH) {
104
104
  throw new Error('Naive multi-sig address is not supported for exchanges as it will be replaced by P2SH');
@@ -106,12 +106,12 @@ function getAddressFromUnlockScript(unlockScript) {
106
106
  if (unlockScriptType === UnlockScriptType.P2SH) {
107
107
  let p2sh;
108
108
  try {
109
- p2sh = unlock_script_codec_1.unlockScriptCodec.decode(buffer_1.Buffer.from(decoded)).script;
109
+ p2sh = unlock_script_codec_1.unlockScriptCodec.decode(decoded).script;
110
110
  }
111
111
  catch (_) {
112
112
  throw new Error(`Invalid p2sh unlock script: ${unlockScript}`);
113
113
  }
114
- return (0, utils_1.addressFromScript)(script_codec_1.scriptCodec.encode(p2sh.script));
114
+ return (0, address_1.addressFromScript)(script_codec_1.scriptCodec.encode(p2sh.script));
115
115
  }
116
116
  throw new Error('Invalid unlock script type');
117
117
  }
@@ -0,0 +1 @@
1
+ export { validateExchangeAddress, isALPHTransferTx, getSenderAddress, getALPHDepositInfo } from './exchange';
@@ -0,0 +1,25 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.getALPHDepositInfo = exports.getSenderAddress = exports.isALPHTransferTx = exports.validateExchangeAddress = void 0;
21
+ var exchange_1 = require("./exchange");
22
+ Object.defineProperty(exports, "validateExchangeAddress", { enumerable: true, get: function () { return exchange_1.validateExchangeAddress; } });
23
+ Object.defineProperty(exports, "isALPHTransferTx", { enumerable: true, get: function () { return exchange_1.isALPHTransferTx; } });
24
+ Object.defineProperty(exports, "getSenderAddress", { enumerable: true, get: function () { return exchange_1.getSenderAddress; } });
25
+ Object.defineProperty(exports, "getALPHDepositInfo", { enumerable: true, get: function () { return exchange_1.getALPHDepositInfo; } });
@@ -10,3 +10,5 @@ export * as codec from './codec';
10
10
  export * as utils from './utils';
11
11
  export * from './debug';
12
12
  export * from './block';
13
+ export * from './address';
14
+ export * from './exchange';
package/dist/src/index.js CHANGED
@@ -59,3 +59,5 @@ exports.codec = __importStar(require("./codec"));
59
59
  exports.utils = __importStar(require("./utils"));
60
60
  __exportStar(require("./debug"), exports);
61
61
  __exportStar(require("./block"), exports);
62
+ __exportStar(require("./address"), exports);
63
+ __exportStar(require("./exchange"), exports);
@@ -44,13 +44,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
44
44
  };
45
45
  Object.defineProperty(exports, "__esModule", { value: true });
46
46
  exports.fromApiDestination = exports.toApiDestinations = exports.toApiDestination = exports.verifySignedMessage = exports.hashMessage = exports.extendMessage = exports.SignerProviderWithCachedAccounts = exports.SignerProviderWithMultipleAccounts = exports.SignerProviderSimple = exports.InteractiveSignerProvider = exports.SignerProvider = void 0;
47
- const buffer_1 = require("buffer/");
48
47
  const crypto_1 = require("crypto");
49
48
  const api_1 = require("../api");
50
49
  const utils = __importStar(require("../utils"));
51
50
  const blakejs_1 = __importDefault(require("blakejs"));
52
51
  const tx_builder_1 = require("./tx-builder");
53
- const utils_1 = require("../utils");
52
+ const address_1 = require("../address");
54
53
  class SignerProvider {
55
54
  async getSelectedAccount() {
56
55
  const account = await this.unsafeGetSelectedAccount();
@@ -58,8 +57,8 @@ class SignerProvider {
58
57
  return account;
59
58
  }
60
59
  static validateAccount(account) {
61
- const derivedAddress = (0, utils_1.addressFromPublicKey)(account.publicKey, account.keyType);
62
- const derivedGroup = (0, utils_1.groupOfAddress)(derivedAddress);
60
+ const derivedAddress = (0, address_1.addressFromPublicKey)(account.publicKey, account.keyType);
61
+ const derivedGroup = (0, address_1.groupOfAddress)(derivedAddress);
63
62
  if (derivedAddress !== account.address || derivedGroup !== account.group) {
64
63
  throw Error(`Invalid accounot data: ${JSON.stringify(account)}`);
65
64
  }
@@ -206,7 +205,7 @@ function hashMessage(message, hasher) {
206
205
  return utils.binToHex(blakejs_1.default.blake2b(extendMessage(message), undefined, 32));
207
206
  case 'sha256':
208
207
  const sha256 = (0, crypto_1.createHash)('sha256');
209
- sha256.update(buffer_1.Buffer.from(message));
208
+ sha256.update(new TextEncoder().encode(message));
210
209
  return utils.binToHex(sha256.digest());
211
210
  case 'blake2b':
212
211
  return utils.binToHex(blakejs_1.default.blake2b(message, undefined, 32));
@@ -20,7 +20,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.TransactionBuilder = void 0;
21
21
  const utils_1 = require("../utils");
22
22
  const api_1 = require("../api");
23
- const utils_2 = require("../utils");
23
+ const address_1 = require("../address");
24
24
  const signer_1 = require("./signer");
25
25
  class TransactionBuilder {
26
26
  static from(param0, param1, customFetch) {
@@ -32,7 +32,7 @@ class TransactionBuilder {
32
32
  })();
33
33
  }
34
34
  static validatePublicKey(params, publicKey, keyType) {
35
- const address = (0, utils_2.addressFromPublicKey)(publicKey, keyType);
35
+ const address = (0, address_1.addressFromPublicKey)(publicKey, keyType);
36
36
  if (address !== params.signerAddress) {
37
37
  throw new Error('Unmatched public key');
38
38
  }
@@ -63,7 +63,7 @@ class TransactionBuilder {
63
63
  ...rest
64
64
  };
65
65
  const response = await this.nodeProvider.contracts.postContractsUnsignedTxDeployContract(data);
66
- const contractId = (0, utils_1.binToHex)((0, utils_1.contractIdFromAddress)(response.contractAddress));
66
+ const contractId = (0, utils_1.binToHex)((0, address_1.contractIdFromAddress)(response.contractAddress));
67
67
  return { ...response, groupIndex: response.fromGroup, contractId, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
68
68
  }
69
69
  async buildExecuteScriptTx(params, publicKey) {
@@ -1,2 +1,3 @@
1
1
  export * from './status';
2
2
  export * from './sign-verify';
3
+ export * from './utils';
@@ -33,3 +33,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
34
  __exportStar(require("./status"), exports);
35
35
  __exportStar(require("./sign-verify"), exports);
36
+ __exportStar(require("./utils"), exports);
@@ -0,0 +1,2 @@
1
+ import { node } from '../api';
2
+ export declare function waitForTxConfirmation(txId: string, confirmations: number, requestInterval: number): Promise<node.Confirmed>;
@@ -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
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.waitForTxConfirmation = void 0;
21
+ const global_1 = require("../global");
22
+ function isConfirmed(txStatus) {
23
+ return txStatus.type === 'Confirmed';
24
+ }
25
+ async function waitForTxConfirmation(txId, confirmations, requestInterval) {
26
+ const provider = (0, global_1.getCurrentNodeProvider)();
27
+ const status = await provider.transactions.getTransactionsStatus({ txId: txId });
28
+ if (isConfirmed(status) && status.chainConfirmations >= confirmations) {
29
+ return status;
30
+ }
31
+ await new Promise((r) => setTimeout(r, requestInterval));
32
+ return waitForTxConfirmation(txId, confirmations, requestInterval);
33
+ }
34
+ exports.waitForTxConfirmation = waitForTxConfirmation;
@@ -1,9 +1,7 @@
1
1
  export * from './webcrypto';
2
- export * from './address';
3
2
  export * from './bs58';
4
3
  export * from './djb2';
5
4
  export * from './utils';
6
5
  export * from './subscription';
7
6
  export * from './sign';
8
7
  export * from './number';
9
- export { validateExchangeAddress, isALPHTransferTx, getSenderAddress, getALPHDepositInfo } from './exchange';
@@ -31,17 +31,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
31
31
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
- exports.getALPHDepositInfo = exports.getSenderAddress = exports.isALPHTransferTx = exports.validateExchangeAddress = void 0;
35
34
  __exportStar(require("./webcrypto"), exports);
36
- __exportStar(require("./address"), exports);
37
35
  __exportStar(require("./bs58"), exports);
38
36
  __exportStar(require("./djb2"), exports);
39
37
  __exportStar(require("./utils"), exports);
40
38
  __exportStar(require("./subscription"), exports);
41
39
  __exportStar(require("./sign"), exports);
42
40
  __exportStar(require("./number"), exports);
43
- var exchange_1 = require("./exchange");
44
- Object.defineProperty(exports, "validateExchangeAddress", { enumerable: true, get: function () { return exchange_1.validateExchangeAddress; } });
45
- Object.defineProperty(exports, "isALPHTransferTx", { enumerable: true, get: function () { return exchange_1.isALPHTransferTx; } });
46
- Object.defineProperty(exports, "getSenderAddress", { enumerable: true, get: function () { return exchange_1.getSenderAddress; } });
47
- Object.defineProperty(exports, "getALPHDepositInfo", { enumerable: true, get: function () { return exchange_1.getALPHDepositInfo; } });
@@ -53,7 +53,7 @@ necc.utils.sha256Sync = (...messages) => {
53
53
  return sha256.digest();
54
54
  };
55
55
  necc.utils.hmacSha256Sync = (key, ...messages) => {
56
- const hash = (0, crypto_1.createHmac)('sha256', Buffer.from(key));
56
+ const hash = (0, crypto_1.createHmac)('sha256', key);
57
57
  messages.forEach((m) => hash.update(m));
58
58
  return Uint8Array.from(hash.digest());
59
59
  };
@@ -23,6 +23,7 @@ export declare function sleep(ms: number): Promise<void>;
23
23
  export declare function isDevnet(networkId?: number): boolean;
24
24
  export declare function targetToDifficulty(compactedTarget: HexString): bigint;
25
25
  export declare function difficultyToTarget(diff: bigint): HexString;
26
+ export declare function concatBytes(arrays: Uint8Array[]): Uint8Array;
26
27
  type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
27
28
  export type Eq<X, Y> = _Eq<{
28
29
  [P in keyof X]: X[P];
@@ -20,10 +20,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
20
20
  return (mod && mod.__esModule) ? mod : { "default": mod };
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.assertType = exports.difficultyToTarget = exports.targetToDifficulty = exports.isDevnet = exports.sleep = exports.hexToString = exports.stringToHex = exports.blockChainIndex = exports.binToHex = exports.hexToBinUnsafe = exports.toNonNegativeBigInt = exports.isHexString = exports.signatureDecode = exports.encodeHexSignature = exports.encodeSignature = exports.networkIds = void 0;
23
+ exports.assertType = exports.concatBytes = exports.difficultyToTarget = exports.targetToDifficulty = exports.isDevnet = exports.sleep = exports.hexToString = exports.stringToHex = exports.blockChainIndex = exports.binToHex = exports.hexToBinUnsafe = exports.toNonNegativeBigInt = exports.isHexString = exports.signatureDecode = exports.encodeHexSignature = exports.encodeSignature = exports.networkIds = void 0;
24
24
  const elliptic_1 = require("elliptic");
25
25
  const bn_js_1 = __importDefault(require("bn.js"));
26
- const buffer_1 = require("buffer/");
27
26
  const constants_1 = require("../constants");
28
27
  exports.networkIds = ['mainnet', 'testnet', 'devnet'];
29
28
  const ec = new elliptic_1.ec('secp256k1');
@@ -72,11 +71,17 @@ function toNonNegativeBigInt(input) {
72
71
  }
73
72
  exports.toNonNegativeBigInt = toNonNegativeBigInt;
74
73
  function hexToBinUnsafe(hex) {
75
- return buffer_1.Buffer.from(hex, 'hex');
74
+ const bytes = [];
75
+ for (let i = 0; i < hex.length; i += 2) {
76
+ bytes.push(parseInt(hex.slice(i, i + 2), 16));
77
+ }
78
+ return new Uint8Array(bytes);
76
79
  }
77
80
  exports.hexToBinUnsafe = hexToBinUnsafe;
78
81
  function binToHex(bin) {
79
- return buffer_1.Buffer.from(bin).toString('hex');
82
+ return Array.from(bin)
83
+ .map((byte) => byte.toString(16).padStart(2, '0'))
84
+ .join('');
80
85
  }
81
86
  exports.binToHex = binToHex;
82
87
  function blockChainIndex(blockHash) {
@@ -99,7 +104,8 @@ function hexToString(str) {
99
104
  if (!isHexString(str)) {
100
105
  throw new Error(`Invalid hex string: ${str}`);
101
106
  }
102
- return buffer_1.Buffer.from(str, 'hex').toString();
107
+ const bytes = hexToBinUnsafe(str);
108
+ return new TextDecoder().decode(bytes);
103
109
  }
104
110
  exports.hexToString = hexToString;
105
111
  function sleep(ms) {
@@ -125,15 +131,28 @@ function difficultyToTarget(diff) {
125
131
  const maxBigInt = 1n << 256n;
126
132
  const target = diff === 1n ? maxBigInt - 1n : maxBigInt / diff;
127
133
  const size = Math.floor((target.toString(2).length + 7) / 8);
128
- const mantissa = size <= 3
134
+ const mantissa = Number(size <= 3
129
135
  ? BigInt.asIntN(32, target) << BigInt(8 * (3 - size))
130
- : BigInt.asIntN(32, target >> BigInt(8 * (size - 3)));
131
- const mantissaBytes = buffer_1.Buffer.alloc(4);
132
- mantissaBytes.writeInt32BE(Number(mantissa), 0);
133
- const bytes = new Uint8Array([size, ...mantissaBytes.slice(1)]);
134
- return binToHex(bytes);
136
+ : BigInt.asIntN(32, target >> BigInt(8 * (size - 3))));
137
+ const mantissaBytes = new Uint8Array(4);
138
+ mantissaBytes[0] = size;
139
+ mantissaBytes[1] = (mantissa >> 16) & 0xff;
140
+ mantissaBytes[2] = (mantissa >> 8) & 0xff;
141
+ mantissaBytes[3] = mantissa & 0xff;
142
+ return binToHex(mantissaBytes);
135
143
  }
136
144
  exports.difficultyToTarget = difficultyToTarget;
145
+ function concatBytes(arrays) {
146
+ const totalLength = arrays.reduce((acc, arr) => acc + arr.length, 0);
147
+ const result = new Uint8Array(totalLength);
148
+ let offset = 0;
149
+ for (const array of arrays) {
150
+ result.set(array, offset);
151
+ offset += array.length;
152
+ }
153
+ return result;
154
+ }
155
+ exports.concatBytes = concatBytes;
137
156
  // eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
138
157
  function assertType() { }
139
158
  exports.assertType = assertType;
@@ -18,7 +18,6 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.WebCrypto = void 0;
21
- const buffer_1 = require("buffer/");
22
21
  const crypto_1 = require("crypto");
23
22
  const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
24
23
  class WebCrypto {
@@ -29,12 +28,12 @@ class WebCrypto {
29
28
  if (!ArrayBuffer.isView(array)) {
30
29
  throw new TypeError("Failed to execute 'getRandomValues' on 'Crypto': parameter 1 is not of type 'ArrayBufferView'");
31
30
  }
32
- const buffer = buffer_1.Buffer.from(array.buffer, array.byteOffset, array.byteLength);
31
+ const bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
33
32
  if (isBrowser) {
34
- globalThis.crypto.getRandomValues(buffer);
33
+ globalThis.crypto.getRandomValues(bytes);
35
34
  }
36
35
  else {
37
- (0, crypto_1.randomFillSync)(buffer);
36
+ (0, crypto_1.randomFillSync)(bytes);
38
37
  }
39
38
  return array;
40
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.41.0",
3
+ "version": "0.43.0",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -37,7 +37,6 @@
37
37
  "bignumber.js": "^9.1.1",
38
38
  "blakejs": "1.2.1",
39
39
  "bn.js": "5.2.1",
40
- "buffer": "^6.0.3",
41
40
  "cross-fetch": "^3.1.5",
42
41
  "crypto-browserify": "^3.12.0",
43
42
  "elliptic": "6.5.4",
@@ -20,12 +20,11 @@ import { ec as EC } from 'elliptic'
20
20
  import BN from 'bn.js'
21
21
  import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
22
22
  import blake from 'blakejs'
23
- import bs58 from './bs58'
24
- import djb2 from './djb2'
25
- import { binToHex, hexToBinUnsafe } from './utils'
23
+ import bs58 from '../utils/bs58'
24
+ import djb2 from '../utils/djb2'
25
+ import { binToHex, concatBytes, hexToBinUnsafe } from '../utils'
26
26
  import { KeyType } from '../signer'
27
27
  import { MultiSig, lockupScriptCodec } from '../codec/lockup-script-codec'
28
- import { Buffer } from 'buffer/'
29
28
  import { compactSignedIntCodec } from '../codec'
30
29
 
31
30
  const ec = new EC('secp256k1')
@@ -54,7 +53,7 @@ function decodeAndValidateAddress(address: string): Uint8Array {
54
53
  if (addressType === AddressType.P2MPKH) {
55
54
  let multisig: MultiSig
56
55
  try {
57
- multisig = lockupScriptCodec.decode(Buffer.from(decoded)).script as MultiSig
56
+ multisig = lockupScriptCodec.decode(decoded).script as MultiSig
58
57
  } catch (_) {
59
58
  throw new Error(`Invalid multisig address: ${address}`)
60
59
  }
@@ -163,26 +162,23 @@ export function addressFromPublicKey(publicKey: string, _keyType?: KeyType): str
163
162
  const keyType = _keyType ?? 'default'
164
163
 
165
164
  if (keyType === 'default') {
166
- const addressType = Buffer.from([AddressType.P2PKH])
167
- const hash = Buffer.from(blake.blake2b(Buffer.from(publicKey, 'hex'), undefined, 32))
168
- const bytes = Buffer.concat([addressType, hash])
165
+ const hash = blake.blake2b(hexToBinUnsafe(publicKey), undefined, 32)
166
+ const bytes = new Uint8Array([AddressType.P2PKH, ...hash])
169
167
  return bs58.encode(bytes)
170
168
  } else {
171
- const lockupScript = Buffer.from(`0101000000000458144020${publicKey}8685`, 'hex')
169
+ const lockupScript = hexToBinUnsafe(`0101000000000458144020${publicKey}8685`)
172
170
  return addressFromScript(lockupScript)
173
171
  }
174
172
  }
175
173
 
176
174
  export function addressFromScript(script: Uint8Array): string {
177
175
  const scriptHash = blake.blake2b(script, undefined, 32)
178
- const addressType = Buffer.from([AddressType.P2SH])
179
- return bs58.encode(Buffer.concat([addressType, scriptHash]))
176
+ return bs58.encode(new Uint8Array([AddressType.P2SH, ...scriptHash]))
180
177
  }
181
178
 
182
179
  export function addressFromContractId(contractId: string): string {
183
- const addressType = Buffer.from([AddressType.P2C])
184
- const hash = Buffer.from(hexToBinUnsafe(contractId))
185
- const bytes = Buffer.concat([addressType, hash])
180
+ const hash = hexToBinUnsafe(contractId)
181
+ const bytes = new Uint8Array([AddressType.P2C, ...hash])
186
182
  return bs58.encode(bytes)
187
183
  }
188
184
 
@@ -193,7 +189,7 @@ export function addressFromTokenId(tokenId: string): string {
193
189
 
194
190
  export function contractIdFromTx(txId: string, outputIndex: number): string {
195
191
  const txIdBin = hexToBinUnsafe(txId)
196
- const data = Buffer.concat([txIdBin, Buffer.from([outputIndex])])
192
+ const data = new Uint8Array([...txIdBin, outputIndex])
197
193
  const hash = blake.blake2b(data, undefined, 32)
198
194
  return binToHex(hash)
199
195
  }
@@ -202,10 +198,10 @@ export function subContractId(parentContractId: string, pathInHex: string, group
202
198
  if (group < 0 || group >= TOTAL_NUMBER_OF_GROUPS) {
203
199
  throw new Error(`Invalid group ${group}`)
204
200
  }
205
- const data = Buffer.concat([hexToBinUnsafe(parentContractId), hexToBinUnsafe(pathInHex)])
206
- const bytes = Buffer.concat([
207
- blake.blake2b(blake.blake2b(data, undefined, 32), undefined, 32).slice(0, -1),
208
- Buffer.from([group])
201
+ const data = concatBytes([hexToBinUnsafe(parentContractId), hexToBinUnsafe(pathInHex)])
202
+ const bytes = new Uint8Array([
203
+ ...blake.blake2b(blake.blake2b(data, undefined, 32), undefined, 32).slice(0, -1),
204
+ group
209
205
  ])
210
206
  return binToHex(bytes)
211
207
  }
@@ -0,0 +1,19 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ export * from './address'
@@ -28,15 +28,8 @@ import {
28
28
  requestWithLog
29
29
  } from './types'
30
30
  import { Api as NodeApi, CallContractFailed, CallContractSucceeded } from './api-alephium'
31
- import {
32
- HexString,
33
- addressFromContractId,
34
- addressFromTokenId,
35
- groupOfAddress,
36
- hexToString,
37
- isHexString,
38
- toNonNegativeBigInt
39
- } from '../utils'
31
+ import { HexString, hexToString, isHexString, toNonNegativeBigInt } from '../utils'
32
+ import { addressFromContractId, addressFromTokenId, groupOfAddress } from '../address'
40
33
  import * as node from '../api/api-alephium'
41
34
 
42
35
  function initializeNodeApi(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): NodeApi<string> {
package/src/api/types.ts CHANGED
@@ -18,7 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import { ZERO_ADDRESS } from '../constants'
20
20
  import { isDebugModeEnabled } from '../debug'
21
- import { assertType, bs58, Eq, isBase58, isHexString } from '../utils'
21
+ import { assertType, binToHex, bs58, Eq, isBase58, isHexString } from '../utils'
22
22
  import * as node from './api-alephium'
23
23
 
24
24
  export type Number256 = bigint | string
@@ -93,7 +93,7 @@ export function toApiByteVec(v: Val): string {
93
93
  try {
94
94
  const address = bs58.decode(v)
95
95
  if (address.length == 33 && address[0] == 3) {
96
- return Buffer.from(address.slice(1)).toString('hex')
96
+ return binToHex(address.slice(1))
97
97
  }
98
98
  } catch (_) {
99
99
  throw new Error(`Invalid hex-string: ${v}`)
@@ -15,7 +15,6 @@ GNU Lesser General Public License for more details.
15
15
  You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
- import { Buffer } from 'buffer/'
19
18
  import { Parser } from 'binary-parser'
20
19
  import { compactSignedIntCodec, compactUnsignedIntCodec, DecodedCompactInt } from './compact-int-codec'
21
20
  import { Codec } from './codec'
@@ -28,15 +27,15 @@ export interface DecodedArray<T> {
28
27
  export class ArrayCodec<T> implements Codec<T[]> {
29
28
  constructor(private childCodec: Codec<T>, public parser = ArrayCodec.arrayParser(childCodec.parser)) {}
30
29
 
31
- encode(input: T[]): Buffer {
30
+ encode(input: T[]): Uint8Array {
32
31
  const result = [...compactSignedIntCodec.encodeI256(BigInt(input.length))]
33
32
  for (const element of input) {
34
33
  result.push(...this.childCodec.encode(element))
35
34
  }
36
- return Buffer.from(result)
35
+ return new Uint8Array(result)
37
36
  }
38
37
 
39
- decode(input: Buffer): T[] {
38
+ decode(input: Uint8Array): T[] {
40
39
  const result = this.parser.parse(input)
41
40
  return result.value.map((v) => this.childCodec.decode(v.value))
42
41
  }