Skip to content

Commit

Permalink
Merge 3811ec6 into d1b42a6
Browse files Browse the repository at this point in the history
  • Loading branch information
ca98am79 committed Aug 12, 2020
2 parents d1b42a6 + 3811ec6 commit 546e1bb
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 4 deletions.
12 changes: 11 additions & 1 deletion lib/wallet/http.js
Expand Up @@ -787,14 +787,24 @@ 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;

const names = await req.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 req.wallet.getCoin(hash, index);
if (coin)
items.push(ns.getJSON(height, network));
} else {
items.push(ns.getJSON(height, network));
}

res.json(200, items);
});
Expand Down
15 changes: 12 additions & 3 deletions lib/wallet/rpc.js
Expand Up @@ -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;
}
Expand Down
24 changes: 24 additions & 0 deletions test/wallet-http-test.js
Expand Up @@ -50,6 +50,7 @@ const wallet2 = wclient.wallet('secondary');

let name, cbAddress;
const accountTwo = 'foobar';
const ownedNames = [];

const {
treeInterval,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -878,6 +882,8 @@ describe('Wallet HTTP', function() {
name: name
});

ownedNames.push(name);

await mineBlocks(revealPeriod + 1, cbAddress);

{
Expand Down Expand Up @@ -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]);

Expand Down
89 changes: 89 additions & 0 deletions test/wallet-rpc-test.js
Expand Up @@ -45,6 +45,12 @@ const wclient = new WalletClient({
apiKey: 'bar'
});

const {
treeInterval,
biddingPeriod,
revealPeriod
} = network.names;

describe('Wallet RPC Methods', function() {
this.timeout(15000);

Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 546e1bb

Please sign in to comment.