Skip to content

Commit

Permalink
Merge pull request #2 from decentldotland/dev
Browse files Browse the repository at this point in the history
feat: subdomain resolver endpoint
  • Loading branch information
charmful0x authored Feb 24, 2023
2 parents 800edcf + b0da3a4 commit ffb8ab8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@

### 1- `/resolve/:query/:full?`

- `query` : a valid Arweave address or ANS domain
- `full` (optional): return response as the full ANS user object
- `query` : a valid Arweave address or ANS domain.
- `full` (optional): return response as the full ANS user object.

### 2- `/resolve-subdomain/:parent_domain/:query`

- `parent_domain` : a valid ANS domain that issued subdomains.
- `query` : a valid Arweave address or ANS subdomain (target).


## License
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ans-resolver",
"version": "0.0.2",
"version": "0.0.3",
"description": "HTTP resolver for ANS domains",
"main": "./src/api.js",
"type": "module",
Expand Down
42 changes: 42 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,48 @@ app.get("/resolve/:query/:full?", async (req, res) => {
}
});

app.get("/resolve-subdomain/:parent_domain/:query", async (req, res) => {
try {
res.setHeader("Content-Type", "application/json");
const { query, parent_domain } = req.params;
const normalizedDomain = normalizeDomain(parent_domain);
const balances = await readContract();
const domains = balances
.map((usr) => usr.ownedDomains)
.flat()
.filter((domain) => domain.subdomains.length);
const parent = domains.find((domain) => domain.domain === normalizedDomain);

const subdomains = parent.subdomains.filter((subdomain) => subdomain.owner);

if (isArweaveAddress(query)) {
const user = subdomains.find((sub) => sub.owner === query);
const subdomain = user?.subdomain
? `${normalizedDomain}.${user.subdomain}.ar`
: undefined;
res.send({
subdomain,
});
return;
}
const normalizedSubdomain = normalizeDomain(query);
const user = subdomains.find(
(sub) => sub.subdomain === normalizedSubdomain
);
const address = user?.owner ? user.owner : undefined;

res.send({ address });
return;
} catch (error) {
console.log(error);
res.send({
error:
"invalid query paramater. Provide a valid ANS subdomain or Arweave address",
});
return;
}
});

app.listen(port, async () => {
console.log(`listening at PORT: ${port}`);
});

0 comments on commit ffb8ab8

Please sign in to comment.