-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathTestMoneroWalletRpc.ts
436 lines (365 loc) · 19.4 KB
/
TestMoneroWalletRpc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import assert from "assert";
import TestUtils from "./utils/TestUtils";
import TestMoneroWalletCommon from "./TestMoneroWalletCommon";
import TestMoneroWalletFull from "./TestMoneroWalletFull";
import {MoneroError,
GenUtils,
MoneroWalletConfig,
MoneroUtils,
MoneroAccountTag,
MoneroWalletRpc} from "../../index";
/**
* Tests the Monero Wallet RPC client and server.
*/
export default class TestMoneroWalletRpc extends TestMoneroWalletCommon {
constructor(testConfig) {
super(testConfig);
}
async beforeAll() {
await super.beforeAll();
// if full tests ran, wait for full wallet's pool txs to confirm
if (TestMoneroWalletFull.FULL_TESTS_RUN) {
let walletFull = await TestUtils.getWalletFull();
await TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(walletFull);
await walletFull.close(true);
}
}
async beforeEach(currentTest) {
await super.beforeEach(currentTest);
}
async afterAll() {
await super.afterAll();
for (let portOffset of Object.keys(TestUtils.WALLET_PORT_OFFSETS)) { // TODO: this breaks encapsulation, use MoneroWalletRpcManager
console.error("WARNING: Wallet RPC process on port " + (TestUtils.WALLET_RPC_PORT_START + Number(portOffset)) + " was not stopped after all tests, stopping");
await TestUtils.stopWalletRpcProcess(TestUtils.WALLET_PORT_OFFSETS[portOffset]);
}
}
async afterEach(currentTest) {
await super.afterEach(currentTest);
}
async getTestWallet() {
return TestUtils.getWalletRpc();
}
async getTestDaemon() {
return TestUtils.getDaemonRpc();
}
async openWallet(config) {
// assign defaults
config = new MoneroWalletConfig(config);
if (config.getPassword() === undefined) config.setPassword(TestUtils.WALLET_PASSWORD);
if (!config.getServer() && !config.getConnectionManager()) config.setServer(await this.daemon.getRpcConnection());
// create client connected to internal monero-wallet-rpc executable
let offline = config.getServer() && config.getServer().getUri() === TestUtils.OFFLINE_SERVER_URI;
let wallet = await TestUtils.startWalletRpcProcess(offline);
// open wallet
try {
await wallet.openWallet(config);
await wallet.setDaemonConnection(await wallet.getDaemonConnection(), true, undefined); // set daemon as trusted
if (await wallet.isConnectedToDaemon()) await wallet.startSyncing(TestUtils.SYNC_PERIOD_IN_MS);
return wallet;
} catch (err) {
await TestUtils.stopWalletRpcProcess(wallet);
throw err;
}
}
async createWallet(config: Partial<MoneroWalletConfig>) {
// assign defaults
config = new MoneroWalletConfig(config);
let random = !config.getSeed() && !config.getPrimaryAddress();
if (!config.getPath()) config.setPath(GenUtils.getUUID());
if (config.getPassword() === undefined) config.setPassword(TestUtils.WALLET_PASSWORD);
if (!config.getRestoreHeight() && !random) config.setRestoreHeight(0);
if (!config.getServer() && !config.getConnectionManager()) config.setServer(await this.daemon.getRpcConnection());
// create client connected to internal monero-wallet-rpc executable
let offline = config.getServer() && config.getServer().getUri() === GenUtils.normalizeUri(TestUtils.OFFLINE_SERVER_URI);
let wallet = await TestUtils.startWalletRpcProcess(offline);
// create wallet
try {
await wallet.createWallet(config);
await wallet.setDaemonConnection(await wallet.getDaemonConnection(), true, undefined); // set daemon as trusted
if (await wallet.isConnectedToDaemon()) await wallet.startSyncing(TestUtils.SYNC_PERIOD_IN_MS);
return wallet;
} catch (err) {
await TestUtils.stopWalletRpcProcess(wallet);
throw err;
}
}
async closeWallet(wallet, save?) {
await wallet.close(save);
await TestUtils.stopWalletRpcProcess(wallet);
}
async getSeedLanguages(): Promise<string[]> {
return await (this.wallet as MoneroWalletRpc).getSeedLanguages();
}
runTests() {
let that = this;
let testConfig = this.testConfig;
describe("TEST MONERO WALLET RPC", function() {
// register handlers to run before and after tests
before(async function() { await that.beforeAll(); });
beforeEach(async function() { await that.beforeEach(this.currentTest); });
after(async function() { await that.afterAll(); });
afterEach(async function() { await that.afterEach(this.currentTest); });
// run tests specific to wallet rpc
that.testWalletRpc(testConfig);
// run common tests
that.runCommonTests(testConfig);
});
}
// ---------------------------------- PRIVATE -------------------------------
// rpc-specific tx test
async testTxWallet(tx, ctx) {
ctx = Object.assign({}, ctx);
// run common tests
await super.testTxWallet(tx, ctx);
}
// rpc-specific out-of-range subaddress test
async testGetSubaddressAddressOutOfRange() {
let accounts = await this.wallet.getAccounts(true);
let accountIdx = accounts.length - 1;
let subaddressIdx = accounts[accountIdx].getSubaddresses().length;
let address = await this.wallet.getAddress(accountIdx, subaddressIdx);
assert.equal(address, undefined);
}
testInvalidAddressError(err) {
super.testInvalidAddressError(err);
assert.equal(-2, err.getCode());
}
testInvalidTxHashError(err) {
super.testInvalidTxHashError(err);
assert.equal(-8, err.getCode());
}
testInvalidTxKeyError(err) {
super.testInvalidTxKeyError(err);
assert.equal(-25, err.getCode());
}
testInvalidSignatureError(err) {
super.testInvalidSignatureError(err);
assert.equal(-1, err.getCode());
}
testNoSubaddressError(err) {
super.testNoSubaddressError(err);
assert.equal(-1, err.getCode());
}
testSignatureHeaderCheckError(err) {
super.testSignatureHeaderCheckError(err);
assert.equal(-1, err.getCode());
}
protected testWalletRpc(testConfig) {
let that = this;
describe("Tests specific to RPC wallet", function() {
// ---------------------------- BEGIN TESTS ---------------------------------
if (testConfig.testNonRelays)
it("Can create a wallet with a randomly generated mnemonic", async function() {
// create random wallet with defaults
let path = GenUtils.getUUID();
let wallet = await that.createWallet({ path: path });
let mnemonic = await wallet.getSeed();
await MoneroUtils.validateMnemonic(mnemonic);
assert.notEqual(mnemonic, TestUtils.SEED);
await MoneroUtils.validateAddress(await wallet.getPrimaryAddress(), TestUtils.NETWORK_TYPE);
await wallet.sync(); // very quick because restore height is chain height
await that.closeWallet(wallet);
// create random wallet with non defaults
path = GenUtils.getUUID();
wallet = await that.createWallet({ path: path, language: "Spanish" });
await MoneroUtils.validateMnemonic(await wallet.getSeed());
assert.notEqual(await wallet.getSeed(), mnemonic);
mnemonic = await wallet.getSeed();
await MoneroUtils.validateAddress(await wallet.getPrimaryAddress(), TestUtils.NETWORK_TYPE);
// attempt to create wallet which already exists
try {
await that.createWallet({ path: path, language: "Spanish" });
} catch (e: any) {
assert.equal(e.message, "Wallet already exists: " + path);
assert.equal(-21, e.getCode())
assert.equal(mnemonic, await wallet.getSeed());
}
await that.closeWallet(wallet);
});
if (testConfig.testNonRelays)
it("Can create a RPC wallet from a mnemonic phrase", async function() {
// create wallet with mnemonic and defaults
let path = GenUtils.getUUID();
let wallet = await that.createWallet({ path: path, password: TestUtils.WALLET_PASSWORD, seed: TestUtils.SEED, restoreHeight: TestUtils.FIRST_RECEIVE_HEIGHT });
assert.equal(await wallet.getSeed(), TestUtils.SEED);
assert.equal(await wallet.getPrimaryAddress(), TestUtils.ADDRESS);
await wallet.sync();
assert.equal(await wallet.getHeight(), await that.daemon.getHeight());
let txs = await wallet.getTxs();
assert(txs.length > 0); // wallet is used
assert.equal(txs[0].getHeight(), TestUtils.FIRST_RECEIVE_HEIGHT);
await that.closeWallet(wallet);
// create wallet with non-defaults
path = GenUtils.getUUID();
wallet = await that.createWallet({ path: path, password: TestUtils.WALLET_PASSWORD, seed: TestUtils.SEED, restoreHeight: TestUtils.FIRST_RECEIVE_HEIGHT, language: "German", seedOffset: "my offset!", saveCurrent: false });
await MoneroUtils.validateMnemonic(await wallet.getSeed());
assert.notEqual(await wallet.getSeed(), TestUtils.SEED); // mnemonic is different because of offset
assert.notEqual(await wallet.getPrimaryAddress(), TestUtils.ADDRESS);
await that.closeWallet(wallet);
});
if (testConfig.testNonRelays)
it("Can open wallets", async function() {
// create names of test wallets
let numTestWallets = 3;
let names: string[] = [];
for (let i = 0; i < numTestWallets; i++) names.push(GenUtils.getUUID());
// create test wallets
let mnemonics: string[] = [];
for (let name of names) {
let wallet = await that.createWallet({ path: name, password: TestUtils.WALLET_PASSWORD });
mnemonics.push(await wallet.getSeed());
await that.closeWallet(wallet, true);
}
// open test wallets
let wallets: MoneroWalletRpc[] = [];
for (let i = 0; i < numTestWallets; i++) {
let wallet = await that.openWallet({ path: names[i], password: TestUtils.WALLET_PASSWORD });
assert.equal(await wallet.getSeed(), mnemonics[i]);
wallets.push(wallet);
}
// attempt to re-open already opened wallet
try {
await that.openWallet({ path: names[numTestWallets - 1], password: TestUtils.WALLET_PASSWORD });
} catch (e: any) {
assert.equal(e.getCode(), -1);
}
// attempt to open non-existent
try {
await that.openWallet({ path: "btc_integrity", password: TestUtils.WALLET_PASSWORD });
throw new Error("Cannot open wallet which is already open");
} catch (e: any) {
assert(e instanceof MoneroError);
assert.equal(e.getCode(), -1); // -1 indicates wallet does not exist (or is open by another app)
}
// close wallets
for (let wallet of wallets) await that.closeWallet(wallet);
});
if (testConfig.testNonRelays)
it("Can indicate if multisig import is needed for correct balance information", async function() {
assert.equal(await that.wallet.isMultisigImportNeeded(), false);
});
if (testConfig.testNonRelays)
it("Can tag accounts and query accounts by tag", async function() {
// get accounts
let accounts = await that.wallet.getAccounts();
assert(accounts.length >= 3, "Not enough accounts to test; run create account test");
// tag some of the accounts
let tag = new MoneroAccountTag({tag: "my_tag_" + GenUtils.getUUID(), label: "my tag label", accountIndices: [0, 1]});
await that.wallet.tagAccounts(tag.getTag(), tag.getAccountIndices());
// query accounts by tag
let taggedAccounts = await that.wallet.getAccounts(undefined, tag.getTag());
assert.equal(taggedAccounts.length, 2);
assert.equal(taggedAccounts[0].getIndex(), 0);
assert.equal(taggedAccounts[0].getTag(), tag.getTag());
assert.equal(taggedAccounts[1].getIndex(), 1);
assert.equal(taggedAccounts[1].getTag(), tag.getTag());
// set tag label
await that.wallet.setAccountTagLabel(tag.getTag(), tag.getLabel());
// fetch tags and ensure new tag is contained
let tags = await that.wallet.getAccountTags();
assert(GenUtils.arrayContains(tags, tag));
// re-tag an account
let tag2 = new MoneroAccountTag({tag: "my_tag_" + GenUtils.getUUID(), label: "my tag label 2", accountIndices: [1]});
await that.wallet.tagAccounts(tag2.getTag(), tag2.getAccountIndices());
let taggedAccounts2 = await that.wallet.getAccounts(undefined, tag2.getTag())
assert.equal(taggedAccounts2.length, 1);
assert.equal(taggedAccounts2[0].getIndex(), 1);
assert.equal(taggedAccounts2[0].getTag(), tag2.getTag());
// re-query original tag which only applies to one account now
taggedAccounts = await that.wallet.getAccounts(undefined, tag.getTag());
assert.equal(taggedAccounts.length, 1);
assert.equal(taggedAccounts[0].getIndex(), 0);
assert.equal(taggedAccounts[0].getTag(), tag.getTag());
// untag and query accounts
await that.wallet.untagAccounts([0, 1]);
assert.equal((await that.wallet.getAccountTags()).length, 0);
try {
await that.wallet.getAccounts(undefined, tag.getTag());
throw new Error("Should have thrown exception with unregistered tag");
} catch (e: any) {
assert.equal(e.getCode(), -1);
}
// test that non-existing tag returns no accounts
try {
await that.wallet.getAccounts(undefined, "non_existing_tag");
throw new Error("Should have thrown exception with unregistered tag");
} catch (e: any) {
assert.equal(e.getCode(), -1);
}
});
if (testConfig.testNonRelays)
it("Can fetch accounts and subaddresses without balance info because this is another RPC call", async function() {
let accounts = await (that.wallet as MoneroWalletRpc).getAccounts(true, undefined, true);
assert(accounts.length > 0);
for (let account of accounts) {
assert(account.getSubaddresses().length > 0);
for (let subaddress of account.getSubaddresses()) {
assert.equal(typeof subaddress.getAddress(), "string");
assert(subaddress.getAddress().length > 0);
assert(subaddress.getAccountIndex() >= 0);
assert(subaddress.getIndex() >= 0);
assert(subaddress.getLabel() === undefined || typeof subaddress.getLabel() === "string");
if (typeof subaddress.getLabel() === "string") assert(subaddress.getLabel().length > 0);
assert.equal(typeof subaddress.getIsUsed(), "boolean");
assert.equal(subaddress.getNumUnspentOutputs(), undefined);
assert.equal(subaddress.getBalance(), undefined);
assert.equal(subaddress.getUnlockedBalance(), undefined);
}
}
});
if (testConfig.testNonRelays)
it("Can rescan spent", async function() {
await that.wallet.rescanSpent();
});
if (testConfig.testNonRelays)
it("Can save the wallet file", async function() {
await that.wallet.save();
});
if (testConfig.testNonRelays)
it("Can close a wallet", async function() {
// create a test wallet
let path = GenUtils.getUUID();
let wallet = await that.createWallet({ path: path, password: TestUtils.WALLET_PASSWORD });
await wallet.sync();
assert((await wallet.getHeight()) > 1);
// close the wallet
await wallet.close();
// attempt to interact with the wallet
try {
await wallet.getHeight();
} catch (e: any) {
assert.equal(e.getCode(), -13);
assert.equal(e.message, "No wallet file");
}
try {
await wallet.getSeed();
} catch (e: any) {
assert.equal(e.getCode(), -13);
assert.equal(e.message, "No wallet file");
}
try {
await wallet.sync();
} catch (e: any) {
assert.equal(e.getCode(), -13);
assert.equal(e.message, "No wallet file");
}
// re-open the wallet
await wallet.openWallet(path, TestUtils.WALLET_PASSWORD);
await wallet.sync();
assert.equal(await wallet.getHeight(), await that.daemon.getHeight());
// close the wallet
await that.closeWallet(wallet, true);
});
if (false && testConfig.testNonRelays) // disabled so server not actually stopped
it("Can stop the RPC server", async function() {
await (that.wallet as MoneroWalletRpc).stop();
});
});
}
}
function testAddressBookEntry(entry) {
assert(entry.getIndex() >= 0);
assert(entry.getAddress());
assert(entry.getDescription());
}