Skip to content

Commit 25e7246

Browse files
committed
wallet-http: Add own method to the getName.
1 parent ab2f5f8 commit 25e7246

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

lib/client/wallet.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,20 +308,22 @@ class WalletClient extends bcurl.Client {
308308
* {@see hsd.NameState}
309309
* @param {String} id
310310
* @param {String} name
311+
* @param {Object} [options]
312+
* @param {Boolean} [options.own=false]
311313
* @returns {Promise}
312314
*/
313315

314-
getName(id, name) {
315-
return this.get(`/wallet/${id}/name/${name}`);
316+
getName(id, name, options) {
317+
return this.get(`/wallet/${id}/name/${name}`, options);
316318
}
317319

318320
/**
319321
* Get name state for all names
320322
* that the wallet is managing.
321323
* {@see hsd.NameState}
322324
* @param {String} id
323-
* @param {Object} options
324-
* @param {Boolean} [optoins.own=false]
325+
* @param {Object} [options]
326+
* @param {Boolean} [options.own=false]
325327
* @returns {Promise}
326328
*/
327329

@@ -1016,19 +1018,21 @@ class Wallet extends EventEmitter {
10161018
* Get name state for the given name.
10171019
* {@see hsd.NameState}
10181020
* @param {String} name
1021+
* @param {Object} [options]
1022+
* @param {Boolean} [options.own=false]
10191023
* @returns {Promise}
10201024
*/
10211025

1022-
getName(name) {
1023-
return this.client.getName(this.id, name);
1026+
getName(name, options) {
1027+
return this.client.getName(this.id, name, options);
10241028
}
10251029

10261030
/**
10271031
* Get name state for all names
10281032
* that the wallet is managing.
10291033
* {@see hsd.NameState}
1030-
* @param {Object} options
1031-
* @param {Boolean} [optoins.own=false]
1034+
* @param {Object} [options]
1035+
* @param {Boolean} [options.own=false]
10321036
* @returns {Promise}
10331037
*/
10341038

lib/wallet/http.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const common = require('./common');
3030

3131
/** @typedef {import('../types').NetworkType} NetworkType */
3232
/** @typedef {ReturnType<Validator['fromRequest']>} RequestValidator */
33+
/** @typedef {import('../covenants/namestate')} NameState */
3334

3435
/**
3536
* HTTP
@@ -862,17 +863,27 @@ class HTTP extends Server {
862863
this.get('/wallet/:id/name/:name', async (req, res) => {
863864
const valid = Validator.fromRequest(req);
864865
const name = valid.str('name');
866+
const own = valid.bool('own', false);
865867

866868
enforce(name, 'Must pass name.');
867869
enforce(rules.verifyName(name), 'Must pass valid name.');
868870

869871
const height = this.wdb.height;
870872
const network = this.network;
873+
/** @type {NameState?} */
871874
const ns = await req.wallet.getNameStateByName(name);
872875

873876
if (!ns)
874877
return res.json(404);
875878

879+
if (own) {
880+
const {hash, index} = ns.owner;
881+
const coin = await req.wallet.getCoin(hash, index);
882+
883+
if (!coin)
884+
return res.json(404);
885+
}
886+
876887
return res.json(200, ns.getJSON(height, network));
877888
});
878889

test/wallet-http-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,6 +1903,21 @@ describe('Wallet HTTP', function() {
19031903
assert(ownedNames.includes(name));
19041904
}
19051905
});
1906+
1907+
it('should get owned names name info', async () => {
1908+
const ownedNSes = await wallet.getNames({ own: true });
1909+
const ownedNames = new Map(ownedNSes.map(ns => [ns.name, ns]));
1910+
1911+
for (const name of allNames) {
1912+
const isOwned = ownedNames.has(name);
1913+
const ns = await wallet.getName(name, { own: true });
1914+
1915+
assert.strictEqual(ns != null, isOwned);
1916+
1917+
if (isOwned)
1918+
assert.deepEqual(ns, ownedNames.get(name));
1919+
}
1920+
});
19061921
});
19071922

19081923
describe('HTTP tx races (Integration)', function() {

0 commit comments

Comments
 (0)