Skip to content

Commit

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

async makeRenewal(name) {
async makeRenewal(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 @@ -2520,6 +2525,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 @@ -2559,7 +2567,8 @@ class Wallet extends EventEmitter {
*/

async _createRenewal(name, options) {
const mtx = await this.makeRenewal(name);
const acct = options ? options.account : null;
const mtx = await this.makeRenewal(name, acct);
await this.fill(mtx, options);
return this.finalize(mtx, options);
}
Expand Down
127 changes: 126 additions & 1 deletion test/wallet-accounts-auction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,9 @@ describe('Multiple accounts participating in same auction', function() {

await wallet.abandon(Buffer.from(tx.hash, 'hex'));

// This time we will confirm the REGISTER so we can RENEW and TRANSFER
assert.strictEqual(node.mempool.map.size, 1);
await node.mempool.reset();
await mineBlocks(1);
assert.strictEqual(node.mempool.map.size, 0);
});
});
Expand Down Expand Up @@ -471,4 +472,128 @@ describe('Multiple accounts participating in same auction', function() {
});
});
});

describe('RENEW', function() {
it('should advance chain to allow renewal', async () => {
await mineBlocks(network.names.treeInterval);
await wdb.rescan(0);
});

describe('Library methods', function() {
it('reject from wrongly specified account', async () => {
await assert.rejects(async () => {
await wallet.sendRenewal(name, {account: 'bob'});
}, {
name: 'Error',
message: `Account does not own: "${name}".`
});
});

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

describe('HTTP API', function () {
it('reject from wrongly specified account', async () => {
await assert.rejects(async () => {
await wclient.post(`wallet/${wallet.id}/renewal`, {
name: name,
account: 'bob'
});
}, {
name: 'Error',
message: `Account does not own: "${name}".`
});
});

it('send from correctly specified account', async () => {
const tx = await wclient.post(`wallet/${wallet.id}/renewal`, {
name: name,
account: 'default'
});
assert(tx);

await wallet.abandon(Buffer.from(tx.hash, 'hex'));

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

it('send from correct account automatically', async () => {
const tx = await wclient.post(`wallet/${wallet.id}/renewal`, {
name: name
});
assert(tx);

await wallet.abandon(Buffer.from(tx.hash, 'hex'));

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

describe('RPC API', function() {
it('reject from wrongly specified account', async () => {
await wclient.execute('selectwallet', [wallet.id]);

await assert.rejects(async () => {
await wclient.execute('sendrenewal', [
name,
'bob'
]);
}, {
name: 'Error',
message: `Account does not own: "${name}".`
});
});

it('send from correctly specified account', async () => {
const tx = await wclient.execute('sendrenewal', [
name,
'default'
]);
assert(tx);

await wallet.abandon(Buffer.from(tx.hash, 'hex'));

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

it('send from correct account automatically', async () => {
const tx = await wclient.execute('sendrenewal', [
name
]);
assert(tx);

await wallet.abandon(Buffer.from(tx.hash, 'hex'));

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

0 comments on commit 19219ca

Please sign in to comment.