Check vouch status on appId in addition to contentHash (#6719)

* Check vouch status on appId in addition to contentHash

* Simplify var expansion
This commit is contained in:
Jaco Greeff 2017-10-12 19:28:56 +02:00 committed by Gav Wood
parent e51e54eeeb
commit edbbd42d80

View File

@ -20,55 +20,68 @@ import { uniq } from 'lodash';
import Contracts from '~/contracts'; import Contracts from '~/contracts';
import { vouchfor as vouchForAbi } from '~/contracts/abi'; import { vouchfor as vouchForAbi } from '~/contracts/abi';
let contractPromise = null;
export default class Store { export default class Store {
@observable vouchers = []; @observable vouchers = [];
constructor (api, app) { constructor (api, app) {
this._api = api; this._api = api;
const { contentHash } = app; this.findVouchers(app);
if (contentHash) {
this.lookupVouchers(contentHash);
}
} }
lookupVouchers (contentHash) { async attachContract () {
Contracts const address = await Contracts.get().registry.lookupAddress('vouchfor');
.get().registry
.lookupAddress('vouchfor')
.then((address) => {
if (!address || /^0x0*$/.test(address)) { if (!address || /^0x0*$/.test(address)) {
return null;
}
const contract = await this._api.newContract(vouchForAbi, address);
return contract;
}
async findVouchers ({ contentHash, id }) {
if (!contentHash) {
return; return;
} }
return this._api.newContract(vouchForAbi, address); if (!contractPromise) {
}) contractPromise = this.attachContract();
.then(async (contract) => { }
const contract = await contractPromise;
if (!contract) { if (!contract) {
return; return;
} }
const vouchHash = await this.lookupHash(contract, `0x${contentHash}`);
const vouchId = await this.lookupHash(contract, id);
this.addVouchers(vouchHash, vouchId);
}
async lookupHash (contract, hash) {
const vouchers = [];
let lastItem = false; let lastItem = false;
for (let index = 0; !lastItem; index++) { for (let index = 0; !lastItem; index++) {
const voucher = await contract.instance.vouched.call({}, [`0x${contentHash}`, index]); const voucher = await contract.instance.vouched.call({}, [hash, index]);
if (/^0x0*$/.test(voucher)) { if (/^0x0*$/.test(voucher)) {
lastItem = true; lastItem = true;
} else { } else {
this.addVoucher(voucher); vouchers.push(voucher);
} }
} }
})
.catch((error) => {
console.error('vouchFor', error);
return;
});
}
@action addVoucher = (voucher) => { return vouchers;
this.vouchers = uniq([].concat(this.vouchers.peek(), [voucher])); }
@action addVouchers = (vouchHash, vouchId) => {
this.vouchers = uniq([].concat(this.vouchers.peek(), vouchHash, vouchId));
} }
} }