-
Notifications
You must be signed in to change notification settings - Fork 0
/
random-cid.go
190 lines (162 loc) · 4.04 KB
/
random-cid.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
"github.com/application-research/random-cid/ipfslite"
"github.com/brianvoe/gofakeit/v6"
"github.com/gin-gonic/gin"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multicodec"
"github.com/multiformats/go-multihash"
"github.com/urfave/cli/v2"
)
type newCid struct {
Reader io.Reader
Version int
}
var newFiles = make(chan newCid)
var cidout = make(chan string)
var peerID peer.ID
func newIpfsDaemon() *ipfslite.Peer {
ctx := context.Background()
ds := ipfslite.NewInMemoryDatastore()
priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
fmt.Printf("error: %s\n", 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 {
fmt.Printf("error: %s\n", err)
}
lite, err := ipfslite.New(ctx, ds, h, dht, nil)
if err != nil {
fmt.Printf("error: %s\n", err)
}
fmt.Println("IPFS peer address: ", h.ID())
peerID = h.ID()
return lite
}
func ipfsDaemon() {
fmt.Println("Starting IPFS daemon")
ctx := context.Background()
lite := newIpfsDaemon()
lite.Bootstrap(ipfslite.DefaultBootstrapPeers())
fmt.Println("IPFS daemon started!")
for {
select {
case newCid := <-newFiles:
pref := cid.Prefix{
Version: uint64(newCid.Version),
Codec: uint64(multicodec.Raw),
MhType: multihash.SHA2_256,
MhLength: -1, // default length
}
fmt.Println("Adding new cid to blockstore")
node, err := lite.AddFile(ctx, newCid.Reader, &ipfslite.AddParams{Prefix: &pref}) // add file
if err != nil {
fmt.Println("Could not add node to blockstore: ", err)
}
fmt.Println("Added CID to blockstore: ", node.Cid())
cidout <- node.Cid().String()
}
}
}
func NewCid(version int) (string, error) {
// And then feed it some data
data := []byte(gofakeit.HackerPhrase()) // random hacker phrase
fmt.Println("Adding cid to local blockstore...")
newFiles <- newCid{Reader: bytes.NewReader(data), Version: version}
addedCid := <-cidout
fmt.Println("Added cid: ", addedCid)
return addedCid, nil
}
func getPeerID(ctx *gin.Context) {
ctx.String(http.StatusOK, peerID.String()+"\n")
}
func getCidV1(ctx *gin.Context) {
version := 1
c, err := NewCid(version)
if err != nil {
ctx.String(http.StatusInternalServerError, fmt.Sprintf("%v", err))
return
}
ctx.String(http.StatusOK, c+"\n")
}
func getCidV0(ctx *gin.Context) {
version := 0
c, err := NewCid(version)
if err != nil {
ctx.String(http.StatusInternalServerError, fmt.Sprintf("%v", err))
return
}
ctx.String(http.StatusOK, c+"\n")
}
func main() {
app := &cli.App{
Name: "random-cid",
Usage: "generate random CIDs",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "cversion",
Aliases: []string{"c"},
Value: 1,
Usage: "specify CID version to generate",
DefaultText: "1",
},
},
Commands: []*cli.Command{
{
Name: "api",
Usage: "start the random CID HTTP API",
Action: func(c *cli.Context) error {
router := gin.Default()
router.GET("/", getCidV1)
router.GET("/v1", getCidV1)
router.GET("/v0", getCidV0)
router.GET("/peer", getPeerID)
port := os.Getenv("PORT")
if port == "" {
port = "8081"
}
if err := router.Run(":" + port); err != nil {
fmt.Printf("error: %s\n", err)
}
return nil
},
},
},
Action: func(ctx *cli.Context) error {
// Create a cid manually by specifying the 'prefix' parameters
cidVersion := ctx.Int("cversion")
if cidVersion != 0 && cidVersion != 1 {
return fmt.Errorf("Invalid CID version. Got %d, should be 1 or 0", cidVersion)
}
c, err := NewCid(cidVersion)
if err != nil {
return err
}
fmt.Println(c)
return nil
},
}
go ipfsDaemon()
err := app.Run(os.Args)
if err != nil {
fmt.Println("error: ", err)
}
}