Skip to content

Commit

Permalink
feat(neon-domain): neon-domain plugin (#337)
Browse files Browse the repository at this point in the history
neon-domain plugin to resolve user-friendly address formats into scripthashes.
  • Loading branch information
merl111 authored and snowypowers committed Nov 17, 2018
1 parent 02f8981 commit d98a842
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 1 deletion.
22 changes: 22 additions & 0 deletions packages/neon-domain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# neon-domain

## Overview

Neon-Domain plugin. Provides the package `domain` within `neon-js`.

- Provides functionality to resolve domain names to addresses.

## Installation

```sh
yarn i @cityofzion/neon-domain
```

```js
const neonCore = require("@cityofzion/neon-core");
const domainPlugin = require("@cityofzion/neon-api");

const neonJs = domainPlugin(neonCore);

module.exports = neonJs;
```
72 changes: 72 additions & 0 deletions packages/neon-domain/__integration__/NeoNS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as neonCore from "@cityofzion/neon-core";
import domainPlugin from "../src/index";

const neonJs = domainPlugin(neonCore);
const { CONST, rpc, u, api, wallet } = neonJs;

export function getMainnetUrls(): string[] {
return [
"http://seed1.ngd.network:10332",
"http://seed2.ngd.network:10332",
"http://seed3.ngd.network:10332",
"http://seed4.ngd.network:10332",
"http://seed5.ngd.network:10332",
"http://seed6.ngd.network:10332",
"http://seed7.ngd.network:10332",
"http://seed8.ngd.network:10332",
"http://seed9.ngd.network:10332",
"http://seed10.ngd.network:10332",
"https://seed0.cityofzion.io:443",
"https://seed1.cityofzion.io:443",
"https://seed2.cityofzion.io:443",
"https://seed3.cityofzion.io:443",
"https://seed4.cityofzion.io:443",
"https://seed5.cityofzion.io:443",
"https://seed6.cityofzion.io:443",
"https://seed7.cityofzion.io:443",
"https://seed8.cityofzion.io:443",
"https://seed9.cityofzion.io:443"
]
};

let MAINNET_URL = "";
let provider;

const MAINNET_URLS = getMainnetUrls();

beforeAll(async () => {
provider = new neonJs.domain.nns.instance(
"348387116c4a75e420663277d9c02049907128c7"
);

for (let i = 0; i < MAINNET_URLS.length; i++) {
try {
await rpc.Query.getBlockCount().execute(MAINNET_URLS[i]);
MAINNET_URL = MAINNET_URLS[i];
break;
} catch (e) {
if (i === MAINNET_URLS.length) {
throw new Error("Exhausted all urls but found no available RPC");
}
continue;
}
}
});

describe("domainResolve", () => {
test("name found", async () => {
const address = await provider.resolveDomain(
MAINNET_URL,
"test.neo"
);
expect(wallet.isAddress(address)).toBe(true);
});

test("name not found", async () => {
const address = await provider.resolveDomain(
MAINNET_URL,
"alkdjfklasjdlfkjasdklf.neo"
);
expect(address).toMatch('');
});
});
4 changes: 4 additions & 0 deletions packages/neon-domain/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const config = require("../../jest.config");
config.globals["ts-jest"]["tsConfigFile"] = "../../tsconfig-test.json";
config.testRegex = "./(__(tests|integration)__/NeoNS)\\.ts$";
module.exports = config;
59 changes: 59 additions & 0 deletions packages/neon-domain/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "@cityofzion/neon-domain",
"description": "Neon-Domain Module",
"version": "1.0.0",
"repository": {
"type": "git",
"url": "git+https://github.com/CityOfZion/neon-js.git"
},
"publishConfig": {
"access": "public"
},
"keywords": [
"neo",
"antshares",
"javascript",
"libraries",
"neon-domain"
],
"author": "Mathias Enzensberger <merl111@protonmail.com> (https://github.com/merl111)",
"contributors": [
{
"name": "Nick Fujita",
"email": "nickfujita@gmail.com",
"url": "https://github.com/nickfujita"
}
],
"license": "MIT",
"main": "lib/index.js",
"module": "src/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "tsc -b",
"clean": "rimraf ./lib ./dist",
"prepublishOnly": "yarn clean && yarn build",
"lint": "tslint src/**/*.ts __tests__/**/*.ts __integration__/**/*.ts",
"pretty": "prettier --write --loglevel=warn \"./{src,__{tests,integration}__}/**/*.ts\"",
"start": "jest --watch",
"test": "jest",
"test:integration": "jest /packages/.*/__integration__/.*",
"test:unit": "jest /packages/.*/__tests__/.*"
},
"devDependencies": {
"@types/jest": "^23.1.0",
"cross-env": "^5.2.0",
"jest": "^23.1.0",
"prettier": "^1.12.1",
"rimraf": "^2.6.2",
"ts-jest": "^23.0.1",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.14.0",
"typescript": "^3.0.1"
},
"peerDependencies": {
"@cityofzion/neon-core": "^4.0.0"
},
"files": [
"lib/"
]
}
11 changes: 11 additions & 0 deletions packages/neon-domain/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as _Neon from "@cityofzion/neon-core";
import * as plugin from "./plugin";

