-
Notifications
You must be signed in to change notification settings - Fork 4
/
multicodec.go
55 lines (43 loc) · 1.77 KB
/
multicodec.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package tx_trie
import (
"io"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/multicodec"
"github.com/ipld/go-ipld-prime/traversal"
"github.com/multiformats/go-multihash"
dageth "github.com/vulcanize/go-codec-dageth"
)
var (
_ ipld.Decoder = Decode
_ ipld.Encoder = Encode
MultiCodecType = uint64(cid.EthTxTrie) // 0x92
MultiHashType = uint64(multihash.KECCAK_256)
)
func init() {
multicodec.RegisterDecoder(MultiCodecType, Decode)
multicodec.RegisterEncoder(MultiCodecType, Encode)
}
// AddSupportToChooser takes an existing node prototype chooser and subs in
// TrieNode for the eth tx trie multicodec code.
func AddSupportToChooser(existing traversal.LinkTargetNodePrototypeChooser) traversal.LinkTargetNodePrototypeChooser {
return func(lnk ipld.Link, lnkCtx ipld.LinkContext) (ipld.NodePrototype, error) {
if lnk, ok := lnk.(cidlink.Link); ok && lnk.Cid.Prefix().Codec == MultiCodecType {
return dageth.Type.TrieNode, nil
}
return existing(lnk, lnkCtx)
}
}
// We switched to simpler API names after v1.0.0, so keep the old names around
// as deprecated forwarding funcs until a future v2+.
// TODO: consider deprecating Marshal/Unmarshal too, since it's a bit
// unnecessary to have two supported names for each API.
// Deprecated: use Decode instead.
func Decoder(na ipld.NodeAssembler, r io.Reader) error { return Decode(na, r) }
// Deprecated: use Decode instead.
func Unmarshal(na ipld.NodeAssembler, r io.Reader) error { return Decode(na, r) }
// Deprecated: use Encode instead.
func Encoder(inNode ipld.Node, w io.Writer) error { return Encode(inNode, w) }
// Deprecated: use Encode instead.
func Marshal(inNode ipld.Node, w io.Writer) error { return Encode(inNode, w) }