IPFS private chain construction

IPFS installation

Officially download the Releases version, version number v0.10.0

[root@localhost ~]# mkdir ipfs && cd ipfs
[root@localhost ipfs]# tar -zxf go-ipfs_v0.10.0_linux-amd64.tar.gz
[root@localhost ipfs]# cd go-ipfs
[root@localhost go-ipfs]# ls
install.sh  ipfs  LICENSE  LICENSE-APACHE  LICENSE-MIT  README.md
[root@localhost go-ipfs]# sudo ./install.sh
Moved ./ipfs to /usr/local/bin
[root@localhost go-ipfs]# ipfs --version
ipfs version 0.10.0

IPFS initialization

[root@localhost go-ipfs]# ipfs init
generating ED25519 keypair...done
peer identity: 12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu
initializing IPFS node at /root/.ipfs
to get started, enter:

        ipfs cat /ipfs/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/readme

[root@localhost go-ipfs]# ipfs cat /ipfs/QmQPeNsJPyVWPFDVHb77w8G42Fvo15z4bG2X8D2GhfbSXc/readme
Hello and Welcome to IPFS!

██╗██████╗ ███████╗███████╗
██║██╔══██╗██╔════╝██╔════╝
██║██████╔╝█████╗  ███████╗
██║██╔═══╝ ██╔══╝  ╚════██║
██║██║     ██║     ███████║
╚═╝╚═╝     ╚═╝     ╚══════╝

If you're seeing this, you have successfully installed
IPFS and are now interfacing with the ipfs merkledag!

 -------------------------------------------------------
| Warning:                                              |
|   This is alpha software. Use at your own discretion! |
|   Much is missing or lacking polish. There are bugs.  |
|   Not yet secure. Read the security notes for more.   |
 -------------------------------------------------------

Check out some of the other files in this directory:

  ./about
  ./help
  ./quick-start     <-- usage examples
  ./readme          <-- this file
  ./security-notes
[root@localhost go-ipfs]# 

View native ID

[root@localhost go-ipfs]# ipfs id
{
        "ID": "12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
        "PublicKey": "CAESILBvoj7rWdH8AQ6M++06Ze9nLiHqWCODUlyGzwr5Hk5G",
        "Addresses": null,
        "AgentVersion": "go-ipfs/0.10.0/",
        "ProtocolVersion": "ipfs/0.1.0",
        "Protocols": null
}

[root@localhost go-ipfs]# 

Node configuration

After IPFS is installed, some nearby nodes will be configured in the Bootstrap node of ~ /. ipfs/config configuration file by default. If you build a private chain, you need to set Bootstrap to null

 "Bootstrap": [
    "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
    "/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
    "/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
    "/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
    "/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
    "/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ"
  ],

Allow access to other machines

[root@localhost go-ipfs]# ipfs config --json Addresses.API '"/ip4/0.0.0.0/tcp/5001"'

Cross domain resource sharing CORS configuration

[root@localhost go-ipfs]# ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'

Start service

[root@localhost go-ipfs]# ipfs daemon
Initializing daemon...
go-ipfs version: 0.10.0
Repo version: 11
System version: amd64/linux
Golang version: go1.16.8
2021/11/16 22:16:25 failed to sufficiently increase receive buffer size (was: 208 kiB, wanted: 2048 kiB, got: 416 kiB). See https://github.com/lucas-clemente/quic-go/wiki/UDP-Receive-Buffer-Size for details.
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/127.0.0.1/udp/4001/quic
Swarm listening on /ip4/192.168.75.131/tcp/4001
Swarm listening on /ip4/192.168.75.131/udp/4001/quic
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /ip6/::1/udp/4001/quic
Swarm listening on /p2p-circuit
Swarm announcing /ip4/127.0.0.1/tcp/4001
Swarm announcing /ip4/127.0.0.1/udp/4001/quic
Swarm announcing /ip4/192.168.75.131/tcp/4001
Swarm announcing /ip4/192.168.75.131/udp/4001/quic
Swarm announcing /ip6/::1/tcp/4001
Swarm announcing /ip6/::1/udp/4001/quic
API server listening on /ip4/0.0.0.0/tcp/5001
WebUI: http://0.0.0.0:5001/webui
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready

Background start

