Skip to content
This repository has been archived by the owner on Dec 19, 2020. It is now read-only.

Commit

Permalink
use cid for search
Browse files Browse the repository at this point in the history
  • Loading branch information
Pechalka committed Dec 6, 2018
1 parent 91dd230 commit c6f6766
Show file tree
Hide file tree
Showing 3 changed files with 1,248 additions and 582 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"ethjs-provider-signer": "0.1.4",
"ethjs-query": "0.3.8",
"ethjs-signer": "0.1.1",
"ipfs-api": "^26.1.2",
"moment": "^2.22.2",
"number-to-bn": "1.7.0",
"prop-types": "15.6.2",
Expand Down
127 changes: 90 additions & 37 deletions src/cyber/Cyber.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,116 @@ import {
import builder from './builder';

const chainId = 'test-chain-gRXWCL';
const claimNodeUrl = 'http://earth.cybernode.ai:34666';
const claimNodeUrl = 'http://earth.cybernode.ai:34666'; //proxy TODO: remove when fix chain
const defaultAmount = 10000;

let __accounts = {};

const IPFS = require('ipfs-api');



const saveInIPFS = (ipfs, jsonStr) => new Promise((resolve, reject) => {
const buffer = Buffer.from(jsonStr);

ipfs.add(buffer, (err, ipfsHash) => {
if (err) {
reject(err);
} else {
const hash = ipfsHash[0].path;

resolve(hash);
}
});
});

const getIPFS = (ipfs, ipfsHash) => new Promise((resolve) => {
ipfs.get(ipfsHash, (err, files) => {
const buf = files[0].content;
resolve(buf.toString());
});
});

function Cyber(nodeUrl) {
const self = this;

//TOOD: use url from settings
const ipfs = new IPFS({
host: 'localhost',
port: 5001,
protocol: 'http',
});

let defaultAccount = null;

self.search = function (cid) {
self.search = function (text) {
return new Promise((resolve) => {
axios({
method: 'get',
url: `${nodeUrl}/search?cid=${cid}`,
})
.then((data) => {
saveInIPFS(ipfs, text)
.then(cid => axios({
method: 'get',
url: `${nodeUrl}/search?cid=${cid}`,
})).then((data) => {
const cids = data.data.result.cids;
const links = cids.map(cid => ({ hash: cid.Cid }));

resolve(links);
const links = cids.map(cid => ({ ...cid, hash: cid.Cid }));

const itemsPromises = links.map(item => {
return Promise.all([
getIPFS(ipfs, item.Cid),
Promise.resolve(item)
]).then(([content, _item]) => {
return {
..._item,
content
}
});
});

Promise.all(itemsPromises).then(items => {
resolve(items)
});
})
.catch(error => resolve([]));
});
};


self.link = function (from, to, address = '') {
return axios({
method: 'get',
url: `${nodeUrl}/account?address=${address}`,
}).then((response) => {
if (!response.data.result) { return false; }

return response.data.result.account;
}).then((account) => {
if (!account) { return; }

const acc = {
address: account.address,
chain_id: chainId, // todo: get from node
account_number: parseInt(account.account_number, 10),
sequence: parseInt(account.sequence, 10),
};
const linkRequest = {
acc,
fromCid: from,
toCid: to,
type: 'link',
};

return Promise.all([
saveInIPFS(ipfs, from),
saveInIPFS(ipfs, to),
]).then(([_from, _to]) => {
return axios({
method: 'post',
url: `${nodeUrl}/link`,
data: builder.buildAndSignTxRequest(linkRequest, __accounts[address].privateKey, chainId),
}).then(data => console.log('Link results: ', data)).catch(error => console.log('Cannot link', error));
method: 'get',
url: `${nodeUrl}/account?address=${address}`,
}).then((response) => {
if (!response.data.result) { return false; }

return response.data.result.account;
}).then((account) => {
if (!account) { return; }

const acc = {
address: account.address,
chain_id: chainId, // todo: get from node
account_number: parseInt(account.account_number, 10),
sequence: parseInt(account.sequence, 10),
};
const linkRequest = {
acc,
fromCid: _from,
toCid: _to,
type: 'link',
};

return axios({
method: 'post',
url: `${nodeUrl}/link`,
data: builder.buildAndSignTxRequest(linkRequest, __accounts[address].privateKey, chainId),
}).then(data => console.log('Link results: ', data)).catch(error => console.log('Cannot link', error));
});
});


};

self.claimFunds = function (address, amount) {
Expand Down
Loading

0 comments on commit c6f6766

Please sign in to comment.