feat(GET /ipfs/node): Retrieves thisNode object

This commit is contained in:
Chris Troutner
2024-01-27 09:26:50 -08:00
parent 5f965e7146
commit ec3ed165e4
8 changed files with 325 additions and 917 deletions
+289 -906
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -35,7 +35,7 @@
"datastore-fs": "9.1.6",
"glob": "7.1.6",
"helia": "2.1.0",
"helia-coord": "1.2.24",
"helia-coord": "1.4.0",
"jsonrpc-lite": "2.2.0",
"jsonwebtoken": "8.5.1",
"jwt-bch-lib": "1.3.0",
@@ -57,7 +57,7 @@
"node-fetch": "npm:@achingbrain/node-fetch@2.6.7",
"nodemailer": "6.7.5",
"passport-local": "1.0.0",
"public-ip": "4.0.4",
"public-ip": "6.0.1",
"winston": "3.3.3",
"winston-daily-rotate-file": "4.5.0"
},
+3 -3
View File
@@ -10,7 +10,7 @@ import IpfsCoord from 'helia-coord'
// import BCHJS from '@psf/bch-js';
import SlpWallet from 'minimal-slp-wallet'
import publicIp from 'public-ip'
import { publicIpv4 } from 'public-ip'
// Local libraries
import config from '../../../config/index.js'
@@ -35,7 +35,7 @@ class IpfsCoordAdapter {
// this.bchjs = new BCHJS()
this.wallet = new SlpWallet()
this.config = config
this.publicIp = publicIp
this.publicIp = publicIpv4
// Properties of this class instance.
this.isReady = false
@@ -52,7 +52,7 @@ class IpfsCoordAdapter {
// If configured as a Circuit Relay, get the public IP addresses for this node.
if (this.config.isCircuitRelay) {
try {
const ip4 = await this.publicIp.v4()
const ip4 = await this.publicIp()
// const ip6 = await publicIp.v6()
circuitRelayInfo.ip4 = ip4
+3 -3
View File
@@ -24,7 +24,7 @@ import { circuitRelayServer, circuitRelayTransport } from 'libp2p/circuit-relay'
// import { circuitRelayServer, circuitRelayTransport } from '@libp2p/circuit-relay-v2'
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
import { webSockets } from '@libp2p/websockets'
import publicIp from 'public-ip'
import { publicIpv4 } from 'public-ip'
import { multiaddr } from '@multiformats/multiaddr'
import { webRTC } from '@libp2p/webrtc'
@@ -46,7 +46,7 @@ class IpfsAdapter {
this.config = config
this.fs = fs
this.createLibp2p = createLibp2p
this.publicIp = publicIp
this.publicIp = publicIpv4
this.multiaddr = multiaddr
// Properties of this class instance.
@@ -73,7 +73,7 @@ class IpfsAdapter {
console.log('IPFS ID: ', this.id)
// Attempt to guess our ip4 IP address.
const ip4 = await this.publicIp.v4()
const ip4 = await this.publicIp()
let detectedMultiaddr = `/ip4/${ip4}/tcp/${this.config.ipfsTcpPort}/p2p/${this.id}`
detectedMultiaddr = this.multiaddr(detectedMultiaddr)
@@ -30,6 +30,7 @@ class IpfsRESTControllerLib {
this.getRelays = this.getRelays.bind(this)
this.handleError = this.handleError.bind(this)
this.connect = this.connect.bind(this)
this.getThisNode = this.getThisNode.bind(this)
}
/**
@@ -99,6 +100,29 @@ class IpfsRESTControllerLib {
}
}
/**
* @api {get} /ipfs/node Get a copy of the thisNode object from helia-coord
* @apiPermission public
* @apiName GetThisNode
* @apiGroup REST BCH
*
* @apiExample Example usage:
* curl -H "Content-Type: application/json" -X GET localhost:5001/ipfs/node
*
*/
async getThisNode (ctx) {
try {
// const status = await this.adapters.ipfs.getStatus()
const thisNode = this.adapters.ipfs.ipfsCoordAdapter.ipfsCoord.thisNode
ctx.body = { thisNode }
} catch (err) {
wlogger.error('Error in ipfs/controller.js/getThisNode(): ')
// ctx.throw(422, err.message)
this.handleError(ctx, err)
}
}
// DRY error handler
handleError (ctx, err) {
// If an HTTP status is specified by the buisiness logic, use that.
+1
View File
@@ -55,6 +55,7 @@ class IpfsRouter {
this.router.post('/peers', this.ipfsRESTController.getPeers)
this.router.post('/relays', this.ipfsRESTController.getRelays)
this.router.post('/connect', this.ipfsRESTController.connect)
this.router.get('/node', this.ipfsRESTController.getThisNode)
// Attach the Controller routes to the Koa app.
app.use(this.router.routes())
@@ -52,7 +52,7 @@ describe('#IPFS', () => {
it('should get the public IP address if this node is a Circuit Relay', async () => {
// Mock dependencies.
uut.IpfsCoord = IPFSCoordMock
sandbox.stub(uut.publicIp, 'v4').resolves('123')
sandbox.stub(uut, 'publicIp').resolves('123')
// Force Circuit Relay
uut.config.isCircuitRelay = true
@@ -66,7 +66,7 @@ describe('#IPFS', () => {
it('should exit quietly if this node is a Circuit Relay and there is an issue getting the IP address', async () => {
// Mock dependencies.
uut.IpfsCoord = IPFSCoordMock
sandbox.stub(uut.publicIp, 'v4').rejects(new Error('test error'))
sandbox.stub(uut, 'publicIp').rejects(new Error('test error'))
// Force Circuit Relay
uut.config.isCircuitRelay = true
+1 -1
View File
@@ -53,7 +53,7 @@ describe('#IPFS-adapter', () => {
it('should return a promise that resolves into an instance of IPFS.', async () => {
// Mock dependencies.
sandbox.stub(uut, 'createNode').resolves(ipfs)
sandbox.stub(uut.publicIp, 'v4').resolves('192.168.2.4')
sandbox.stub(uut, 'publicIp').resolves('192.168.2.4')
sandbox.stub(uut, 'multiaddr').returns('/ip4/fake-multiaddr')
const result = await uut.start()