[root@localhost go-ipfs]# ipfs daemon &
[root@localhost go-ipfs]# ps -ef|grep ipfs
root      68929   4208  0 02:12 pts/0    00:00:00 ipfs daemon
root      71768   4208  0 02:14 pts/0    00:00:00 grep --color=auto ipfs

Review the native ID again

[root@localhost ~]# ipfs id
{
        "ID": "12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
        "PublicKey": "CAESILBvoj7rWdH8AQ6M++06Ze9nLiHqWCODUlyGzwr5Hk5G",
        "Addresses": [
                "/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
                "/ip4/127.0.0.1/udp/4001/quic/p2p/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
                "/ip4/192.168.75.131/tcp/4001/p2p/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
                "/ip4/192.168.75.131/udp/4001/quic/p2p/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
                "/ip6/::1/tcp/4001/p2p/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
                "/ip6/::1/udp/4001/quic/p2p/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu"
        ],
        "AgentVersion": "go-ipfs/0.10.0/",
        "ProtocolVersion": "ipfs/0.1.0",
        "Protocols": [
                "/ipfs/bitswap",
                "/ipfs/bitswap/1.0.0",
                "/ipfs/bitswap/1.1.0",
                "/ipfs/bitswap/1.2.0",
                "/ipfs/id/1.0.0",
                "/ipfs/id/push/1.0.0",
                "/ipfs/lan/kad/1.0.0",
                "/ipfs/ping/1.0.0",
                "/libp2p/autonat/1.0.0",
                "/libp2p/circuit/relay/0.1.0",
                "/p2p/id/delta/1.0.0",
                "/x/"
        ]
}

IPFS command line operation

Official documents

Add file

## The QmeV1kwh3333bsnT6YRfdCRrSgUPngKmAhhTa4RrqYPbKT returned from the file addition test is the hash value of the file
## Text files can be viewed directly through the browser 
## http://192.168.75.131:8080/ipfs/QmeV1kwh3333bsnT6YRfdCRrSgUPngKmAhhTa4RrqYPbKT
## Check whether the Gateway in the ~ /. ipfs/config configuration allows other machines to access  
[root@localhost ~]# vim hello.txt
## Add hello world!
[root@localhost ~]# ls
anaconda-ks.cfg  go  hello.txt  ipfs
[root@localhost ~]# ipfs add hello.txt 
added QmeV1kwh3333bsnT6YRfdCRrSgUPngKmAhhTa4RrqYPbKT hello.txt

The returned QmeV1kwh3333bsnT6YRfdCRrSgUPngKmAhhTa4RrqYPbKT is the hash value of the file

Pictures, files, etc. can be directly through the browser language = = > http://192.168.75.131:8080/ipfs/QmeV1kwh3333bsnT6YRfdCRrSgUPngKmAhhTa4RrqYPbKT

Check whether the firewall allows port access and whether the Gateway allows other machines to access in ~ /. ipfs/config configuration

"Gateway": "/ip4/0.0.0.0/tcp/8080",

View files from the command line
[root@localhost ~]# ipfs cat QmeV1kwh3333bsnT6YRfdCRrSgUPngKmAhhTa4RrqYPbKT
hello world!

IPFS WebUI

When the daemon service is enabled, it will try to connect to the nodes in the Bootstrap node list to synchronize data. At the same time, it will also start two services locally: API service and Web Gateway Service

  1. API services

    The default is 5001 port, which can be accessed through http://192.168.75.131:5001/webui This is also a Web version of IPFS management console. You can add files and view node connections through this console

  2. Gateway service

    The default is port 8080. Because the current browser does not support accessing files through ipfs protocol (ipfs: / /), if we want to access files in the browser, we need to use the gateway service provided by ipfs. The browser first accesses the gateway, and the gateway obtains the files killed by ipfs network. With the gateway service, you can use this link: http://192.168.75.131:8080/ipfs/QmeV1kwh3333bsnT6YRfdCRrSgUPngKmAhhTa4RrqYPbKT To access the file just uploaded to ipfs.

Multi node connection

Prepare two virtual machines, install IPFS according to the above steps respectively, and view the node ID through the ipfs id command

  • 192.168.75.131 12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu
  • 192.168.75.5 12D3KooWHGzA47vMzmfcWzpQLsHQN19q2t2i7mFi4hPaDoFRMpho
Add node

