@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.
- package/dist/actionlog/actionLog.d.ts +4 -0
- package/dist/actionlog/actionLog.js +9 -0
- package/dist/actionlog/actionLogOpened.d.ts +1 -0
- package/dist/actionlog/actionLogOpened.js +1 -0
- package/dist/api.d.ts +3 -2
- package/dist/api.js +3 -2
- package/dist/scanning-syncing/backgroundWorker.js +1 -1
- package/dist/scanning-syncing/blocksbuffer/blocksBufferFetchLoop.js +29 -3
- package/dist/scanning-syncing/blocksbuffer/blocksbufferCoordination.js +6 -3
- package/dist/scanning-syncing/connectionStatus.d.ts +1 -4
- package/dist/scanning-syncing/connectionStatus.js +1 -31
- package/dist/scanning-syncing/connectionStatusOpened.d.ts +15 -0
- package/dist/scanning-syncing/connectionStatusOpened.js +51 -0
- package/dist/scanning-syncing/openWallet.js +1 -1
- package/dist/scanning-syncing/scanresult/scanCacheOpened.d.ts +53 -7
- package/dist/scanning-syncing/scanresult/scanCacheOpened.js +185 -50
- package/dist/scanning-syncing/scanresult/scanCoordination.d.ts +1 -1
- package/dist/scanning-syncing/scanresult/scanCoordination.js +64 -2
- package/dist/scanning-syncing/scansettings/scanSettings.d.ts +165 -0
- package/dist/scanning-syncing/scansettings/scanSettings.js +338 -0
- package/dist/scanning-syncing/worker-entrypoints/worker.js +1 -1
- package/dist/scanning-syncing/worker-mains/worker.js +1 -2
- package/dist/scansettings/scanSettings.d.ts +165 -0
- package/dist/scansettings/scanSettings.js +338 -0
- package/dist/scansettings/scanSettingsOpened.d.ts +102 -0
- package/dist/scansettings/scanSettingsOpened.js +299 -0
- package/dist/viewpair/ViewPair.js +1 -1
- package/package.json +1 -1
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { coordinatorMainWorker } from "./coordinator-main";
|
|
2
2
|
import { handleCpuboundScanTry, sendFromCpuWorker } from "./cpubound-main";
|
|
3
3
|
import { log, setupLoggingPath } from "../../io/logging";
|
|
4
|
-
import { SCAN_SETTINGS_STORE_NAME_DEFAULT } from "../scanSettings";
|
|
5
4
|
import { multisigMainWorkerCall } from "./multisig-main";
|
|
6
|
-
import { DistributedKeyGenerator } from "../../api";
|
|
5
|
+
import { DistributedKeyGenerator, SCAN_SETTINGS_STORE_NAME_DEFAULT, } from "../../api";
|
|
7
6
|
self.onerror = (e) => self.postMessage({ type: "ERROR", payload: e });
|
|
8
7
|
self.addEventListener("unhandledrejection", (e) => self.postMessage({ type: "ERROR", payload: e.reason }));
|
|
9
8
|
let SCAN_SETTINGS_PATH;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type { LogSetting, PossibleLogs } from "../io/logging";
|
|
2
|
+
export declare const SCAN_SETTINGS_STORE_NAME_DEFAULT = "ScanSettings.json";
|
|
3
|
+
export declare const SUB_ADDRESS_INDEX_DEFAULT_VALUE = 1;
|
|
4
|
+
export type ScanSetting = {
|
|
5
|
+
primary_address: string;
|
|
6
|
+
subaddress_index?: number;
|
|
7
|
+
halted?: boolean;
|
|
8
|
+
wallet_route?: string;
|
|
9
|
+
wallet_name?: string;
|
|
10
|
+
wallet_slot?: number;
|
|
11
|
+
};
|
|
12
|
+
export type WriteScanSettingParams = {
|
|
13
|
+
primary_address: string;
|
|
14
|
+
start_height?: number | null;
|
|
15
|
+
subaddress_index?: number;
|
|
16
|
+
halted?: boolean;
|
|
17
|
+
scan_settings_path?: string;
|
|
18
|
+
node_url?: string;
|
|
19
|
+
wallet_route?: string;
|
|
20
|
+
wallet_name?: string;
|
|
21
|
+
wallet_slot?: number;
|
|
22
|
+
logs?: LogSetting;
|
|
23
|
+
logs_include?: PossibleLogs[];
|
|
24
|
+
logs_exclude?: PossibleLogs[];
|
|
25
|
+
};
|
|
26
|
+
export type ScanSettingOpened = {
|
|
27
|
+
primary_address: string;
|
|
28
|
+
start_height: number | null;
|
|
29
|
+
node_url: string;
|
|
30
|
+
subaddress_index?: number;
|
|
31
|
+
secret_view_key?: string;
|
|
32
|
+
halted?: boolean;
|
|
33
|
+
secret_spend_key?: string;
|
|
34
|
+
wallet_route?: string;
|
|
35
|
+
wallet_name?: string;
|
|
36
|
+
wallet_slot?: number;
|
|
37
|
+
};
|
|
38
|
+
export type ScanSettings = {
|
|
39
|
+
wallets: ScanSetting[];
|
|
40
|
+
node_url: string;
|
|
41
|
+
start_height: number | null;
|
|
42
|
+
merchant_confirmations?: number | null;
|
|
43
|
+
cpu_worker_count?: number;
|
|
44
|
+
logs?: LogSetting;
|
|
45
|
+
logs_include?: PossibleLogs[];
|
|
46
|
+
logs_exclude?: PossibleLogs[];
|
|
47
|
+
};
|
|
48
|
+
export type ScanSettingsOpened = {
|
|
49
|
+
wallets: (ScanSettingOpened | undefined)[];
|
|
50
|
+
node_url: string;
|
|
51
|
+
start_height: number | null;
|
|
52
|
+
merchant_confirmations?: number | null;
|
|
53
|
+
cpu_worker_count?: number;
|
|
54
|
+
logs?: LogSetting;
|
|
55
|
+
logs_include?: PossibleLogs[];
|
|
56
|
+
logs_exclude?: PossibleLogs[];
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Writes scan settings to the default or specified storage file in json.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```
|
|
63
|
+
* const settings: ScanSettings = {
|
|
64
|
+
* wallets: [{
|
|
65
|
+
* primary_address: "5dsf...",
|
|
66
|
+
* start_height: 1741707,
|
|
67
|
+
* }],
|
|
68
|
+
* node_urls: ["https://monerooo.roooo"]
|
|
69
|
+
* };
|
|
70
|
+
* await writeScanSettings(settings);
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @param scan_settings - The complete {@link ScanSettings} configuration to persist.
|
|
74
|
+
* @param scan_settings_path - Optional path for the settings file. Defaults to `SCAN_SETTINGS_STORE_NAME_DEFAULT`.
|
|
75
|
+
* @returns A promise that resolves when the file is successfully written.
|
|
76
|
+
* @throws Will throw if file writing fails (e.g., permissions, disk space).
|
|
77
|
+
*/
|
|
78
|
+
export declare function writeScanSettings(scan_settings: ScanSettings, scan_settings_path?: string): Promise<number>;
|
|
79
|
+
export type WriteScanSettingsFileParams = {
|
|
80
|
+
scan_settings_path?: string;
|
|
81
|
+
writeCallback: (settings: ScanSettings) => void | Promise<void>;
|
|
82
|
+
};
|
|
83
|
+
export declare function writeScanSettingsFileDefaultLocation(params: WriteScanSettingsFileParams): Promise<void>;
|
|
84
|
+
export declare function writeStartHeightToScanSettings(start_height: number | null, scan_settings_path?: string): Promise<number>;
|
|
85
|
+
export declare function writeDaemonHeightAsStartHeightToScanSettings(node_url: string, scan_settings_path?: string): Promise<number>;
|
|
86
|
+
export declare function cullTooLargeScanHeight(node_url: string, scan_settings_path?: string): Promise<number>;
|
|
87
|
+
export declare function writeNodeUrlToScanSettings(node_url: string, scan_settings_path?: string): Promise<number>;
|
|
88
|
+
export declare function readNodeUrlFromScanSettings(scan_settings_path?: string): Promise<string | undefined>;
|
|
89
|
+
/**
|
|
90
|
+
* Writes the merchant confirmation threshold to scan settings.
|
|
91
|
+
*
|
|
92
|
+
* This setting only has effect when the wallet is run as part of a checkout system,
|
|
93
|
+
* that accepts payments.
|
|
94
|
+
* It determines after how many confirmations a transaction is accepted
|
|
95
|
+
* for a payment. The actual enforcement of this policy is left to the
|
|
96
|
+
* custom payment-system code that consumes this library.
|
|
97
|
+
*
|
|
98
|
+
* @param merchant_confirmations - Number of confirmations required before
|
|
99
|
+
* accepting a payment, or `null` to leave it unset.
|
|
100
|
+
* @param scan_settings_path - Optional path for the settings file.
|
|
101
|
+
* Defaults to `SCAN_SETTINGS_STORE_NAME_DEFAULT`.
|
|
102
|
+
* @returns A promise that resolves when the file is successfully written.
|
|
103
|
+
*/
|
|
104
|
+
export declare function writeMerchantConfirmationsToScanSettings(merchant_confirmations: number | null, scan_settings_path?: string): Promise<number>;
|
|
105
|
+
/**
|
|
106
|
+
* Reads the merchant confirmation threshold from scan settings.
|
|
107
|
+
*
|
|
108
|
+
* This setting only has effect when the wallet is run as part of a checkout system,
|
|
109
|
+
* that accepts payments.
|
|
110
|
+
*
|
|
111
|
+
* It determines after how many confirmations a transaction is accepted
|
|
112
|
+
* for a payment. The actual enforcement of this policy is left to the
|
|
113
|
+
* custom payment-system code that consumes this library.
|
|
114
|
+
*
|
|
115
|
+
* @param scan_settings_path - Optional path for the settings file.
|
|
116
|
+
* Defaults to `SCAN_SETTINGS_STORE_NAME_DEFAULT`.
|
|
117
|
+
* @returns The configured number of confirmations, null, or `undefined`
|
|
118
|
+
* if the setting has not been persisted yet.
|
|
119
|
+
*/
|
|
120
|
+
export declare function readMerchantConfirmationsFromScanSettings(scan_settings_path?: string): Promise<number | null | undefined>;
|
|
121
|
+
/**
|
|
122
|
+
* Reads scan settings from the default or specified storage file.
|
|
123
|
+
* secret_view_key and spend_private_key are read from environment variables
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```
|
|
127
|
+
* const settings = await readScanSettings();
|
|
128
|
+
* if (settings) {
|
|
129
|
+
* console.log(settings.wallets?.primary_address);
|
|
130
|
+
* }
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @param scan_settings_path - Path to the settings file. Defaults to `SCAN_SETTINGS_STORE_NAME_DEFAULT`.
|
|
134
|
+
* @returns The parsed {@link ScanSettings} object if file exists and is valid JSON, otherwise `undefined`.
|
|
135
|
+
*/
|
|
136
|
+
export declare function readScanSettings(scan_settings_path?: string): Promise<ScanSettingsOpened | undefined>;
|
|
137
|
+
export declare function readPrivateSpendKeyFromEnv(primary_address: string): string | undefined;
|
|
138
|
+
export declare function readPrivateViewKeyFromEnv(primary_address: string): string | undefined;
|
|
139
|
+
export declare function readWalletFromScanSettings(primary_address: string, scan_settings_path?: string): Promise<ScanSettingOpened | undefined>;
|
|
140
|
+
export declare function readWalletsFromScanSettings(scan_settings_path?: string): Promise<ScanSettingOpened[]>;
|
|
141
|
+
export declare function walletSettingsPlusKeys(wallet_settings: ScanSettingOpened, secret_view_key?: string, secret_spend_key?: string): Promise<{
|
|
142
|
+
secret_view_key: string;
|
|
143
|
+
secret_spend_key: string | undefined;
|
|
144
|
+
primary_address: string;
|
|
145
|
+
start_height: number | null;
|
|
146
|
+
node_url: string;
|
|
147
|
+
subaddress_index?: number;
|
|
148
|
+
halted?: boolean;
|
|
149
|
+
wallet_route?: string;
|
|
150
|
+
wallet_name?: string;
|
|
151
|
+
wallet_slot?: number;
|
|
152
|
+
}>;
|
|
153
|
+
export declare function writeWalletToScanSettings(params: WriteScanSettingParams): Promise<number>;
|
|
154
|
+
export declare function openScanSettingsFile(scan_settings_path?: string): Promise<ScanSettings | undefined>;
|
|
155
|
+
export declare function openNonHaltedWallets(scan_settings_path?: string): Promise<ScanSettingOpened[]>;
|
|
156
|
+
export declare function getNonHaltedWallets(scan_settings?: ScanSettings): ScanSetting[];
|
|
157
|
+
export declare function doesScanSettingsFileExist(scan_settings_path?: string): Promise<boolean>;
|
|
158
|
+
/**
|
|
159
|
+
* Either 1. the consumer explicitly sets a pathprefix, we extract it from the scan_settings_path
|
|
160
|
+
* or we pick the default which is nothing
|
|
161
|
+
* @param scan_settings_path optional scan settings path
|
|
162
|
+
* @param pathPrefix optional path prefix
|
|
163
|
+
* @returns definite path prefix
|
|
164
|
+
*/
|
|
165
|
+
export declare function getPathPrefix(scan_settings_path?: string, pathPrefix?: string): string;
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { type LogSetting, type PossibleLogs } from "../io/logging";
|
|
2
|
+
import { type ScanSetting, type ScanSettingOpened } from "../api";
|
|
3
|
+
export declare class ScanSettingsOpened {
|
|
4
|
+
private _scan_settings_path;
|
|
5
|
+
private _pathPrefix;
|
|
6
|
+
/**
|
|
7
|
+
* open the settings file and return a new ScanSettingsOpened instance.
|
|
8
|
+
* if the file does not exist, creates one with defaults.
|
|
9
|
+
*/
|
|
10
|
+
static create(scan_settings_path?: string, pathPrefix?: string): Promise<ScanSettingsOpened>;
|
|
11
|
+
get node_url(): string;
|
|
12
|
+
get start_height(): number | null;
|
|
13
|
+
get merchant_confirmations(): number | null | undefined;
|
|
14
|
+
get cpu_worker_count(): number | undefined;
|
|
15
|
+
get logs(): LogSetting | undefined;
|
|
16
|
+
get logs_include(): PossibleLogs[] | undefined;
|
|
17
|
+
get logs_exclude(): PossibleLogs[] | undefined;
|
|
18
|
+
get wallets(): ScanSetting[];
|
|
19
|
+
get scan_settings_path(): string;
|
|
20
|
+
get pathPrefix(): string;
|
|
21
|
+
setNodeUrl(node_url: string): Promise<void>;
|
|
22
|
+
setStartHeight(start_height: number | null): Promise<void>;
|
|
23
|
+
setMerchantConfirmations(merchant_confirmations: number | undefined | null): Promise<void>;
|
|
24
|
+
setCpuWorkerCount(cpu_worker_count: number | undefined | null): Promise<void>;
|
|
25
|
+
setLogSettings(logs?: LogSetting | null, logs_include?: PossibleLogs[] | null, logs_exclude?: PossibleLogs[] | null): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* get a wallet's settings (without env keys).
|
|
28
|
+
* returns undefined if not found.
|
|
29
|
+
*/
|
|
30
|
+
getWallet(primary_address: string): ScanSetting | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* get a wallet's settings merged with env keys.
|
|
33
|
+
* throws if not found or view key missing.
|
|
34
|
+
*/
|
|
35
|
+
getWalletOpened(primary_address: string): Promise<ScanSettingOpened>;
|
|
36
|
+
/**
|
|
37
|
+
* get all wallets merged with env keys.
|
|
38
|
+
* wallets missing env keys are skipped.
|
|
39
|
+
*/
|
|
40
|
+
getWalletsOpened(): Promise<ScanSettingOpened[]>;
|
|
41
|
+
walletExists(primary_address: string): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* add a new view wallet to settings and write its view key to env.
|
|
44
|
+
* if the wallet already exists, updates it instead.
|
|
45
|
+
*/
|
|
46
|
+
addViewWallet(primary_address: string, view_key: string, fields?: {
|
|
47
|
+
wallet_name?: string;
|
|
48
|
+
wallet_slot?: number;
|
|
49
|
+
wallet_route?: string;
|
|
50
|
+
subaddress_index?: number;
|
|
51
|
+
halted?: boolean;
|
|
52
|
+
}): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* add a new view wallet to settings and write its view key to env.
|
|
55
|
+
* if the wallet already exists, updates it instead.
|
|
56
|
+
*/
|
|
57
|
+
addSpendWallet(wallet_secret: Uint8Array, fields?: {
|
|
58
|
+
wallet_name?: string;
|
|
59
|
+
wallet_slot?: number;
|
|
60
|
+
wallet_route?: string;
|
|
61
|
+
subaddress_index?: number;
|
|
62
|
+
halted?: boolean;
|
|
63
|
+
}): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* remove a wallet from settings by primary address.
|
|
66
|
+
* does not remove the view key from env.
|
|
67
|
+
*/
|
|
68
|
+
removeWallet(primary_address: string): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* update specific fields on an existing wallet.
|
|
71
|
+
* set a field to null to unset it.
|
|
72
|
+
*/
|
|
73
|
+
updateWallet(primary_address: string, fields: {
|
|
74
|
+
wallet_name?: string | null;
|
|
75
|
+
wallet_slot?: number | null;
|
|
76
|
+
wallet_route?: string | null;
|
|
77
|
+
subaddress_index?: number | null;
|
|
78
|
+
halted?: boolean | null;
|
|
79
|
+
}): Promise<void>;
|
|
80
|
+
haltWallet(primary_address: string): Promise<void>;
|
|
81
|
+
unhaltWallet(primary_address: string): Promise<void>;
|
|
82
|
+
setWalletName(primary_address: string, name?: string): Promise<void>;
|
|
83
|
+
setWalletSlot(primary_address: string, slot?: number): Promise<void>;
|
|
84
|
+
setWalletRoute(primary_address: string, route?: string): Promise<void>;
|
|
85
|
+
setSubaddressIndex(primary_address: string, index?: number): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* reread the settings file from disk and replace inmemory state.
|
|
88
|
+
*/
|
|
89
|
+
reload(): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* validate that all entries in logs_include and logs_exclude
|
|
92
|
+
* are valid function names from LOGGING_FUNCTIONS.
|
|
93
|
+
* throws if any entry is invalid.
|
|
94
|
+
*/
|
|
95
|
+
private static _validateLogOptions;
|
|
96
|
+
private _settings;
|
|
97
|
+
private constructor();
|
|
98
|
+
/**
|
|
99
|
+
* write current in memory state to disk.
|
|
100
|
+
*/
|
|
101
|
+
private _persist;
|
|
102
|
+
}
|