Js-ipfs swarm.key for private network

#1

How do I access a private network using a swarm.key using js-ipfs?

#2

Are you trying to access it from a JS-IPFS Node or from the browser?

From node.js it is pretty straight forward, just put the key in the .jsipfs (IPFS_REPO path / repository home) folder.

From browser, it is little bit tricky. I believe you have to create a private connection by defining a libp2p-pnet connection protector (and passing it the swarm key as contructor). I am trying to make it work, but not getting through a custom libp2p bundle.

#3

Trying to access through browser, having the same issues

#4

Can you post your code & configuration here? Are you using the tag or creating a bundle using browserify/webpack? What are the versions of the libraries/modules for your dependencies?
What errors are you getting?

#5

I have problems with making it work with jsipfs.
I created the swarm.key file, but the text

Swarm is limited to private network of peers with the swarm key
Swarm key fingerprint: -

doesn’t show up in the command line when I run jsipfs daemon.
It does work for ipfs daemon though, this prints the text.

I have a copy of swarm.key in both .jsipfs and .ipfs folders.

#6

Same here
It seems that setting up private net via swarm key feature hasn’t been implemented yet in js-ipfs

#7

ok i’ve figured it out…

#8

Can you please explain how to enable private swarm in js-ipfs?

#9

I got an error recently when trying to initiate a private js-ipfs swarm. Something along the lines of “groups are not supported”. Should we take this to mean that private swarms are not functional in js-ipfs yet?

#10

Hey @sha , I figured it out.

The swarm.key gets ignored at the moment, so one has to do it manually.

const fs = require('fs');
const IPFS = require('ipfs');
const Protector = require('libp2p-pnet')
// your path to the key file
const swarmKeyPath = './repo/swarm.key';
const node = new IPFS({ 
    libp2p: {
      modules: {
        connProtector: new Protector(fs.readFileSync(swarmKeyPath))
      }
    }
})

Just as the config, you can define some settings for libp2p.
Take a look at the following issues: https://github.com/ipfs/js-ipfs/issues/1857 and https://github.com/libp2p/js-libp2p-pnet/issues/3

#11

Hi, can you detail how to get the swarm.key. I’m unable to follow the github / other sites. It will be great help if shared in detail. Thanks.

#12

Anyway i found the solution by doing below:

Run the below after installing Go (Git bash)

go get github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen
ipfs-swarm-key-gen > ~/.ipfs/swarm.key

In case if you want to share the key with other private node, perform below

ipfs bootstrap rm all
ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/8080
ipgs config Addresses.API/ip4/0.0.0.0/tcp/5001

Set LIBP2P_FORCE_PNET=1 (Windows)
export LIBP2P_FORCE_PNET=1

systemctl restart ipfsd (Optional)
systemctl status ipfsd (Optional)

ipfs bootstrap add
Do the same process in both the private nodes.