After adding nodes using ipfs bootstrap add /ip4/ip/tcp/4001/ipfs/hash, restart the daemon services of both parties

  • 192.168.75.131

    [root@localhost ~]# ipfs bootstrap add /ip4/192.168.75.5/tcp/4001/ipfs/12D3KooWHGzA47vMzmfcWzpQLsHQN19q2t2i7mFi4hPaDoFRMpho
    added /ip4/192.168.75.5/tcp/4001/ipfs/12D3KooWHGzA47vMzmfcWzpQLsHQN19q2t2i7mFi4hPaDoFRMpho
    [root@localhost ~]# ipfs id
    {
            "ID": "12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
            "PublicKey": "CAESILBvoj7rWdH8AQ6M++06Ze9nLiHqWCODUlyGzwr5Hk5G",
            "Addresses": [
                    "/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
                    "/ip4/127.0.0.1/udp/4001/quic/p2p/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
                    "/ip4/192.168.75.131/tcp/4001/p2p/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
                    "/ip4/192.168.75.131/udp/4001/quic/p2p/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
                    "/ip6/::1/tcp/4001/p2p/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu",
                    "/ip6/::1/udp/4001/quic/p2p/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu"
            ],
            "AgentVersion": "go-ipfs/0.10.0/",
            "ProtocolVersion": "ipfs/0.1.0",
            "Protocols": [
                    "/ipfs/bitswap",
                    "/ipfs/bitswap/1.0.0",
                    "/ipfs/bitswap/1.1.0",
                    "/ipfs/bitswap/1.2.0",
                    "/ipfs/id/1.0.0",
                    "/ipfs/id/push/1.0.0",
                    "/ipfs/lan/kad/1.0.0",
                    "/ipfs/ping/1.0.0",
                    "/libp2p/autonat/1.0.0",
                    "/libp2p/circuit/relay/0.1.0",
                    "/p2p/id/delta/1.0.0",
                    "/x/"
            ]
    }
    
  • 192.168.75.5

    [root@www go]# ipfs bootstrap add /ip4/192.168.75.131/tcp/4001/ipfs/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu
    added /ip4/192.168.75.131/tcp/4001/ipfs/12D3KooWMh6eqFVSEgotLyZPFvhZpadYzyN23vZK2yBSpzogBGxu
    [root@www go]# ipfs id
    {
            "ID": "12D3KooWHGzA47vMzmfcWzpQLsHQN19q2t2i7mFi4hPaDoFRMpho",
            "PublicKey": "CAESIG7TmfV0oluqavhf44tG71wx/XlrDt+s2yreZNmnV6i6",
            "Addresses": [
                    "/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWHGzA47vMzmfcWzpQLsHQN19q2t2i7mFi4hPaDoFRMpho",
                    "/ip4/127.0.0.1/udp/4001/quic/p2p/12D3KooWHGzA47vMzmfcWzpQLsHQN19q2t2i7mFi4hPaDoFRMpho",
                    "/ip4/192.168.75.5/tcp/4001/p2p/12D3KooWHGzA47vMzmfcWzpQLsHQN19q2t2i7mFi4hPaDoFRMpho",
                    "/ip4/192.168.75.5/udp/4001/quic/p2p/12D3KooWHGzA47vMzmfcWzpQLsHQN19q2t2i7mFi4hPaDoFRMpho",
                    "/ip6/::1/tcp/4001/p2p/12D3KooWHGzA47vMzmfcWzpQLsHQN19q2t2i7mFi4hPaDoFRMpho",
                    "/ip6/::1/udp/4001/quic/p2p/12D3KooWHGzA47vMzmfcWzpQLsHQN19q2t2i7mFi4hPaDoFRMpho"
            ],
            "AgentVersion": "go-ipfs/0.10.0/",
            "ProtocolVersion": "ipfs/0.1.0",
            "Protocols": [
                    "/ipfs/bitswap",
                    "/ipfs/bitswap/1.0.0",
                    "/ipfs/bitswap/1.1.0",
                    "/ipfs/bitswap/1.2.0",
                    "/ipfs/id/1.0.0",
                    "/ipfs/id/push/1.0.0",
                    "/ipfs/lan/kad/1.0.0",
                    "/ipfs/ping/1.0.0",
                    "/libp2p/autonat/1.0.0",
                    "/libp2p/circuit/relay/0.1.0",
                    "/p2p/id/delta/1.0.0",
                    "/x/"
            ]
    }
    
    

Multi node shared key

