Skip to content

Commit

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

async makeRedeem(name) {
async makeRedeem(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 Down Expand Up @@ -2045,6 +2050,7 @@ class Wallet extends EventEmitter {
if (!own)
continue;

// Winner can not redeem
if (prevout.equals(ns.owner))
continue;

Expand All @@ -2053,6 +2059,9 @@ class Wallet extends EventEmitter {
if (!coin)
continue;

if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index))
continue;

// Is local?
if (coin.height < ns.height)
continue;
Expand Down Expand Up @@ -2084,7 +2093,8 @@ class Wallet extends EventEmitter {
*/

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

describe('REDEEM', function() {
describe('Library methods', function() {
it('reject from wrongly specified account', async () => {
await assert.rejects(async () => {
await wallet.sendRedeem(name, {account: 0});
}, {
name: 'Error',
message: 'No reveals to redeem.'
});
});

it('send from correctly specified account', async () => {
const tx = await wallet.sendRedeem(name, {account: 'bob'});
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.sendRedeem(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}/redeem`, {
name: name,
account: 'default'
});
}, {
name: 'Error',
message: 'No reveals to redeem.'
});
});

it('send from correctly specified account', async () => {
const tx = await wclient.post(`wallet/${wallet.id}/redeem`, {
name: name,
account: 'bob'
});
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}/redeem`, {
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('sendredeem', [
name,
'default'
]);
}, {
name: 'Error',
message: 'No reveals to redeem.'
});
});

it('send from correctly specified account', async () => {
const tx = await wclient.execute('sendredeem', [
name,
'bob'
]);
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('sendredeem', [
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 3c075a4

Please sign in to comment.