Skip to content

Commit 33e3f0c

Browse files
authored
feat(ipns): helper ValidateWithPeerID and UnmarshalIpnsEntry (#294)
1 parent dc731ca commit 33e3f0c

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

gateway/handler_ipns_record.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import (
1010
"time"
1111

1212
"github.com/cespare/xxhash/v2"
13-
"github.com/gogo/protobuf/proto"
1413
ipath "github.com/ipfs/boxo/coreiface/path"
15-
ipns_pb "github.com/ipfs/boxo/ipns/pb"
14+
"github.com/ipfs/boxo/ipns"
1615
"github.com/ipfs/go-cid"
1716
"go.opentelemetry.io/otel/attribute"
1817
"go.opentelemetry.io/otel/trace"
@@ -50,8 +49,7 @@ func (i *handler) serveIpnsRecord(ctx context.Context, w http.ResponseWriter, r
5049
return false
5150
}
5251

53-
var record ipns_pb.IpnsEntry
54-
err = proto.Unmarshal(rawRecord, &record)
52+
record, err := ipns.UnmarshalIpnsEntry(rawRecord)
5553
if err != nil {
5654
webError(w, err, http.StatusInternalServerError)
5755
return false

ipns/ipns.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ func createCborDataForIpnsEntry(e *pb.IpnsEntry) ([]byte, error) {
130130
return buf.Bytes(), nil
131131
}
132132

133+
// ValidateWithPeerID validates the given IPNS entry against the given peer ID.
134+
func ValidateWithPeerID(pid peer.ID, entry *pb.IpnsEntry) error {
135+
pk, err := ExtractPublicKey(pid, entry)
136+
if err != nil {
137+
return err
138+
}
139+
140+
return Validate(pk, entry)
141+
}
142+
133143
// Validates validates the given IPNS entry against the given public key.
134144
func Validate(pk ic.PubKey, entry *pb.IpnsEntry) error {
135145
// Make sure max size is respected
@@ -287,6 +297,17 @@ func EmbedPublicKey(pk ic.PubKey, entry *pb.IpnsEntry) error {
287297
return nil
288298
}
289299

300+
// UnmarshalIpnsEntry unmarshalls an IPNS entry from a slice of bytes.
301+
func UnmarshalIpnsEntry(data []byte) (*pb.IpnsEntry, error) {
302+
var entry pb.IpnsEntry
303+
err := proto.Unmarshal(data, &entry)
304+
if err != nil {
305+
return nil, err
306+
}
307+
308+
return &entry, nil
309+
}
310+
290311
// ExtractPublicKey extracts a public key matching `pid` from the IPNS record,
291312
// if possible.
292313
//

ipns/validate_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
pstore "github.com/libp2p/go-libp2p/core/peerstore"
2020
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
2121
"github.com/multiformats/go-multicodec"
22+
"github.com/stretchr/testify/assert"
2223
)
2324

2425
func testValidatorCase(t *testing.T, priv crypto.PrivKey, kbook pstore.KeyBook, key string, val []byte, eol time.Time, exp error) {
@@ -143,8 +144,6 @@ func TestEmbeddedPubKeyValidate(t *testing.T) {
143144
}
144145

145146
func TestPeerIDPubKeyValidate(t *testing.T) {
146-
t.Skip("disabled until libp2p/go-libp2p-crypto#51 is fixed")
147-
148147
goodeol := time.Now().Add(time.Hour)
149148
kbook, err := pstoremem.NewPeerstore()
150149
if err != nil {
@@ -413,3 +412,38 @@ func genKeys(t *testing.T) (crypto.PrivKey, peer.ID, string) {
413412

414413
return priv, pid, ipnsKey
415414
}
415+
416+
func TestValidateWithPeerID(t *testing.T) {
417+
path := []byte("/ipfs/bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4")
418+
eol := time.Now().Add(time.Hour)
419+
420+
rnd := rand.New(rand.NewSource(42))
421+
422+
sk, pk, err := crypto.GenerateEd25519Key(rnd)
423+
assert.NoError(t, err)
424+
425+
pid, err := peer.IDFromPublicKey(pk)
426+
assert.NoError(t, err)
427+
428+
entry, err := Create(sk, path, 1, eol, 0)
429+
assert.NoError(t, err)
430+
431+
t.Run("valid peer ID", func(t *testing.T) {
432+
t.Parallel()
433+
err = ValidateWithPeerID(pid, entry)
434+
assert.NoError(t, err)
435+
})
436+
437+
t.Run("invalid peer ID", func(t *testing.T) {
438+
t.Parallel()
439+
440+
_, pk2, err := crypto.GenerateEd25519Key(rnd)
441+
assert.NoError(t, err)
442+
443+
pid2, err := peer.IDFromPublicKey(pk2)
444+
assert.NoError(t, err)
445+
446+
err = ValidateWithPeerID(pid2, entry)
447+
assert.ErrorIs(t, err, ErrSignature)
448+
})
449+
}

0 commit comments

Comments
 (0)