Skip to content

Commit

Permalink
Add PeerID method to did
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 7, 2024
1 parent 5982fbd commit 9437e04
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions did/peer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package did

import (
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multibase"
log "github.com/sirupsen/logrus"
)

func (d *DID) PeerID() (peer.ID, error) {
_, decodedBytes, err := multibase.Decode(d.Identifier)
if err != nil {
log.Debugf("(Failed to decode DID %s: %v", d.Identifier, err)
return "", err
}

c, err := cid.Cast(decodedBytes)
if err != nil {
return "", err
}
return peer.FromCid(c)
}
2 changes: 1 addition & 1 deletion did/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func getFragment(did string) string {
}

// Get the identifier from the DID string
// The prefix is not required, ut'll just be stripped off.
// The prefix is not required, it'll just be stripped off.
func getIdentifier(did string) string {

didName := strings.TrimPrefix(did, ma.DID_PREFIX)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-ipld-cbor v0.1.0
github.com/ipfs/kubo v0.26.0
github.com/libp2p/go-libp2p v0.32.2
github.com/libp2p/go-libp2p-pubsub v0.10.0
github.com/matoous/go-nanoid/v2 v2.0.0
github.com/multiformats/go-multiaddr v0.12.2
Expand Down Expand Up @@ -60,7 +61,6 @@ require (
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-cidranger v1.1.0 // indirect
github.com/libp2p/go-libp2p v0.32.2 // indirect
github.com/libp2p/go-libp2p-asn-util v0.4.1 // indirect
github.com/libp2p/go-libp2p-kad-dht v0.24.4 // indirect
github.com/libp2p/go-libp2p-kbucket v0.6.3 // indirect
Expand Down

0 comments on commit 9437e04

Please sign in to comment.