mirror of https://github.com/embarklabs/embark.git
fix(@embark/ens): make resolve() work with promises and callbacks
Changes in c64c093a48
resulted in a regression
that ENS functions within console/dashboard didn't work properly anymore.
This commit ensures that both APIs, `EmbarkJS.Names.resolve()` as well as
`EmbarkJS.Names.lookup()` can be either used using `async/await` or promised
based syntax within the console/dashboard.
Example:
```
await EmbarkJS.Names.resolve('me.eth.eth');
EmbarkJS.Names.resolve('me.eth.eth').then(val => ..., err => ...)
EmbarkJS.Names.resolve('me.eth.eth', (err, val) => ...)
```
Same with:
```
await EmbarkJS.Names.lookup('0x...');
EmbarkJS.Names.lookup('0x...').then(val => ..., err => ...)
EmbarkJS.Names.lookup('0x...', (err, val) => ...)
```
This commit is contained in:
parent
ffcff4a16c
commit
2195475fe6
|
@ -96,7 +96,7 @@ class CodeRunner {
|
||||||
cb(null, value);
|
cb(null, value);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Improve error message when there's no connection to node
|
// Improve error message when there's no connection to node
|
||||||
if (error.message.indexOf(WEB3_INVALID_RESPONSE_ERROR) !== -1) {
|
if (error.message && error.message.indexOf(WEB3_INVALID_RESPONSE_ERROR) !== -1) {
|
||||||
error.message += '. Are you connected to an Ethereum node?';
|
error.message += '. Are you connected to an Ethereum node?';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -172,71 +172,81 @@ __embarkENS.setProvider = function (config) {
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkENS.resolve = function (name, callback) {
|
__embarkENS.resolve = function (name, callback) {
|
||||||
callback = callback || function () {};
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
|
function resolveOrReject(err, addr) {
|
||||||
|
if (err) {
|
||||||
|
if (err === NoDecodeAddrError) {
|
||||||
|
err = `${name} is not registered`;
|
||||||
|
addr = '0x';
|
||||||
|
}
|
||||||
|
return !callback ? reject(err) : callback(err);
|
||||||
|
}
|
||||||
|
return !callback ? resolve(addr) : callback(err, addr);
|
||||||
|
}
|
||||||
|
|
||||||
if (!this.ens) {
|
if (!this.ens) {
|
||||||
return callback(providerNotSetError);
|
resolveOrReject(providerNotSetError);
|
||||||
}
|
}
|
||||||
if (!web3.eth.defaultAccount) {
|
if (!web3.eth.defaultAccount) {
|
||||||
return callback(defaultAccountNotSetError);
|
resolveOrReject(defaultAccountNotSetError);
|
||||||
}
|
}
|
||||||
|
|
||||||
let node = namehash.hash(name);
|
let node = namehash.hash(name);
|
||||||
|
|
||||||
function cb(err, addr) {
|
this.ens.methods.resolver(node).call().then(resolvedAddress => {
|
||||||
if (err === NoDecodeAddrError) {
|
if (resolvedAddress === voidAddress) {
|
||||||
return callback(name + " is not registered", "0x");
|
return resolveOrReject('Name not yet registered');
|
||||||
}
|
|
||||||
callback(err, addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.ens.methods.resolver(node).call()
|
|
||||||
.then(resolverAddress => {
|
|
||||||
if (resolverAddress === voidAddress) {
|
|
||||||
return cb('Name not yet registered');
|
|
||||||
}
|
}
|
||||||
let resolverContract = new EmbarkJS.Blockchain.Contract({
|
let resolverContract = new EmbarkJS.Blockchain.Contract({
|
||||||
abi: this.resolverInterface,
|
abi: this.resolverInterface,
|
||||||
address: resolverAddress,
|
address: resolvedAddress,
|
||||||
web3: web3
|
web3: web3
|
||||||
});
|
});
|
||||||
return resolverContract.methods.addr(node).call(cb);
|
resolverContract.methods.addr(node).call(resolveOrReject);
|
||||||
})
|
}).catch(resolveOrReject);
|
||||||
.catch(cb);
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkENS.lookup = function (address, callback) {
|
__embarkENS.lookup = function (address, callback) {
|
||||||
callback = callback || function () {};
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
|
function resolveOrReject(err, name) {
|
||||||
|
if (err) {
|
||||||
|
if (err === NoDecodeStringError || err === NoDecodeAddrError) {
|
||||||
|
err = 'Address does not resolve to name. Try syncing chain.';
|
||||||
|
}
|
||||||
|
return !callback ? reject(err) : callback(err);
|
||||||
|
}
|
||||||
|
return !callback ? resolve(name) : callback(err, name);
|
||||||
|
}
|
||||||
|
|
||||||
if (!this.ens) {
|
if (!this.ens) {
|
||||||
return callback(providerNotSetError);
|
return resolveOrReject(providerNotSetError);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!web3.eth.defaultAccount) {
|
if (!web3.eth.defaultAccount) {
|
||||||
return callback(defaultAccountNotSetError);
|
return resolveOrReject(defaultAccountNotSetError);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (address.startsWith("0x")) {
|
if (address.startsWith("0x")) {
|
||||||
address = address.slice(2);
|
address = address.slice(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
let node = web3.utils.soliditySha3(address.toLowerCase() + reverseAddrSuffix);
|
let node = web3.utils.soliditySha3(address.toLowerCase() + reverseAddrSuffix);
|
||||||
|
|
||||||
function cb(err, name) {
|
this.ens.methods.resolver(node).call().then(resolverAddress => {
|
||||||
if (err === NoDecodeStringError || err === NoDecodeAddrError) {
|
|
||||||
return callback('Address does not resolve to name. Try syncing chain.');
|
|
||||||
}
|
|
||||||
return callback(err, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.ens.methods.resolver(node).call()
|
|
||||||
.then(resolverAddress => {
|
|
||||||
if (resolverAddress === voidAddress) {
|
if (resolverAddress === voidAddress) {
|
||||||
return cb('Address not associated to a resolver');
|
return resolveOrReject('Address not associated to a resolver');
|
||||||
}
|
}
|
||||||
const resolverContract = new EmbarkJS.Blockchain.Contract({
|
const resolverContract = new EmbarkJS.Blockchain.Contract({
|
||||||
abi: this.resolverInterface,
|
abi: this.resolverInterface,
|
||||||
address: resolverAddress,
|
address: resolverAddress,
|
||||||
web3: web3
|
web3: web3
|
||||||
});
|
});
|
||||||
return resolverContract.methods.name(node).call(cb);
|
resolverContract.methods.name(node).call(resolveOrReject);
|
||||||
})
|
}).catch(resolveOrReject);
|
||||||
.catch(cb);
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
__embarkENS.registerSubDomain = function (name, address, callback) {
|
__embarkENS.registerSubDomain = function (name, address, callback) {
|
||||||
|
|
Loading…
Reference in New Issue