/
walker.go
93 lines (77 loc) · 1.9 KB
/
walker.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"context"
"fmt"
"github.com/awalterschulze/gographviz"
ipfslite "github.com/hsanjuan/ipfs-lite"
"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/multiformats/go-multiaddr"
)
var (
testCID = "QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.SetLogLevel("*", "warn")
ds := ipfslite.NewInMemoryDatastore()
priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
panic(err)
}
listen, _ := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/4005")
h, dht, err := ipfslite.SetupLibp2p(
ctx,
priv,
nil,
[]multiaddr.Multiaddr{listen},
ds,
ipfslite.Libp2pOptionsExtra...,
)
if err != nil {
panic(err)
}
lite, err := ipfslite.New(ctx, ds, nil, h, dht, nil)
if err != nil {
panic(err)
}
lite.Bootstrap(ipfslite.DefaultBootstrapPeers())
c, _ := cid.Decode(testCID)
node, err := lite.Get(ctx, c)
if err != nil {
panic(err)
}
graphAst, err := gographviz.ParseString(`digraph G {}`)
if err != nil {
panic(err)
}
graph := gographviz.NewGraph()
if err := gographviz.Analyse(graphAst, graph); err != nil {
panic(err)
}
navNode := format.NewNavigableIPLDNode(node, lite.DAGService)
rootNode := node.Cid().String()
// add the root node to the graph
if err := graph.AddNode("Example", rootNode, nil); err != nil {
panic(err)
}
for i := 0; i < int(navNode.ChildTotal()); i++ {
childNode, err := navNode.FetchChild(ctx, uint(i))
if err != nil {
panic(err)
}
n := format.ExtractIPLDNode(childNode)
childCID := n.Cid().String()
if err := graph.AddNode("Example", childCID, nil); err != nil {
panic(err)
}
if err := graph.AddEdge(rootNode, childCID, true, nil); err != nil {
panic(err)
}
}
graphOut := graph.String()
fmt.Println(graphOut)
}