diff --git a/ipns/name.go b/ipns/name.go index b35e8f9d85..c1ef506a38 100644 --- a/ipns/name.go +++ b/ipns/name.go @@ -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 []byte + src string } // NameFromString creates a [Name] from the given IPNS Name in its [string representation]. @@ -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: []byte(pid)} + return Name{src: string(pid)} } // NameFromCid creates a [Name] from the given [cid.Cid]. @@ -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: c.Hash()}, nil + return Name{src: string(c.Hash())}, nil } // RoutingKey returns the binary IPNS Routing Key for the given [Name]. Note that @@ -77,7 +77,7 @@ func NameFromCid(c cid.Cid) (Name, error) { func (n Name) RoutingKey() []byte { var buffer bytes.Buffer buffer.WriteString(NamespacePrefix) - buffer.Write(n.src) // Note: we append raw multihash bytes (no multibase) + buffer.WriteString(n.src) // Note: we append raw multihash bytes (no multibase) return buffer.Bytes() } @@ -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 bytes.Equal(n.src, other.src) + return n.src == other.src } // AsPath returns the IPNS Name as a [path.Path] prefixed by [path.IPNSNamespace]. diff --git a/namesys/ipns_publisher.go b/namesys/ipns_publisher.go index 8ac687de04..74ef35853c 100644 --- a/namesys/ipns_publisher.go +++ b/namesys/ipns_publisher.go @@ -56,8 +56,8 @@ func (p *IPNSPublisher) Publish(ctx context.Context, priv crypto.PrivKey, value // IpnsDsKey returns a datastore key given an IPNS identifier (peer // ID). Defines the storage key for IPNS records in the local datastore. -func IpnsDsKey(id peer.ID) ds.Key { - return ds.NewKey("/ipns/" + base32.RawStdEncoding.EncodeToString([]byte(id))) +func IpnsDsKey(name ipns.Name) ds.Key { + return ds.NewKey("/ipns/" + base32.RawStdEncoding.EncodeToString([]byte(name.Peer()))) } // ListPublished returns the latest IPNS records published by this node and @@ -65,7 +65,7 @@ func IpnsDsKey(id peer.ID) ds.Key { // // This method will not search the routing system for records published by other // nodes. -func (p *IPNSPublisher) ListPublished(ctx context.Context) (map[peer.ID]*ipns.Record, error) { +func (p *IPNSPublisher) ListPublished(ctx context.Context) (map[ipns.Name]*ipns.Record, error) { query, err := p.ds.Query(ctx, dsquery.Query{ Prefix: ipns.NamespacePrefix, }) @@ -74,7 +74,7 @@ func (p *IPNSPublisher) ListPublished(ctx context.Context) (map[peer.ID]*ipns.Re } defer query.Close() - records := make(map[peer.ID]*ipns.Record) + records := make(map[ipns.Name]*ipns.Record) for { select { case result, ok := <-query.Next(): @@ -100,7 +100,7 @@ func (p *IPNSPublisher) ListPublished(ctx context.Context) (map[peer.ID]*ipns.Re log.Errorf("ipns ds key invalid: %s", result.Key) continue } - records[peer.ID(pid)] = rec + records[ipns.NameFromPeer(peer.ID(pid))] = rec case <-ctx.Done(): return nil, ctx.Err() } @@ -112,24 +112,24 @@ func (p *IPNSPublisher) ListPublished(ctx context.Context) (map[peer.ID]*ipns.Re // // If `checkRouting` is true and we have no existing record, this method will // check the routing system for any existing records. -func (p *IPNSPublisher) GetPublished(ctx context.Context, id peer.ID, checkRouting bool) (*ipns.Record, error) { +func (p *IPNSPublisher) GetPublished(ctx context.Context, name ipns.Name, checkRouting bool) (*ipns.Record, error) { ctx, cancel := context.WithTimeout(ctx, time.Second*30) defer cancel() - value, err := p.ds.Get(ctx, IpnsDsKey(id)) + value, err := p.ds.Get(ctx, IpnsDsKey(name)) switch err { case nil: case ds.ErrNotFound: if !checkRouting { return nil, nil } - routingKey := ipns.NameFromPeer(id).RoutingKey() + routingKey := name.RoutingKey() value, err = p.routing.GetValue(ctx, string(routingKey)) if err != nil { // Not found or other network issue. Can't really do // anything about this case. if err != routing.ErrNotFound { - log.Debugf("error when determining the last published IPNS record for %s: %s", id, err) + log.Debugf("error when determining the last published IPNS record for %s: %s", name, err) } return nil, nil @@ -146,12 +146,13 @@ func (p *IPNSPublisher) updateRecord(ctx context.Context, k crypto.PrivKey, valu if err != nil { return nil, err } + name := ipns.NameFromPeer(id) p.mu.Lock() defer p.mu.Unlock() // get previous records sequence number - rec, err := p.GetPublished(ctx, id, true) + rec, err := p.GetPublished(ctx, name, true) if err != nil { return nil, err } @@ -188,7 +189,7 @@ func (p *IPNSPublisher) updateRecord(ctx context.Context, k crypto.PrivKey, valu } // Put the new record. - dsKey := IpnsDsKey(id) + dsKey := IpnsDsKey(name) if err := p.ds.Put(ctx, dsKey, data); err != nil { return nil, err } diff --git a/namesys/ipns_publisher_test.go b/namesys/ipns_publisher_test.go index e60c6bb6ee..a783ddd618 100644 --- a/namesys/ipns_publisher_test.go +++ b/namesys/ipns_publisher_test.go @@ -99,7 +99,7 @@ func TestAsyncDS(t *testing.T) { err = publisher.Publish(ctx, ipnsFakeID.PrivateKey(), ipnsVal) require.NoError(t, err) - ipnsKey := IpnsDsKey(ipnsFakeID.ID()) + ipnsKey := IpnsDsKey(ipns.NameFromPeer(ipnsFakeID.ID())) for k := range ds.syncKeys { if k.IsAncestorOf(ipnsKey) || k.Equal(ipnsKey) { diff --git a/namesys/republisher/repub.go b/namesys/republisher/repub.go index 2f53cade69..9023f16ad6 100644 --- a/namesys/republisher/repub.go +++ b/namesys/republisher/repub.go @@ -141,7 +141,7 @@ func (rp *Republisher) republishEntry(ctx context.Context, priv ic.PrivKey) erro log.Debugf("republishing ipns entry for %s", id) // Look for it locally only - rec, err := rp.getLastIPNSRecord(ctx, id) + rec, err := rp.getLastIPNSRecord(ctx, ipns.NameFromPeer(id)) if err != nil { if err == errNoEntry { span.SetAttributes(attribute.Bool("NoEntry", true)) @@ -173,9 +173,9 @@ func (rp *Republisher) republishEntry(ctx context.Context, priv ic.PrivKey) erro return err } -func (rp *Republisher) getLastIPNSRecord(ctx context.Context, id peer.ID) (*ipns.Record, error) { +func (rp *Republisher) getLastIPNSRecord(ctx context.Context, name ipns.Name) (*ipns.Record, error) { // Look for it locally only - val, err := rp.ds.Get(ctx, namesys.IpnsDsKey(id)) + val, err := rp.ds.Get(ctx, namesys.IpnsDsKey(name)) switch err { case nil: case ds.ErrNotFound: diff --git a/namesys/republisher/repub_test.go b/namesys/republisher/repub_test.go index 2759e21b75..74297a41cb 100644 --- a/namesys/republisher/repub_test.go +++ b/namesys/republisher/repub_test.go @@ -191,7 +191,7 @@ func TestLongEOLRepublish(t *testing.T) { err = verifyResolution(nsystems, name, p) require.NoError(t, err) - rec, err := getLastIPNSRecord(ctx, publisher.store, publisher.h.ID()) + rec, err := getLastIPNSRecord(ctx, publisher.store, ipns.NameFromPeer(publisher.h.ID())) require.NoError(t, err) finalEol, err := rec.Validity() @@ -199,9 +199,9 @@ func TestLongEOLRepublish(t *testing.T) { require.Equal(t, expiration.UTC(), finalEol.UTC()) } -func getLastIPNSRecord(ctx context.Context, dstore ds.Datastore, id peer.ID) (*ipns.Record, error) { +func getLastIPNSRecord(ctx context.Context, dstore ds.Datastore, name ipns.Name) (*ipns.Record, error) { // Look for it locally only - val, err := dstore.Get(ctx, namesys.IpnsDsKey(id)) + val, err := dstore.Get(ctx, namesys.IpnsDsKey(name)) if err != nil { return nil, err }