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 { vouchfor as vouchForAbi } from '~/contracts/abi';
let contractPromise = null;
export default class Store {
@observable vouchers = [];
constructor (api, app) {
this._api = api;
const { contentHash } = app;
if (contentHash) {
this.lookupVouchers(contentHash);
}
this.findVouchers(app);
}
lookupVouchers (contentHash) {
Contracts
.get().registry
.lookupAddress('vouchfor')
.then((address) => {
async attachContract () {
const address = await Contracts.get().registry.lookupAddress('vouchfor');
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 this._api.newContract(vouchForAbi, address);
})
.then(async (contract) => {
if (!contractPromise) {
contractPromise = this.attachContract();
}
const contract = await contractPromise;
if (!contract) {
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;
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)) {
lastItem = true;
} else {
this.addVoucher(voucher);
vouchers.push(voucher);
}
}
})
.catch((error) => {
console.error('vouchFor', error);
return;
});
}
@action addVoucher = (voucher) => {
this.vouchers = uniq([].concat(this.vouchers.peek(), [voucher]));
return vouchers;
}
@action addVouchers = (vouchHash, vouchId) => {
this.vouchers = uniq([].concat(this.vouchers.peek(), vouchHash, vouchId));
}
}