From b32b636697dac347b2d9de259d6c97ccd6acbd46 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Thu, 8 Dec 2016 13:07:27 +0100 Subject: [PATCH] fetch certifiers by id --- js/src/contracts/badgereg.js | 44 ++++++++++++------- .../providers/certifications/middleware.js | 29 +++++++++--- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/js/src/contracts/badgereg.js b/js/src/contracts/badgereg.js index 02d08e516..45f7df315 100644 --- a/js/src/contracts/badgereg.js +++ b/js/src/contracts/badgereg.js @@ -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 }; }); } diff --git a/js/src/redux/providers/certifications/middleware.js b/js/src/redux/providers/certifications/middleware.js index a5406051f..6c443cea5 100644 --- a/js/src/redux/providers/certifications/middleware.js +++ b/js/src/redux/providers/certifications/middleware.js @@ -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); } }); });