-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(neon-domain): neon-domain plugin (#337)
neon-domain plugin to resolve user-friendly address formats into scripthashes.
- Loading branch information
1 parent
02f8981
commit d98a842
Showing
11 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as nns from "./provider/NeoNS"; | ||
|
||
export { nns }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as instance } from "./class"; | ||
export * from "./core"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters