Skip to content

Commit 8204e66

Browse files
committed
wallet: check account ownership of name before making cancel TX
1 parent e326f94 commit 8204e66

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

lib/wallet/wallet.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,15 +2768,21 @@ class Wallet extends EventEmitter {
27682768
* Make a transfer-cancelling MTX.
27692769
* @private
27702770
* @param {String} name
2771+
* @param {String|Number} acct
27712772
* @returns {MTX}
27722773
*/
27732774

2774-
async makeCancel(name) {
2775+
async makeCancel(name, acct) {
27752776
assert(typeof name === 'string');
27762777

27772778
if (!rules.verifyName(name))
27782779
throw new Error('Invalid name.');
27792780

2781+
if (acct != null) {
2782+
assert((acct >>> 0) === acct || typeof acct === 'string');
2783+
acct = await this.getAccountIndex(acct);
2784+
}
2785+
27802786
const rawName = Buffer.from(name, 'ascii');
27812787
const nameHash = rules.hashName(rawName);
27822788
const ns = await this.getNameState(nameHash);
@@ -2799,6 +2805,9 @@ class Wallet extends EventEmitter {
27992805
if (coin.height < ns.height)
28002806
throw new Error(`Wallet does not own: "${name}".`);
28012807

2808+
if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index))
2809+
throw new Error(`Account does not own: "${name}".`);
2810+
28022811
const state = ns.state(height, network);
28032812

28042813
if (state !== states.CLOSED)
@@ -2831,7 +2840,8 @@ class Wallet extends EventEmitter {
28312840
*/
28322841

28332842
async _createCancel(name, options) {
2834-
const mtx = await this.makeCancel(name);
2843+
const acct = options ? options.account : null;
2844+
const mtx = await this.makeCancel(name, acct);
28352845
await this.fill(mtx, options);
28362846
return this.finalize(mtx, options);
28372847
}

test/wallet-accounts-auction-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,37 @@ describe('Multiple accounts participating in same auction', function() {
333333
assert.strictEqual(node.mempool.map.size, 0);
334334
});
335335
});
336+
337+
describe('CANCEL', function() {
338+
it('should reject CANCEL from wrong account', async () => {
339+
await assert.rejects(async () => {
340+
await wallet.sendCancel(name, {account: 'bob'});
341+
}, {
342+
name: 'Error',
343+
message: `Account does not own: "${name}".`
344+
});
345+
});
346+
347+
it('should send CANCEL from correct account', async () => {
348+
const tx = await wallet.sendCancel(name, {account: 0});
349+
assert(tx);
350+
351+
await wallet.abandon(tx.hash());
352+
353+
assert.strictEqual(node.mempool.map.size, 1);
354+
await node.mempool.reset();
355+
assert.strictEqual(node.mempool.map.size, 0);
356+
});
357+
358+
it('should send CANCEL from correct account automatically', async () => {
359+
const tx = await wallet.sendCancel(name);
360+
assert(tx);
361+
362+
await wallet.abandon(tx.hash());
363+
364+
assert.strictEqual(node.mempool.map.size, 1);
365+
await node.mempool.reset();
366+
assert.strictEqual(node.mempool.map.size, 0);
367+
});
368+
});
336369
});

0 commit comments

Comments
 (0)