Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Adds core logic for ipfs init.
Browse files Browse the repository at this point in the history
  • Loading branch information
hackergrrl committed Mar 22, 2016
1 parent d01c82f commit fca76b0
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 10 deletions.
38 changes: 38 additions & 0 deletions default-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"Addresses": {
"Swarm": [
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001"
],
"API": "/ip4/127.0.0.1/tcp/5001",
"Gateway": "/ip4/127.0.0.1/tcp/8080"
},
"Discovery": {
"MDNS": {
"Enabled": true,
"Interval": 10
}
},
"Mounts": {
"IPFS": "/ipfs",
"IPNS": "/ipns"
},
"Ipns": {
"ResolveCacheSize": 128
},
"Gateway": {
"RootRedirect": "",
"Writable": false
},
"Bootstrap": [
"/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z",
"/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
"/ip4/162.243.248.213/tcp/4001/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm",
"/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu",
"/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64",
"/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd",
"/ip4/178.62.61.185/tcp/4001/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3",
"/ip4/104.236.151.122/tcp/4001/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx"
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"hapi": "^12.0.0",
"ipfs-api": "^2.13.1",
"ipfs-blocks": "^0.1.0",
"ipfs-data-importing": "^0.3.0",
"ipfs-data-importing": "^0.3.3",
"ipfs-merkle-dag": "^0.2.1",
"ipfs-multipart": "^0.1.0",
"ipfs-repo": "^0.5.0",
Expand Down
4 changes: 3 additions & 1 deletion src/core/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const defaultRepo = require('./default-repo')
// const bl = require('bl')
const blocks = require('ipfs-blocks')
const BlockService = blocks.BlockService
const Block = blocks.Block
Expand All @@ -13,6 +12,7 @@ const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const importer = require('ipfs-data-importing').import
const libp2p = require('libp2p-ipfs')
const init = require('./init')

exports = module.exports = IPFS

Expand Down Expand Up @@ -107,6 +107,8 @@ function IPFS (repo) {
gc: function () {}
}

this.init = (opts, callback) => { init(repo, opts, callback) }

this.bootstrap = {
list: (callback) => {
repo.config.get((err, config) => {
Expand Down
81 changes: 81 additions & 0 deletions src/core/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const peerId = require('peer-id')
const IpfsBlocks = require('ipfs-blocks').BlockService
const IpfsDagService = require('ipfs-merkle-dag').DAGService
const path = require('path')

module.exports = (repo, opts, callback) => {
opts = opts || {}
opts.force = opts.force || false
opts.emptyRepo = opts.emptyRepo || false
opts.bits = opts.bits || 2048

// Pre-set config values.
var config = require('../../default-config.json')

// Verify repo does not yet exist (or that 'force' is provided).
repo.exists((err, res) => {
if (err) { return callback(err) }
if (res === true && !opts.force) {
return callback(new Error('repo already exists and \'force\' is not set'))
}

generateAndSetKeypair()
})

// Generate peer identity keypair + transform to desired format + add to config.
function generateAndSetKeypair () {
var keys = peerId.create({
bits: opts.bits
})
config.Identity = {
PeerID: keys.toB58String(),
PrivKey: keys.privKey.toString('base64')
}

writeVersion()
}

function writeVersion () {
const version = '3'
repo.version.set(version, (err) => {
if (err) { return callback(err) }

writeConfig()
})
}

// Write the config to the repo.
function writeConfig () {
repo.config.set(config, (err) => {
if (err) { return callback(err) }

addDefaultAssets()
})
}

// Add the default assets to the repo.
function addDefaultAssets () {
// Skip this step on the browser, or if emptyRepo was supplied.
const isNode = !global.window
if (!isNode || opts.emptyRepo) {
return doneImport(null)
}

const importer = require('ipfs-data-importing')
const blocks = new IpfsBlocks(repo)
const dag = new IpfsDagService(blocks)

const initDocsPath = path.join(__dirname, '../../init-doc')

importer.import(initDocsPath, dag, {
recursive: true
}, doneImport)

function doneImport (err, stat) {
if (err) { return callback(err) }

// All finished!
callback(null, true)
}
}
}
7 changes: 1 addition & 6 deletions tests/test-core/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ describe('IPFS Repo Tests on the Browser', function () {
testsContext
.keys()
.filter((key) => {
if (key === './test-swarm-node.js' ||
key === './test-swarm-node') {
return false
} else {
return true
}
return !(key.endsWith('-node.js') || key.endsWith('-node'))
})
.forEach((key) => {
testsContext(key)
Expand Down
3 changes: 1 addition & 2 deletions tests/test-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ describe('core', () => {
const tests = fs.readdirSync(__dirname)
tests.filter((file) => {
if (file === 'index.js' ||
file === 'browser.js' ||
file === 'test-swarm-browser.js') {
file.endsWith('browser.js')) {
return false
} else {
return true
Expand Down
89 changes: 89 additions & 0 deletions tests/test-core/test-init-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-env mocha */

const expect = require('chai').expect
const IPFS = require('../../src/core')
const IPFSRepo = require('ipfs-repo')

function createTestRepo () {
const repoPath = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + '/'

var store
var teardown

const isNode = !global.window
if (isNode) {
store = require('fs-blob-store')
teardown = (done) => {
const rimraf = require('rimraf')
rimraf(repoPath, (err) => {
expect(err).to.not.exist
done()
})
}
} else {
const idb = window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
window.msIndexedDB
store = require('idb-plus-blob-store')
teardown = (done) => {
idb.deleteDatabase(repoPath)
idb.deleteDatabase(repoPath + '/blocks')
done()
}
}

const options = {
bits: 64,
stores: {
keys: store,
config: store,
datastore: store,
logs: store,
locks: store,
version: store
}
}

var repo = new IPFSRepo(repoPath, options)

repo.teardown = teardown

return repo
}

describe('node: init', function () {
this.timeout(10000)

it('init docs written', (done) => {
var repo = createTestRepo()
const ipfs = new IPFS(repo)
ipfs.init({ bits: 64 }, (err) => {
expect(err).to.not.exist

// Check for default assets
var multihash = new Buffer('12205e7c3ce237f936c76faf625e90f7751a9f5eeb048f59873303c215e9cce87599', 'hex')
ipfs.object.get(multihash, {}, (err, node) => {
expect(err).to.not.exist
expect(node.links).to.exist

repo.teardown(done)
})
})
})

it('empty repo', (done) => {
var repo = createTestRepo()
const ipfs = new IPFS(repo)
ipfs.init({ bits: 64, emptyRepo: true }, (err) => {
expect(err).to.not.exist

// Check for default assets
var multihash = new Buffer('12205e7c3ce237f936c76faf625e90f7751a9f5eeb048f59873303c215e9cce87599', 'hex')
ipfs.object.get(multihash, {}, (err, node) => {
expect(err).to.exist
repo.teardown(done)
})
})
})
})
123 changes: 123 additions & 0 deletions tests/test-core/test-init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* eslint-env mocha */

const expect = require('chai').expect
const IPFS = require('../../src/core')
const IPFSRepo = require('ipfs-repo')

function createTestRepo () {
const repoPath = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + '/'

var store
var teardown

const isNode = !global.window
if (isNode) {
store = require('fs-blob-store')
teardown = (done) => {
const rimraf = require('rimraf')
rimraf(repoPath, (err) => {
expect(err).to.not.exist
done()
})
}
} else {
const idb = window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
window.msIndexedDB
store = require('idb-plus-blob-store')
teardown = (done) => {
idb.deleteDatabase(repoPath)
idb.deleteDatabase(repoPath + '/blocks')
done()
}
}

const options = {
bits: 64,
stores: {
keys: store,
config: store,
datastore: store,
logs: store,
locks: store,
version: store
}
}

var repo = new IPFSRepo(repoPath, options)

repo.teardown = teardown

return repo
}

describe('init', function () {
this.timeout(10000)

it('basic', (done) => {
var repo = createTestRepo()
const ipfs = new IPFS(repo)
ipfs.init({ emptyRepo: true }, (err) => {
expect(err).to.not.exist

repo.exists((err, res) => {
expect(err).to.not.exist
expect(res).to.equal(true)

repo.config.get((err, config) => {
expect(err).to.not.exist
expect(config.Identity).to.exist

repo.teardown(done)
})
})
})
})

it('set # of bits in key', (done) => {
var repo1 = createTestRepo()
var repo2 = createTestRepo()
const ipfsShort = new IPFS(repo1)
const ipfsLong = new IPFS(repo2)
ipfsShort.init({ bits: 128, emptyRepo: true }, (err) => {
expect(err).to.not.exist

ipfsLong.init({ bits: 256, emptyRepo: true }, (err) => {
expect(err).to.not.exist

repo1.config.get((err, config1) => {
expect(err).to.not.exist

repo2.config.get((err, config2) => {
expect(err).to.not.exist
expect(config1.Identity.PrivKey.length).is.below(config2.Identity.PrivKey.length)

repo1.teardown(() => {
repo2.teardown(done)
})
})
})
})
})
})

it('force init (overwrite)', (done) => {
var repo = createTestRepo()
const ipfs1 = new IPFS(repo)
const ipfs2 = new IPFS(repo)
ipfs1.init({ bits: 128, emptyRepo: true }, (err) => {
expect(err).to.not.exist

ipfs2.init({ bits: 128, force: false }, (err) => {
expect(err).to.exist

ipfs2.init({ force: true }, (err) => {
expect(err).to.not.exist

repo.teardown(done)
})
})
})
})
})

0 comments on commit fca76b0

Please sign in to comment.