Skip to content

Commit 4490d2b

Browse files
authored
feat: add script to configure private IPFS network (#458)
1 parent c4fa4a3 commit 4490d2b

File tree

7 files changed

+158
-9
lines changed

7 files changed

+158
-9
lines changed

packages/ethereum-storage/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,23 @@ In order to use the package in a test environment, IPFS can be installed locally
8282

8383
```bash
8484
npm install ipfs --global
85-
ipfs init
85+
yarn ipfs-init #documented below
8686
ipfs daemon
8787
```
8888

8989
Local IPFS listening on port 5001 is used by default by the `ethereum-storage` package.
9090

91+
### Setup IPFS private network
92+
93+
Request uses a private network to allow all nodes to connect to each other directly,
94+
instead of having to navigate through the global IPFS network.
95+
96+
To setup your IPFS node to the private network, you can run the following utility script:
97+
98+
```bash
99+
yarn init-ipfs
100+
```
101+
91102
## Contributing
92103

93104
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

packages/ethereum-storage/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"test:lib": "nyc mocha --timeout=5000 --require ts-node/register --require source-map-support/register \"test/lib/**/*.ts\"",
4848
"test:lib:watch": "nyc mocha --watch --watch-extensions ts --timeout=5000 --require ts-node/register --require source-map-support/register \"test/lib/**/*.ts\"",
4949
"test:sol": "truffle test --contracts_directory=./src test/contracts/*.js",
50-
"test": "yarn run test:lib && yarn run test:sol"
50+
"test": "yarn run test:lib && yarn run test:sol",
51+
"init-ipfs": "node scripts/init-ipfs.js"
5152
},
5253
"dependencies": {
5354
"@requestnetwork/types": "0.2.0",
@@ -58,9 +59,11 @@
5859
"ipfs-unixfs": "0.1.16",
5960
"keyv": "3.1.0",
6061
"node-fetch": "2.6.0",
62+
"shelljs": "0.8.3",
6163
"sinon": "7.3.2",
6264
"web3-eth": "1.0.0-beta.37",
63-
"web3-utils": "1.0.0-beta.37"
65+
"web3-utils": "1.0.0-beta.37",
66+
"yargs": "12.0.5"
6467
},
6568
"devDependencies": {
6669
"@types/bluebird": "3.5.27",
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/* eslint-disable spellcheck/spell-checker */
2+
const shell = require('shelljs');
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const argv = require('yargs')
7+
.usage('Usage: $0 [options]')
8+
// Allow the user to force IPFS config override
9+
.option('force', {
10+
alias: 'f',
11+
describe: 'Force the private IPFS config even if a config already exists',
12+
type: 'boolean',
13+
})
14+
// Get IPFS path from cli argument, environment variable or expected default path
15+
.option('path', {
16+
alias: 'p',
17+
describe:
18+
'The path to the IPFS config directory (the default path is usually already set on IPFS_PATH environment variable)',
19+
nargs: 1,
20+
type: 'string',
21+
default: process.env.IPFS_PATH || path.join(require('os').homedir(), '.ipfs'),
22+
}).argv;
23+
24+
// Swarm key content
25+
const swarmKeyContent = `/key/swarm/psk/1.0.0/
26+
/base16/
27+
5f3af0599d991e5eb4c37da2472aa299759ee3350ba26c125d0c7579dd04dd52
28+
`;
29+
30+
// Exit if IPFS is not installed
31+
if (!shell.which('ipfs')) {
32+
shell.echo('IPFS is not installed or not in the PATH');
33+
shell.exit(1);
34+
}
35+
36+
const ipfsPath = argv.path;
37+
shell.echo('IPFS Path:');
38+
shell.echo(ipfsPath);
39+
40+
// Initialize the IPFS node
41+
initializeNode(ipfsPath);
42+
43+
// Setup the swarm key file
44+
setupSwarmKey(ipfsPath, swarmKeyContent);
45+
46+
shell.echo('Done');
47+
shell.exit(0);
48+
49+
/**
50+
* Initialize the IPFS node with private network settings
51+
*/
52+
function initializeNode(ipfsPath) {
53+
// Check if the ipfs folder exists and if it is writable.
54+
try {
55+
fs.accessSync(ipfsPath, fs.constants.F_OK | fs.constants.W_OK);
56+
// If --force argument is set, the config will be overwritten
57+
if (argv.force) {
58+
shell.echo('IPFS config already exists. Force argument set, overriding it.');
59+
} else {
60+
shell.echo('IPFS config already exists (use --force if you want to override it)');
61+
shell.exit(0);
62+
}
63+
} catch (err) {
64+
if (err.code !== 'ENOENT') {
65+
shell.echo(`No read access to ipfs config folder: ${ipfsPath}`);
66+
shell.exit(1);
67+
}
68+
69+
// Initializing ipfs repo
70+
shell.echo('No IPFS repo found in $IPFS_PATH. Initializing...');
71+
shell.exec('ipfs init');
72+
}
73+
74+
// Reinitialize bootstrap nodes
75+
shell.echo('Removing all bootstrap nodes...');
76+
shell.echo(
77+
'(see https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#private-networks)',
78+
);
79+
shell.exec('ipfs bootstrap rm --all');
80+
81+
shell.echo('Adding private swarm bootstrap nodes...');
82+
shell.exec(
83+
`ipfs bootstrap add /dns4/ipfs-bootstrap.request.network/tcp/4001/ipfs/QmPBPgTDVjveRu6KjGVMYixkCSgGtVyV8aUe6wGQeLZFVd /dns4/ipfs-bootstrap-2.request.network/tcp/4001/ipfs/QmYdcSoVNU1axgSnkRAyHtwsKiSvFHXeVvRonGCAV9LVEj /dns4/ipfs-2.request.network/tcp/4001/ipfs/QmPBPgTDVjveRu6KjGVMYixkCSgGtVyV8aUe6wGQeLZFVd /dns4/ipfs-survival.request.network/tcp/4001/ipfs/Qmb6a5DH45k8JwLdLVZUhRhv1rnANpsbXjtsH41esGhNCh`,
84+
);
85+
}
86+
87+
/**
88+
* Setup the IPFS private swarm key
89+
*/
90+
function setupSwarmKey(ipfsPath, swarmKeyContent) {
91+
const swarmKeyPath = path.join(ipfsPath, 'swarm.key');
92+
// Check if the swarm key file exists
93+
try {
94+
fs.accessSync(swarmKeyPath, fs.constants.F_OK | fs.constants.W_OK);
95+
return;
96+
} catch (err) {
97+
shell.echo('No swarm.key found, creating a new key.');
98+
}
99+
100+
// Create a new swarm key file
101+
try {
102+
fs.writeFileSync(swarmKeyPath, swarmKeyContent, {
103+
mode: 0o600,
104+
});
105+
} catch (err) {
106+
throw new Error(`Error creating swarm key file: ${err.message}`);
107+
return;
108+
}
109+
110+
shell.echo('Swarm key file created.');
111+
}

packages/request-node/README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ npm install
169169
npm run build
170170
```
171171

172+
### Setup
173+
174+
#### IPFS private network
175+
176+
The Request Node uses IPFS to store and share transactions in a private network.
177+
We use a private network to allow all nodes to connect to each other directly,
178+
instead of having to navigate through the global IPFS network.
179+
180+
To setup your IPFS node to the private network, you can run the following utility script:
181+
182+
```bash
183+
yarn init-ipfs
184+
```
185+
172186
### Launch
173187

174188
#### Command line
@@ -315,23 +329,30 @@ yarn build
315329
#### 3. On a new terminal, launch a local [IPFS node](https://docs.ipfs.io/introduction/install/)
316330

317331
```bash
318-
ipfs daemon --offline
332+
ipfs daemon
333+
```
334+
335+
#### 4. On a new terminal, configure your IPFS node to connect to the private Request IPFS network
336+
337+
```bash
338+
cd packages/request-node
339+
yarn init-ipfs
319340
```
320341

321-
#### 4. On a new terminal, launch [ganache](https://github.com/trufflesuite/ganache-cli#installation) with the default Request Node mnemonic
342+
#### 5. Launch [ganache](https://github.com/trufflesuite/ganache-cli#installation) with the default Request Node mnemonic
322343

323344
```bash
324345
ganache-cli -l 90000000 -p 8545 -m \"candy maple cake sugar pudding cream honey rich smooth crumble sweet treat\"
325346
```
326347

327-
#### 5. Deploy the smart contracts on ganache
348+
#### 6. Deploy the smart contracts on ganache
328349

329350
```bash
330351
cd packages/ethereum-storage
331352
yarn deploy
332353
```
333354

334-
#### 6. Run the Request Node
355+
#### 7. Run the Request Node
335356

336357
```bash
337358
cd ../packages/request-node

packages/request-node/init-ipfs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../ethereum-storage/scripts/init-ipfs.js

packages/request-node/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"start:watch": "ts-node-dev src/server.ts",
3838
"clean": "shx rm -rf dist",
3939
"lint-staged": "lint-staged",
40-
"lint": "tslint --project ."
40+
"lint": "tslint --project .",
41+
"init-ipfs": "node init-ipfs.js"
4142
},
4243
"dependencies": {
4344
"@requestnetwork/data-access": "0.2.0",
@@ -50,6 +51,7 @@
5051
"express": "4.17.1",
5152
"http-status-codes": "1.3.0",
5253
"keyv-file": "0.1.13",
54+
"shelljs": "0.8.3",
5355
"truffle-hdwallet-provider": "1.0.3",
5456
"yargs": "12.0.5"
5557
},

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9821,7 +9821,7 @@ shell-quote@^1.6.1:
98219821
array-reduce "~0.0.0"
98229822
jsonify "~0.0.0"
98239823

9824-
shelljs@^0.8.1:
9824+
shelljs@^0.8.1, shelljs@^0.8.3:
98259825
version "0.8.3"
98269826
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.8.3.tgz#a7f3319520ebf09ee81275b2368adb286659b097"
98279827
integrity sha512-fc0BKlAWiLpwZljmOvAOTE/gXawtCoNrP5oaY7KIaQbbyHeQVg01pSEuEGvGh3HEdBU4baCD7wQBwADmM/7f7A==

0 commit comments

Comments
 (0)