@alephium/web3 0.0.3

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 (59) hide show
  1. package/.gitattributes +1 -0
  2. package/LICENSE +165 -0
  3. package/README.md +136 -0
  4. package/contracts/add.ral +12 -0
  5. package/contracts/greeter-main.ral +8 -0
  6. package/contracts/greeter.ral +5 -0
  7. package/contracts/main.ral +8 -0
  8. package/contracts/sub.ral +9 -0
  9. package/dev/user.conf +24 -0
  10. package/dist/alephium-web3.min.js +3 -0
  11. package/dist/alephium-web3.min.js.LICENSE.txt +27 -0
  12. package/dist/alephium-web3.min.js.map +1 -0
  13. package/dist/api/api-alephium.d.ts +1292 -0
  14. package/dist/api/api-alephium.js +761 -0
  15. package/dist/api/api-explorer.d.ts +350 -0
  16. package/dist/api/api-explorer.js +297 -0
  17. package/dist/cli/create-project.d.ts +2 -0
  18. package/dist/cli/create-project.js +87 -0
  19. package/dist/lib/address.d.ts +1 -0
  20. package/dist/lib/address.js +42 -0
  21. package/dist/lib/bs58.d.ts +4 -0
  22. package/dist/lib/bs58.js +26 -0
  23. package/dist/lib/clique.d.ts +23 -0
  24. package/dist/lib/clique.js +149 -0
  25. package/dist/lib/constants.d.ts +2 -0
  26. package/dist/lib/constants.js +22 -0
  27. package/dist/lib/contract.d.ts +152 -0
  28. package/dist/lib/contract.js +711 -0
  29. package/dist/lib/djb2.d.ts +1 -0
  30. package/dist/lib/djb2.js +27 -0
  31. package/dist/lib/explorer.d.ts +8 -0
  32. package/dist/lib/explorer.js +46 -0
  33. package/dist/lib/index.d.ts +12 -0
  34. package/dist/lib/index.js +60 -0
  35. package/dist/lib/node.d.ts +10 -0
  36. package/dist/lib/node.js +64 -0
  37. package/dist/lib/numbers.d.ts +7 -0
  38. package/dist/lib/numbers.js +128 -0
  39. package/dist/lib/password-crypto.d.ts +2 -0
  40. package/dist/lib/password-crypto.js +68 -0
  41. package/dist/lib/signer.d.ts +17 -0
  42. package/dist/lib/signer.js +70 -0
  43. package/dist/lib/storage-browser.d.ts +9 -0
  44. package/dist/lib/storage-browser.js +52 -0
  45. package/dist/lib/storage-node.d.ts +9 -0
  46. package/dist/lib/storage-node.js +65 -0
  47. package/dist/lib/utils.d.ts +11 -0
  48. package/dist/lib/utils.js +135 -0
  49. package/dist/lib/wallet.d.ts +30 -0
  50. package/dist/lib/wallet.js +142 -0
  51. package/gitignore +9 -0
  52. package/package.json +113 -0
  53. package/scripts/check-versions.js +45 -0
  54. package/scripts/rename-gitignore.js +24 -0
  55. package/scripts/start-devnet.js +141 -0
  56. package/scripts/stop-devnet.js +32 -0
  57. package/templates/README.md +34 -0
  58. package/templates/package.json +29 -0
  59. package/webpack.config.js +57 -0
