Skip to content

Commit

Permalink
wallet: check account ownership of name before making finalize TX
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Mar 20, 2020
1 parent cb7b90e commit ab7faca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2885,12 +2885,17 @@ class Wallet extends EventEmitter {
* @returns {MTX}
*/

async makeFinalize(name) {
async makeFinalize(name, acct) {
assert(typeof name === 'string');

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

if (acct != null) {
assert((acct >>> 0) === acct || typeof acct === 'string');
acct = await this.getAccountIndex(acct);
}

const rawName = Buffer.from(name, 'ascii');
const nameHash = rules.hashName(rawName);
const ns = await this.getNameState(nameHash);
Expand All @@ -2913,6 +2918,9 @@ class Wallet extends EventEmitter {
if (coin.height < ns.height)
throw new Error(`Wallet does not own: "${name}".`);

if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index))
throw new Error(`Account does not own: "${name}".`);

const state = ns.state(height, network);

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

async _createFinalize(name, options) {
const mtx = await this.makeFinalize(name);
const acct = options ? options.account : null;
const mtx = await this.makeFinalize(name, acct);
await this.fill(mtx, options);
return this.finalize(mtx, options);
}
Expand Down
41 changes: 41 additions & 0 deletions test/wallet-accounts-auction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,47 @@ describe('Multiple accounts participating in same auction', function() {

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

assert.strictEqual(node.mempool.map.size, 1);
await mineBlocks(1);
assert.strictEqual(node.mempool.map.size, 0);
});
});

describe('FINALIZE', function() {
it('should advance chain until FINALIZE is allowed', async () => {
await mineBlocks(network.names.transferLockup);
const ns = await node.chain.db.getNameStateByName(name);
assert(ns.isClosed(node.chain.height, network));

await wdb.rescan(0);
});

it('should reject FINALIZE from wrong account', async () => {
await assert.rejects(async () => {
await wallet.sendFinalize(name, {account: 'bob'});
}, {
name: 'Error',
message: `Account does not own: "${name}".`
});
});

it('should send FINALIZE from correct account', async () => {
const tx = await wallet.sendFinalize(name, {account: 0});
assert(tx);

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

assert.strictEqual(node.mempool.map.size, 1);
await node.mempool.reset();
assert.strictEqual(node.mempool.map.size, 0);
});

it('should send FINALIZE from correct account automatically', async () => {
const tx = await wallet.sendFinalize(name);
assert(tx);

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

assert.strictEqual(node.mempool.map.size, 1);
await node.mempool.reset();
assert.strictEqual(node.mempool.map.size, 0);
Expand Down

0 comments on commit ab7faca

Please sign in to comment.