address selector: support reverse lookup (#4033)

* address selector: simplify lookup

* address selector: support reverse lookup
This commit is contained in:
Jannis Redmann 2017-01-04 15:15:36 +01:00 committed by Jaco Greeff
parent 71e7a429d7
commit 839ee9afd7
2 changed files with 83 additions and 48 deletions

View File

@ -215,8 +215,9 @@ class AddressSelect extends Component {
} }
const { address, addressError } = validateAddress(inputValue); const { address, addressError } = validateAddress(inputValue);
const { registryValues } = this.store;
if (addressError) { if (addressError || registryValues.length > 0) {
return null; return null;
} }

View File

@ -22,6 +22,8 @@ import { FormattedMessage } from 'react-intl';
import Contracts from '~/contracts'; import Contracts from '~/contracts';
import { sha3 } from '~/api/util/sha3'; import { sha3 } from '~/api/util/sha3';
const ZERO = /^(0x)?0*$/;
export default class AddressSelectStore { export default class AddressSelectStore {
@observable values = []; @observable values = [];
@ -38,41 +40,75 @@ export default class AddressSelectStore {
registry registry
.getContract('emailverification') .getContract('emailverification')
.then((emailVerification) => { .then((emailVerification) => {
this.regLookups.push({ this.regLookups.push((email) => {
lookup: (value) => { return emailVerification
return emailVerification .instance
.instance .reverse
.reverse.call({}, [ sha3(value) ]); .call({}, [ sha3(email) ])
}, .then((address) => {
describe: (value) => ( return {
<FormattedMessage address,
id='addressSelect.fromEmail' description: (
defaultMessage='Verified using email {value}' <FormattedMessage
values={ { id='addressSelect.fromEmail'
value defaultMessage='Verified using email {email}'
} } values={ {
/> email
) } }
/>
)
};
});
}); });
}); });
registry registry
.getInstance() .getInstance()
.then((registryInstance) => { .then((registryInstance) => {
this.regLookups.push({ this.regLookups.push((name) => {
lookup: (value) => { return registryInstance
return registryInstance .getAddress
.getAddress.call({}, [ sha3(value), 'A' ]); .call({}, [ sha3(name), 'A' ])
}, .then((address) => {
describe: (value) => ( return {
<FormattedMessage address,
id='addressSelect.fromRegistry' name,
defaultMessage='{value} (from registry)' description: (
values={ { <FormattedMessage
value id='addressSelect.fromRegistry'
} } defaultMessage='{name} (from registry)'
/> values={ {
) name
} }
/>
)
};
});
});
this.regLookups.push((address) => {
return registryInstance
.reverse
.call({}, [ address ])
.then((name) => {
if (!name) {
return null;
}
return {
address,
name,
description: (
<FormattedMessage
id='addressSelect.fromRegistry'
defaultMessage='{name} (from registry)'
values={ {
name
} }
/>
)
};
});
}); });
}); });
} }
@ -149,32 +185,30 @@ export default class AddressSelectStore {
// Registries Lookup // Registries Lookup
this.registryValues = []; this.registryValues = [];
const lookups = this.regLookups.map((regLookup) => regLookup.lookup(value)); const lookups = this.regLookups.map((regLookup) => regLookup(value));
Promise Promise
.all(lookups) .all(lookups)
.then((results) => { .then((results) => {
return results return results
.map((result, index) => { .filter((result) => result && !ZERO.test(result.address));
if (/^(0x)?0*$/.test(result)) { })
return; .then((results) => {
} this.registryValues = results
.map((result) => {
const lowercaseResult = result.toLowerCase(); const lowercaseAddress = result.address.toLowerCase();
const account = flatMap(this.initValues, (cat) => cat.values) const account = flatMap(this.initValues, (cat) => cat.values)
.find((account) => account.address.toLowerCase() === lowercaseResult); .find((account) => account.address.toLowerCase() === lowercaseAddress);
return { if (account && account.name) {
description: this.regLookups[index].describe(value), result.name = account.name;
address: result, } else if (!result.name) {
name: account && account.name || value result.name = value;
}; }
})
.filter((data) => data); return result;
}) });
.then((registryValues) => {
this.registryValues = registryValues;
}); });
} }