@alephium/web3 0.5.0-rc.2 → 0.5.0-rc.20

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 (51) hide show
  1. package/dist/alephium-web3.min.js +1 -1
  2. package/dist/alephium-web3.min.js.LICENSE.txt +2 -0
  3. package/dist/alephium-web3.min.js.map +1 -1
  4. package/dist/src/api/api-alephium.d.ts +113 -26
  5. package/dist/src/api/api-alephium.js +62 -18
  6. package/dist/src/api/api-explorer.d.ts +222 -52
  7. package/dist/src/api/api-explorer.js +17 -15
  8. package/dist/src/api/explorer-provider.d.ts +18 -0
  9. package/dist/src/api/explorer-provider.js +65 -0
  10. package/dist/src/api/index.d.ts +2 -41
  11. package/dist/src/api/index.js +6 -116
  12. package/dist/src/api/node-provider.d.ts +21 -0
  13. package/dist/src/api/node-provider.js +68 -0
  14. package/dist/src/api/types.d.ts +10 -2
  15. package/dist/src/api/types.js +23 -3
  16. package/dist/src/contract/contract.d.ts +15 -7
  17. package/dist/src/contract/contract.js +62 -42
  18. package/dist/src/signer/signer.d.ts +25 -30
  19. package/dist/src/signer/signer.js +57 -30
  20. package/dist/src/signer/tx-builder.d.ts +2 -7
  21. package/dist/src/signer/tx-builder.js +10 -7
  22. package/dist/src/signer/types.d.ts +12 -16
  23. package/dist/src/transaction/sign-verify.d.ts +3 -2
  24. package/dist/src/transaction/sign-verify.js +4 -14
  25. package/dist/src/utils/index.d.ts +2 -0
  26. package/dist/src/utils/index.js +2 -0
  27. package/dist/src/utils/number.d.ts +17 -0
  28. package/dist/src/utils/number.fixture.d.ts +12 -0
  29. package/dist/src/utils/number.fixture.js +189 -0
  30. package/dist/src/utils/number.js +144 -0
  31. package/dist/src/utils/sign.d.ts +3 -0
  32. package/dist/src/utils/sign.js +89 -0
  33. package/dist/src/utils/utils.d.ts +4 -3
  34. package/dist/src/utils/utils.js +25 -10
  35. package/package.json +7 -5
  36. package/src/api/api-alephium.ts +260 -207
  37. package/src/api/api-explorer.ts +327 -126
  38. package/src/api/explorer-provider.ts +78 -0
  39. package/src/api/index.ts +2 -146
  40. package/src/api/node-provider.ts +84 -0
  41. package/src/api/types.ts +36 -3
  42. package/src/contract/contract.ts +80 -49
  43. package/src/signer/signer.ts +87 -66
  44. package/src/signer/tx-builder.ts +13 -7
  45. package/src/signer/types.ts +22 -11
  46. package/src/transaction/sign-verify.ts +10 -15
  47. package/src/utils/index.ts +2 -0
  48. package/src/utils/number.fixture.ts +187 -0
  49. package/src/utils/number.ts +158 -0
  50. package/src/utils/sign.ts +66 -0
  51. package/src/utils/utils.ts +26 -10
@@ -0,0 +1,144 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
20
+ return (mod && mod.__esModule) ? mod : { "default": mod };
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.convertAlphAmount = exports.convertAmountWithDecimals = exports.prettifyNumber = exports.prettifyExactAmount = exports.prettifyTokenAmount = exports.prettifyAttoAlphAmount = exports.prettifyNumberConfig = exports.isNumeric = void 0;
24
+ // Credits:
25
+ // 1. https://github.com/argentlabs/argent-x/blob/e63affa7f28b27333dca4081a3dcd375bb2da40b/packages/extension/src/shared/utils/number.ts
26
+ // 2. https://github.com/ethers-io/ethers.js/blob/724881f34d428406488a1c9f9dbebe54b6edecda/src.ts/utils/fixednumber.ts
27
+ const bignumber_js_1 = __importDefault(require("bignumber.js"));
28
+ const isNumeric = (numToCheck) => !isNaN(parseFloat(numToCheck)) && isFinite(numToCheck);
29
+ exports.isNumeric = isNumeric;
30
+ exports.prettifyNumberConfig = {
31
+ ALPH: {
32
+ minDecimalPlaces: 2,
33
+ maxDecimalPlaces: 10,
34
+ minDecimalSignificantDigits: 2,
35
+ decimalPlacesWhenZero: 2
36
+ },
37
+ TOKEN: {
38
+ minDecimalPlaces: 4,
39
+ maxDecimalPlaces: 16,
40
+ minDecimalSignificantDigits: 2,
41
+ decimalPlacesWhenZero: 1
42
+ },
43
+ Exact: {
44
+ minDecimalPlaces: 18,
45
+ maxDecimalPlaces: 18,
46
+ minDecimalSignificantDigits: 0,
47
+ decimalPlacesWhenZero: 0
48
+ }
49
+ };
50
+ function prettifyAttoAlphAmount(amount) {
51
+ return prettifyNumber(amount, 18, exports.prettifyNumberConfig.ALPH);
52
+ }
53
+ exports.prettifyAttoAlphAmount = prettifyAttoAlphAmount;
54
+ function prettifyTokenAmount(amount, decimals) {
55
+ return prettifyNumber(amount, decimals, exports.prettifyNumberConfig.TOKEN);
56
+ }
57
+ exports.prettifyTokenAmount = prettifyTokenAmount;
58
+ function prettifyExactAmount(amount, decimals) {
59
+ return prettifyNumber(amount, decimals, exports.prettifyNumberConfig.Exact);
60
+ }
61
+ exports.prettifyExactAmount = prettifyExactAmount;
62
+ function prettifyNumber(amount, decimals, config) {
63
+ const number = typeof amount === 'string' ? amount : toFixedNumber(amount, decimals);
64
+ if (!(0, exports.isNumeric)(number)) {
65
+ return undefined;
66
+ }
67
+ const numberBN = new bignumber_js_1.default(number);
68
+ let untrimmed;
69
+ if (numberBN.gte(1)) {
70
+ /** simplest case, formatting to minDecimalPlaces will look good */
71
+ untrimmed = numberBN.toFormat(config.minDecimalPlaces);
72
+ }
73
+ else {
74
+ /** now need to interrogate the appearance of decimal number < 1 */
75
+ /** longest case - max decimal places e.g. 0.0008923088123 -> 0.0008923088 */
76
+ const maxDecimalPlacesString = numberBN.toFormat(config.maxDecimalPlaces);
77
+ /** count the zeros, which will then allow us to know the final length with desired significant digits */
78
+ const decimalPart = maxDecimalPlacesString.split('.')[1];
79
+ const zeroMatches = decimalPart?.match(/^0+/);
80
+ const leadingZerosInDecimalPart = zeroMatches && zeroMatches.length ? zeroMatches[0].length : 0;
81
+ /** now we can re-format with leadingZerosInDecimalPart + maxDecimalSignificanDigits to give us the pretty version */
82
+ /** e.g. 0.0008923088123 -> 0.00089 */
83
+ const prettyDecimalPlaces = Math.max(leadingZerosInDecimalPart + config.minDecimalSignificantDigits, config.minDecimalPlaces);
84
+ untrimmed = numberBN.toFormat(prettyDecimalPlaces);
85
+ }
86
+ /** the untrimmed string may have trailing zeros, e.g. 0.0890 */
87
+ /** trim to a minimum specified by the config, e.g. we may want to display $0.00 or 0.0 ETH */
88
+ let trimmed = untrimmed.replace(/0+$/, '');
89
+ const minLength = 1 + untrimmed.indexOf('.') + config.decimalPlacesWhenZero;
90
+ if (trimmed.length < minLength) {
91
+ trimmed = untrimmed.substring(0, minLength);
92
+ }
93
+ if (trimmed[trimmed.length - 1] === '.') {
94
+ trimmed = trimmed.slice(0, -1);
95
+ }
96
+ return trimmed;
97
+ }
98
+ exports.prettifyNumber = prettifyNumber;
99
+ const BN_N1 = BigInt(-1);
100
+ const BN_0 = BigInt(0);
101
+ // Constant to pull zeros from for multipliers
102
+ const Zeros = '0000';
103
+ function toFixedNumber(val, decimals) {
104
+ let negative = '';
105
+ if (val < BN_0) {
106
+ negative = '-';
107
+ val *= BN_N1;
108
+ }
109
+ let str = val.toString();
110
+ // No decimal point for whole values
111
+ if (decimals === 0) {
112
+ return negative + str;
113
+ }
114
+ // Pad out to the whole component (including a whole digit)
115
+ while (str.length <= decimals) {
116
+ str = Zeros + str;
117
+ }
118
+ // Insert the decimal point
119
+ const index = str.length - decimals;
120
+ str = str.substring(0, index) + '.' + str.substring(index);
121
+ // Trim the whole component (leaving at least one 0)
122
+ while (str[0] === '0' && str[1] !== '.') {
123
+ str = str.substring(1);
124
+ }
125
+ // Trim the decimal component (leaving at least one 0)
126
+ while (str[str.length - 1] === '0' && str[str.length - 2] !== '.') {
127
+ str = str.substring(0, str.length - 1);
128
+ }
129
+ return negative + str;
130
+ }
131
+ function convertAmountWithDecimals(amount, decimals) {
132
+ try {
133
+ const result = new bignumber_js_1.default(amount).multipliedBy(Math.pow(10, decimals));
134
+ return BigInt(result.toFormat(0, { groupSeparator: '' }));
135
+ }
136
+ catch (e) {
137
+ return undefined;
138
+ }
139
+ }
140
+ exports.convertAmountWithDecimals = convertAmountWithDecimals;
141
+ function convertAlphAmount(amount) {
142
+ return convertAmountWithDecimals(amount, 18);
143
+ }
144
+ exports.convertAlphAmount = convertAlphAmount;
@@ -0,0 +1,3 @@
1
+ import { KeyType } from '../signer';
2
+ export declare function sign(hash: string, privateKey: string, _keyType?: KeyType): string;
3
+ export declare function verifySignature(hash: string, publicKey: string, signature: string, _keyType?: KeyType): boolean;
@@ -0,0 +1,89 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
31
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
32
+ }) : function(o, v) {
33
+ o["default"] = v;
34
+ });
35
+ var __importStar = (this && this.__importStar) || function (mod) {
36
+ if (mod && mod.__esModule) return mod;
37
+ var result = {};
38
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
39
+ __setModuleDefault(result, mod);
40
+ return result;
41
+ };
42
+ Object.defineProperty(exports, "__esModule", { value: true });
43
+ exports.verifySignature = exports.sign = void 0;
44
+ const elliptic_1 = require("elliptic");
45
+ const __1 = require("..");
46
+ const necc = __importStar(require("@noble/secp256k1"));
47
+ const crypto_1 = require("crypto");
48
+ const ec = new elliptic_1.ec('secp256k1');
49
+ necc.utils.sha256Sync = (...messages) => {
50
+ const sha256 = (0, crypto_1.createHash)('sha256');
51
+ for (const message of messages)
52
+ sha256.update(message);
53
+ return sha256.digest();
54
+ };
55
+ necc.utils.hmacSha256Sync = (key, ...messages) => {
56
+ const hash = (0, crypto_1.createHmac)('sha256', Buffer.from(key));
57
+ messages.forEach((m) => hash.update(m));
58
+ return Uint8Array.from(hash.digest());
59
+ };
60
+ // hash has to be 32 bytes
61
+ function sign(hash, privateKey, _keyType) {
62
+ const keyType = _keyType ?? 'default';
63
+ if (keyType === 'default') {
64
+ const key = ec.keyFromPrivate(privateKey);
65
+ const signature = key.sign(hash);
66
+ return (0, __1.encodeSignature)(signature);
67
+ }
68
+ else {
69
+ const signature = necc.schnorr.signSync((0, __1.hexToBinUnsafe)(hash), (0, __1.hexToBinUnsafe)(privateKey));
70
+ return (0, __1.binToHex)(signature);
71
+ }
72
+ }
73
+ exports.sign = sign;
74
+ function verifySignature(hash, publicKey, signature, _keyType) {
75
+ const keyType = _keyType ?? 'default';
76
+ try {
77
+ if (keyType === 'default') {
78
+ const key = ec.keyFromPublic(publicKey, 'hex');
79
+ return key.verify(hash, (0, __1.signatureDecode)(ec, signature));
80
+ }
81
+ else {
82
+ return necc.schnorr.verifySync((0, __1.hexToBinUnsafe)(signature), (0, __1.hexToBinUnsafe)(hash), (0, __1.hexToBinUnsafe)(publicKey));
83
+ }
84
+ }
85
+ catch (error) {
86
+ return false;
87
+ }
88
+ }
89
+ exports.verifySignature = verifySignature;
@@ -1,5 +1,6 @@
1
1
  import { ec as EC, SignatureInput } from 'elliptic';
