Skip to content

Commit

Permalink
feat: Query peer data via RPC.
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenyoung committed Nov 21, 2024
1 parent b9d8470 commit 334da41
Showing 9 changed files with 112 additions and 4 deletions.
36 changes: 36 additions & 0 deletions src/bin/cli.js
Original file line number Diff line number Diff line change
@@ -93,6 +93,42 @@ yargs(hideBin(process.argv))
})
.demandCommand(1, 'Error: use add or remove')
})
.command('peer', 'Peer details', yargs => {
yargs
.command(
'id',
'Get the peer ID',
() => {},
async argv => {
const { peerId } = await RPC(argv)
const res = await peerId()
if (res.type === Responses.OK) {
const id = res.message
console.log(id)
process.exit(0)
} else {
console.error(res)
process.exit(1)
}
})
.command(
'addresses',
'Get the peer addresses',
() => {},
async argv => {
const { peerAddresses } = await RPC(argv)
const res = await peerAddresses()
if (res.type === Responses.OK) {
for (const id of res.message) {
console.log(id)
}
process.exit(0)
} else {
console.error(res)
process.exit(1)
}
})
})
.option('verbose', {
alias: 'v',
description: 'Be more verbose. Outputs errors and other connection messages. Use multiple -vvv for more verbose logging.',
12 changes: 11 additions & 1 deletion src/rpc-client.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,14 @@ import { loadConfig } from './utils/config-manager.js'
import { config as libp2pConfig } from './utils/libp2p-config.js'
import { privateKeyFromRaw } from '@libp2p/crypto/keys'

const peerId = (identity, libp2p, address) => async () => {
return sendCommand(identity, libp2p, address, Commands.PEER_ID)
}

const peerAddresses = (identity, libp2p, address) => async () => {
return sendCommand(identity, libp2p, address, Commands.PEER_ADDRESSES)
}

const authAdd = (identity, libp2p, address) => async ({ id }) => {
return sendCommand(identity, libp2p, address, Commands.AUTH_ADD, [id])
}
@@ -42,6 +50,8 @@ export default async ({ directory }) => {
return {
authAdd: authAdd(identity, libp2p, config.orbiter.api),
authDel: authDel(identity, libp2p, config.orbiter.api),
authList: authList(identity, libp2p, config.orbiter.api)
authList: authList(identity, libp2p, config.orbiter.api),
peerId: peerId(identity, libp2p, config.orbiter.api),
peerAddresses: peerAddresses(identity, libp2p, config.orbiter.api)
}
}
4 changes: 3 additions & 1 deletion src/rpc/commands.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const Commands = Object.freeze({
AUTH_ADD: 1,
AUTH_DEL: 2,
AUTH_LIST: 3
AUTH_LIST: 3,
PEER_ID: 4,
PEER_ADDRESSES: 5
})
14 changes: 14 additions & 0 deletions src/rpc/handle-command.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@ import { Commands } from './commands.js'
import handleAuthAddRequest from './handlers/auth/add.js'
import handleAuthDelRequest from './handlers/auth/del.js'
import handleAuthListRequest from './handlers/auth/list.js'
import handlePeerIdRequest from './handlers/peer/id.js'
import handlePeerAddressesRequest from './handlers/peer/addresses.js'
import { createResponseMessage, parseMessage, Responses } from '../lib/messages/index.js'

export const handleCommand = (orbiter) => source => {
@@ -39,6 +41,18 @@ export const handleCommand = (orbiter) => source => {
response = createResponseMessage(Responses.OK, list)
break
}
case Commands.PEER_ID: {
const libp2p = orbiter.orbitdb.ipfs.libp2p
const peerId = handlePeerIdRequest({ libp2p })
response = createResponseMessage(Responses.OK, peerId)
break
}
case Commands.PEER_ADDRESSES: {
const libp2p = orbiter.orbitdb.ipfs.libp2p
const peers = handlePeerAddressesRequest({ libp2p })
response = createResponseMessage(Responses.OK, peers)
break
}
default:
throw Object.assign(new Error(`unknown message type ${type}`), { type: Responses.E_INTERNAL_ERROR })
}
8 changes: 8 additions & 0 deletions src/rpc/handlers/peer/addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default ({ libp2p }) => {
const addresses = []
libp2p.getMultiaddrs().forEach((address) => {
addresses.push(address.toString())
})

return addresses
}
3 changes: 3 additions & 0 deletions src/rpc/handlers/peer/id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default ({ libp2p }) => {
return libp2p.peerId.toString()
}
2 changes: 1 addition & 1 deletion test/commands/auth.test.js
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import { renameSync } from 'fs'
import { rimraf } from 'rimraf'
import waitForDaemonStarted from '../utils/wait-for-daemon-start.js'

describe('auth', function () {
describe('Commands - auth', function () {
let daemon

before(async function () {
2 changes: 1 addition & 1 deletion test/commands/daemon.test.js
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import { rimraf } from 'rimraf'
import { join } from 'path'
import waitForDaemonStarted from '../utils/wait-for-daemon-start.js'

describe('daemon', function () {
describe('Commands - daemon', function () {
describe('defaults', function () {
it('starts daemon in default directory', async function () {
const daemon = spawn('./src/bin/cli.js', ['daemon'])
35 changes: 35 additions & 0 deletions test/commands/peer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { spawn, execSync } from 'node:child_process'
import { strictEqual } from 'assert'
import { rimraf } from 'rimraf'
import { loadConfig } from '../../src/utils/config-manager.js'
import waitForDaemonStarted from '../utils/wait-for-daemon-start.js'

describe.only('Commands - peer', function () {
let daemon
let config

before(async function () {
daemon = spawn('./src/bin/cli.js', ['daemon'])
await waitForDaemonStarted(daemon)
const path = 'voyager'
config = await loadConfig({ path })
})

after(async function () {
if (daemon) {
daemon.kill()
}
await rimraf('voyager')
})

it('gets the peer id', function () {
const peerId = execSync('./src/bin/cli.js peer id')
strictEqual(peerId.toString(), `${config.orbiter.peerId}\n`)
})

it('gets the peer addresses', function () {
const peerAddresses = execSync('./src/bin/cli.js peer addresses')
const addresses = peerAddresses.toString().split("\n");
strictEqual(addresses.includes(config.orbiter.api), true)
})
})

0 comments on commit 334da41

Please sign in to comment.