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

feat: cli supports peer id path #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/server/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

'use strict'

// Usage: $0 [--libp2pMultiaddr <ma> ... <ma>] [--metricsMultiaddr <ma>] [--disableMetrics]
// Usage: $0 [--peerId <jsonFilePath>] [--libp2pMultiaddr <ma> ... <ma>] [--metricsMultiaddr <ma>] [--disableMetrics]

/* eslint-disable no-console */

const debug = require('debug')
const log = debug('libp2p:stardust:server:bin')

const fs = require('fs')
const http = require('http')
const menoetius = require('menoetius')

const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const Server = require('.')

const argv = require('minimist')(process.argv.slice(2))
Expand All @@ -25,6 +28,16 @@ async function run () {
const libp2pMa = argv.libp2pMultiaddr || argv.lm || process.env.LIBP2PMA || '/ip6/::/tcp/5892/ws'
const addresses = [multiaddr(libp2pMa)]

let peerInfo
if (argv.peerId) {
const peerData = fs.readFileSync(argv.peerId)
const peerId = await PeerId.createFromJSON(JSON.parse(peerData))
peerInfo = await PeerInfo.create(peerId)
} else {
log('You are using an automatically generated peer. \n')
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
log('If you want to keep the same address for the server you should provide a peerId with --peerId <jsonFilePath>')
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, maybe we should add a log here when the peer id is not provided, just to warn that a new id will be created at each restart, and recommend using the peer id option.


// Add remaining addresses
if (argv.libp2pMultiaddr || argv.lm) {
argv._.forEach((addr) => {
Expand All @@ -34,7 +47,7 @@ async function run () {

let metricsServer

const server = new Server({ addresses, hasMetrics: metrics })
const server = new Server({ addresses, hasMetrics: metrics, peerInfo })
await server.start()

console.log('server peerID: ', server.libp2p.peerInfo.id.toB58String())
Expand Down