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.call({}, [ sha3(value) ]); .reverse
}, .call({}, [ sha3(email) ])
describe: (value) => ( .then((address) => {
return {
address,
description: (
<FormattedMessage <FormattedMessage
id='addressSelect.fromEmail' id='addressSelect.fromEmail'
defaultMessage='Verified using email {value}' defaultMessage='Verified using email {email}'
values={ { values={ {
value email
} } } }
/> />
) )
};
});
}); });
}); });
registry registry
.getInstance() .getInstance()
.then((registryInstance) => { .then((registryInstance) => {
this.regLookups.push({ this.regLookups.push((name) => {
lookup: (value) => {
return registryInstance return registryInstance
.getAddress.call({}, [ sha3(value), 'A' ]); .getAddress
}, .call({}, [ sha3(name), 'A' ])
describe: (value) => ( .then((address) => {
return {
address,
name,
description: (
<FormattedMessage <FormattedMessage
id='addressSelect.fromRegistry' id='addressSelect.fromRegistry'
defaultMessage='{value} (from registry)' defaultMessage='{name} (from registry)'
values={ { values={ {
value 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;
}); });
} }