Skip to content

Commit ab7faca

Browse files
committed
wallet: check account ownership of name before making finalize TX
1 parent cb7b90e commit ab7faca

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

lib/wallet/wallet.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,12 +2885,17 @@ class Wallet extends EventEmitter {
28852885
* @returns {MTX}
28862886
*/
28872887

2888-
async makeFinalize(name) {
2888+
async makeFinalize(name, acct) {
28892889
assert(typeof name === 'string');
28902890

28912891
if (!rules.verifyName(name))
28922892
throw new Error('Invalid name.');
28932893

2894+
if (acct != null) {
2895+
assert((acct >>> 0) === acct || typeof acct === 'string');
2896+
acct = await this.getAccountIndex(acct);
2897+
}
2898+
28942899
const rawName = Buffer.from(name, 'ascii');
28952900
const nameHash = rules.hashName(rawName);
28962901
const ns = await this.getNameState(nameHash);
@@ -2913,6 +2918,9 @@ class Wallet extends EventEmitter {
29132918
if (coin.height < ns.height)
29142919
throw new Error(`Wallet does not own: "${name}".`);
29152920

2921+
if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index))
2922+
throw new Error(`Account does not own: "${name}".`);
2923+
29162924
const state = ns.state(height, network);
29172925

29182926
if (state !== states.CLOSED)
@@ -2961,7 +2969,8 @@ class Wallet extends EventEmitter {
29612969
*/
29622970

29632971
async _createFinalize(name, options) {
2964-
const mtx = await this.makeFinalize(name);
2972+
const acct = options ? options.account : null;
2973+
const mtx = await this.makeFinalize(name, acct);
29652974
await this.fill(mtx, options);
29662975
return this.finalize(mtx, options);
29672976
}

test/wallet-accounts-auction-test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,47 @@ describe('Multiple accounts participating in same auction', function() {
300300

301301
await wallet.abandon(tx.hash());
302302

303+
assert.strictEqual(node.mempool.map.size, 1);
304+
await mineBlocks(1);
305+
assert.strictEqual(node.mempool.map.size, 0);
306+
});
307+
});
308+
309+
describe('FINALIZE', function() {
310+
it('should advance chain until FINALIZE is allowed', async () => {
311+
await mineBlocks(network.names.transferLockup);
312+
const ns = await node.chain.db.getNameStateByName(name);
313+
assert(ns.isClosed(node.chain.height, network));
314+
315+
await wdb.rescan(0);
316+
});
317+
318+
it('should reject FINALIZE from wrong account', async () => {
319+
await assert.rejects(async () => {
320+
await wallet.sendFinalize(name, {account: 'bob'});
321+
}, {
322+
name: 'Error',
323+
message: `Account does not own: "${name}".`
324+
});
325+
});
326+
327+
it('should send FINALIZE from correct account', async () => {
328+
const tx = await wallet.sendFinalize(name, {account: 0});
329+
assert(tx);
330+
331+
await wallet.abandon(tx.hash());
332+
333+
assert.strictEqual(node.mempool.map.size, 1);
334+
await node.mempool.reset();
335+
assert.strictEqual(node.mempool.map.size, 0);
336+
});
337+
338+
it('should send FINALIZE from correct account automatically', async () => {
339+
const tx = await wallet.sendFinalize(name);
340+
assert(tx);
341+
342+
await wallet.abandon(tx.hash());
343+
303344
assert.strictEqual(node.mempool.map.size, 1);
304345
await node.mempool.reset();
305346
assert.strictEqual(node.mempool.map.size, 0);

0 commit comments

Comments
 (0)