From be568840db0382d1550cde0983ed72d50b3ddcd8 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Fri, 17 Feb 2017 18:20:46 +0800 Subject: [PATCH 1/4] Add IPFS service Add the `ipfs-api` JS library and a service using the browserified version of it. The service has functions for storing and reading file contests. --- app/services/ipfs.js | 32 ++++++++++++++++++++++++++++++++ app/services/kredits.js | 2 ++ config/environment.js | 7 ++++++- package.json | 5 +++-- tests/unit/services/ipfs-test.js | 12 ++++++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 app/services/ipfs.js create mode 100644 tests/unit/services/ipfs-test.js diff --git a/app/services/ipfs.js b/app/services/ipfs.js new file mode 100644 index 0000000..483f1b8 --- /dev/null +++ b/app/services/ipfs.js @@ -0,0 +1,32 @@ +import Ember from 'ember'; +import ipfsAPI from 'npm:ipfs-api'; +import config from 'kredits-web/config/environment'; + +export default Ember.Service.extend({ + + ipfsInstance: null, + + ipfs: function() { + if (this.get('ipfsInstance')) { + return this.get('ipfsInstance'); + } + let ipfs = ipfsAPI(config.ipfs); + this.set('ipfsInstance', ipfs); + window.ipfs = ipfs; + return ipfs; + }.property('ipfsInstance'), + + storeFile(content) { + let ipfs = this.get('ipfs'); + return ipfs.add(new ipfs.Buffer(content)).then((res) => { + return res[0].hash; + }); + }, + + getFile(hash) { + return this.get('ipfs').cat(hash, { buffer: true }, (err, res) => { + return res.toString(); + }); + } + +}); diff --git a/app/services/kredits.js b/app/services/kredits.js index 6e6c586..2f3d7d1 100644 --- a/app/services/kredits.js +++ b/app/services/kredits.js @@ -7,6 +7,8 @@ import kreditsContracts from 'npm:kredits-contracts'; export default Ember.Service.extend({ + ipfs: Ember.inject.service(), + web3Instance: null, web3Provided: false, // Web3 provided (using Mist Browser, Metamask et al.) diff --git a/config/environment.js b/config/environment.js index 77dd642..a9dfba2 100644 --- a/config/environment.js +++ b/config/environment.js @@ -22,7 +22,12 @@ module.exports = function(environment) { }, web3ProviderUrl: "https://parity.kosmos.org:8545", - ethereumChain: "testnet" + ethereumChain: "testnet", + ipfs: { + host: 'localhost', + port: '5001', + protocol: 'http' + } }; if (environment === 'development') { diff --git a/package.json b/package.json index 170bd77..4d4596d 100644 --- a/package.json +++ b/package.json @@ -39,9 +39,10 @@ "ember-export-application-global": "^1.0.5", "ember-load-initializers": "^0.5.1", "ember-resolver": "^2.0.3", + "ipfs-api": "^12.1.7", + "kredits-contracts": "67P/kredits-contracts", "loader.js": "^4.0.10", - "web3": "^0.18.2", - "kredits-contracts": "67P/kredits-contracts" + "web3": "^0.18.2" }, "engines": { "node": ">= 0.12.0" diff --git a/tests/unit/services/ipfs-test.js b/tests/unit/services/ipfs-test.js new file mode 100644 index 0000000..3a10cca --- /dev/null +++ b/tests/unit/services/ipfs-test.js @@ -0,0 +1,12 @@ +import { moduleFor, test } from 'ember-qunit'; + +moduleFor('service:ipfs', 'Unit | Service | ipfs', { + // Specify the other units that are required for this test. + // needs: ['service:foo'] +}); + +// Replace this with your real tests. +test('it exists', function(assert) { + let service = this.subject(); + assert.ok(service); +}); -- 2.50.1 From 31dc7c9c8ac9bdcc6425b1a3f4a0e723248f18be Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Fri, 17 Feb 2017 18:26:08 +0800 Subject: [PATCH 2/4] Remove debug object from window global --- app/services/ipfs.js | 1 - 1 file changed, 1 deletion(-) diff --git a/app/services/ipfs.js b/app/services/ipfs.js index 483f1b8..9b689d0 100644 --- a/app/services/ipfs.js +++ b/app/services/ipfs.js @@ -12,7 +12,6 @@ export default Ember.Service.extend({ } let ipfs = ipfsAPI(config.ipfs); this.set('ipfsInstance', ipfs); - window.ipfs = ipfs; return ipfs; }.property('ipfsInstance'), -- 2.50.1 From 0bf09fbe49b3e16c85c58cd40c1624a00d80c209 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Fri, 17 Feb 2017 18:31:27 +0800 Subject: [PATCH 3/4] Fix getFile --- app/services/ipfs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/ipfs.js b/app/services/ipfs.js index 9b689d0..8c6fb1a 100644 --- a/app/services/ipfs.js +++ b/app/services/ipfs.js @@ -23,7 +23,7 @@ export default Ember.Service.extend({ }, getFile(hash) { - return this.get('ipfs').cat(hash, { buffer: true }, (err, res) => { + return this.get('ipfs').cat(hash, { buffer: true }).then(res) => { return res.toString(); }); } -- 2.50.1 From fe356429fe61dac598d4912234c5686f52a3c063 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Fri, 17 Feb 2017 21:30:53 +0800 Subject: [PATCH 4/4] Fix tests One can use transforms with Browserify, so I added babelify in order to compile all node modules to ES5. --- app/services/ipfs.js | 4 ++-- config/environment.js | 9 +++++++++ package.json | 2 ++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/services/ipfs.js b/app/services/ipfs.js index 8c6fb1a..e3f9f41 100644 --- a/app/services/ipfs.js +++ b/app/services/ipfs.js @@ -17,13 +17,13 @@ export default Ember.Service.extend({ storeFile(content) { let ipfs = this.get('ipfs'); - return ipfs.add(new ipfs.Buffer(content)).then((res) => { + return ipfs.add(new ipfs.Buffer(content)).then(res => { return res[0].hash; }); }, getFile(hash) { - return this.get('ipfs').cat(hash, { buffer: true }).then(res) => { + return this.get('ipfs').cat(hash, { buffer: true }).then(res => { return res.toString(); }); } diff --git a/config/environment.js b/config/environment.js index a9dfba2..2a65fce 100644 --- a/config/environment.js +++ b/config/environment.js @@ -21,6 +21,15 @@ module.exports = function(environment) { // when it is created }, + browserify: { + transform: [ + ["babelify", { + presets: ["es2015"], + global: true + }] + ] + }, + web3ProviderUrl: "https://parity.kosmos.org:8545", ethereumChain: "testnet", ipfs: { diff --git a/package.json b/package.json index 4d4596d..4dbc620 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,8 @@ "deploy": "npm run build-prod && npm run update-version-file && bash scripts/deploy.sh" }, "devDependencies": { + "babel-preset-es2015": "^6.22.0", + "babelify": "^7.3.0", "broccoli-asset-rev": "^2.4.5", "ember-ajax": "^2.4.1", "ember-browserify": "^1.1.13", -- 2.50.1