Skip to content

Commit

Permalink
Merge pull request #463 from fm2055/feat/sns-name-resolver
Browse files Browse the repository at this point in the history
feat: added SNS name resolver
  • Loading branch information
kvhnuke authored Jul 23, 2024
2 parents 9896d91 + 74287e1 commit fc65990
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 2 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
"resolutions": {
"@ledgerhq/compressjs": "https://registry.yarnpkg.com/@favware/skip-dependency/-/skip-dependency-1.2.1.tgz",
"@noble/hashes": "^1.4.0"
},
"dependencies": {
"@bonfida/sns-warp-evm": "^0.0.4"
}
}
3 changes: 3 additions & 0 deletions packages/extension/src/libs/name-resolver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class GenericNameResolver {
arb: "https://nodes.mewapi.io/rpc/arb",
},
},
sns: {
network: "mainnet",
},
});
}

Expand Down
10 changes: 9 additions & 1 deletion packages/name-resolution/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ENSResolver from "./ens";
import SNSResolver from "./sns";
import { CoinType, NameResolverOptions } from "./types";
import UDResolver from "./ud";
import RNSResolver from "./rns";
Expand All @@ -7,6 +8,8 @@ import SIDResolver from "./sid";
class NameResolver {
ens: ENSResolver;

sns: SNSResolver;

rns: RNSResolver;

ud: UDResolver;
Expand All @@ -17,11 +20,13 @@ class NameResolver {

constructor(options: NameResolverOptions) {
this.ens = new ENSResolver(options.ens);
this.sns = new SNSResolver(options.sns);
this.rns = new RNSResolver();
this.ud = new UDResolver();
this.sid = new SIDResolver(options.sid);
this.initDone = Promise.all([
this.ens.init(),
this.sns.init(),
this.rns.init(),
this.ud.init(),
this.sid.init(),
Expand All @@ -32,6 +37,7 @@ class NameResolver {
return this.initDone.then(() =>
Promise.all([
this.ens.resolveReverseName(address),
this.sns.resolveReverseName(address),
this.sid.resolveReverseName(address),
this.rns.resolveReverseName(address),
this.ud.resolveReverseName(address),
Expand All @@ -50,6 +56,8 @@ class NameResolver {
coin: CoinType = "ETH"
): Promise<string | null> {
return this.initDone.then(() => {
if (this.sns.isSupportedName(name))
return this.sns.resolveAddress(name, coin);
if (this.sid.isSupportedName(name)) return this.sid.resolveAddress(name);
if (this.rns.isSupportedName(name))
return this.rns.resolveAddress(name, coin);
Expand All @@ -62,5 +70,5 @@ class NameResolver {
});
}
}
export { ENSResolver, UDResolver, CoinType };
export { ENSResolver, SNSResolver, UDResolver, CoinType };
export default NameResolver;
66 changes: 66 additions & 0 deletions packages/name-resolution/src/sns/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { SupportedChains, SNS } from "@bonfida/sns-warp-evm";
import { BaseResolver, CoinType } from "../types";
import { getTLD } from "../utils";
import { ChainMap, Resolvers, SNSOptions } from "./types";

const MAINNET_CHAIN_MAP: ChainMap = {
"BASE": SupportedChains.BASE,
"BNB": SupportedChains.BNBMainnet
}

const TESTNET_CHAIN_MAP: ChainMap = {
"BASE": SupportedChains.BASESepolia,
"BNB": SupportedChains.BNBTestNet
}

class SNSResolver implements BaseResolver {
options: SNSOptions;

name: string;

chainMap: ChainMap;

chainResolvers: Resolvers;

constructor(options: SNSOptions) {
this.options = options;
this.name = "sns";
this.chainMap = options.network === "testnet"
? TESTNET_CHAIN_MAP
: MAINNET_CHAIN_MAP;
this.chainResolvers = Object.fromEntries(
Object.entries(this.chainMap).map(([key, val]) => (
[key, new SNS(val)]
))
);
}

// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-empty-function
public async init(): Promise<void> { }

// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
public async resolveReverseName(_address: string): Promise<string | null> {
return null
}

public async resolveAddress(
name: string,
coin: CoinType = "ETH"
): Promise<string | null> {
const resolver = this.chainResolvers[coin];
if (resolver) {
return resolver.resolveName(name)
.then((address) => address ?? null)
.catch(() => null)
}

return null
}

// eslint-disable-next-line class-methods-use-this
public isSupportedName(name: string): boolean {
return getTLD(name) === "sol";
}
}

export default SNSResolver;
11 changes: 11 additions & 0 deletions packages/name-resolution/src/sns/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SupportedChains, SNS } from "@bonfida/sns-warp-evm";

type SupportedNetwork = "BASE" | "BNB";

export interface SNSOptions {
network: "testnet" | "mainnet";
}

export type ChainMap = Record<SupportedNetwork, SupportedChains>;

export type Resolvers = Record<string, SNS>;
6 changes: 5 additions & 1 deletion packages/name-resolution/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ENSOptions } from "./ens/types";
import { SIDOptions } from "./sid/types";
import { SNSOptions } from "./sns/types";

export type CoinType =
| "BTC"
Expand Down Expand Up @@ -137,7 +138,9 @@ export type CoinType =
| "NRG"
| "ARB1"
| "CELO"
| "AVAXC";
| "AVAXC"
| "BASE";


export abstract class BaseResolver {
name: string;
Expand All @@ -157,4 +160,5 @@ export abstract class BaseResolver {
export interface NameResolverOptions {
ens: ENSOptions;
sid: SIDOptions;
sns: SNSOptions;
}
6 changes: 6 additions & 0 deletions packages/name-resolution/tests/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ describe("Name Resolver resolving", () => {
arb: "https://nodes.mewapi.io/rpc/arb",
},
},
sns: {
network: "testnet",
},
});
let address = await resolver.resolveAddress("test.eth", "ETH");
expect(address).to.be.eq("0xeefB13C7D42eFCc655E528dA6d6F7bBcf9A2251d");
Expand All @@ -34,6 +37,9 @@ describe("Name Resolver resolving", () => {
arb: "https://nodes.mewapi.io/rpc/arb",
},
},
sns: {
network: "testnet",
},
});
let name = await resolver.resolveReverseName(
"0xe5dc07bdcdb8c98850050c7f67de7e164b1ea391"
Expand Down
26 changes: 26 additions & 0 deletions packages/name-resolution/tests/sns.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from "chai";
import SNSResolver from "../src/sns"

describe("SNS Name resolving", () => {
// the tests container
it("it should properly resolve address", async () => {
const resolver = new SNSResolver({ network: "testnet" });
await resolver.init();
const address = await resolver.resolveAddress("mock3.sol", "BNB");
expect(address).to.be.eq("0x1D719d2dB763f905b1924F46a5185e001Dd93786");
}).timeout(10000);

it("it should return null if not found", async () => {
const resolver = new SNSResolver({ network: "testnet" });
await resolver.init();
const name = await resolver.resolveReverseName(
"0xe5dc07bdcdb8c98850050c7f67de7e164b1ea392"
);
expect(name).to.be.eq(null);
const address = await resolver.resolveAddress(
"sdfsfsdfsdfsdf.sol",
"BASE"
);
expect(address).to.be.eq(null);
}).timeout(10000);
});
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,17 @@ __metadata:
languageName: node
linkType: hard

"@bonfida/sns-warp-evm@npm:^0.0.4":
version: 0.0.4
resolution: "@bonfida/sns-warp-evm@npm:0.0.4"
peerDependencies:
"@ethersproject/address": ^5.7.0
"@ethersproject/hash": ^5.7.0
"@ethersproject/providers": ^5.7.2
checksum: ad7e8c8a157f3c79668d8704eecb96708384f54f07088aaabcfb4798984d5d93082ddc80bb5b020e114d6bf9769781205aad53a1a9aa3c652558a6985b24a36f
languageName: node
linkType: hard

"@cardano-foundation/ledgerjs-hw-app-cardano@npm:^6.0.1":
version: 6.0.1
resolution: "@cardano-foundation/ledgerjs-hw-app-cardano@npm:6.0.1"
Expand Down Expand Up @@ -14875,6 +14886,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "enkrypt@workspace:."
dependencies:
"@bonfida/sns-warp-evm": ^0.0.4
"@commitlint/cli": ^19.3.0
"@commitlint/config-conventional": ^19.2.2
"@swc/core": ^1.6.3
Expand Down

1 comment on commit fc65990

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.