diff --git a/packages/aragon-wrapper/src/identity/AddressBookIdentityProvider.js b/packages/aragon-wrapper/src/identity/AddressBookIdentityProvider.js index b44843d3..9ceac783 100644 --- a/packages/aragon-wrapper/src/identity/AddressBookIdentityProvider.js +++ b/packages/aragon-wrapper/src/identity/AddressBookIdentityProvider.js @@ -9,7 +9,7 @@ const addressBookAppIds = [ /** * An identity provider for Address Book Entries * - * @class AddressIdentityProvider + * @extends AddressIdentityProvider */ export default class AddressBookIdentityProvider extends AddressIdentityProvider { constructor (apps, cache) { @@ -51,6 +51,45 @@ export default class AddressBookIdentityProvider extends AddressIdentityProvider }, null) } + async search (searchTerm = '') { + const isAddressSearch = searchTerm.substring(0, 2).toLowerCase() === '0x' + const identities = await this.getAll() + console.log('identities ', identities) + const results = Object.entries(identities) + .filter( + ([address, { name }]) => + (isAddressSearch && + searchTerm.length > 3 && + address.toLowerCase().indexOf(searchTerm.toLowerCase()) === 0) || + name.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1 + ) + .map(([address, { name }]) => ({ name, address })) + return results + } + + /** + * get all identities from all installed address book instances + */ + async getAll () { + const addressBookApps = await this.apps.pipe( + first(), + map(apps => apps.filter(app => addressBookAppIds.includes(app.appId))) + ).toPromise() + + return addressBookApps.reduce(async (allEntries, app) => { + console.log("app: ", app) + const cacheKey = getCacheKey(app.proxyAddress, 'state') + const { entries = [] } = await this.cache.get(cacheKey) + const allEntriesResolved = await allEntries + const entriesObject = entries.reduce((obj, entry) => { + return { ...obj, [entry.addr.toLowerCase()]: entry.data } + }, {}) + console.log('entries:', entriesObject, 'all entries: ', allEntries) + // ensure the entries retrieved from the first-installed address book aren't overwritten + return { ...entriesObject, ...allEntriesResolved } + }, Promise.resolve({})) + } + /** * Modify the identity metadata of an address * diff --git a/packages/aragon-wrapper/src/identity/AddressBookIdentityProvider.test.js b/packages/aragon-wrapper/src/identity/AddressBookIdentityProvider.test.js index 0046e09f..8d569dbe 100644 --- a/packages/aragon-wrapper/src/identity/AddressBookIdentityProvider.test.js +++ b/packages/aragon-wrapper/src/identity/AddressBookIdentityProvider.test.js @@ -22,8 +22,8 @@ test.before(async t => { cache = new Cache('stubbedAddressBook') await cache.init() - cache.set('0x0.state', { entries: [{ addr: '0x3', data: { name: 'testEntity' } }] }) - cache.set('0x11.state', { entries: [{ addr: '0x3', data: { name: 'testEntity2' } }] }) + cache.set('0x0.state', { entries: [{ addr: '0x3a', data: { name: 'testEntity' } }, { addr: '0x33', data: { name: 'testDude' } } ] }) + cache.set('0x11.state', { entries: [{ addr: '0x3a', data: { name: 'testEntity2' } }] }) }) test.beforeEach(async t => { @@ -33,7 +33,7 @@ test.beforeEach(async t => { test('should resolve identity from first address book in app array', async t => { const provider = t.context.addressBookIdentityProvider - const identityMetadata = await provider.resolve('0x3') + const identityMetadata = await provider.resolve('0x3a') t.is(identityMetadata.name, 'testEntity') }) @@ -47,3 +47,29 @@ test('should throw error on any modify attempt', async t => { const provider = t.context.addressBookIdentityProvider await t.throwsAsync(() => provider.modify('0x9', { name: 'newEntity' })) }) + +test('getAll should return a combined Object containing all entries', async t => { + const provider = t.context.addressBookIdentityProvider + const allIdentities = await provider.getAll() + t.deepEqual(allIdentities, { + '0x3a': { name: 'testEntity' }, + '0x33': { name: 'testDude' } + }) +}) + +test('search should return an aray of results of freely matching identities', async t => { + t.plan(3) + const provider = t.context.addressBookIdentityProvider + let result = await provider.search('0x3a') + console.log('result: ', result) + t.deepEqual(result, [ { name: 'testEntity', address: '0x3a' } ]) + + result = await provider.search('test') + console.log('next result', result) + t.deepEqual(result, [ { name: 'testEntity', address: '0x3a' }, + { name: 'testDude', address: '0x33' } ]) + + result = await provider.search('testd') + console.log('next result', result) + t.deepEqual(result, [{ name: 'testDude', address: '0x33' } ]) +})