Skip to content

Commit e326f94

Browse files
committed
wallet: check account ownership of name before making transfer TX
also: make finalize
1 parent a4755e6 commit e326f94

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

lib/wallet/wallet.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2626,16 +2626,22 @@ class Wallet extends EventEmitter {
26262626
* Make a transfer MTX.
26272627
* @param {String} name
26282628
* @param {Address} address
2629+
* @param {String|Number} acct
26292630
* @returns {MTX}
26302631
*/
26312632

2632-
async makeTransfer(name, address) {
2633+
async makeTransfer(name, address, acct) {
26332634
assert(typeof name === 'string');
26342635
assert(address instanceof Address);
26352636

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

2640+
if (acct != null) {
2641+
assert((acct >>> 0) === acct || typeof acct === 'string');
2642+
acct = await this.getAccountIndex(acct);
2643+
}
2644+
26392645
const rawName = Buffer.from(name, 'ascii');
26402646
const nameHash = rules.hashName(rawName);
26412647
const ns = await this.getNameState(nameHash);
@@ -2658,6 +2664,9 @@ class Wallet extends EventEmitter {
26582664
if (coin.height < ns.height)
26592665
throw new Error(`Wallet does not own: "${name}".`);
26602666

2667+
if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index))
2668+
throw new Error(`Account does not own: "${name}".`);
2669+
26612670
const state = ns.state(height, network);
26622671

26632672
if (state !== states.CLOSED)
@@ -2696,7 +2705,8 @@ class Wallet extends EventEmitter {
26962705
*/
26972706

26982707
async _createTransfer(name, address, options) {
2699-
const mtx = await this.makeTransfer(name, address);
2708+
const acct = options ? options.account : null;
2709+
const mtx = await this.makeTransfer(name, address, acct);
27002710
await this.fill(mtx, options);
27012711
return this.finalize(mtx, options);
27022712
}
@@ -2876,15 +2886,21 @@ class Wallet extends EventEmitter {
28762886
* Make a transfer-finalizing MTX.
28772887
* @private
28782888
* @param {String} name
2889+
* @param {String|Number} acct
28792890
* @returns {MTX}
28802891
*/
28812892

2882-
async makeFinalize(name) {
2893+
async makeFinalize(name, acct) {
28832894
assert(typeof name === 'string');
28842895

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

2899+
if (acct != null) {
2900+
assert((acct >>> 0) === acct || typeof acct === 'string');
2901+
acct = await this.getAccountIndex(acct);
2902+
}
2903+
28882904
const rawName = Buffer.from(name, 'ascii');
28892905
const nameHash = rules.hashName(rawName);
28902906
const ns = await this.getNameState(nameHash);
@@ -2907,6 +2923,9 @@ class Wallet extends EventEmitter {
29072923
if (coin.height < ns.height)
29082924
throw new Error(`Wallet does not own: "${name}".`);
29092925

2926+
if (acct != null && !await this.txdb.hasCoinByAccount(acct, hash, index))
2927+
throw new Error(`Account does not own: "${name}".`);
2928+
29102929
const state = ns.state(height, network);
29112930

29122931
if (state !== states.CLOSED)
@@ -2955,7 +2974,8 @@ class Wallet extends EventEmitter {
29552974
*/
29562975

29572976
async _createFinalize(name, options) {
2958-
const mtx = await this.makeFinalize(name);
2977+
const acct = options ? options.account : null;
2978+
const mtx = await this.makeFinalize(name, acct);
29592979
await this.fill(mtx, options);
29602980
return this.finalize(mtx, options);
29612981
}

test/wallet-accounts-auction-test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,81 @@ describe('Multiple accounts participating in same auction', function() {
256256
assert.strictEqual(node.mempool.map.size, 0);
257257
});
258258
});
259+
260+
describe('TRANSFER', function() {
261+
// Alice will transfer to Bob
262+
let toAddr;
263+
264+
before(async () => {
265+
toAddr = await bob.receiveAddress();
266+
});
267+
268+
it('should reject TRANSFER from wrong account', async () => {
269+
await assert.rejects(async () => {
270+
await wallet.sendTransfer(name, toAddr, {account: 'bob'});
271+
}, {
272+
name: 'Error',
273+
message: `Account does not own: "${name}".`
274+
});
275+
});
276+
277+
it('should send TRANSFER from correct account', async () => {
278+
const tx = await wallet.sendTransfer(name, toAddr, {account: 0});
279+
assert(tx);
280+
281+
await wallet.abandon(tx.hash());
282+
283+
assert.strictEqual(node.mempool.map.size, 1);
284+
await node.mempool.reset();
285+
assert.strictEqual(node.mempool.map.size, 0);
286+
});
287+
288+
it('should send TRANSFER from correct account automatically', async () => {
289+
const tx = await wallet.sendTransfer(name, toAddr);
290+
assert(tx);
291+
292+
await mineBlocks(1);
293+
});
294+
});
295+
296+
describe('FINALIZE', function() {
297+
it('should advance chain until FINALIZE is allowed', async () => {
298+
await mineBlocks(network.names.transferLockup);
299+
const ns = await node.chain.db.getNameStateByName(name);
300+
assert(ns.isClosed(node.chain.height, network));
301+
302+
await wdb.rescan(0);
303+
});
304+
305+
it('should reject FINALIZE from wrong account', async () => {
306+
await assert.rejects(async () => {
307+
await wallet.sendFinalize(name, {account: 'bob'});
308+
}, {
309+
name: 'Error',
310+
message: `Account does not own: "${name}".`
311+
});
312+
});
313+
314+
it('should send FINALIZE from correct account', async () => {
315+
const tx = await wallet.sendFinalize(name, {account: 0});
316+
assert(tx);
317+
318+
await wallet.abandon(tx.hash());
319+
320+
assert.strictEqual(node.mempool.map.size, 1);
321+
await node.mempool.reset();
322+
assert.strictEqual(node.mempool.map.size, 0);
323+
});
324+
325+
it('should send FINALIZE from correct account automatically', async () => {
326+
const tx = await wallet.sendFinalize(name);
327+
assert(tx);
328+
329+
await wallet.abandon(tx.hash());
330+
331+
assert.strictEqual(node.mempool.map.size, 1);
332+
await node.mempool.reset();
333+
assert.strictEqual(node.mempool.map.size, 0);
334+
});
335+
});
259336
});

0 commit comments

Comments
 (0)