@spirobel/monero-wallet-api 0.5.1 → 0.5.2

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 (28) hide show
  1. package/dist/actionlog/actionLog.d.ts +4 -0
  2. package/dist/actionlog/actionLog.js +9 -0
  3. package/dist/actionlog/actionLogOpened.d.ts +1 -0
  4. package/dist/actionlog/actionLogOpened.js +1 -0
  5. package/dist/api.d.ts +3 -2
  6. package/dist/api.js +3 -2
  7. package/dist/scanning-syncing/backgroundWorker.js +1 -1
  8. package/dist/scanning-syncing/blocksbuffer/blocksBufferFetchLoop.js +29 -3
  9. package/dist/scanning-syncing/blocksbuffer/blocksbufferCoordination.js +6 -3
  10. package/dist/scanning-syncing/connectionStatus.d.ts +1 -4
  11. package/dist/scanning-syncing/connectionStatus.js +1 -31
  12. package/dist/scanning-syncing/connectionStatusOpened.d.ts +15 -0
  13. package/dist/scanning-syncing/connectionStatusOpened.js +51 -0
  14. package/dist/scanning-syncing/openWallet.js +1 -1
  15. package/dist/scanning-syncing/scanresult/scanCacheOpened.d.ts +53 -7
  16. package/dist/scanning-syncing/scanresult/scanCacheOpened.js +185 -50
  17. package/dist/scanning-syncing/scanresult/scanCoordination.d.ts +1 -1
  18. package/dist/scanning-syncing/scanresult/scanCoordination.js +64 -2
  19. package/dist/scanning-syncing/scansettings/scanSettings.d.ts +165 -0
  20. package/dist/scanning-syncing/scansettings/scanSettings.js +338 -0
  21. package/dist/scanning-syncing/worker-entrypoints/worker.js +1 -1
  22. package/dist/scanning-syncing/worker-mains/worker.js +1 -2
  23. package/dist/scansettings/scanSettings.d.ts +165 -0
  24. package/dist/scansettings/scanSettings.js +338 -0
  25. package/dist/scansettings/scanSettingsOpened.d.ts +102 -0
  26. package/dist/scansettings/scanSettingsOpened.js +299 -0
  27. package/dist/viewpair/ViewPair.js +1 -1
  28. package/package.json +1 -1
