Skip to content

Commit c8ae6b9

Browse files
committed
wallet: check account ownership of name before making cancel TX
1 parent 0b27ee2 commit c8ae6b9

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

lib/wallet/wallet.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,12 +2767,17 @@ class Wallet extends EventEmitter {
27672767
* @returns {MTX}
27682768
*/
27692769

2770-
async makeCancel(name) {
2770+
async makeCancel(name, acct) {
27712771
assert(typeof name === 'string');
27722772

27732773
if (!rules.verifyName(name))
27742774
throw new Error('Invalid name.');
27752775

2776+
if (acct != null) {
2777+
assert((acct >>> 0) === acct || typeof acct === 'string');
2778+
acct = await this.getAccountIndex(acct);
2779+
}
2780+
27762781
const rawName = Buffer.from(name, 'ascii');
27772782
const nameHash = rules.hashName(rawName);
27782783
const ns = await this.getNameState(nameHash);
@@ -2795,6 +2800,9 @@ class Wallet extends EventEmitter {
27952800
if (coin.height < ns.height)
27962801
throw new Error(`Wallet does not own: "${name}".`);
27972802

2803+
if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index))
2804+
throw new Error(`Account does not own: "${name}".`);
2805+
27982806
const state = ns.state(height, network);
27992807

28002808
if (state !== states.CLOSED)
@@ -2827,7 +2835,8 @@ class Wallet extends EventEmitter {
28272835
*/
28282836

28292837
async _createCancel(name, options) {
2830-
const mtx = await this.makeCancel(name);
2838+
const acct = options ? options.account : null;
2839+
const mtx = await this.makeCancel(name, acct);
28312840
await this.fill(mtx, options);
28322841
return this.finalize(mtx, options);
28332842
}

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)