@@ -3,6 +3,7 @@ package cmd
33import (
44 "bytes"
55 "context"
6+ "encoding/base64"
67 "encoding/json"
78 "fmt"
89 "sort"
@@ -17,6 +18,7 @@ import (
1718 "github.com/libp2p/go-libp2p-core/peer"
1819 "github.com/libp2p/go-libp2p-core/protocol"
1920 "github.com/libp2p/go-libp2p-core/routing"
21+ ma "github.com/multiformats/go-multiaddr"
2022
2123 "github.com/filecoin-project/venus/app/node"
2224 "github.com/filecoin-project/venus/pkg/net"
@@ -451,15 +453,23 @@ var idCmd = &cmds.Command{
451453 return err
452454 }
453455
454- peerID := addrs .ID .Pretty ()
455- buf := & bytes.Buffer {}
456- writer := NewSilentWriter (buf )
457- for _ , addr := range addrs .Addrs {
458- writer .Printf ("%s/p2p/%s\n " , addr , peerID )
456+ hostID := addrs .ID
457+ details := IDDetails {
458+ Addresses : make ([]ma.Multiaddr , len (addrs .Addrs )),
459+ ID : hostID ,
459460 }
460461
461- return re .Emit (buf )
462+ for i , addr := range addrs .Addrs {
463+ subAddr , err := ma .NewMultiaddr (fmt .Sprintf ("/ipfs/%s" , hostID .Pretty ()))
464+ if err != nil {
465+ return err
466+ }
467+ details .Addresses [i ] = addr .Encapsulate (subAddr )
468+ }
469+
470+ return re .Emit (& details )
462471 },
472+ Type : & IDDetails {},
463473}
464474
465475var disconnectCmd = & cmds.Command {
@@ -629,3 +639,87 @@ var protectListCmd = &cmds.Command{
629639 return re .Emit (buf )
630640 },
631641}
642+
643+ // IDDetails is a collection of information about a node.
644+ type IDDetails struct {
645+ Addresses []ma.Multiaddr
646+ ID peer.ID
647+ AgentVersion string
648+ ProtocolVersion string
649+ PublicKey []byte // raw bytes
650+ }
651+
652+ // MarshalJSON implements json.Marshaler
653+ func (idd IDDetails ) MarshalJSON () ([]byte , error ) {
654+ addressStrings := make ([]string , len (idd .Addresses ))
655+ for i , addr := range idd .Addresses {
656+ addressStrings [i ] = addr .String ()
657+ }
658+
659+ v := map [string ]interface {}{
660+ "Addresses" : addressStrings ,
661+ }
662+
663+ if idd .ID != "" {
664+ v ["ID" ] = idd .ID .Pretty ()
665+ }
666+ if idd .AgentVersion != "" {
667+ v ["AgentVersion" ] = idd .AgentVersion
668+ }
669+ if idd .ProtocolVersion != "" {
670+ v ["ProtocolVersion" ] = idd .ProtocolVersion
671+ }
672+ if idd .PublicKey != nil {
673+ // Base64-encode the public key explicitly.
674+ // This is what the built-in JSON encoder does to []byte too.
675+ v ["PublicKey" ] = base64 .StdEncoding .EncodeToString (idd .PublicKey )
676+ }
677+ return json .Marshal (v )
678+ }
679+
680+ // UnmarshalJSON implements Unmarshaler
681+ func (idd * IDDetails ) UnmarshalJSON (data []byte ) error {
682+ var v map [string ]* json.RawMessage
683+ var err error
684+ if err = json .Unmarshal (data , & v ); err != nil {
685+ return err
686+ }
687+
688+ var addresses []string
689+ if err := decode (v , "Addresses" , & addresses ); err != nil {
690+ return err
691+ }
692+ idd .Addresses = make ([]ma.Multiaddr , len (addresses ))
693+ for i , addr := range addresses {
694+ a , err := ma .NewMultiaddr (addr )
695+ if err != nil {
696+ return err
697+ }
698+ idd .Addresses [i ] = a
699+ }
700+
701+ var id string
702+ if err := decode (v , "ID" , & id ); err != nil {
703+ return err
704+ }
705+ if idd .ID , err = peer .Decode (id ); err != nil {
706+ return err
707+ }
708+
709+ if err := decode (v , "AgentVersion" , & idd .AgentVersion ); err != nil {
710+ return err
711+ }
712+ if err := decode (v , "ProtocolVersion" , & idd .ProtocolVersion ); err != nil {
713+ return err
714+ }
715+ return decode (v , "PublicKey" , & idd .PublicKey )
716+ }
717+
718+ func decode (idd map [string ]* json.RawMessage , key string , dest interface {}) error {
719+ if raw := idd [key ]; raw != nil {
720+ if err := json .Unmarshal (* raw , & dest ); err != nil {
721+ return err
722+ }
723+ }
724+ return nil
725+ }
0 commit comments