-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (83 loc) · 1.89 KB
/
main.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
package main
import (
"bytes"
"crypto/sha256"
"flag"
"fmt"
"log"
"math/rand"
"os"
"time"
cid "github.com/ipfs/go-cid"
shell "github.com/ipfs/go-ipfs-api"
"github.com/joho/godotenv"
)
func main() {
var hash string
flag.StringVar(&hash, "hash", "", "Hash to search")
flag.Parse()
if hash == "" {
// valid hash "QmTp2hEo8eXRp6wg7jXv1BLCMh5a4F3B7buAUZNZUu772j"
hash = generateCID()
}
// find hash from IPFS network
if !find(hash) {
fmt.Printf("Hash %s not found ", hash)
return
}
fmt.Printf("Congrats!... Hash %s found on ipfs network", hash)
}
func generateCID() string {
var builder cid.V0Builder
msg := randomString(35)
c, _ := cid.V0Builder.Sum(builder, []byte(msg))
s := c.Hash().B58String()
fmt.Println("Created CID: ", s)
return s
}
func find(hash string) bool {
out := fmt.Sprintf("%s.txt", hash)
url := loadConfig("IPFS_URL")
sh := shell.NewShell(url)
err := sh.Get(hash, out)
if err != nil {
fmt.Printf("unable to find hash %s", err)
return false
}
data, err := sh.Cat(hash)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
return false
}
buf := new(bytes.Buffer)
buf.ReadFrom(data)
newStr := buf.String()
fmt.Printf("data %s", newStr)
return true
}
func loadConfig(key string) string {
// load .env file
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
return os.Getenv(key)
}
func generateHash() string {
noRandomCharacters := 32
randString := randomString(noRandomCharacters)
hash := sha256.New()
hash.Write([]byte(randString))
bs := hash.Sum(nil)
fmt.Println(len(bs))
return fmt.Sprintf("%x", bs)
}
var characterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
func randomString(n int) string {
rand.Seed(time.Now().UnixNano())
b := make([]rune, n)
for i := range b {
b[i] = characterRunes[rand.Intn(len(characterRunes))]
}
return string(b)
}