forked from aragon/aragon.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Address Book Identity Provider
- Loading branch information
Showing
5 changed files
with
132 additions
and
9 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
packages/aragon-wrapper/src/identity/AddressBookIdentityProvider.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import AddressIdentityProvider from './AddressIdentityProvider' | ||
import { first, map } from 'rxjs/operators' | ||
import { getCacheKey } from '../utils/index' | ||
|
||
const addressBookAppIds = [ | ||
'0x32ec8cc9f3136797e0ae30e7bf3740905b0417b81ff6d4a74f6100f9037425de' | ||
// TODO Add in App Ids for rinkeby and mainnet appIds | ||
] | ||
/** | ||
* An identity provider for Address Book Entries | ||
* | ||
* @class AddressIdentityProvider | ||
*/ | ||
export default class AddressBookIdentityProvider extends AddressIdentityProvider { | ||
constructor (apps, cache) { | ||
super() | ||
this.apps = apps | ||
this.cache = cache | ||
} | ||
/** | ||
* Optional initialization, if required by the provider | ||
*/ | ||
async init () { | ||
// no-op | ||
} | ||
|
||
/** | ||
* Resolve the identity metadata for an address | ||
* Should resolve to null if an identity does not exist | ||
* Will return the first successful resolution tity could not be found | ||
* | ||
* @param {string} address Address to resolve | ||
* @return {Promise} Resolved metadata or rejected error | ||
*/ | ||
async resolve (address) { | ||
address = address.toLowerCase() | ||
const addressBookApps = await this.apps.pipe( | ||
first(), | ||
map(apps => apps.filter(app => addressBookAppIds.includes(app.appId))) | ||
).toPromise() | ||
|
||
return addressBookApps.reduce(async (identity, app) => { | ||
if (identity) { | ||
return identity | ||
} | ||
const cacheKey = getCacheKey(app.proxyAddress, 'state') | ||
const { entries = [] } = await this.cache.get(cacheKey) | ||
const { data: entryData } = entries | ||
.find(entry => entry.addr.toLowerCase() === address) || {} | ||
return entryData || null | ||
}, null) | ||
} | ||
|
||
/** | ||
* Modify the identity metadata of an address | ||
* | ||
* @param {string} address Address to resolve | ||
* @param {Object} metadata Metadata to modify | ||
* @return {Promise} Resolved success action or rejected error | ||
*/ | ||
async modify (address, metadata) { | ||
throw new Error('Use the Address Book to change this label, or create your own local label') | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/aragon-wrapper/src/identity/AddressBookIdentityProvider.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import test from 'ava' | ||
import Cache from '../cache' | ||
import { of } from 'rxjs' | ||
|
||
import { AddressBookIdentityProvider } from './index' | ||
let apps, cache | ||
test.before(async t => { | ||
apps = of([ | ||
{ | ||
appId: '0x32ec8cc9f3136797e0ae30e7bf3740905b0417b81ff6d4a74f6100f9037425de', | ||
proxyAddress: '0x0' | ||
}, | ||
{ | ||
appId: '0x123', | ||
proxyAddress: '0x1' | ||
}, | ||
{ | ||
appId: '0x32ec8cc9f3136797e0ae30e7bf3740905b0417b81ff6d4a74f6100f9037425de', | ||
proxyAddress: '0x11' | ||
} | ||
]) | ||
|
||
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' } }] }) | ||
}) | ||
|
||
test.beforeEach(async t => { | ||
t.context.addressBookIdentityProvider = new AddressBookIdentityProvider(apps, cache) | ||
await t.context.addressBookIdentityProvider.init() | ||
}) | ||
|
||
test('should resolve identity from first address book in app array', async t => { | ||
const provider = t.context.addressBookIdentityProvider | ||
const identityMetadata = await provider.resolve('0x3') | ||
t.is(identityMetadata.name, 'testEntity') | ||
}) | ||
|
||
test('should resolve to null for non-existent identity', async t => { | ||
const provider = t.context.addressBookIdentityProvider | ||
const identityMetadata = await provider.resolve('0x9') | ||
t.is(identityMetadata, null) | ||
}) | ||
|
||
test('should throw error on any modify attempt', async t => { | ||
const provider = t.context.addressBookIdentityProvider | ||
await t.throwsAsync(() => provider.modify('0x9', { name: 'newEntity' })) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as AddressIdentityProvider } from './AddressIdentityProvider' | ||
export { default as LocalIdentityProvider } from './LocalIdentityProvider' | ||
export { default as AddressBookIdentityProvider } from './AddressBookIdentityProvider' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters