ethershell 0.1.0-alpha.0 → 0.1.0-alpha.11

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.
@@ -1,213 +1,213 @@
1
- /**
2
- * @fileoverview Account management utilities
3
- * @description Internal utilities for managing account arrays, including deletion
4
- * and information retrieval. Handles synchronization across multiple account arrays.
5
- * @module accounter
6
- */
7
-
8
- import { allAccounts, accounts, hdAccounts } from '../services/wallet.js';
9
- import { provider } from '../services/network.js';
10
-
11
- /**
12
- * Delete account(s) by index
13
- * @param {number|Array<number>|null} index - Account index, array of indices, or null to delete all
14
- * @returns {void}
15
- * @example
16
- * deleteByIndex(0); // Delete account at index 0
17
- * deleteByIndex([1, 3, 5]); // Delete multiple accounts
18
- * deleteByIndex(null); // Delete all accounts
19
- */
20
- export function deleteByIndex(index) {
21
- if (Array.isArray(index)) {
22
- deleteByIndexArr(index);
23
- } else if (typeof index === 'number') {
24
- _deleteBySingIndex(index);
25
- }
26
- }
27
-
28
- /**
29
- * Delete multiple accounts by array of indices
30
- * @param {Array<number>} indices - Array of account indices to delete
31
- * @returns {void}
32
- * @example
33
- * deleteByIndexArr([0, 2, 4]);
34
- */
35
- export function deleteByIndexArr(indices) {
36
- if (!indices || !indices.length) {
37
- console.error('Error: Empty input is NOT valid!');
38
- return;
39
- }
40
-
41
- // Sort indices in descending order to avoid shifting issues
42
- const sortedIndices = [...indices].sort((a, b) => b - a);
43
-
44
- // Delete each index from highest to lowest
45
- for (const index of sortedIndices) {
46
- _deleteBySingIndex(index);
47
- }
48
- }
49
-
50
- /**
51
- * Get account information including balance and nonce
52
- * @async
53
- * @param {number|Array<number>} index - Account index or array of indices
54
- * @returns {Promise<void>}
55
- * @example
56
- * await getAccountInfo(0);
57
- * await getAccountInfo([0, 1, 2]);
58
- */
59
- export async function getAccountInfo(index) {
60
- if (Array.isArray(index)) {
61
- await _getAccArrInfo(index);
62
- } else if (typeof index === 'number') {
63
- await _getAccountInfo(index);
64
- }
65
- }
66
-
67
- /**
68
- * Checks if an imported wallet added before or not.
69
- * @param {string|Array<string>} privKeyArr - Account private key or array of private keys
70
- * @returns {Object}
71
- * @example
72
- * detectDupWallet('0x12cb...');
73
- * detectDupWallet(['0x12cb...','0x34cd...', ...]);
74
- */
75
- export function detectDupWallet(privKeyArr) {
76
- if(typeof privKeyArr === 'string') {
77
- return _findDupWalletByKey(privKeyArr);
78
- }
79
-
80
- if(Array.isArray(privKeyArr)) {
81
- return _findDupWalletByArr(privKeyArr);
82
- }
83
- }
84
-
85
- /**
86
- * Get information for multiple accounts (internal)
87
- * @private
88
- * @async
89
- * @param {Array<number>} _indices - Array of account indices
90
- * @returns {Promise<void>}
91
- */
92
- async function _getAccArrInfo(_indices) {
93
- if (!_indices || !_indices.length) {
94
- console.error('Error: Empty input is NOT valid!');
95
- return;
96
- }
97
-
98
- // Sort indices in descending order to avoid shifting issues
99
- const sortedIndices = [..._indices].sort((a, b) => b - a);
100
-
101
- // Delete each index from highest to lowest
102
- for (const index of sortedIndices) {
103
- await _getAccountInfo(index);
104
- }
105
- }
106
-
107
- /**
108
- * Get information for a single account (internal)
109
- * @private
110
- * @async
111
- * @param {number} _index - Account index
112
- * @returns {Promise<void>}
113
- */
114
- async function _getAccountInfo(_index) {
115
- const accInfo = allAccounts[_index];
116
-
117
- accInfo.nonce = await provider.getTransactionCount(accInfo.address);
118
- accInfo.balance = await provider.getBalance(accInfo.address);
119
-
120
- console.log(accInfo);
121
- }
122
-
123
- /**
124
- * Delete a single account by index (internal implementation)
125
- * @private
126
- * @param {number|null} _index - Account index or null to clear all
127
- * @returns {void}
128
- */
129
- function _deleteBySingIndex(_index) {
130
- if (_index === null || _index === undefined) {
131
- // Clear all arrays
132
- allAccounts.splice(0);
133
- accounts.splice(0);
134
- hdAccounts.splice(0);
135
- return;
136
- }
137
-
138
- // Find and remove from allAccounts
139
- const accountIndex = allAccounts.findIndex(acc => acc.index === _index);
140
- if (accountIndex !== -1) {
141
- allAccounts.splice(accountIndex, 1);
142
-
143
- // Update indices in allAccounts
144
- for (let i = accountIndex; i < allAccounts.length; i++) {
145
- allAccounts[i].index = i;
146
- }
147
-
148
- // Remove from accounts array if it exists there
149
- const regularIndex = accounts.findIndex(acc => acc.index === _index);
150
- if (regularIndex !== -1) {
151
- accounts.splice(regularIndex, 1);
152
- // Update indices in accounts
153
- for (let i = regularIndex; i < accounts.length; i++) {
154
- accounts[i].index = _index;
155
- _index++;
156
- }
157
- }
158
-
159
- // Remove from hdAccounts array if it exists there
160
- const hdIndex = hdAccounts.findIndex(acc => acc.index === _index);
161
- if (hdIndex !== -1) {
162
- hdAccounts.splice(hdIndex, 1);
163
- // Update indices in hdAccounts
164
- for (let i = hdIndex; i < hdAccounts.length; i++) {
165
- hdAccounts[i].index = _index;
166
- _index++;
167
- }
168
- }
169
- }
170
- }
171
-
172
- /**
173
- * Find a duplicated wallet using passed private key
174
- * @private
175
- * @param {string} privKey - Account private key
176
- * @returns {Object}
177
- */
178
- function _findDupWalletByKey(privKey) {
179
- const foundWallet = allAccounts.find(wallet => wallet.privateKey == privKey);
180
- if(foundWallet) {
181
- return {
182
- status: true,
183
- privateKey: privKey,
184
- index: foundWallet.index
185
- }
186
- } else {
187
- return {
188
- status: false
189
- }
190
- }
191
- }
192
-
193
- /**
194
- * Find a duplicated wallet using passed private key array
195
- * @private
196
- * @param {Array<string>} privKeyArr - Account private key array
197
- * @returns {Object}
198
- */
199
- function _findDupWalletByArr(privKeyArr) {
200
- for(let i = 0; i < privKeyArr.length; i++) {
201
- const foundWallet = allAccounts.find(wallet => wallet.privateKey == privKeyArr[i]);
202
- if(foundWallet) {
203
- return {
204
- status: true,
205
- privateKey: privKeyArr[i],
206
- index: foundWallet.index
207
- }
208
- }
209
- }
210
- return {
211
- status: false
212
- }
1
+ /**
2
+ * @fileoverview Account management utilities
3
+ * @description Internal utilities for managing account arrays, including deletion
4
+ * and information retrieval. Handles synchronization across multiple account arrays.
5
+ * @module accounter
6
+ */
7
+
8
+ import { allAccounts, accounts, hdAccounts } from '../services/wallet.js';
9
+ import { provider } from '../services/network.js';
10
+
11
+ /**
12
+ * Delete account(s) by index
13
+ * @param {number|Array<number>|null} index - Account index, array of indices, or null to delete all
14
+ * @returns {void}
15
+ * @example
16
+ * deleteByIndex(0); // Delete account at index 0
17
+ * deleteByIndex([1, 3, 5]); // Delete multiple accounts
18
+ * deleteByIndex(null); // Delete all accounts
19
+ */
20
+ export function deleteByIndex(index) {
21
+ if (Array.isArray(index)) {
22
+ deleteByIndexArr(index);
23
+ } else if (typeof index === 'number') {
24
+ _deleteBySingIndex(index);
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Delete multiple accounts by array of indices
30
+ * @param {Array<number>} indices - Array of account indices to delete
31
+ * @returns {void}
32
+ * @example
33
+ * deleteByIndexArr([0, 2, 4]);
34
+ */
35
+ export function deleteByIndexArr(indices) {
36
+ if (!indices || !indices.length) {
37
+ console.error('Error: Empty input is NOT valid!');
38
+ return;
39
+ }
40
+
41
+ // Sort indices in descending order to avoid shifting issues
42
+ const sortedIndices = [...indices].sort((a, b) => b - a);
43
+
44
+ // Delete each index from highest to lowest
45
+ for (const index of sortedIndices) {
46
+ _deleteBySingIndex(index);
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Get account information including balance and nonce
52
+ * @async
53
+ * @param {number|Array<number>} index - Account index or array of indices
54
+ * @returns {Promise<void>}
55
+ * @example
56
+ * await getAccountInfo(0);
57
+ * await getAccountInfo([0, 1, 2]);
58
+ */
59
+ export async function getAccountInfo(index) {
60
+ if (Array.isArray(index)) {
61
+ await _getAccArrInfo(index);
62
+ } else if (typeof index === 'number') {
63
+ await _getAccountInfo(index);
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Checks if an imported wallet added before or not.
69
+ * @param {string|Array<string>} privKeyArr - Account private key or array of private keys
70
+ * @returns {Object}
71
+ * @example
72
+ * detectDupWallet('0x12cb...');
73
+ * detectDupWallet(['0x12cb...','0x34cd...', ...]);
74
+ */
75
+ export function detectDupWallet(privKeyArr) {
76
+ if(typeof privKeyArr === 'string') {
77
+ return _findDupWalletByKey(privKeyArr);
78
+ }
79
+
80
+ if(Array.isArray(privKeyArr)) {
81
+ return _findDupWalletByArr(privKeyArr);
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Get information for multiple accounts (internal)
87
+ * @private
88
+ * @async
89
+ * @param {Array<number>} _indices - Array of account indices
90
+ * @returns {Promise<void>}
91
+ */
92
+ async function _getAccArrInfo(_indices) {
93
+ if (!_indices || !_indices.length) {
94
+ console.error('Error: Empty input is NOT valid!');
95
+ return;
96
+ }
97
+
98
+ // Sort indices in descending order to avoid shifting issues
99
+ const sortedIndices = [..._indices].sort((a, b) => b - a);
100
+
101
+ // Delete each index from highest to lowest
102
+ for (const index of sortedIndices) {
103
+ await _getAccountInfo(index);
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Get information for a single account (internal)
109
+ * @private
110
+ * @async
111
+ * @param {number} _index - Account index
112
+ * @returns {Promise<void>}
113
+ */
114
+ async function _getAccountInfo(_index) {
115
+ const accInfo = allAccounts[_index];
116
+
117
+ accInfo.nonce = await provider.getTransactionCount(accInfo.address);
118
+ accInfo.balance = await provider.getBalance(accInfo.address);
119
+
120
+ console.log(accInfo);
121
+ }
122
+
123
+ /**
124
+ * Delete a single account by index (internal implementation)
125
+ * @private
126
+ * @param {number|null} _index - Account index or null to clear all
127
+ * @returns {void}
128
+ */
129
+ function _deleteBySingIndex(_index) {
130
+ if (_index === null || _index === undefined) {
131
+ // Clear all arrays
132
+ allAccounts.splice(0);
133
+ accounts.splice(0);
134
+ hdAccounts.splice(0);
135
+ return;
136
+ }
137
+
138
+ // Find and remove from allAccounts
139
+ const accountIndex = allAccounts.findIndex(acc => acc.index === _index);
140
+ if (accountIndex !== -1) {
141
+ allAccounts.splice(accountIndex, 1);
142
+
143
+ // Update indices in allAccounts
144
+ for (let i = accountIndex; i < allAccounts.length; i++) {
145
+ allAccounts[i].index = i;
146
+ }
147
+
148
+ // Remove from accounts array if it exists there
149
+ const regularIndex = accounts.findIndex(acc => acc.index === _index);
150
+ if (regularIndex !== -1) {
151
+ accounts.splice(regularIndex, 1);
152
+ // Update indices in accounts
153
+ for (let i = regularIndex; i < accounts.length; i++) {
154
+ accounts[i].index = _index;
155
+ _index++;
156
+ }
157
+ }
158
+
159
+ // Remove from hdAccounts array if it exists there
160
+ const hdIndex = hdAccounts.findIndex(acc => acc.index === _index);
161
+ if (hdIndex !== -1) {
162
+ hdAccounts.splice(hdIndex, 1);
163
+ // Update indices in hdAccounts
164
+ for (let i = hdIndex; i < hdAccounts.length; i++) {
165
+ hdAccounts[i].index = _index;
166
+ _index++;
167
+ }
168
+ }
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Find a duplicated wallet using passed private key
174
+ * @private
175
+ * @param {string} privKey - Account private key
176
+ * @returns {Object}
177
+ */
178
+ function _findDupWalletByKey(privKey) {
179
+ const foundWallet = allAccounts.find(wallet => wallet.privateKey == privKey);
180
+ if(foundWallet) {
181
+ return {
182
+ status: true,
183
+ privateKey: privKey,
184
+ index: foundWallet.index
185
+ }
186
+ } else {
187
+ return {
188
+ status: false
189
+ }
190
+ }
191
+ }
192
+
193
+ /**
194
+ * Find a duplicated wallet using passed private key array
195
+ * @private
196
+ * @param {Array<string>} privKeyArr - Account private key array
197
+ * @returns {Object}
198
+ */
199
+ function _findDupWalletByArr(privKeyArr) {
200
+ for(let i = 0; i < privKeyArr.length; i++) {
201
+ const foundWallet = allAccounts.find(wallet => wallet.privateKey == privKeyArr[i]);
202
+ if(foundWallet) {
203
+ return {
204
+ status: true,
205
+ privateKey: privKeyArr[i],
206
+ index: foundWallet.index
207
+ }
208
+ }
209
+ }
210
+ return {
211
+ status: false
212
+ }
213
213
  }