function bundle<T extends typeof _Neon>(
neonCore: T
): T & { domain: typeof plugin } {
return { ...(neonCore as any), domain: plugin };
}

export default bundle;
export * from "./plugin";
3 changes: 3 additions & 0 deletions packages/neon-domain/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as nns from "./provider/NeoNS";

export { nns };
23 changes: 23 additions & 0 deletions packages/neon-domain/src/provider/NeoNS/class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { logging } from "@cityofzion/neon-core";
import { resolveDomain } from "./core";

const log = logging.default("neon-domain");

export class NeoNS {
private contract: string;

public get name() {
return `NeoNs[${this.contract}]`;
}

constructor(contract: string) {
this.contract = contract;
log.info(`Created NeoNS Provider: ${this.contract}`);
}

public resolveDomain(url: string, domain: string): Promise<string> {
return resolveDomain(url, this.contract, domain);
}
}

export default NeoNS;
48 changes: 48 additions & 0 deletions packages/neon-domain/src/provider/NeoNS/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { logging, u, rpc, sc, wallet } from "@cityofzion/neon-core";

const log = logging.default("neon-domain");

const operation = "resolve";

/**
* Resolve a domain to a public address.
* @param url - URL of an NEO RPC service.
* @param contract - the contract used to resolve
* @param domain - the domain to resolve.
* @return public address as string
*/

export async function resolveDomain(
url: string,
contract: string,
domain: string
): Promise<string> {
const protocol = {
type: "String",
value: "addr"
};

const empty = {
type: "String",
value: ""
};

const tld = domain.split(".").reverse()[0];
const regExp = new RegExp(`.${tld}$`);

const subdomain = domain.replace(regExp, "");
const hashSubdomain = u.sha256(u.str2hexstring(subdomain));
const hashDomain = u.sha256(u.str2hexstring(tld));

const hashName = u.sha256(hashSubdomain.concat(hashDomain));
const parsedName = sc.ContractParam.byteArray(hashName, "name");

const args = [protocol, parsedName, empty];

const sb = new sc.ScriptBuilder();
const script = sb.emitAppCall(contract, operation, args).str;
const res = await rpc.Query.invokeScript(script).execute(url);

return rpc.StringParser(res.result.stack[0]);

}
2 changes: 2 additions & 0 deletions packages/neon-domain/src/provider/NeoNS/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as instance } from "./class";
export * from "./core";
9 changes: 9 additions & 0 deletions packages/neon-domain/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig-base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
},
"references": [{ "path": "../neon-core" }],
"include": ["src/**/*"]
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{ "path": "./packages/neon-api" },
{ "path": "./packages/neon-nep5" },
{ "path": "./packages/neon-nep9" },
{ "path": "./packages/neon-js" }
{ "path": "./packages/neon-js" },
{ "path": "./packages/neon-domain" }
]
}

0 comments on commit d98a842

Please sign in to comment.