-
Notifications
You must be signed in to change notification settings - Fork 19
/
decoder.go
39 lines (31 loc) · 971 Bytes
/
decoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package ipld
import (
"context"
"github.com/ipfs/go-cid"
legacy "github.com/ipfs/go-ipld-legacy"
"github.com/ipfs/go-libipfs/blocks"
"github.com/ipfs/go-merkledag"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime/node/basicnode"
)
func init() {
legacy.RegisterCodec(cid.DagProtobuf, dagpb.Type.PBNode, merkledag.ProtoNodeConverter)
legacy.RegisterCodec(cid.Raw, basicnode.Prototype.Bytes, merkledag.RawNodeConverter)
}
// DecodeNode handle codec not match case
func DecodeNode(ctx context.Context, b blocks.Block) (legacy.UniversalNode, error) {
node, err := legacy.DecodeNode(ctx, b)
if err == nil {
return node, nil
}
codec := cid.Raw
if b.Cid().Type() == uint64(cid.Raw) {
codec = cid.DagProtobuf
}
c := cid.NewCidV1(uint64(codec), b.Cid().Hash())
block, err := blocks.NewBlockWithCid(b.RawData(), c)
if err != nil {
return nil, err
}
return legacy.DecodeNode(ctx, block)
}