Refactor contract interaction in its own module #42
@@ -1,6 +1,3 @@
|
||||
import Kredits from '../kredits';
|
||||
import multihashes from 'npm:multihashes';
|
||||
|
||||
export default class Base {
|
||||
constructor(contract) {
|
||||
this.contract = contract;
|
||||
@@ -10,28 +7,4 @@ export default class Base {
|
||||
return this.contract.functions;
|
||||
}
|
||||
|
||||
// TODO: move into utils
|
||||
fetchAndMergeIpfsData(data, Serializer) {
|
||||
if (!data.hashSize || data.hashSize === 0) {
|
||||
return data;
|
||||
}
|
||||
let digest = Kredits.ipfs.Buffer.from(data.ipfsHash.slice(2), 'hex');
|
||||
let ipfsHash = multihashes.encode(digest, data.hashFunction, data.hashSize);
|
||||
|
||||
return Kredits.ipfs
|
||||
.cat(ipfsHash)
|
||||
.then(Serializer.deserialize)
|
||||
.then((attributes) => {
|
||||
return Object.assign({}, data, attributes);
|
||||
});
|
||||
}
|
||||
|
||||
decodeIpfsHash(ipfsHash) {
|
||||
let multihash = multihashes.decode(multihashes.fromB58String(ipfsHash));
|
||||
return {
|
||||
ipfsHash: '0x' + multihashes.toHexString(multihash.digest),
|
||||
hashSize: multihash.length,
|
||||
hashFunction: multihash.code
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,30 +35,23 @@ export default class Contributor extends Base {
|
||||
})
|
||||
// Fetch IPFS data if available
|
||||
.then((data) => {
|
||||
return this.fetchAndMergeIpfsData(data, ContributorSerializer);
|
||||
return Kredits.ipfs.catAndMerge(data, ContributorSerializer.deserialize);
|
||||
});
|
||||
}
|
||||
|
||||
add(attributes) {
|
||||
console.log(attributes);
|
||||
|
||||
let json = ContributorSerializer.serialize(attributes);
|
||||
add(contributorAttr) {
|
||||
let json = ContributorSerializer.serialize(contributorAttr);
|
||||
// TODO: validate against schema
|
||||
|
||||
return Kredits.ipfs
|
||||
.add(new Kredits.ipfs.Buffer(json))
|
||||
.then((res) => res[0].hash)
|
||||
.then((ipfsHash) => {
|
||||
Object.assign(attributes, this.decodeIpfsHash(ipfsHash));
|
||||
return attributes;
|
||||
})
|
||||
.then((attr) => {
|
||||
.add(json)
|
||||
.then((ipfsHashAttr) => {
|
||||
let contributor = [
|
||||
attr.address,
|
||||
attr.ipfsHash,
|
||||
attr.hashFunction,
|
||||
attr.hashSize,
|
||||
attr.isCore,
|
||||
contributorAttr.address,
|
||||
ipfsHashAttr.ipfsHash,
|
||||
ipfsHashAttr.hashFunction,
|
||||
ipfsHashAttr.hashSize,
|
||||
contributorAttr.isCore,
|
||||
];
|
||||
|
||||
console.log('[kredits] addContributor', ...contributor);
|
||||
|
||||
@@ -35,30 +35,23 @@ export default class Operator extends Base {
|
||||
})
|
||||
// Fetch IPFS data if available
|
||||
.then((data) => {
|
||||
return this.fetchAndMergeIpfsData(data, ContributionSerializer);
|
||||
return Kredits.ipfs.catAndMerge(data, ContributionSerializer.deserialize);
|
||||
});
|
||||
}
|
||||
|
||||
addProposal(attributes) {
|
||||
console.log(attributes);
|
||||
|
||||
let json = ContributionSerializer.serialize(attributes);
|
||||
addProposal(proposalAttr) {
|
||||
let json = ContributionSerializer.serialize(proposalAttr);
|
||||
// TODO: validate against schema
|
||||
|
||||
return Kredits.ipfs
|
||||
.add(new Kredits.ipfs.Buffer(json))
|
||||
.then((res) => res[0].hash)
|
||||
.then((ipfsHash) => {
|
||||
Object.assign(attributes, this.decodeIpfsHash(ipfsHash));
|
||||
return attributes;
|
||||
})
|
||||
.then((attr) => {
|
||||
.add(json)
|
||||
.then((ipfsHashAttr) => {
|
||||
let proposal = [
|
||||
attr.recipientId,
|
||||
attr.amount,
|
||||
attr.ipfsHash,
|
||||
attr.hashFunction,
|
||||
attr.hashSize,
|
||||
proposalAttr.recipientId,
|
||||
proposalAttr.amount,
|
||||
ipfsHashAttr.ipfsHash,
|
||||
ipfsHashAttr.hashFunction,
|
||||
ipfsHashAttr.hashSize,
|
||||
];
|
||||
|
||||
console.log('[kredits] addProposal', ...proposal);
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import ethers from 'npm:ethers';
|
||||
import ipfsAPI from 'npm:ipfs-api';
|
||||
import RSVP from 'rsvp';
|
||||
|
||||
import abis from 'contracts/abis';
|
||||
import addresses from 'contracts/addresses';
|
||||
|
||||
import contracts from './contracts';
|
||||
import IPFS from './utils/ipfs';
|
||||
|
||||
// Helpers
|
||||
function capitalize(word) {
|
||||
@@ -25,7 +25,7 @@ export default class Kredits {
|
||||
|
||||
static setup(provider, signer, ipfsConfig) {
|
||||
this.ipfsConfig = ipfsConfig;
|
||||
this.ipfs = ipfsAPI(ipfsConfig);
|
||||
this.ipfs = new IPFS(ipfsConfig);
|
||||
|
||||
let registryContract = this.initRegistryContract(provider);
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import ipfsAPI from 'npm:ipfs-api';
|
||||
|
|
||||
import multihashes from 'npm:multihashes';
|
||||
|
||||
export default class IPFS {
|
||||
|
||||
constructor(config) {
|
||||
this._ipfsAPI = ipfsAPI(config);
|
||||
this._config = config;
|
||||
}
|
||||
|
||||
catAndMerge(data, deserializer) {
|
||||
// if no hash details are found simply return the data; nothing to merge
|
||||
if (!data.hashSize || data.hashSize === 0) {
|
||||
return data;
|
||||
}
|
||||
return this.cat(data)
|
||||
.then(deserializer)
|
||||
.then((attributes) => {
|
||||
return Object.assign({}, data, attributes);
|
||||
});
|
||||
}
|
||||
|
||||
add(data) {
|
||||
return this._ipfsAPI
|
||||
.add(new this._ipfsAPI.Buffer(data))
|
||||
.then((res) => {
|
||||
return this.decodeHash(res[0].hash);
|
||||
});
|
||||
}
|
||||
|
||||
cat(hashData) {
|
||||
|
❤️ ❤️
|
||||
let ipfsHash = hashData; // default - if it is a string
|
||||
if (hashData.hasOwnProperty('hashSize')) {
|
||||
ipfsHash = this.encodeHash(hashData);
|
||||
}
|
||||
return this._ipfsAPI.cat(ipfsHash);
|
||||
}
|
||||
|
||||
decodeHash(ipfsHash) {
|
||||
let multihash = multihashes.decode(multihashes.fromB58String(ipfsHash));
|
||||
return {
|
||||
ipfsHash: '0x' + multihashes.toHexString(multihash.digest),
|
||||
hashSize: multihash.length,
|
||||
hashFunction: multihash.code,
|
||||
sourceHash: ipfsHash
|
||||
};
|
||||
}
|
||||
|
||||
encodeHash(hashData) {
|
||||
let digest = this._ipfsAPI.Buffer.from(hashData.ipfsHash.slice(2), 'hex');
|
||||
return multihashes.encode(digest, hashData.hashFunction, hashData.hashSize);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user
Should we call it
deserializebecause it's the function and not the deserializer?good point here.
actually wanted to ask you. I felt it is nicer to pass a function here than an object. what do you think?