diff --git a/codec/dagcbor/marshal.go b/codec/dagcbor/marshal.go index ad95d9cc..40ba29a1 100644 --- a/codec/dagcbor/marshal.go +++ b/codec/dagcbor/marshal.go @@ -134,6 +134,7 @@ func marshal(n ipld.Node, tk *tok.Token, sink shared.TokenSink) error { tk.Tagged = true tk.Tag = linkTag _, err = sink.Step(tk) + tk.Tagged = false return err default: return fmt.Errorf("schemafree link emission only supported by this codec for CID type links!") diff --git a/codec/dagcbor/roundtrip_test.go b/codec/dagcbor/roundtrip_test.go index 3d56d525..9fb7030a 100644 --- a/codec/dagcbor/roundtrip_test.go +++ b/codec/dagcbor/roundtrip_test.go @@ -2,11 +2,17 @@ package dagcbor import ( "bytes" + "context" + "crypto/rand" + "io" "testing" + cid "github.com/ipfs/go-cid" . "github.com/warpfork/go-wish" + ipld "github.com/ipld/go-ipld-prime" "github.com/ipld/go-ipld-prime/fluent" + cidlink "github.com/ipld/go-ipld-prime/linking/cid" basicnode "github.com/ipld/go-ipld-prime/node/basic" ) @@ -62,3 +68,37 @@ func TestRoundtripScalar(t *testing.T) { Wish(t, nb.Build(), ShouldEqual, simple) }) } + +func TestRoundtripLinksAndBytes(t *testing.T) { + lb := cidlink.LinkBuilder{cid.Prefix{ + Version: 1, + Codec: 0x71, + MhType: 0x17, + MhLength: 4, + }} + buf := bytes.Buffer{} + lnk, err := lb.Build(context.Background(), ipld.LinkContext{}, n, + func(ipld.LinkContext) (io.Writer, ipld.StoreCommitter, error) { + return &buf, func(lnk ipld.Link) error { return nil }, nil + }, + ) + Require(t, err, ShouldEqual, nil) + + var linkByteNode = fluent.MustBuildMap(basicnode.Style__Map{}, 4, func(na fluent.MapAssembler) { + nva := na.AssembleEntry("Link") + nva.AssignLink(lnk) + nva = na.AssembleEntry("Bytes") + bytes := make([]byte, 100) + _, _ = rand.Read(bytes) + nva.AssignBytes(bytes) + }) + + buf.Reset() + err = Encoder(linkByteNode, &buf) + Require(t, err, ShouldEqual, nil) + nb := basicnode.Style__Map{}.NewBuilder() + err = Decoder(nb, &buf) + Require(t, err, ShouldEqual, nil) + reconstructed := nb.Build() + Wish(t, reconstructed, ShouldEqual, linkByteNode) +}