Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 29355b4

Browse files
committed
[#1976] repo.SignedListing can Normalize listing data in-place
1 parent 25dd984 commit 29355b4

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

repo/signed_listing.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ func (l *SignedListing) Reset() { *l = SignedListing{} }
4343
func (l *SignedListing) String() string { return proto.CompactTextString(l) }
4444
func (*SignedListing) ProtoMessage() {}
4545

46+
// Normalize is a helper method which will mutate the listing protobuf
47+
// in-place but maintain the original signature for external verification
48+
// purposes.
49+
func (l *SignedListing) Normalize() error {
50+
nl, err := l.GetListing().Normalize()
51+
if err != nil {
52+
return err
53+
}
54+
55+
l.signedListingProto.Listing = nl.listingProto
56+
return nil
57+
}
58+
4659
// GetListing returns the underlying repo.Listing object
4760
func (l SignedListing) GetListing() *Listing {
4861
return &Listing{

repo/signed_listing_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,61 @@ func TestSignAndVerifyListing(t *testing.T) {
266266
t.Errorf("verify signed listing: %v", err)
267267
}
268268
}
269+
270+
func TestNormalize(t *testing.T) {
271+
testnode, err := test.NewNode()
272+
if err != nil {
273+
t.Fatalf("create test node: %v", err)
274+
}
275+
276+
priv, pub, err := crypto.GenerateEd25519Key(rand.Reader)
277+
if err != nil {
278+
t.Fatal(err)
279+
}
280+
281+
pid, err := peer.IDFromPublicKey(pub)
282+
if err != nil {
283+
t.Fatal(err)
284+
}
285+
286+
testnode.IpfsNode.Identity = pid
287+
testnode.IpfsNode.PrivateKey = priv
288+
289+
// v4 listing is guaranteed to mutate when normalized
290+
lb := factory.MustLoadListingFixture("v4-cryptocurrency")
291+
l, err := repo.UnmarshalJSONSignedListing(lb)
292+
if err != nil {
293+
t.Fatalf("unmarshal fixtured listing: %v", err)
294+
}
295+
296+
// sign replaces listing vendor ID with the one from the testnode
297+
sl, err := l.GetListing().Sign(testnode)
298+
if err != nil {
299+
t.Fatalf("sign listing: %v", err)
300+
}
301+
302+
origSig := sl.GetSignature()
303+
origListingJSON, err := sl.MarshalJSON()
304+
if err != nil {
305+
t.Fatalf("marshal listing: %v", err)
306+
}
307+
308+
if err := sl.Normalize(); err != nil {
309+
t.Fatalf("normalize listing: %v", err)
310+
}
311+
312+
if sig := sl.GetSignature(); !bytes.Equal(origSig, sig) {
313+
t.Errorf("expected normalized signature to not change, but did")
314+
}
315+
316+
if listingJSON, err := sl.MarshalJSON(); err != nil {
317+
t.Errorf("marshal normalized listing: %v", err)
318+
} else {
319+
// when normalizing a signed listing, the listing data mutates in place, but
320+
// the signature should remain matched to the original (unchanged)
321+
if bytes.Equal(origListingJSON, listingJSON) {
322+
t.Errorf("expected listing JSON to change from normalization, but did not")
323+
t.Logf("orig: %s\nactual: %s\n", string(origListingJSON), string(listingJSON))
324+
}
325+
}
326+
}

0 commit comments

Comments
 (0)