Skip to content

Commit

Permalink
refactor(ipns) Name.src → Name.multihash
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Oct 17, 2023
1 parent 3501551 commit 8c1d32a
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions ipns/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
// [Multihash]: https://multiformats.io/multihash/
// [IPNS Name]: https://specs.ipfs.tech/ipns/ipns-record/#ipns-name
type Name struct {
src string
multihash string // binary Multihash without multibase envelope
}

// NameFromString creates a [Name] from the given IPNS Name in its [string representation].
Expand Down Expand Up @@ -57,7 +57,7 @@ func NameFromRoutingKey(data []byte) (Name, error) {

// NameFromPeer creates a [Name] from the given [peer.ID].
func NameFromPeer(pid peer.ID) Name {
return Name{src: string(pid)}
return Name{multihash: string(pid)}
}

// NameFromCid creates a [Name] from the given [cid.Cid].
Expand All @@ -66,7 +66,7 @@ func NameFromCid(c cid.Cid) (Name, error) {
if code != mc.Libp2pKey {
return Name{}, fmt.Errorf("CID codec %q is not allowed for IPNS Names, use %q instead", code, mc.Libp2pKey)
}
return Name{src: string(c.Hash())}, nil
return Name{multihash: string(c.Hash())}, nil
}

// RoutingKey returns the binary IPNS Routing Key for the given [Name]. Note that
Expand All @@ -77,14 +77,14 @@ func NameFromCid(c cid.Cid) (Name, error) {
func (n Name) RoutingKey() []byte {
var buffer bytes.Buffer
buffer.WriteString(NamespacePrefix)
buffer.WriteString(n.src) // Note: we append raw multihash bytes (no multibase)
buffer.WriteString(n.multihash) // Note: we append raw multihash bytes (no multibase)
return buffer.Bytes()
}

// Cid returns [Name] encoded as a [cid.Cid] of the public key. If the IPNS Name
// is invalid (e.g., empty), this will return the empty Cid.
func (n Name) Cid() cid.Cid {
m, err := mh.Cast([]byte(n.src))
m, err := mh.Cast([]byte(n.multihash))
if err != nil {
return cid.Undef
}
Expand All @@ -93,7 +93,7 @@ func (n Name) Cid() cid.Cid {

// Peer returns [Name] as a [peer.ID].
func (n Name) Peer() peer.ID {
return peer.ID(n.src)
return peer.ID(n.multihash)
}

// String returns the human-readable IPNS Name, encoded as a CIDv1 with libp2p-key
Expand Down Expand Up @@ -132,7 +132,7 @@ func (n Name) MarshalJSON() ([]byte, error) {

// Equal returns whether the records are equal.
func (n Name) Equal(other Name) bool {
return n.src == other.src
return n.multihash == other.multihash
}

// AsPath returns the IPNS Name as a [path.Path] prefixed by [path.IPNSNamespace].
Expand Down

0 comments on commit 8c1d32a

Please sign in to comment.