All nodes in the same IPFS private chain must share the same key to join.

GO environment installation

Because key generation requires go support, if go is not installed on the local server, please download and install go first

[root@localhost ~]# mkdir go && cd go
[root@localhost go]# wget https://golang.org/dl/go1.17.2.linux-amd64.tar.gz

If the download fails, you can go to GO official website download , go1.17.2, manually copy to the specified directory

[root@localhost go]# tar -zxvf go1.17.2.linux-amd64.tar.gz
[root@localhost go]# sudo mv go /usr/local
[root@localhost go]# vi ~/.profile
 ## Add at the end of file 
 export PATH=$PATH:/usr/local/go/bin
[root@localhost go]# source ~/.profile
[root@localhost go]# go version
go version go1.17.2 linux/amd64

Install key tool

[root@localhost go]# cd /mnt/
## Download key generation tool
[root@localhost mnt]# go get -u github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen

If connection rejected is displayed, you can go to the GitHub warehouse( Kubuxu/go-ipfs-swarm-key-gen (github.com) download

## Compile go IPFs swarm key Gen to generate executable binary files
[root@localhost mnt]# sudo go build -o ipfs-swarm-key-gen go-ipfs-swarm-key-gen/ipfs-swarm-key-gen/main.go
[root@localhost mnt]# ls
go-ipfs-swarm-key-gen  ipfs-swarm-key-gen
## Use the binary file to generate the key file swarm.key
[root@localhost mnt]# sudo ./ipfs-swarm-key-gen > swarm.key
[root@localhost mnt]# ls
go-ipfs-swarm-key-gen  ipfs-swarm-key-gen  swarm.key
## View key string
[root@localhost mnt]# cat swarm.key 
/key/swarm/psk/1.0.0/
/base16/
ea6021fc554524392a6d0112b03cc6055636c500638a6c59e035256ad56b4de3
[root@localhost mnt]# mv swarm.key ~/.ipfs/
[root@localhost .ipfs]# ls
api  blocks  config  datastore  datastore_spec  keystore  repo.lock  swarm.key  version

Synchronize the generated swarm.key file to the ~ /. ipfs / directory of each node

## 192.168.75.5 copy key file through scp command
[root@www .ipfs]# scp -r root@192.168.75.131:~/.ipfs/swarm.key ~/.ipfs/
The authenticity of host '192.168.75.131 (192.168.75.131)' can't be established.
ECDSA key fingerprint is SHA256:pwnOQ8plHgMBKPD9PFRO8m4WtMejP0DXJRMPw4uh4AM.
ECDSA key fingerprint is MD5:0f:4e:49:39:9a:43:0d:84:b0:c2:3c:15:50:ed:04:09.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.75.131' (ECDSA) to the list of known hosts.
root@192.168.75.131's password: 
swarm.key                                                                                                                                                                    100%   95   147.4KB/s   00:00    
[root@www .ipfs]# ls
api  blocks  config  datastore  datastore_spec  keystore  repo.lock  swarm.key  version

sudo: go: command not found

[root@localhost mnt]# sudo vim /etc/sudoers
## Add / usr/local/go/bin path of go after Defaults secure_path
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/go/bin
## : wq! Force save

IPFS multi node file synchronization test

  • 192.168.75.131

    ## Add test file
    [root@localhost ~]# vim test.txt
    ## Enter IPFs multi node file synchronization test!!!
    [root@localhost ~]# ipfs add test.txt 
    added QmVWxqGwov3JPYMgmqrQPbtUWxY3DV2263kNErKt1XaxWo test.txt
    [root@localhost ~]# ipfs cat QmVWxqGwov3JPYMgmqrQPbtUWxY3DV2263kNErKt1XaxWo
    IPFS multi-node file synchronization test!!!
    
  • 192.168.75.5

    ## Query according to hash
    [root@www .ipfs]# ipfs cat QmVWxqGwov3JPYMgmqrQPbtUWxY3DV2263kNErKt1XaxWo
    IPFS multi-node file synchronization test!!!
    

    This indicates that the two nodes are connected. At this time, stop node 75.131 and node 75.5 can still be queried.

Reference article:

  • https://blog.csdn.net/cljdsc/article/details/110849814
  • https://blog.csdn.net/loverson/article/details/120992335

Tags: Blockchain ipfs

Posted on Tue, 16 Nov 2021 05:37:15 -0500 by philhale