Skip to content

Commit

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

async makeRevoke(name) {
async makeRevoke(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 @@ -3057,6 +3062,9 @@ class Wallet extends EventEmitter {
if (!coin)
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}".`);

// Is local?
if (coin.height < ns.height)
throw new Error(`Wallet does not own: "${name}".`);
Expand Down Expand Up @@ -3100,7 +3108,8 @@ class Wallet extends EventEmitter {
*/

async _createRevoke(name, options) {
const mtx = await this.makeRevoke(name);
const acct = options ? options.account : null;
const mtx = await this.makeRevoke(name, acct);
await this.fill(mtx, options);
return this.finalize(mtx, options);
}
Expand Down Expand Up @@ -3131,7 +3140,7 @@ class Wallet extends EventEmitter {

async _sendRevoke(name, options) {
const passphrase = options ? options.passphrase : null;
const mtx = await this._createRevoke(name);
const mtx = await this._createRevoke(name, options);
return this.sendMTX(mtx, passphrase);
}

Expand Down
33 changes: 33 additions & 0 deletions test/wallet-accounts-auction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,37 @@ describe('Multiple accounts participating in same auction', function() {
assert.strictEqual(node.mempool.map.size, 0);
});
});

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

it('should send REVOKE from correct account', async () => {
const tx = await wallet.sendRevoke(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 REVOKE from correct account automatically', async () => {
const tx = await wallet.sendRevoke(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);
});
});
});

0 comments on commit e2f8aa3

Please sign in to comment.