| 1 | import { base58btc } from 'multiformats/bases/base58' |
| 2 | import { useState } from 'react' |
| 3 | import { Button } from './button.tsx' |
| 4 | import { ConsolePanel } from './console.tsx' |
| 5 | import { ErrorPanel } from './error.tsx' |
| 6 | import { Panel } from './panel.tsx' |
| 7 | import { TextInput } from './text-input.tsx' |
| 8 | import type { MetricsRPC } from '@ipshipyard/libp2p-inspector-metrics' |
| 9 | import type { JSX } from 'react' |
| 10 | |
| 11 | interface IdentifyPanelProps { |
| 12 | component: string |
| 13 | metrics: MetricsRPC |
| 14 | } |
| 15 | |
| 16 | export function Identify ({ component, metrics }: IdentifyPanelProps): JSX.Element { |
| 17 | const [peerIdOrMultiaddr, setPeerIdOrMultiaddr] = useState('') |
| 18 | const [result, setResult] = useState<JSX.Element | string>('') |
| 19 | |
| 20 | function identify (evt: { preventDefault(): void }): boolean { |
| 21 | evt.preventDefault() |
| 22 | |
| 23 | metrics.identify(component, peerIdOrMultiaddr, { |
| 24 | signal: AbortSignal.timeout(10_000) |
| 25 | }) |
| 26 | .then((result) => { |
| 27 | const data: any = { |
| 28 | peerId: result.peerId.toString(), |
| 29 | agentVersion: result.agentVersion, |
| 30 | protocolVersion: result.protocolVersion, |
| 31 | observedAddr: result.observedAddr, |
| 32 | publicKey: result.publicKey && base58btc.encode(result.publicKey), |
| 33 | listenAddrs: result.listenAddrs.map(ma => ma.toString()), |
| 34 | protocols: result.protocols |
| 35 | } |
| 36 | |
| 37 | if (result.signedPeerRecord != null) { |
| 38 | data.signedPeerRecord = { |
| 39 | seq: `${result.signedPeerRecord.seq}n`, |
| 40 | addresses: result.signedPeerRecord.addresses.map(ma => ma.toString()) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | setResult(<ConsolePanel>{JSON.stringify(data, null, 2)}</ConsolePanel>) |
| 45 | }, (err) => { |
| 46 | setResult(<ErrorPanel error={err} />) |
| 47 | }) |
| 48 | |
| 49 | return false |
| 50 | } |
| 51 | |
| 52 | return ( |
| 53 | <Panel> |
| 54 | <p>Identify a peer</p> |
| 55 | <form onSubmit={(evt) => identify(evt)}> |
| 56 | <TextInput type='text' value={peerIdOrMultiaddr} placeholder='Peer ID or Multiaddr' onChange={(e) => { setPeerIdOrMultiaddr(e.target.value) }} /> |
| 57 | <Button onClick={(evt) => identify(evt)} primary>Identify</Button> |
| 58 | </form> |
| 59 | {result} |
| 60 | </Panel> |
| 61 | ) |
| 62 | } |