@@ -0,0 +1,338 @@
1
+ import { get_info } from "../../api";
2
+ import { atomicWrite } from "../../io/atomicWrite";
3
+ import { refreshEnvIndexedDB } from "../../io/indexedDB";
4
+ import { LOCAL_NODE_DEFAULT_URL } from "../../node-interaction/nodeUrl";
5
+ export const SCAN_SETTINGS_STORE_NAME_DEFAULT = "ScanSettings.json";
6
+ // default value for subaddress highest minor index eg up to which minor index,
7
+ // to add subaddresses to the scanner
8
+ // (we never touch major index, having two values is just too confusing)
9
+ export const SUB_ADDRESS_INDEX_DEFAULT_VALUE = 1;
10
+ /**
11
+ * Writes scan settings to the default or specified storage file in json.
12
+ *
13
+ * @example
14
+ * ```
15
+ * const settings: ScanSettings = {
16
+ * wallets: [{
17
+ * primary_address: "5dsf...",
18
+ * start_height: 1741707,
19
+ * }],
20
+ * node_urls: ["https://monerooo.roooo"]
21
+ * };
22
+ * await writeScanSettings(settings);
23
+ * ```
24
+ *
25
+ * @param scan_settings - The complete {@link ScanSettings} configuration to persist.
26
+ * @param scan_settings_path - Optional path for the settings file. Defaults to `SCAN_SETTINGS_STORE_NAME_DEFAULT`.
27
+ * @returns A promise that resolves when the file is successfully written.
28
+ * @throws Will throw if file writing fails (e.g., permissions, disk space).
29
+ */
30
+ export async function writeScanSettings(scan_settings, scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
31
+ return await atomicWrite(scan_settings_path, JSON.stringify(scan_settings, null, 2));
32
+ }
33
+ export async function writeScanSettingsFileDefaultLocation(params) {
34
+ let settings = await openScanSettingsFile(params.scan_settings_path);
35
+ if (!settings) {
36
+ // Create empty ScanSettings object with default values
37
+ settings = {
38
+ node_url: LOCAL_NODE_DEFAULT_URL,
39
+ wallets: [],
40
+ start_height: null,
41
+ };
42
+ }
43
+ await params.writeCallback(settings);
44
+ // write to settings file
45
+ await writeScanSettings(settings, params.scan_settings_path);
46
+ }
47
+ export async function writeStartHeightToScanSettings(start_height, scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
48
+ const scanSettings = (await openScanSettingsFile(scan_settings_path)) || {
49
+ node_url: LOCAL_NODE_DEFAULT_URL,
50
+ wallets: [],
51
+ start_height: null,
52
+ };
53
+ scanSettings.start_height = start_height;
54
+ return await atomicWrite(SCAN_SETTINGS_STORE_NAME_DEFAULT, JSON.stringify(scanSettings, null, 2));
55
+ }
56
+ export async function writeDaemonHeightAsStartHeightToScanSettings(node_url, scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
57
+ const getInfo = await get_info(node_url);
58
+ const scanSettings = (await openScanSettingsFile(scan_settings_path)) || {
59
+ node_url: LOCAL_NODE_DEFAULT_URL,
60
+ wallets: [],
61
+ start_height: null,
62
+ };
63
+ scanSettings.start_height = getInfo.height - 1;
64
+ await atomicWrite(scan_settings_path, JSON.stringify(scanSettings, null, 2));
65
+ return getInfo.height;
66
+ }
67
+ export async function cullTooLargeScanHeight(node_url, scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
68
+ const getInfo = await get_info(node_url);
69
+ const scanSettings = (await openScanSettingsFile(scan_settings_path)) || {
70
+ node_url: LOCAL_NODE_DEFAULT_URL,
71
+ wallets: [],
72
+ start_height: null,
73
+ };
74
+ if (scanSettings.start_height === null ||
75
+ scanSettings.start_height > getInfo.height - 1) {
76
+ scanSettings.start_height = getInfo.height - 1;
77
+ await atomicWrite(scan_settings_path, JSON.stringify(scanSettings, null, 2));
78
+ }
79
+ return scanSettings.start_height;
80
+ }
81
+ export async function writeNodeUrlToScanSettings(node_url, scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
82
+ const scanSettings = (await openScanSettingsFile(scan_settings_path)) || {
83
+ node_url: LOCAL_NODE_DEFAULT_URL,
84
+ wallets: [],
85
+ start_height: null,
86
+ };
87
+ scanSettings.node_url = node_url;
88
+ return await atomicWrite(scan_settings_path, JSON.stringify(scanSettings, null, 2));
89
+ }
90
+ export async function readNodeUrlFromScanSettings(scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
91
+ const scanSettings = await openScanSettingsFile(scan_settings_path);
92
+ return scanSettings?.node_url;
93
+ }
94
+ /**
95
+ * Writes the merchant confirmation threshold to scan settings.
96
+ *
97
+ * This setting only has effect when the wallet is run as part of a checkout system,
98
+ * that accepts payments.
99
+ * It determines after how many confirmations a transaction is accepted
100
+ * for a payment. The actual enforcement of this policy is left to the
101
+ * custom payment-system code that consumes this library.
102
+ *
103
+ * @param merchant_confirmations - Number of confirmations required before
104
+ * accepting a payment, or `null` to leave it unset.
105
+ * @param scan_settings_path - Optional path for the settings file.
106
+ * Defaults to `SCAN_SETTINGS_STORE_NAME_DEFAULT`.
107
+ * @returns A promise that resolves when the file is successfully written.
108
+ */
109
+ export async function writeMerchantConfirmationsToScanSettings(merchant_confirmations, scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
110
+ const scanSettings = (await openScanSettingsFile(scan_settings_path)) || {
111
+ node_url: LOCAL_NODE_DEFAULT_URL,
112
+ wallets: [],
113
+ start_height: null,
114
+ };
115
+ scanSettings.merchant_confirmations = merchant_confirmations;
116
+ return await atomicWrite(scan_settings_path, JSON.stringify(scanSettings, null, 2));
117
+ }
118
+ /**
119
+ * Reads the merchant confirmation threshold from scan settings.
120
+ *
121
+ * This setting only has effect when the wallet is run as part of a checkout system,
122
+ * that accepts payments.
123
+ *
124
+ * It determines after how many confirmations a transaction is accepted
125
+ * for a payment. The actual enforcement of this policy is left to the
126
+ * custom payment-system code that consumes this library.
127
+ *
128
+ * @param scan_settings_path - Optional path for the settings file.
129
+ * Defaults to `SCAN_SETTINGS_STORE_NAME_DEFAULT`.
130
+ * @returns The configured number of confirmations, null, or `undefined`
131
+ * if the setting has not been persisted yet.
132
+ */
133
+ export async function readMerchantConfirmationsFromScanSettings(scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
134
+ const scanSettings = await openScanSettingsFile(scan_settings_path);
135
+ return scanSettings?.merchant_confirmations;
136
+ }
137
+ /**
138
+ * Reads scan settings from the default or specified storage file.
139
+ * secret_view_key and spend_private_key are read from environment variables
140
+ *
141
+ * @example
142
+ * ```
143
+ * const settings = await readScanSettings();
144
+ * if (settings) {
145
+ * console.log(settings.wallets?.primary_address);
146
+ * }
147
+ * ```
148
+ *
149
+ * @param scan_settings_path - Path to the settings file. Defaults to `SCAN_SETTINGS_STORE_NAME_DEFAULT`.
150
+ * @returns The parsed {@link ScanSettings} object if file exists and is valid JSON, otherwise `undefined`.
151
+ */
152
+ export async function readScanSettings(scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
153
+ const scanSettings = await openScanSettingsFile(scan_settings_path);
154
+ if (!scanSettings)
155
+ return undefined;
156
+ const openScanSettings = Object.assign({}, scanSettings);
157
+ for (const [i, wallet] of scanSettings.wallets.entries()) {
158
+ if (!wallet.primary_address)
159
+ throw new Error("The entry ${i} in the wallet settings list in ${scan_settings_path} has no primary address");
160
+ const walletWithKeys = await walletSettingsPlusKeys({
161
+ ...wallet,
162
+ node_url: scanSettings.node_url,
163
+ start_height: scanSettings.start_height,
164
+ });
165
+ openScanSettings.wallets[i].secret_view_key =
166
+ walletWithKeys.secret_view_key;
167
+ openScanSettings.wallets[i].secret_spend_key =
168
+ walletWithKeys.secret_spend_key;
169
+ }
170
+ return openScanSettings;
171
+ }
172
+ export function readPrivateSpendKeyFromEnv(primary_address) {
173
+ return Bun.env["sk" + primary_address];
174
+ }
175
+ export function readPrivateViewKeyFromEnv(primary_address) {
176
+ return Bun.env["vk" + primary_address];
177
+ }
178
+ export async function readWalletFromScanSettings(primary_address, scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
179
+ const scanSettings = await openScanSettingsFile(scan_settings_path);
180
+ if (!scanSettings)
181
+ return undefined;
182
+ const walletSettings = scanSettings.wallets.find((wallet) => wallet?.primary_address === primary_address);
183
+ if (!walletSettings)
184
+ throw new Error(`wallet not found in settings. did you call openwallet with the right params?
185
+ Either wrong file name supplied to params.scan_settings_path: ${scan_settings_path}
186
+ Or wrong primary_address supplied params.primary_address: ${primary_address}`);
187
+ return {
188
+ ...walletSettings,
189
+ node_url: scanSettings.node_url,
190
+ start_height: scanSettings.start_height,
191
+ };
192
+ }
193
+ export async function readWalletsFromScanSettings(scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
194
+ const scanSettings = await openScanSettingsFile(scan_settings_path);
195
+ const scanSettingsOpened = [];
196
+ for (const wallet of scanSettings?.wallets || []) {
197
+ scanSettingsOpened.push({
198
+ ...wallet,
199
+ node_url: scanSettings?.node_url || LOCAL_NODE_DEFAULT_URL,
200
+ start_height: scanSettings?.start_height || null,
201
+ });
202
+ }
203
+ return scanSettingsOpened;
204
+ }
205
+ export async function walletSettingsPlusKeys(wallet_settings, secret_view_key, secret_spend_key) {
206
+ // read secret_view_key and secret_spend_key from env
207
+ if (!secret_view_key)
208
+ secret_view_key = Bun.env[`vk${wallet_settings.primary_address}`];
209
+ if (!secret_view_key) {
210
+ await refreshEnvIndexedDB();
211
+ secret_view_key = Bun.env[`vk${wallet_settings.primary_address}`];
212
+ }
213
+ if (!secret_view_key)
214
+ throw ("no secret_view_key provided and not found in env for address: " +
215
+ wallet_settings.primary_address);
216
+ if (!secret_spend_key)
217
+ secret_spend_key = Bun.env[`sk${wallet_settings.primary_address}`];
218
+ return {
219
+ ...wallet_settings,
220
+ secret_view_key,
221
+ secret_spend_key,
222
+ };
223
+ }
224
+ export async function writeWalletToScanSettings(params) {
225
+ if (!params.scan_settings_path)
226
+ params.scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT;
227
+ if (!params.primary_address)
228
+ throw new Error("no primary address provided to writeWalletToScanSettings()");
229
+ const scanSettings = await openScanSettingsFile(params.scan_settings_path);
230
+ if (!scanSettings) {
231
+ if (!params.node_url)
232
+ params.node_url = LOCAL_NODE_DEFAULT_URL;
233
+ // case: no scan settings exist yet
234
+ return await writeScanSettings({
235
+ wallets: [
236
+ {
237
+ primary_address: params.primary_address,
238
+ subaddress_index: params.subaddress_index || SUB_ADDRESS_INDEX_DEFAULT_VALUE,
239
+ halted: params.halted,
240
+ wallet_route: params.wallet_route,
241
+ wallet_name: params.wallet_name,
242
+ wallet_slot: params.wallet_slot,
243
+ },
244
+ ],
245
+ node_url: params.node_url,
246
+ logs: params.logs,
247
+ logs_include: params.logs_include,
248
+ logs_exclude: params.logs_exclude,
249
+ start_height: params.start_height || null,
250
+ }, params.scan_settings_path);
251
+ }
252
+ scanSettings.node_url = params.node_url || scanSettings.node_url;
253
+ if (params.logs)
254
+ scanSettings.logs = params.logs;
255
+ if (params.logs_include)
256
+ scanSettings.logs_include = params.logs_include;
257
+ if (params.logs_exclude)
258
+ scanSettings.logs_exclude = params.logs_exclude;
259
+ scanSettings.start_height =
260
+ params.start_height === undefined
261
+ ? scanSettings.start_height
262
+ : params.start_height;
263
+ const already_has_settings = scanSettings.wallets.findIndex((wallet) => wallet?.primary_address === params.primary_address);
264
+ if (already_has_settings === -1) {
265
+ // wallet does not exist yet in settings
266
+ scanSettings.wallets.push({
267
+ primary_address: params.primary_address,
268
+ subaddress_index: params.subaddress_index || SUB_ADDRESS_INDEX_DEFAULT_VALUE,
269
+ halted: params.halted,
270
+ wallet_route: params.wallet_route,
271
+ wallet_name: params.wallet_name,
272
+ wallet_slot: params.wallet_slot,
273
+ });
274
+ }
275
+ else {
276
+ // wallet already exists
277
+ const wallet = scanSettings.wallets[already_has_settings];
278
+ if (wallet) {
279
+ wallet.subaddress_index =
280
+ params.subaddress_index ||
281
+ wallet.subaddress_index ||
282
+ SUB_ADDRESS_INDEX_DEFAULT_VALUE;
283
+ wallet.halted = params.halted || wallet.halted;
284
+ wallet.wallet_route = params.wallet_route || wallet.wallet_route;
285
+ wallet.wallet_name = params.wallet_name || wallet.wallet_name;
286
+ wallet.wallet_slot = params.wallet_slot || wallet.wallet_slot;
287
+ }
288
+ }
289
+ return await atomicWrite(params.scan_settings_path, JSON.stringify(scanSettings, null, 2));
290
+ }
291
+ export async function openScanSettingsFile(scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
292
+ const jsonString = await Bun.file(scan_settings_path)
293
+ .text()
294
+ .catch(() => undefined);
295
+ return jsonString ? JSON.parse(jsonString) : undefined;
296
+ }
297
+ export async function openNonHaltedWallets(scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
298
+ const scan_settings = await readWalletsFromScanSettings(scan_settings_path);
299
+ if (!scan_settings)
300
+ throw new Error("no scan settings file found at path: " + scan_settings_path);
301
+ if (!scan_settings)
302
+ throw new Error("no wallets in scan settings");
303
+ const nonHaltedWallets = scan_settings.filter((wallet) => !wallet?.halted);
304
+ if (!nonHaltedWallets.length)
305
+ throw new Error("no non halted wallets in scan settings");
306
+ return nonHaltedWallets;
307
+ }
308
+ export function getNonHaltedWallets(scan_settings) {
309
+ if (!scan_settings)
310
+ return [];
311
+ const nonHaltedWallets = scan_settings.wallets.filter((wallet) => !wallet?.halted);
312
+ return nonHaltedWallets;
313
+ }
314
+ export async function doesScanSettingsFileExist(scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT) {
315
+ const scan_settings_file_content = await Bun.file(scan_settings_path)
316
+ .text()
317
+ .catch(() => "");
318
+ if (scan_settings_file_content.length) {
319
+ return true;
320
+ }
321
+ else {
322
+ return false;
323
+ }
324
+ }
325
+ /**
326
+ * Either 1. the consumer explicitly sets a pathprefix, we extract it from the scan_settings_path
327
+ * or we pick the default which is nothing
328
+ * @param scan_settings_path optional scan settings path
329
+ * @param pathPrefix optional path prefix
330
+ * @returns definite path prefix
331
+ */
332
+ export function getPathPrefix(scan_settings_path = SCAN_SETTINGS_STORE_NAME_DEFAULT, pathPrefix) {
333
+ const parts = scan_settings_path.split("/");
334
+ const basename = parts.pop();
335
+ const dir = parts.join("/");
336
+ const prefix = dir ? `${dir}/` : "";
337
+ return pathPrefix ?? prefix;
338
+ }