fetch certifiers by id

This commit is contained in:
Jannis R 2016-12-08 13:07:27 +01:00
parent 2b34d76b8c
commit b32b636697
No known key found for this signature in database
GPG Key ID: 0FE83946296A88A5
2 changed files with 52 additions and 21 deletions

View File

@ -18,7 +18,8 @@ import { bytesToHex, hex2Ascii } from '~/api/util/format';
import ABI from './abi/certifier.json';
const ZERO = '0x0000000000000000000000000000000000000000000000000000000000000000';
const ZERO20 = '0x0000000000000000000000000000000000000000';
const ZERO32 = '0x0000000000000000000000000000000000000000000000000000000000000000';
export default class BadgeReg {
constructor (api, registry) {
@ -26,25 +27,36 @@ export default class BadgeReg {
this._registry = registry;
registry.getContract('badgereg');
this.certifiers = {}; // by name
this.certifiers = []; // by id
this.contracts = {}; // by name
}
fetchCertifier (name) {
if (this.certifiers[name]) {
return Promise.resolve(this.certifiers[name]);
nrOfCertifiers () {
return this._registry.getContract('badgereg')
.then((badgeReg) => {
return badgeReg.instance.badgeCount.call({}, [])
.then((count) => count.valueOf());
});
}
fetchCertifier (id) {
if (this.certifiers[id]) {
return Promise.resolve(this.certifiers[id]);
}
return this._registry.getContract('badgereg')
.then((badgeReg) => {
return badgeReg.instance.fromName.call({}, [name])
.then(([ id, address ]) => {
return this.fetchMeta(id)
.then(({ title, icon }) => {
const data = { address, name, title, icon };
this.certifiers[name] = data;
return data;
});
});
return badgeReg.instance.badge.call({}, [ id ]);
})
.then(([ address, name ]) => {
if (address === ZERO20) throw new Error(`Certifier ${id} does not exist.`);
name = bytesToHex(name);
name = name === ZERO32 ? null : hex2Ascii(name);
return this.fetchMeta(id)
.then(({ title, icon }) => {
const data = { address, name, title, icon };
this.certifiers[id] = data;
return data;
});
});
}
@ -58,8 +70,8 @@ export default class BadgeReg {
})
.then(([ title, icon ]) => {
title = bytesToHex(title);
title = title === ZERO ? null : hex2Ascii(title);
if (bytesToHex(icon) === ZERO) icon = null;
title = title === ZERO32 ? null : hex2Ascii(title);
if (bytesToHex(icon) === ZERO32) icon = null;
return { title, icon };
});
}

View File

@ -17,20 +17,39 @@
import Contracts from '~/contracts';
import { addCertification } from './actions';
const knownCertifiers = [ 'smsverification' ];
const knownCertifiers = [
0 // sms verification
];
export default class CertificationsMiddleware {
toMiddleware () {
return (store) => (next) => (action) => {
if (action.type !== 'fetchCertifications') {
if (action.type === 'fetchCertifiers') {
badgeReg.nrOfCertifiers().then((count) => {
new Array(+count).fill(null).forEach((_, id) => {
badgeReg.fetchCertifier(id)
.then((cert) => {
const { address, name, title, icon } = cert;
store.dispatch(addCertifier(address, name, title, icon));
})
.catch((err) => {
if (err) {
console.error(`Failed to fetch certifier ${id}:`, err);
}
});
});
});
}
else if (action.type !== 'fetchCertifications') {
return next(action);
}
const { address } = action;
const badgeReg = Contracts.get().badgeReg;
knownCertifiers.forEach((name) => {
badgeReg.fetchCertifier(name)
knownCertifiers.forEach((id) => {
badgeReg.fetchCertifier(id)
.then((cert) => {
return badgeReg.checkIfCertified(cert.address, address)
.then((isCertified) => {
@ -42,7 +61,7 @@ export default class CertificationsMiddleware {
})
.catch((err) => {
if (err) {
console.error(`Failed to check if ${address} certified by ${name}:`, err);
console.error(`Failed to check if ${address} certified by ${id}:`, err);
}
});
});