2
2
  import BN from 'bn.js';
3
+ import { KeyType } from '../signer';
3
4
  export declare function encodeSignature(signature: EC.Signature | {
4
5
  r: BN;
5
6
  s: BN;
@@ -13,9 +14,9 @@ export declare function contractIdFromAddress(address: string): Uint8Array;
13
14
  export declare function tokenIdFromAddress(address: string): Uint8Array;
14
15
  export declare function hexToBinUnsafe(hex: string): Uint8Array;
15
16
  export declare function binToHex(bin: Uint8Array): string;
16
- export declare function groupOfPrivateKey(privateKey: string): number;
17
- export declare function publicKeyFromPrivateKey(privateKey: string): string;
18
- export declare function addressFromPublicKey(publicKey: string): string;
17
+ export declare function groupOfPrivateKey(privateKey: string, keyType?: KeyType): number;
18
+ export declare function publicKeyFromPrivateKey(privateKey: string, _keyType?: KeyType): string;
19
+ export declare function addressFromPublicKey(publicKey: string, _keyType?: KeyType): string;
19
20
  export declare function addressFromContractId(contractId: string): string;
20
21
  export declare function contractIdFromTx(txId: string, outputIndex: number): string;
21
22
  export declare function subContractId(parentContractId: string, pathInHex: string, group: number): string;
@@ -153,20 +153,35 @@ function binToHex(bin) {
153
153
  return buffer_1.Buffer.from(bin).toString('hex');
154
154
  }
155
155
  exports.binToHex = binToHex;
156
- function groupOfPrivateKey(privateKey) {
157
- return groupOfAddress(addressFromPublicKey(publicKeyFromPrivateKey(privateKey)));
156
+ function groupOfPrivateKey(privateKey, keyType) {
157
+ return groupOfAddress(addressFromPublicKey(publicKeyFromPrivateKey(privateKey, keyType), keyType));
158
158
  }
159
159
  exports.groupOfPrivateKey = groupOfPrivateKey;
160
- function publicKeyFromPrivateKey(privateKey) {
161
- const key = ec.keyFromPrivate(privateKey);
162
- return key.getPublic(true, 'hex');
160
+ function publicKeyFromPrivateKey(privateKey, _keyType) {
161
+ const keyType = _keyType ?? 'default';
162
+ if (keyType === 'default') {
163
+ const key = ec.keyFromPrivate(privateKey);
164
+ return key.getPublic(true, 'hex');
165
+ }
166
+ else {
167
+ return ec.g.mul(new bn_js_1.default(privateKey, 16)).encode('hex', true).slice(2);
168
+ }
163
169
  }
164
170
  exports.publicKeyFromPrivateKey = publicKeyFromPrivateKey;
165
- function addressFromPublicKey(publicKey) {
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]);
169
- return bs58_1.default.encode(bytes);
171
+ function addressFromPublicKey(publicKey, _keyType) {
172
+ const keyType = _keyType ?? 'default';
173
+ if (keyType === 'default') {
174
+ const addressType = buffer_1.Buffer.from([AddressType.P2PKH]);
175
+ const hash = buffer_1.Buffer.from(blakejs_1.default.blake2b(buffer_1.Buffer.from(publicKey, 'hex'), undefined, 32));
176
+ const bytes = buffer_1.Buffer.concat([addressType, hash]);
177
+ return bs58_1.default.encode(bytes);
178
+ }
179
+ else {
180
+ const lockupScript = buffer_1.Buffer.from(`0101000000000458144020${publicKey}8685`, 'hex');
181
+ const lockupScriptHash = blakejs_1.default.blake2b(lockupScript, undefined, 32);
182
+ const addressType = buffer_1.Buffer.from([AddressType.P2SH]);
183
+ return bs58_1.default.encode(buffer_1.Buffer.concat([addressType, lockupScriptHash]));
184
+ }
170
185
  }
171
186
  exports.addressFromPublicKey = addressFromPublicKey;
172
187
  function addressFromContractId(contractId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.5.0-rc.2",
3
+ "version": "0.5.0-rc.20",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -27,8 +27,8 @@
27
27
  },
28
28
  "author": "Alephium dev <dev@alephium.org>",
29
29
  "config": {
30
- "alephium_version": "1.7.0",
31
- "explorer_backend_version": "1.12.0"
30
+ "alephium_version": "1.7.1",
31
+ "explorer_backend_version": "1.13.0"
32
32
  },
33
33
  "scripts": {
34
34
  "build": "rm -rf dist/* && npx tsc --build . && webpack",
@@ -40,12 +40,14 @@
40
40
  },
41
41
  "type": "commonjs",
42
42
  "dependencies": {
43
+ "@noble/secp256k1": "1.7.1",
43
44
  "base-x": "4.0.0",
44
45
  "blakejs": "1.2.1",
45
46
  "buffer": "^6.0.3",
46
47
  "cross-fetch": "^3.1.5",
47
48
  "elliptic": "6.5.4",
48
- "eventemitter3": "^4.0.7"
49
+ "eventemitter3": "^4.0.7",
50
+ "bignumber.js": "^9.0.2"
49
51
  },
50
52
  "devDependencies": {
51
53
  "@babel/eslint-parser": "^7.18.9",
@@ -77,7 +79,7 @@
77
79
  "rewire": "^6.0.0",
78
80
  "shelljs": "^0.8.5",
79
81
  "stream-browserify": "^3.0.0",
80
- "swagger-typescript-api": "^9.2.0",
82
+ "swagger-typescript-api": "12.0.3",
81
83
  "terser-webpack-plugin": "^5.3.1",
82
84
  "ts-jest": "^28.0.2",
83
85
  "ts-node": "^10.7.0",