Skip to content

Commit 265d222

Browse files
committed
feat: add GetID method to retrieve peer identifier string
1 parent 448f1d5 commit 265d222

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ func (c *Client) GetPeers() []PeerInfo
217217

218218
Returns information about all known peers on subscribed topics.
219219

220+
#### GetID
221+
222+
```go
223+
func (c *Client) GetID() string
224+
```
225+
226+
Returns this peer's ID as a string.
227+
220228
#### GetPrivateKeyHex
221229

222230
```go

client.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ func (c *Client) GetPeers() []PeerInfo {
294294
return peers
295295
}
296296

297+
// GetID returns this peer's ID as a string.
298+
func (c *Client) GetID() string {
299+
return c.host.ID().String()
300+
}
301+
297302
// Close shuts down the client and releases all resources.
298303
func (c *Client) Close() error {
299304
c.cancel()
@@ -590,11 +595,3 @@ func PrivateKeyFromHex(keyHex string) (crypto.PrivKey, error) {
590595

591596
return priv, nil
592597
}
593-
594-
func keyToHex(priv crypto.PrivKey) string {
595-
keyBytes, err := crypto.MarshalPrivateKey(priv)
596-
if err != nil {
597-
return ""
598-
}
599-
return hex.EncodeToString(keyBytes)
600-
}

example/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func main() {
9898
if err := client.Publish(topicName, []byte(data)); err != nil {
9999
return
100100
}
101-
fmt.Printf("[%-20s] %s: %s\n", "local", *name, data)
101+
fmt.Printf("[%-52s] %s: %s\n", "local", *name, data)
102102
}
103103
}
104104
}()

0 commit comments

Comments
 (0)