├── .gitignore ├── README.md ├── package.json ├── src ├── browser.js ├── index.js └── nodejs.js └── test └── test-basics.js /.gitignore: -------------------------------------------------------------------------------- 1 | .nyc_output 2 | coverage 3 | package-lock.json 4 | node_modules -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libp2p-simple 2 | 3 | Pre-configured libp2p module. 4 | 5 | ## Usage 6 | 7 | ```JavaScript 8 | const libp2p = require('libp2p-simple') 9 | 10 | let node = libp2p() // returns libp2p instance. 11 | ``` 12 | 13 | Can be used in Node.js and in the browser and each will be configured 14 | for the transports in that environment. 15 | 16 | The configurations are pulled from 17 | [`js-ipfs`](https://github.com/ipfs/js-ipfs) 18 | [nodejs](https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/libp2p-nodejs.js) and 19 | [browser](https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/libp2p-browser.js) bundles. 20 | 21 | ## API 22 | 23 | ### `libp2p([...args])` 24 | 25 | Arguments can be passed in any order. Valid arguments are: 26 | 27 | * Instance of PeerID. 28 | * Instance of PeerInfo. 29 | * Instance of PeerBook. 30 | 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "libp2p-simple", 3 | "version": "0.1.0", 4 | "description": "Pre-configured libp2p module.", 5 | "main": "src/nodejs.js", 6 | "browser": "src/browserjs.js", 7 | "scripts": { 8 | "lint": "eslint src test", 9 | "pretest": "npm run lint", 10 | "test": "tap test/test-*.js --100" 11 | }, 12 | "keywords": [], 13 | "author": "Mikeal Rogers (http://www.mikealrogers.com)", 14 | "license": "Apache-2.0", 15 | "dependencies": { 16 | "ipfs": "^0.29.3", 17 | "keypair": "^1.0.1", 18 | "libp2p-kad-dht": "^0.10.0", 19 | "libp2p-mdns": "^0.12.0", 20 | "libp2p-mplex": "^0.8.0", 21 | "libp2p-railing": "^0.9.1", 22 | "libp2p-secio": "^0.10.0", 23 | "libp2p-tcp": "^0.12.0", 24 | "libp2p-websocket-star": "^0.8.1", 25 | "libp2p-websockets": "^0.12.0", 26 | "multihashes": "^0.4.13", 27 | "peer-book": "^0.8.0", 28 | "peer-id": "^0.10.7", 29 | "peer-info": "^0.14.1", 30 | "pem-jwk": "^1.5.1" 31 | }, 32 | "devDependencies": { 33 | "eslint": "^5.0.0", 34 | "eslint-config-standard": "^11.0.0", 35 | "eslint-plugin-import": "^2.12.0", 36 | "eslint-plugin-node": "^6.0.1", 37 | "eslint-plugin-promise": "^3.8.0", 38 | "eslint-plugin-standard": "^3.1.0", 39 | "tap": "^12.0.1" 40 | }, 41 | "eslintConfig": { 42 | "extends": [ 43 | "standard" 44 | ] 45 | }, 46 | "directories": { 47 | "test": "test" 48 | }, 49 | "repository": { 50 | "type": "git", 51 | "url": "git+https://github.com/mikeal/libp2p-simple.git" 52 | }, 53 | "bugs": { 54 | "url": "https://github.com/mikeal/libp2p-simple/issues" 55 | }, 56 | "homepage": "https://github.com/mikeal/libp2p-simple#readme" 57 | } 58 | -------------------------------------------------------------------------------- /src/browser.js: -------------------------------------------------------------------------------- 1 | let Node = require('ipfs/src/core/runtime/libp2p-browser') 2 | module.exports = require('./')(Node) 3 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | const crypto = require('crypto') 2 | const keypair = require('keypair') 3 | const rsa = require('libp2p-crypto/src/keys/rsa-class') 4 | const PeerId = require('peer-id') 5 | const PeerInfo = require('peer-info') 6 | const PeerBook = require('peer-book') 7 | const multihashes = require('multihashes') 8 | const {pem2jwk} = require('pem-jwk') 9 | 10 | const sha2 = b => crypto.createHash('sha256').update(b).digest() 11 | 12 | /* synchronous version of PeerId creation */ 13 | const create = () => { 14 | let p = keypair({ bits: 2048 }) 15 | let privKey = new rsa.RsaPrivateKey(pem2jwk(p.private), pem2jwk(p.public)) 16 | let digest = multihashes.encode(sha2(privKey.bytes), 'sha2-256') 17 | return new PeerId(digest, privKey) 18 | } 19 | 20 | module.exports = P2PNode => { 21 | return (...args) => { 22 | let peerID 23 | let peerInfo 24 | let peerBook 25 | args.forEach(arg => { 26 | if (arg instanceof PeerId) peerID = arg 27 | if (arg instanceof PeerInfo) peerInfo = arg 28 | if (arg instanceof PeerBook) peerBook = arg 29 | }) 30 | if (!peerID && !peerInfo) peerID = create() 31 | if (!peerInfo) peerInfo = new PeerInfo(peerID) 32 | if (!peerBook) peerBook = new PeerBook() 33 | return new P2PNode(peerInfo, new PeerBook()) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/nodejs.js: -------------------------------------------------------------------------------- 1 | let Node = require('ipfs/src/core/runtime/libp2p-nodejs') 2 | module.exports = require('./')(Node) 3 | -------------------------------------------------------------------------------- /test/test-basics.js: -------------------------------------------------------------------------------- 1 | const {test} = require('tap') 2 | const libp2p = require('../') 3 | 4 | const PeerId = require('peer-id') 5 | const PeerInfo = require('peer-info') 6 | const PeerBook = require('peer-book') 7 | 8 | test('basic creation', t => { 9 | t.plan(1) 10 | let n = libp2p() 11 | t.ok(n) 12 | }) 13 | 14 | test('set peerId', t => { 15 | t.plan(1) 16 | PeerId.create((err, id) => { 17 | if (err) throw err 18 | let n = libp2p(id) 19 | t.same(id, n.peerInfo.id) 20 | }) 21 | }) 22 | 23 | test('set peerInfo', t => { 24 | t.plan(1) 25 | PeerId.create((err, id) => { 26 | if (err) throw err 27 | let p = new PeerInfo(id) 28 | let n = libp2p(p) 29 | t.same(p, n.peerInfo) 30 | }) 31 | }) 32 | 33 | test('set peerBook', t => { 34 | t.plan(1) 35 | let b = new PeerBook() 36 | let n = libp2p(b) 37 | t.same(n.peerBook, b) 38 | }) 39 | --------------------------------------------------------------------------------