@@ -0,0 +1 @@
1
+ export default function djb2(bytes: Uint8Array): number;
@@ -0,0 +1,27 @@
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
+ function djb2(bytes) {
21
+ let hash = 5381;
22
+ for (let i = 0; i < bytes.length; i++) {
23
+ hash = (hash << 5) + hash + (bytes[i] & 0xff);
24
+ }
25
+ return hash;
26
+ }
27
+ exports.default = djb2;
@@ -0,0 +1,8 @@
1
+ import { Api } from '../api/api-explorer';
2
+ /**
3
+ * Explorer client
4
+ */
5
+ export declare class ExplorerClient extends Api<null> {
6
+ getAddressTransactions(address: string, page: number): Promise<import("../api/api-explorer").HttpResponse<import("../api/api-explorer").Transaction[], import("../api/api-explorer").BadRequest | import("../api/api-explorer").InternalServerError | import("../api/api-explorer").NotFound | import("../api/api-explorer").ServiceUnavailable | import("../api/api-explorer").Unauthorized>>;
7
+ getAddressDetails(address: string): Promise<import("../api/api-explorer").HttpResponse<import("../api/api-explorer").AddressInfo, import("../api/api-explorer").BadRequest | import("../api/api-explorer").InternalServerError | import("../api/api-explorer").NotFound | import("../api/api-explorer").ServiceUnavailable | import("../api/api-explorer").Unauthorized>>;
8
+ }
@@ -0,0 +1,46 @@
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
20
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
21
+ return new (P || (P = Promise))(function (resolve, reject) {
22
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
23
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
24
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
25
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
26
+ });
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.ExplorerClient = void 0;
30
+ const api_explorer_1 = require("../api/api-explorer");
31
+ /**
32
+ * Explorer client
33
+ */
34
+ class ExplorerClient extends api_explorer_1.Api {
35
+ getAddressTransactions(address, page) {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ return yield this.addresses.getAddressesAddressTransactions(address, { page });
38
+ });
39
+ }
40
+ getAddressDetails(address) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ return yield this.addresses.getAddressesAddress(address);
43
+ });
44
+ }
45
+ }
46
+ exports.ExplorerClient = ExplorerClient;
@@ -0,0 +1,12 @@
1
+ export * from './clique';
2
+ export * from './node';
3
+ export * from './utils';
4
+ export * from './wallet';
5
+ export * from './explorer';
6
+ export * from './address';
7
+ export * from './signer';
8
+ export * from './contract';
9
+ export { formatAmountForDisplay, calAmountDelta, convertAlphToSet, addApostrophes, convertSetToAlph, BILLION } from './numbers';
10
+ export * from './constants';
11
+ export * as node from '../api/api-alephium';
12
+ export * as explorer from '../api/api-explorer';
@@ -0,0 +1,60 @@
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
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
22
+ }) : (function(o, m, k, k2) {
23
+ if (k2 === undefined) k2 = k;
24
+ o[k2] = m[k];
25
+ }));
26
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
27
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
28
+ }) : function(o, v) {
29
+ o["default"] = v;
30
+ });
31
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
32
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
33
+ };
34
+ var __importStar = (this && this.__importStar) || function (mod) {
35
+ if (mod && mod.__esModule) return mod;
36
+ var result = {};
37
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
38
+ __setModuleDefault(result, mod);
39
+ return result;
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.explorer = exports.node = exports.BILLION = exports.convertSetToAlph = exports.addApostrophes = exports.convertAlphToSet = exports.calAmountDelta = exports.formatAmountForDisplay = void 0;
43
+ __exportStar(require("./clique"), exports);
44
+ __exportStar(require("./node"), exports);
45
+ __exportStar(require("./utils"), exports);
46
+ __exportStar(require("./wallet"), exports);
47
+ __exportStar(require("./explorer"), exports);
48
+ __exportStar(require("./address"), exports);
49
+ __exportStar(require("./signer"), exports);
50
+ __exportStar(require("./contract"), exports);
51
+ var numbers_1 = require("./numbers");
52
+ Object.defineProperty(exports, "formatAmountForDisplay", { enumerable: true, get: function () { return numbers_1.formatAmountForDisplay; } });
53
+ Object.defineProperty(exports, "calAmountDelta", { enumerable: true, get: function () { return numbers_1.calAmountDelta; } });
54
+ Object.defineProperty(exports, "convertAlphToSet", { enumerable: true, get: function () { return numbers_1.convertAlphToSet; } });
55
+ Object.defineProperty(exports, "addApostrophes", { enumerable: true, get: function () { return numbers_1.addApostrophes; } });
56
+ Object.defineProperty(exports, "convertSetToAlph", { enumerable: true, get: function () { return numbers_1.convertSetToAlph; } });
57
+ Object.defineProperty(exports, "BILLION", { enumerable: true, get: function () { return numbers_1.BILLION; } });
58
+ __exportStar(require("./constants"), exports);
59
+ exports.node = __importStar(require("../api/api-alephium"));
60
+ exports.explorer = __importStar(require("../api/api-explorer"));
@@ -0,0 +1,10 @@
1
+ import { Api } from '../api/api-alephium';
2
+ /**
3
+ * Node client
4
+ */
5
+ export declare class NodeClient extends Api<null> {
6
+ getBalance(address: string): Promise<import("../api/api-alephium").HttpResponse<import("../api/api-alephium").Balance, import("../api/api-alephium").BadRequest | import("../api/api-alephium").InternalServerError | import("../api/api-alephium").NotFound | import("../api/api-alephium").ServiceUnavailable | import("../api/api-alephium").Unauthorized>>;
7
+ transactionCreate(fromPublicKey: string, toAddress: string, amount: string, lockTime?: number, gas?: number, gasPrice?: string): Promise<import("../api/api-alephium").HttpResponse<import("../api/api-alephium").BuildTransactionResult, import("../api/api-alephium").BadRequest | import("../api/api-alephium").InternalServerError | import("../api/api-alephium").NotFound | import("../api/api-alephium").ServiceUnavailable | import("../api/api-alephium").Unauthorized>>;
8
+ transactionConsolidateUTXOs(fromPublicKey: string, toAddress: string): Promise<import("../api/api-alephium").HttpResponse<import("../api/api-alephium").BuildSweepAddressTransactionsResult, import("../api/api-alephium").BadRequest | import("../api/api-alephium").InternalServerError | import("../api/api-alephium").NotFound | import("../api/api-alephium").ServiceUnavailable | import("../api/api-alephium").Unauthorized>>;
9
+ transactionSend(tx: string, signature: string): Promise<import("../api/api-alephium").HttpResponse<import("../api/api-alephium").TxResult, import("../api/api-alephium").BadRequest | import("../api/api-alephium").InternalServerError | import("../api/api-alephium").NotFound | import("../api/api-alephium").ServiceUnavailable | import("../api/api-alephium").Unauthorized>>;
10
+ }
@@ -0,0 +1,64 @@
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
20
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
21
+ return new (P || (P = Promise))(function (resolve, reject) {
22
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
23
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
24
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
25
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
26
+ });
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.NodeClient = void 0;
30
+ const api_alephium_1 = require("../api/api-alephium");
31
+ /**
32
+ * Node client
33
+ */
34
+ class NodeClient extends api_alephium_1.Api {
35
+ getBalance(address) {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ return yield this.addresses.getAddressesAddressBalance(address);
38
+ });
39
+ }
40
+ transactionCreate(fromPublicKey, toAddress, amount, lockTime, gas, gasPrice) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ return yield this.transactions.postTransactionsBuild({
43
+ fromPublicKey,
44
+ destinations: [{ address: toAddress, alphAmount: amount, lockTime }],
45
+ gas,
46
+ gasPrice
47
+ });
48
+ });
49
+ }
50
+ transactionConsolidateUTXOs(fromPublicKey, toAddress) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ return yield this.transactions.postTransactionsSweepAddressBuild({
53
+ fromPublicKey,
54
+ toAddress
55
+ });
56
+ });
57
+ }
58
+ transactionSend(tx, signature) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ return yield this.transactions.postTransactionsSubmit({ unsignedTx: tx, signature });
61
+ });
62
+ }
63
+ }
64
+ exports.NodeClient = NodeClient;
@@ -0,0 +1,7 @@
1
+ import { Transaction } from '../api/api-explorer';
2
+ export declare const BILLION = 1000000000;
3
+ export declare const formatAmountForDisplay: (baseNum: bigint, showFullPrecision?: boolean, nbOfDecimalsToShow?: number | undefined) => string;
4
+ export declare const calAmountDelta: (t: Transaction, id: string) => bigint;
5
+ export declare const convertAlphToSet: (amount: string) => bigint;
6
+ export declare const addApostrophes: (numString: string) => string;
7
+ export declare const convertSetToAlph: (amountInSet: bigint) => string;
@@ -0,0 +1,128 @@
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.convertSetToAlph = exports.addApostrophes = exports.convertAlphToSet = exports.calAmountDelta = exports.formatAmountForDisplay = exports.BILLION = void 0;
21
+ const MONEY_SYMBOL = ['', 'K', 'M', 'B', 'T'];
22
+ const QUINTILLION = 1000000000000000000;
23
+ const NUM_OF_ZEROS_IN_QUINTILLION = 18;
24
+ exports.BILLION = 1000000000;
25
+ const produceZeros = (numberOfZeros) => '0'.repeat(numberOfZeros);
26
+ const getNumberOfTrailingZeros = (numString) => {
27
+ let numberOfZeros = 0;
28
+ for (let i = numString.length - 1; i >= 0; i--) {
29
+ if (numString[i] === '0') {
30
+ numberOfZeros++;
31
+ }
32
+ else {
33
+ break;
34
+ }
35
+ }
36
+ return numberOfZeros;
37
+ };
38
+ const removeTrailingZeros = (numString, minNumberOfDecimals) => {
39
+ const numberOfZeros = getNumberOfTrailingZeros(numString);
40
+ const numStringWithoutTrailingZeros = numString.substring(0, numString.length - numberOfZeros);
41
+ if (!minNumberOfDecimals)
42
+ return numStringWithoutTrailingZeros.endsWith('.')
43
+ ? numStringWithoutTrailingZeros.slice(0, -1)
44
+ : numStringWithoutTrailingZeros;
45
+ if (minNumberOfDecimals < 0)
46
+ throw 'minNumberOfDecimals should be positive';
47
+ const indexOfPoint = numStringWithoutTrailingZeros.indexOf('.');
48
+ if (indexOfPoint === -1)
49
+ throw 'numString should contain decimal point';
50
+ const numberOfDecimals = numStringWithoutTrailingZeros.length - 1 - indexOfPoint;
51
+ return numberOfDecimals < minNumberOfDecimals
52
+ ? numStringWithoutTrailingZeros.concat(produceZeros(minNumberOfDecimals - numberOfDecimals))
53
+ : numStringWithoutTrailingZeros;
54
+ };
55
+ const formatAmountForDisplay = (baseNum, showFullPrecision = false, nbOfDecimalsToShow) => {
56
+ if (baseNum < BigInt(0))
57
+ return '???';
58
+ // For abbreviation, we don't need full precision and can work with number
59
+ const alphNum = Number(baseNum) / QUINTILLION;
60
+ const minNumberOfDecimals = alphNum >= 0.000005 && alphNum < 0.01 ? 3 : 2;
61
+ const numberOfDecimalsToDisplay = nbOfDecimalsToShow || minNumberOfDecimals;
62
+ if (showFullPrecision) {
63
+ const baseNumString = baseNum.toString();
64
+ const numNonDecimals = baseNumString.length - NUM_OF_ZEROS_IN_QUINTILLION;
65
+ const alphNumString = numNonDecimals > 0
66
+ ? baseNumString.substring(0, numNonDecimals).concat('.', baseNumString.substring(numNonDecimals))
67
+ : '0.'.concat(produceZeros(-numNonDecimals), baseNumString);
68
+ return removeTrailingZeros(alphNumString, numberOfDecimalsToDisplay);
69
+ }
70
+ if (alphNum < 0.001) {
71
+ const tinyAmountsMaxNumberDecimals = 5;
72
+ return removeTrailingZeros(alphNum.toFixed(tinyAmountsMaxNumberDecimals), minNumberOfDecimals);
73
+ }
74
+ else if (alphNum <= 1000000) {
75
+ return (0, exports.addApostrophes)(removeTrailingZeros(alphNum.toFixed(numberOfDecimalsToDisplay), minNumberOfDecimals));
76
+ }
77
+ const tier = alphNum < 1000000000 ? 2 : alphNum < 1000000000000 ? 3 : 4;
78
+ // get suffix and determine scale
79
+ const suffix = MONEY_SYMBOL[tier];
80
+ const scale = Math.pow(10, tier * 3);
81
+ // Scale the bigNum
82
+ // Here we need to be careful of precision issues
83
+ const scaled = alphNum / scale;
84
+ return scaled.toFixed(numberOfDecimalsToDisplay) + suffix;
85
+ };
86
+ exports.formatAmountForDisplay = formatAmountForDisplay;
87
+ const calAmountDelta = (t, id) => {
88
+ if (!t.inputs || !t.outputs) {
89
+ throw 'Missing transaction details';
90
+ }
91
+ const inputAmount = t.inputs.reduce((acc, input) => {
92
+ return input.amount && input.address === id ? acc + BigInt(input.amount) : acc;
93
+ }, BigInt(0));
94
+ const outputAmount = t.outputs.reduce((acc, output) => {
95
+ return output.address === id ? acc + BigInt(output.amount) : acc;
96
+ }, BigInt(0));
97
+ return outputAmount - inputAmount;
98
+ };
99
+ exports.calAmountDelta = calAmountDelta;
100
+ const convertAlphToSet = (amount) => {
101
+ if (!isNumber(amount) || Number(amount) < 0)
102
+ throw 'Invalid Alph amount';
103
+ if (amount === '0')
104
+ return BigInt(0);
105
+ const numberOfDecimals = amount.includes('.') ? amount.length - 1 - amount.indexOf('.') : 0;
106
+ const numberOfZerosToAdd = NUM_OF_ZEROS_IN_QUINTILLION - numberOfDecimals;
107
+ const cleanedAmount = amount.replace('.', '') + produceZeros(numberOfZerosToAdd);
108
+ return BigInt(cleanedAmount);
109
+ };
110
+ exports.convertAlphToSet = convertAlphToSet;
111
+ const addApostrophes = (numString) => {
112
+ if (!isNumber(numString))
113
+ throw 'Invalid number';
114
+ return numString.replace(/\B(?=(\d{3})+(?!\d))/g, "'");
115
+ };
116
+ exports.addApostrophes = addApostrophes;
117
+ const convertSetToAlph = (amountInSet) => {
118
+ const amountInSetStr = amountInSet.toString();
119
+ if (amountInSetStr === '0')
120
+ return amountInSetStr;
121
+ const positionForDot = amountInSetStr.length - NUM_OF_ZEROS_IN_QUINTILLION;
122
+ const withDotAdded = positionForDot > 0
123
+ ? amountInSetStr.substring(0, positionForDot) + '.' + amountInSetStr.substring(positionForDot)
124
+ : '0.' + produceZeros(NUM_OF_ZEROS_IN_QUINTILLION - amountInSetStr.length) + amountInSetStr;
125
+ return removeTrailingZeros(withDotAdded);
126
+ };
127
+ exports.convertSetToAlph = convertSetToAlph;
128
+ const isNumber = (numString) => !Number.isNaN(Number(numString)) && numString.length > 0 && !numString.includes('e');
@@ -0,0 +1,2 @@
1
+ export declare const encrypt: (password: string, dataRaw: string) => string;
2
+ export declare const decrypt: (password: string, payloadRaw: string) => string;
@@ -0,0 +1,68 @@
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.decrypt = exports.encrypt = void 0;
21
+ const crypto_1 = require("crypto");
22
+ const saltByteLength = 64;
23
+ const ivByteLength = 64;
24
+ const authTagLength = 16;
25
+ const encrypt = (password, dataRaw) => {
26
+ const data = Buffer.from(dataRaw, 'utf8');
27
+ const salt = (0, crypto_1.randomBytes)(saltByteLength);
28
+ const derivedKey = keyFromPassword(password, salt);
29
+ const iv = (0, crypto_1.randomBytes)(ivByteLength);
30
+ const cipher = createCipher(derivedKey, iv);
31
+ const encrypted = Buffer.concat([cipher.update(data), cipher.final()]);
32
+ const authTag = cipher.getAuthTag();
33
+ const payload = {
34
+ salt: salt.toString('hex'),
35
+ iv: iv.toString('hex'),
36
+ encrypted: Buffer.concat([encrypted, authTag]).toString('hex'),
37
+ version: 1
38
+ };
39
+ return JSON.stringify(payload);
40
+ };
41
+ exports.encrypt = encrypt;
42
+ const decrypt = (password, payloadRaw) => {
43
+ const payload = JSON.parse(payloadRaw);
44
+ const version = payload.version;
45
+ if (version !== 1) {
46
+ throw new Error(`Invalid version: got ${version}, expected: 1`);
47
+ }
48
+ const salt = Buffer.from(payload.salt, 'hex');
49
+ const iv = Buffer.from(payload.iv, 'hex');
50
+ const encrypted = Buffer.from(payload.encrypted, 'hex');
51
+ const derivedKey = keyFromPassword(password, salt);
52
+ const decipher = createDecipher(derivedKey, iv);
53
+ const data = encrypted.slice(0, encrypted.length - authTagLength);
54
+ const authTag = encrypted.slice(encrypted.length - authTagLength, encrypted.length);
55
+ decipher.setAuthTag(authTag);
56
+ const decrypted = Buffer.concat([decipher.update(data), decipher.final()]);
57
+ return decrypted.toString('utf8');
58
+ };
59
+ exports.decrypt = decrypt;
60
+ const createCipher = (key, iv) => {
61
+ return (0, crypto_1.createCipheriv)('aes-256-gcm', key, iv);
62
+ };
63
+ const createDecipher = (key, iv) => {
64
+ return (0, crypto_1.createDecipheriv)('aes-256-gcm', key, iv);
65
+ };
66
+ const keyFromPassword = (password, salt) => {
67
+ return (0, crypto_1.pbkdf2Sync)(password, salt, 10000, 32, 'sha256');
68
+ };
@@ -0,0 +1,17 @@
1
+ import { CliqueClient } from './clique';
2
+ export declare class Signer {
3
+ client: CliqueClient;
4
+ walletName: string;
5
+ address: string;
6
+ private _publicKey?;
7
+ constructor(client: CliqueClient, walletName: string, address: string);
8
+ static testSigner(client: CliqueClient): Signer;
9
+ getPublicKey(): Promise<string>;
10
+ sign(hash: string): Promise<string>;
11
+ submitTransaction(unsignedTx: string, txHash: string): Promise<SubmissionResult>;
12
+ }
13
+ export interface SubmissionResult {
14
+ txId: string;
15
+ fromGroup: number;
16
+ toGroup: number;
17
+ }
@@ -0,0 +1,70 @@
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
20
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
21
+ return new (P || (P = Promise))(function (resolve, reject) {
22
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
23
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
24
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
25
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
26
+ });
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.Signer = void 0;
30
+ const clique_1 = require("./clique");
31
+ class Signer {
32
+ constructor(client, walletName, address) {
33
+ this.client = client;
34
+ this.walletName = walletName;
35
+ this.address = address;
36
+ }
37
+ static testSigner(client) {
38
+ return new Signer(client, 'alephium-js-sdk-test-only-wallet', '12LgGdbjE6EtnTKw5gdBwV2RRXuXPtzYM7SDZ45YJTRht');
39
+ }
40
+ getPublicKey() {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ if (this._publicKey) {
43
+ return this._publicKey;
44
+ }
45
+ else {
46
+ const response = yield this.client.wallets.getWalletsWalletNameAddressesAddress(this.walletName, this.address);
47
+ this._publicKey = clique_1.CliqueClient.convert(response).publicKey;
48
+ return this._publicKey;
49
+ }
50
+ });
51
+ }
52
+ sign(hash) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const response = yield this.client.wallets.postWalletsWalletNameSign(this.walletName, { data: hash });
55
+ return clique_1.CliqueClient.convert(response).signature;
56
+ });
57
+ }
58
+ submitTransaction(unsignedTx, txHash) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ const signature = yield this.sign(txHash);
61
+ const params = { unsignedTx: unsignedTx, signature: signature };
62
+ const response = yield this.client.transactions.postTransactionsSubmit(params);
63
+ return fromApiSubmissionResult(clique_1.CliqueClient.convert(response));
64
+ });
65
+ }
66
+ }
67
+ exports.Signer = Signer;
68
+ function fromApiSubmissionResult(result) {
69
+ return result;
70
+ }
@@ -0,0 +1,9 @@
1
+ declare class BrowserStorage {
2
+ key: string;
3
+ constructor();
4
+ remove: (name: string) => void;
5
+ load: (name: string) => any;
6
+ save: (name: string, json: unknown) => void;
7
+ list: () => string[];
8
+ }
9
+ export default BrowserStorage;
@@ -0,0 +1,52 @@
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
+ class BrowserStorage {
21
+ constructor() {
22
+ this.remove = (name) => {
23
+ window.localStorage.removeItem(`${this.key}-${name}`);
24
+ };
25
+ this.load = (name) => {
26
+ const str = window.localStorage.getItem(`${this.key}-${name}`);
27
+ if (str) {
28
+ return JSON.parse(str);
29
+ }
30
+ else {
31
+ throw new Error(`Unable to load wallet ${name}`);
32
+ }
33
+ };
34
+ this.save = (name, json) => {
35
+ const str = JSON.stringify(json);
36
+ window.localStorage.setItem(`${this.key}-${name}`, str);
37
+ };
38
+ this.list = () => {
39
+ const prefixLen = this.key.length + 1;
40
+ const xs = [];
41
+ for (let i = 0, len = localStorage.length; i < len; ++i) {
42
+ const key = localStorage.key(i);
43
+ if (key && key.startsWith(this.key)) {
44
+ xs.push(key.substring(prefixLen));
45
+ }
46
+ }
47
+ return xs;
48
+ };
49
+ this.key = 'wallet';
50
+ }
51
+ }
52
+ exports.default = BrowserStorage;
@@ -0,0 +1,9 @@
1
+ declare class NodeStorage {
2
+ walletsUrl: string;
3
+ constructor();
4
+ remove: (name: string) => void;
5
+ load: (name: string) => any;
6
+ save: (name: string, json: unknown) => void;
7
+ list: () => string[];
8
+ }
9
+ export default NodeStorage;