-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple demonstration client and make code easier to run with an e…
…xample
- Loading branch information
Showing
20 changed files
with
1,755 additions
and
16 deletions.
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,4 @@ | ||
*.log | ||
.DS_Store | ||
node_modules | ||
dist |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Nick Johnson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,103 @@ | ||
# TSDX User Guide | ||
|
||
Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and how to use it. | ||
|
||
> This TSDX setup is meant for developing libraries (not apps!) that can be published to NPM. If you’re looking to build a Node app, you could use `ts-node-dev`, plain `ts-node`, or simple `tsc`. | ||
> If you’re new to TypeScript, checkout [this handy cheatsheet](https://devhints.io/typescript) | ||
## Commands | ||
|
||
TSDX scaffolds your new library inside `/src`. | ||
|
||
To run TSDX, use: | ||
|
||
```bash | ||
npm start # or yarn start | ||
``` | ||
|
||
This builds to `/dist` and runs the project in watch mode so any edits you save inside `src` causes a rebuild to `/dist`. | ||
|
||
To do a one-off build, use `npm run build` or `yarn build`. | ||
|
||
To run tests, use `npm test` or `yarn test`. | ||
|
||
## Configuration | ||
|
||
Code quality is set up for you with `prettier`, `husky`, and `lint-staged`. Adjust the respective fields in `package.json` accordingly. | ||
|
||
### Jest | ||
|
||
Jest tests are set up to run with `npm test` or `yarn test`. | ||
|
||
### Bundle Analysis | ||
|
||
[`size-limit`](https://github.com/ai/size-limit) is set up to calculate the real cost of your library with `npm run size` and visualize the bundle with `npm run analyze`. | ||
|
||
#### Setup Files | ||
|
||
This is the folder structure we set up for you: | ||
|
||
```txt | ||
/src | ||
index.tsx # EDIT THIS | ||
/test | ||
blah.test.tsx # EDIT THIS | ||
.gitignore | ||
package.json | ||
README.md # EDIT THIS | ||
tsconfig.json | ||
``` | ||
|
||
### Rollup | ||
|
||
TSDX uses [Rollup](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details. | ||
|
||
### TypeScript | ||
|
||
`tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs. | ||
|
||
## Continuous Integration | ||
|
||
### GitHub Actions | ||
|
||
Two actions are added by default: | ||
|
||
- `main` which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix | ||
- `size` which comments cost comparison of your library on every pull request using [`size-limit`](https://github.com/ai/size-limit) | ||
|
||
## Optimizations | ||
|
||
Please see the main `tsdx` [optimizations docs](https://github.com/palmerhq/tsdx#optimizations). In particular, know that you can take advantage of development-only optimizations: | ||
|
||
```js | ||
// ./types/index.d.ts | ||
declare var __DEV__: boolean; | ||
|
||
// inside your code... | ||
if (__DEV__) { | ||
console.log('foo'); | ||
} | ||
``` | ||
|
||
You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant) and [warning](https://github.com/palmerhq/tsdx#warning) functions. | ||
|
||
## Module Formats | ||
|
||
CJS, ESModules, and UMD module formats are supported. | ||
|
||
The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found. | ||
|
||
## Named Exports | ||
|
||
Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split inside your React app instead of your React library. | ||
|
||
## Including Styles | ||
|
||
There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like. | ||
|
||
For vanilla CSS, you can include it at the root directory and add it to the `files` section in your `package.json`, so that it can be imported separately by your users and run through their bundler's loader. | ||
|
||
## Publishing to NPM | ||
|
||
We recommend using [np](https://github.com/sindresorhus/np). |
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 @@ | ||
{ | ||
"version": "0.1.0", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"engines": { | ||
"node": ">=10" | ||
}, | ||
"scripts": { | ||
"start": "node dist/index.js", | ||
"build": "tsdx build", | ||
"lint": "tsdx lint", | ||
"prepare": "tsdx build", | ||
"size": "size-limit", | ||
"analyze": "size-limit --why" | ||
}, | ||
"peerDependencies": {}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "tsdx lint" | ||
} | ||
}, | ||
"prettier": { | ||
"printWidth": 80, | ||
"semi": true, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
}, | ||
"name": "@ensdomains/offchain-resolver-client", | ||
"author": "Nick Johnson", | ||
"module": "dist/client.esm.js", | ||
"size-limit": [ | ||
{ | ||
"path": "dist/client.cjs.production.min.js", | ||
"limit": "10 KB" | ||
}, | ||
{ | ||
"path": "dist/client.esm.js", | ||
"limit": "10 KB" | ||
} | ||
], | ||
"devDependencies": { | ||
"@size-limit/preset-small-lib": "^7.0.5", | ||
"husky": "^7.0.4", | ||
"size-limit": "^7.0.5", | ||
"tsdx": "^0.14.1", | ||
"tslib": "^2.3.1", | ||
"typescript": "^4.5.4" | ||
}, | ||
"dependencies": { | ||
"@chainlink/ethers-ccip-read-provider": "^0.2.0", | ||
"commander": "^8.3.0", | ||
"ethers": "^5.5.3" | ||
} | ||
} |
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 { Command } from 'commander'; | ||
import ethers from 'ethers'; | ||
import { CCIPReadProvider } from '@chainlink/ethers-ccip-read-provider'; | ||
import { abi as Resolver_abi } from '@ensdomains/ens-contracts/artifacts/contracts/resolvers/Resolver.sol/Resolver.json'; | ||
import { abi as ENSRegistry_abi } from '@ensdomains/ens-contracts/artifacts/contracts/registry/ENSRegistry.sol/ENSRegistry.json'; | ||
import { abi as IExtendedResolver_abi } from '@ensdomains/offchain-resolver-contracts/artifacts/contracts/IExtendedResolver.sol/IExtendedResolver.json'; | ||
|
||
// Almost all of this boilerplate will be unnecessary once ethers.js adds support | ||
// for ENSIP 10 and EIP 3668, but we're *early*. | ||
const Resolver = new ethers.utils.Interface(Resolver_abi); | ||
const ENSRegistry = new ethers.utils.Interface(ENSRegistry_abi); | ||
const IExtendedResolver = new ethers.utils.Interface(IExtendedResolver_abi); | ||
|
||
function dnsName(name: string) { | ||
// strip leading and trailing . | ||
const n = name.replace(/^\.|\.$/gm, ''); | ||
|
||
var bufLen = (n === '') ? 1 : n.length + 2; | ||
var buf = Buffer.allocUnsafe(bufLen); | ||
|
||
let offset = 0; | ||
if (n.length) { | ||
const list = n.split('.'); | ||
for (let i = 0; i < list.length; i++) { | ||
const len = buf.write(list[i], offset + 1) | ||
buf[offset] = len; | ||
offset += len + 1; | ||
} | ||
} | ||
buf[offset++] = 0; | ||
return '0x' + buf.reduce((output, elem) => (output + ('0' + elem.toString(16)).slice(-2)), ''); | ||
} | ||
|
||
const program = new Command(); | ||
program | ||
.requiredOption('-r --registry <address>', 'ENS registry address') | ||
.option('-p --provider <url>', 'web3 provider URL', 'http://localhost:9545/') | ||
.argument('<name>'); | ||
|
||
program.parse(process.argv); | ||
|
||
const options = program.opts(); | ||
const baseProvider = ethers.getDefaultProvider(options.provider); | ||
const provider = new CCIPReadProvider(baseProvider); | ||
|
||
(async () => { | ||
try { | ||
const registry = new ethers.Contract(options.registry, ENSRegistry, provider); | ||
const name = program.args[0]; | ||
const node = ethers.utils.namehash(name); | ||
|
||
const labels = name.split('.'); | ||
let resolverAddress = undefined; | ||
for(let i = 0; i < labels.length; i++) { | ||
resolverAddress = await registry.resolver(ethers.utils.namehash(labels.slice(i).join('.'))); | ||
if(resolverAddress !== "0x0000000000000000000000000000000000000000") { | ||
break; | ||
} | ||
} | ||
if(resolverAddress === undefined) { | ||
console.log(`${name} could not be resolved`); | ||
} | ||
|
||
const resolver = new ethers.Contract(resolverAddress, IExtendedResolver, provider); | ||
const data = Resolver.encodeFunctionData('addr(bytes32)', [node]); | ||
const responseData = await resolver.resolve(dnsName(name), data); | ||
const addr = Resolver.decodeFunctionResult('addr(bytes32)', responseData); | ||
console.log(`${name}: ${addr}`); | ||
} catch(e) { | ||
console.log(e); | ||
} | ||
})(); |
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,36 @@ | ||
{ | ||
// see https://www.typescriptlang.org/tsconfig to better understand tsconfigs | ||
"include": ["src", "types"], | ||
"compilerOptions": { | ||
"module": "esnext", | ||
"lib": ["dom", "esnext"], | ||
"importHelpers": true, | ||
// output .d.ts declaration files for consumers | ||
"declaration": true, | ||
// output .js.map sourcemap files for consumers | ||
"sourceMap": true, | ||
// match output dir to input dir. e.g. dist/index instead of dist/src/index | ||
"rootDir": "./src", | ||
// stricter type-checking for stronger correctness. Recommended by TS | ||
"strict": true, | ||
// linter checks for common issues | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
// noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
// use Node's module resolution algorithm, instead of the legacy TS one | ||
"moduleResolution": "node", | ||
// transpile JSX to React.createElement | ||
"jsx": "react", | ||
// interop between ESM and CJS modules. Recommended by TS | ||
"esModuleInterop": true, | ||
// significant perf increase by skipping checking .d.ts files, particularly those in node_modules. Recommended by TS | ||
"skipLibCheck": true, | ||
// error out if import and file system have a casing mismatch. Recommended by TS | ||
"forceConsistentCasingInFileNames": true, | ||
// `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc` | ||
"noEmit": true, | ||
"resolveJsonModule": true, | ||
} | ||
} |
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 @@ | ||
import "@ensdomains/ens-contracts/contracts/registry/ENSRegistry.sol"; |
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,13 @@ | ||
const { ethers } = require("hardhat"); | ||
|
||
module.exports = async ({deployments}) => { | ||
const {deploy} = deployments; | ||
const signers = await ethers.getSigners(); | ||
const owner = signers[0].address; | ||
await deploy('ENSRegistry', { | ||
from: owner, | ||
args: [], | ||
log: true, | ||
}); | ||
}; | ||
module.exports.tags = ['ens']; |
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,18 @@ | ||
const { ethers } = require("hardhat"); | ||
|
||
module.exports = async ({deployments}) => { | ||
const {deploy} = deployments; | ||
const signers = await ethers.getSigners(); | ||
const owner = signers[0].address; | ||
const resolver = await deploy('OffchainResolver', { | ||
from: owner, | ||
args: ['http://localhost:8000/{sender}/{callData}.json', ['0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266']], | ||
log: true, | ||
}); | ||
|
||
const registry = await ethers.getContract('ENSRegistry'); | ||
await registry.setSubnodeOwner("0x0000000000000000000000000000000000000000000000000000000000000000", ethers.utils.id('eth'), owner, {from: owner}); | ||
await registry.setSubnodeOwner(ethers.utils.namehash('eth'), ethers.utils.id('test'), owner, {from: owner}); | ||
await registry.setResolver(ethers.utils.namehash('test.eth'), resolver.address, {from: owner}); | ||
}; | ||
module.exports.tags = ['ens']; |
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 @@ | ||
31337 |
Oops, something went wrong.