Skip to content

Commit

Permalink
web3 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jdevcs committed Feb 25, 2022
1 parent 3149683 commit 0ec2465
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
2 changes: 2 additions & 0 deletions packages/web3-common/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@ export const JSONRPC_ERR_UNAUTHORIZED = 4001;
export const JSONRPC_ERR_UNSUPPORTED_METHOD = 4200;
export const JSONRPC_ERR_DISCONNECTED = 4900;
export const JSONRPC_ERR_CHAIN_DISCONNECTED = 4901;

export const ERR_ENS_CHECK_INTERFACE_SUPPORT = 901;
8 changes: 8 additions & 0 deletions packages/web3-common/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
ERR_INVALID_PASSWORD,
ERR_IV_LENGTH,
ERR_PBKDF2_ITERATIONS,
ERR_ENS_CHECK_INTERFACE_SUPPORT,
} from './constants';
import { isResponseWithError } from './json_rpc';

Expand Down Expand Up @@ -481,3 +482,10 @@ export class PBKDF2IterationsError extends Web3Error {
super('c > 1000, pbkdf2 is less secure with less iterations');
}
}

export class ENSCheckInterfaceSupportError extends Web3Error {
public code = ERR_ENS_CHECK_INTERFACE_SUPPORT;
public constructor(errorDetails: string) {
super(`ENS resolver check interface support error. "${errorDetails}"`);
}
}
63 changes: 28 additions & 35 deletions packages/web3-eth-ens/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Address, sha3, isHexStrict } from 'web3-utils';
import { Contract, NonPayableCallOptions } from 'web3-eth-contract';
import { inputAddressFormatter } from 'web3-common';
import {
inputAddressFormatter,
ResolverMethodMissingError,
ENSCheckInterfaceSupportError,
} from 'web3-common';
import { interfaceIds, methodsInInterface } from './config';
import { Registry } from './registry';
import { RESOLVER } from './abi/resolver';
Expand All @@ -27,56 +31,49 @@ export class Resolver {
resolverContract: Contract<typeof RESOLVER>,
methodName: string,
) {
if (interfaceIds[methodName] === undefined) throw new Error(); // ResolverMethodMissingError(resolver address, methodName);
if (interfaceIds[methodName] === undefined)
throw new ResolverMethodMissingError(
resolverContract.options.address ?? '',
methodName,
);

let supported = false;
try {
supported = (await resolverContract.methods
.supportsInterface(interfaceIds[methodName])
.call()) as Awaited<Promise<boolean>>;
} catch (err) {
// TODO throw new Error
}

if (!supported) throw new Error(); // new ResolverMethodMissingError(resolver address, methodName);
supported = (await resolverContract.methods
.supportsInterface(interfaceIds[methodName])
.call()) as Awaited<Promise<boolean>>;

if (!supported)
throw new ResolverMethodMissingError(
resolverContract.options.address ?? '',
methodName,
);
}

public async setAddress(ENSName: string, address: Address, txConfig: NonPayableCallOptions) {
const resolverContract = await this.getResolverContractAdapter(ENSName);
await this.checkInterfaceSupport(resolverContract, methodsInInterface.setAddr);

try {
return resolverContract.methods
.setAddr(namehash(ENSName), inputAddressFormatter(address))
.send(txConfig);
} catch (error) {
throw new Error(); // to do web3 error
}
return resolverContract.methods
.setAddr(namehash(ENSName), inputAddressFormatter(address))
.send(txConfig);
}

public async setPubkey(ENSName: string, x: string, y: string, txConfig: NonPayableCallOptions) {
const resolverContract = await this.getResolverContractAdapter(ENSName);
await this.checkInterfaceSupport(resolverContract, methodsInInterface.setPubkey);

try {
// TODO: verify that X and Y coordinates of pub key are normalized?
return resolverContract.methods
.setPubkey(namehash(ENSName), namehash(x), namehash(y))
.send(txConfig);
} catch (error) {
throw new Error(); // to do web3 error
}
// TODO: verify that X and Y coordinates of pub key are normalized?
return resolverContract.methods
.setPubkey(namehash(ENSName), namehash(x), namehash(y))
.send(txConfig);
}

public async setContenthash(ENSName: string, hash: string, txConfig: NonPayableCallOptions) {
const resolverContract = await this.getResolverContractAdapter(ENSName);
await this.checkInterfaceSupport(resolverContract, methodsInInterface.setContenthash);

try {
return resolverContract.methods.setContenthash(namehash(ENSName), hash).send(txConfig);
} catch (error) {
throw new Error(); // to do web3 error
}
return resolverContract.methods.setContenthash(namehash(ENSName), hash).send(txConfig);
}

public async supportsInterface(ENSName: string, interfaceId: string) {
Expand All @@ -92,11 +89,7 @@ export class Resolver {
interfaceIdParam = interfaceIdParam.slice(0, 10);
}

try {
return resolverContract.methods.supportsInterface(interfaceIdParam).call();
} catch (error) {
throw new Error(); // to do web3 error
}
return resolverContract.methods.supportsInterface(interfaceIdParam).call();
}

public async getAddress(ENSName: string) {
Expand Down

0 comments on commit 0ec2465

Please sign in to comment.