Skip to content

Commit

Permalink
feat: updated the contract address and added the comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceban Mihail committed Oct 11, 2023
1 parent 7469874 commit 8258e79
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 67 deletions.
63 changes: 25 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@

### Add NPM Token

Add your npm token to your GitHub repository secrets as `NPM_TOKEN`.

### Add Codecov integration

Enable the Codecov GitHub App [here](https://github.com/apps/codecov).

**Remove everything from here and above**

---

# bns-resolver

[![npm package][npm-img]][npm-url]
Expand All @@ -21,7 +9,7 @@ Enable the Codecov GitHub App [here](https://github.com/apps/codecov).
[![Commitizen Friendly][commitizen-img]][commitizen-url]
[![Semantic Release][semantic-release-img]][semantic-release-url]

> My awesome module
> The Blockprime Name Resolver Library
## Install

Expand All @@ -31,44 +19,43 @@ npm install bns-resolver

## Usage

### Import and initialization

```ts
import { myPackage } from 'bns-resolver';
import { BnsResolver } from "bns-resolver";

myPackage('hello');
//=> 'hello from my package'
const bnsResolver = await BnsResolver.init(RPC_NODE);
```
### Name resolution

## API

### myPackage(input, options?)

#### input

Type: `string`

Lorem ipsum.

#### options
- This function should be used when the input is a human readable name or alias like `alex.sxt`. The sample code:
```ts
const addr = await bnsResolver.resolveName('alex.sxt');
```

Type: `object`
### Lookup the address

##### postfix
- This function performs reverse resolution returning the primary alias of the given address. Typically it is used when the Ethereum account is connected to the dApp. The sample code:
```ts
const addr = await bnsResolver.lookupAddress('0x084B5B4967b6EaB4EeDc628C12c7E63292cD5FC6');
```

Type: `string`
Default: `rainbows`
## Running tests

Lorem ipsum.
```shell
npm run test
```

[build-img]:https://github.com/ryansonshine/bns-resolver/actions/workflows/release.yml/badge.svg
[build-url]:https://github.com/ryansonshine/bns-resolver/actions/workflows/release.yml
[build-img]:https://github.com/Syndika-Corp/bns-resolver/actions/workflows/release.yml/badge.svg
[build-url]:https://github.com/Syndika-Corp/bns-resolver/actions/workflows/release.yml
[downloads-img]:https://img.shields.io/npm/dt/bns-resolver
[downloads-url]:https://www.npmtrends.com/bns-resolver
[npm-img]:https://img.shields.io/npm/v/bns-resolver
[npm-url]:https://www.npmjs.com/package/bns-resolver
[issues-img]:https://img.shields.io/github/issues/ryansonshine/bns-resolver
[issues-url]:https://github.com/ryansonshine/bns-resolver/issues
[codecov-img]:https://codecov.io/gh/ryansonshine/bns-resolver/branch/main/graph/badge.svg
[codecov-url]:https://codecov.io/gh/ryansonshine/bns-resolver
[issues-img]:https://img.shields.io/github/issues/Syndika-Corp/bns-resolver
[issues-url]:https://github.com/Syndika-Corp/bns-resolver/issues
[codecov-img]:https://codecov.io/gh/Syndika-Corp/bns-resolver/branch/main/graph/badge.svg
[codecov-url]:https://codecov.io/gh/Syndika-Corp/bns-resolver
[semantic-release-img]:https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-release-url]:https://github.com/semantic-release/semantic-release
[commitizen-img]:https://img.shields.io/badge/commitizen-friendly-brightgreen.svg
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bns-resolver",
"version": "0.0.0-development",
"description": "A template for creating npm packages using TypeScript and VSCode",
"version": "1.1.0",
"description": "The Blockprime Name Resolver Library",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"files": [
Expand Down
8 changes: 4 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export const BNS_REGISTRY_ADDRESS = {
1: '0x13FCAb43cd9deA934528350eAd71754949b73303',
5: '0x13FCAb43cd9deA934528350eAd71754949b73303',
11155111: '0x13FCAb43cd9deA934528350eAd71754949b73303',
1: '0x77A10a68f1BC651a0CD965F696B4C32b7202b016',
5: '0x77A10a68f1BC651a0CD965F696B4C32b7202b016',
11155111: '0x77A10a68f1BC651a0CD965F696B4C32b7202b016',
} as Record<number, string>;

export const REGISTRY_ABI = [
'function resolver(bytes32) view returns (address)',
'function ttl(bytes32) view returns (uint64)',
'function expiration(bytes32) view returns (uint256)',
];

export const RESOLVER_ABI = [
Expand Down
47 changes: 29 additions & 18 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ import type { Web3Connection } from './types/web3-connection.type';

export class BnsResolver {
/**
* Initiate Multicall class with specified connection mechanism
* Initiate BnsResolver class
*
* @param connection Connection instance for Multicall service
* @returns New Multicall instance
* @param _provider JsonRpcProvider provider
* @param _bnsRegistry BnsRegistry smart-contract
* @returns New BnsResolver instance
*/
constructor(
private _provider: JsonRpcProvider,
private _bnsRegistry: Contract
) {}

/**
* Creates the BnsResolver instance with specified Web3Connection type
*
* @param connection A string representing RPC URL or the JsonRpcProvider object
* @returns New BnsResolver instance
*/
public static async init(connection: Web3Connection): Promise<BnsResolver> {
const provider =
connection instanceof JsonRpcProvider
Expand All @@ -41,6 +48,12 @@ export class BnsResolver {
return new BnsResolver(provider, bnsRegistry);
}

/**
* Resolves the name matching it with the corresponding Ethereum address registered in Bloprime Name Service
*
* @param name The alias name e.g. 'alex.sxt'
* @returns Returns the Ethereum address or null if the alias expired or doesn't exist
*/
public async resolveName(name: string): Promise<string | null> {
// Example of supported format: alex.sxt
if (name.split(DOMAIN_SEPARATOR).length != 2) {
Expand All @@ -51,10 +64,11 @@ export class BnsResolver {
const tld = name.split(DOMAIN_SEPARATOR)[1];
try {
const node = namehash(name);
const parentNameHash = namehash(tld);

// Get the name and TLD expiration
const nameExpiration = await this._bnsRegistry.ttl(node);
const tldExpiration = await this._bnsRegistry.ttl(namehash(tld));
const nameExpiration = await this._bnsRegistry.expiration(node);
const tldExpiration = await this._bnsRegistry.expiration(parentNameHash);
const currentTimestampInSeconds = this.getCurrentTimestamp();

// Domain is not valid if it or it's tld are expired
Expand All @@ -67,8 +81,8 @@ export class BnsResolver {
return null;
}

// Get the resolver address
const resolver = await this._bnsRegistry.resolver(node);
// Get the resolver address from TLD
const resolver = await this._bnsRegistry.resolver(parentNameHash);
if (resolver == null || resolver === ZeroAddress) {
return null;
}
Expand Down Expand Up @@ -99,21 +113,18 @@ export class BnsResolver {
return null;
}

/**
* This function performs reverse resolution returning the primary alias of the given address.
* It also performs the reverse check
*
* @param address The Ethereum address e.g. '0x084B5B4967b6EaB4EeDc628C12c7E63292cD5FC6'
* @returns Returns the alias or null
*/
public async lookupAddress(address: string): Promise<string | null> {
address = getAddress(address);
const node = namehash(address.substring(2).toLowerCase() + '.addr.reverse');
try {
// Get the expiration
const expiration = await this._bnsRegistry.ttl(node);
const currentTimestampInSeconds = this.getCurrentTimestamp();

/**
* Ommit this for now
* // Domain is not valid if it is expired
* if (expiration == null || expiration < currentTimestampInSeconds) {
* return null;
* }
*/
// @note The expiration will be checked in reverse check

// Get the resolver address
const resolver = await this._bnsRegistry.resolver(node);
Expand Down
15 changes: 10 additions & 5 deletions test/resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ describe('BnsResolver', () => {

describe('Common tests', () => {
it('should create an instance of BnsResolver', async () => {
const provider = new JsonRpcProvider(getEnvVar('PUBLIC_RPC'));
const provider = new JsonRpcProvider(PUBLIC_RPC_NODES[0]);

await expect(BnsResolver.init('invalid')).rejects.toThrow();

expect((bnsResolver = await BnsResolver.init(provider)));
expect((bnsResolver = await BnsResolver.init(getEnvVar('PUBLIC_RPC'))));
expect((bnsResolver = await BnsResolver.init(PUBLIC_RPC_NODES[0])));
});
});

describe('resolveName', () => {
it('resolveName: test1.sxt - success resolution', async () => {
const addr = await bnsResolver.resolveName('test1.sxt');
const addr = await bnsResolver.resolveName('test0.sxt');
expect(addr).toEqual('0x084B5B4967b6EaB4EeDc628C12c7E63292cD5FC6');
});

Expand All @@ -32,15 +32,20 @@ describe('BnsResolver', () => {
expect(addr).toEqual(null);
});

it('resolveName: invalid name format', async () => {
const addr = await bnsResolver.resolveName('test2.sxt.eth');
expect(addr).toEqual(null);
});

// add TLD expired test
});

describe('resolveName', () => {
describe('lookupAddress', () => {
it('lookupAddress: 0x084B5B4967b6EaB4EeDc628C12c7E63292cD5FC6 - success resolution', async () => {
const addr = await bnsResolver.lookupAddress(
'0x084B5B4967b6EaB4EeDc628C12c7E63292cD5FC6'
);
expect(addr).toEqual('test1.sxt');
expect(addr).toEqual('test0.sxt');
});

it('lookupAddress: 0xf2EA5Fd6538EAb3B0466f1b1A447C742d8b30eFe - expired', async () => {
Expand Down

0 comments on commit 8258e79

Please sign in to comment.