@investorid/identity-sdk
Version:
Interact with BlockChain Identities.
75 lines • 3.03 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const ethers_1 = require("ethers");
const providers_1 = require("ethers/providers");
const Config_1 = require("../Config");
/**
* Thrown when an invalid address is given
*/
class InvalidAddressError extends Error {
constructor({ message = 'Specified address is not a valid Ethereum address.', value } = {}) {
super(message);
this.name = 'INVALID_ADDRESS';
this.value = value;
this.message = message || 'Specified value is not a valid Ethereum address.';
Object.setPrototypeOf(this, InvalidAddressError.prototype);
}
}
exports.InvalidAddressError = InvalidAddressError;
/**
* Check if a string is a valid address (and return it checksummed).
* @param address
* @throws InvalidAddressError
*/
function normalizeAddress(address) {
try {
return ethers_1.utils.getAddress(address);
}
catch (err) {
throw new InvalidAddressError({ value: address });
}
}
exports.normalizeAddress = normalizeAddress;
/**
* Resolve a string with the Ethereum Naming Service.
*
* It will use the default Provider set for the SDK, or the optional provider in parameters.
* @param ens
* @param [provider] Override the default SDK provider.
* @throws InvalidAddressError If the string could not be resolved to an address.
* @throws InvalidProviderError If the provider is not a valid provider.
*/
function resolveENS(ens, provider) {
return __awaiter(this, void 0, void 0, function* () {
const _provider = provider || Config_1.getProvider();
if (!(_provider instanceof providers_1.Provider)) {
throw new Config_1.InvalidProviderError('Given provider or SDK provider must be a valid Provider to resolve ENS, or a Signer with a provider.');
}
try {
const address = yield _provider.resolveName(ens);
if (!address) {
throw new InvalidAddressError({
message: 'ENS does not resolve to a valid Ethereum address.',
value: ens,
});
}
return address;
}
catch (err) {
throw new InvalidAddressError({
value: ens,
message: 'Could not resolve the ENS to an ethereum address.',
});
}
});
}
exports.resolveENS = resolveENS;
//# sourceMappingURL=ENS.js.map