From bca71d95cdd9d05d85cccaf642b754384abc877b Mon Sep 17 00:00:00 2001 From: Mike Carson Date: Tue, 19 May 2020 15:57:59 -0400 Subject: [PATCH 1/3] wallet: rpc getNames add parameter that only returns names owned by UTXOs controlled by the wallet --- lib/wallet/rpc.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/wallet/rpc.js b/lib/wallet/rpc.js index ff0222aad5..951d9cdefe 100644 --- a/lib/wallet/rpc.js +++ b/lib/wallet/rpc.js @@ -1794,18 +1794,27 @@ class RPC extends RPCBase { } async getNames(args, help) { - if (help || args.length !== 0) - throw new RPCError(errs.MISC_ERROR, 'getnames'); + if (help || args.length > 1) + throw new RPCError(errs.MISC_ERROR, 'getnames ( own )'); + const valid = new Validator(args); const wallet = this.wallet; const height = this.wdb.height; const network = this.network; + const own = valid.bool(0, false); const names = await wallet.getNames(); const items = []; for (const ns of names) - items.push(ns.getJSON(height, network)); + if (own) { + const {hash, index} = ns.owner; + const coin = await wallet.getCoin(hash, index); + if (coin) + items.push(ns.getJSON(height, network)); + } else { + items.push(ns.getJSON(height, network)); + } return items; } From 797383cedea0d7b49e46c728de47a3d6eb8f946b Mon Sep 17 00:00:00 2001 From: Matthew Zipkin Date: Fri, 22 May 2020 11:43:46 -0400 Subject: [PATCH 2/3] test: cover wallet rpc auctions --- test/wallet-rpc-test.js | 89 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/test/wallet-rpc-test.js b/test/wallet-rpc-test.js index db952b7cbe..f332c3314f 100644 --- a/test/wallet-rpc-test.js +++ b/test/wallet-rpc-test.js @@ -45,6 +45,12 @@ const wclient = new WalletClient({ apiKey: 'bar' }); +const { + treeInterval, + biddingPeriod, + revealPeriod +} = network.names; + describe('Wallet RPC Methods', function() { this.timeout(15000); @@ -325,4 +331,87 @@ describe('Wallet RPC Methods', function() { assert.strictEqual(info.height, node.chain.height); }); }); + + describe('Wallet RPC Auction', function() { + let addr1, addr2, name1, name2; + + it('should create wallets', async () => { + await wclient.createWallet('wallet1'); + await wclient.createWallet('wallet2'); + }); + + it('should get wallet addresses', async () => { + await wclient.execute('selectwallet', ['wallet1']); + addr1 = await wclient.execute('getnewaddress', []); + await wclient.execute('selectwallet', ['wallet2']); + addr2 = await wclient.execute('getnewaddress', []); + }); + + it('should fund wallets', async () => { + await nclient.execute('generatetoaddress', [10, addr1]); + await nclient.execute('generatetoaddress', [10, addr2]); + }); + + it('should open names', async () => { + name1 = await nclient.execute('grindname', [5]); + name2 = await nclient.execute('grindname', [5]); + + await wclient.execute('selectwallet', ['wallet1']); + await wclient.execute('sendopen', [name1]); + await wclient.execute('sendopen', [name2]); + + // confirm and advance to bidding phase + await nclient.execute('generatetoaddress', [treeInterval + 1, addr1]); + }); + + it('should bid on names', async () => { + // wallet1 will win name1 + await wclient.execute('selectwallet', ['wallet1']); + await wclient.execute('sendbid', [name1, 10, 10]); + await wclient.execute('sendbid', [name2, 5, 5]); + + // wallet2 will win name2 + await wclient.execute('selectwallet', ['wallet2']); + await wclient.execute('sendbid', [name1, 5, 5]); + await wclient.execute('sendbid', [name2, 10, 10]); + + // confirm and advance to reveal phase + await nclient.execute('generatetoaddress', [biddingPeriod + 1, addr1]); + }); + + it('should reveal names', async () => { + await wclient.execute('selectwallet', ['wallet1']); + await wclient.execute('sendreveal', []); + + await wclient.execute('selectwallet', ['wallet2']); + await wclient.execute('sendreveal', []); + + // confirm and advance to close auction + await nclient.execute('generatetoaddress', [revealPeriod + 1, addr1]); + }); + + it('should get all wallet names', async () => { + await wclient.execute('selectwallet', ['wallet1']); + const wallet1AllNames = await wclient.execute('getnames', []); + + await wclient.execute('selectwallet', ['wallet2']); + const wallet2AllNames = await wclient.execute('getnames', []); + + assert.strictEqual(wallet1AllNames.length, 2); + assert.deepStrictEqual(wallet1AllNames, wallet2AllNames); + }); + + it('should only get wallet-owned names', async () => { + await wclient.execute('selectwallet', ['wallet1']); + const wallet1OwnedNames = await wclient.execute('getnames', [true]); + + await wclient.execute('selectwallet', ['wallet2']); + const wallet2OwnedNames = await wclient.execute('getnames', [true]); + + assert.strictEqual(wallet1OwnedNames.length, 1); + assert.strictEqual(wallet2OwnedNames.length, 1); + assert.strictEqual(wallet1OwnedNames[0].name, name1); + assert.strictEqual(wallet2OwnedNames[0].name, name2); + }); + }); }); From 3811ec6432a49fce61f9a7f998da612bfcea2918 Mon Sep 17 00:00:00 2001 From: Mike Carson Date: Thu, 6 Aug 2020 21:42:28 -0400 Subject: [PATCH 3/3] add own parameter to the HTTP API --- lib/wallet/http.js | 12 +++++++++++- test/wallet-http-test.js | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/wallet/http.js b/lib/wallet/http.js index 907201e979..1a8be46fbb 100644 --- a/lib/wallet/http.js +++ b/lib/wallet/http.js @@ -787,6 +787,9 @@ class HTTP extends Server { // Wallet Name States this.get('/wallet/:id/name', async (req, res) => { + const valid = Validator.fromRequest(req); + const own = valid.bool('own', false); + const height = this.wdb.height; const network = this.network; @@ -794,7 +797,14 @@ class HTTP extends Server { const items = []; for (const ns of names) - items.push(ns.getJSON(height, network)); + if (own) { + const {hash, index} = ns.owner; + const coin = await req.wallet.getCoin(hash, index); + if (coin) + items.push(ns.getJSON(height, network)); + } else { + items.push(ns.getJSON(height, network)); + } res.json(200, items); }); diff --git a/test/wallet-http-test.js b/test/wallet-http-test.js index cf196ba0b3..4deb73b3a4 100644 --- a/test/wallet-http-test.js +++ b/test/wallet-http-test.js @@ -50,6 +50,7 @@ const wallet2 = wclient.wallet('secondary'); let name, cbAddress; const accountTwo = 'foobar'; +const ownedNames = []; const { treeInterval, @@ -716,6 +717,9 @@ describe('Wallet HTTP', function() { const reveals = await wallet.getRevealsByName(name); assert.equal(reveals.length, 1); } + + ownedNames.push(name); + ownedNames.push(name2); }); // this test creates namestate to use duing the @@ -878,6 +882,8 @@ describe('Wallet HTTP', function() { name: name }); + ownedNames.push(name); + await mineBlocks(revealPeriod + 1, cbAddress); { @@ -936,6 +942,24 @@ describe('Wallet HTTP', function() { assert.deepEqual(state, res); }); + it('should get all wallet names', async () => { + const names = await wallet.getNames(); + + assert.equal(names.length, 11); + }); + + it('should only get wallet-owned names', async () => { + // TODO: convert to using hs-client method + // when wallet.getNames() allows `options` + const names = await wallet.client.get(`/wallet/${wallet.id}/name`, {own: true}); + + assert.equal(names.length, 3); + + for (const {name} of names) { + assert(ownedNames.includes(name)); + } + }); + it('should fail to get name resource for non existent name', async () => { const name = await nclient.execute('grindname', [10]);