go-dvote/data/data.go
data/data.goRaw
1package data
2
3import (
4 "os"
5 "fmt"
6 "bytes"
7 "io/ioutil"
8 shell "github.com/ipfs/go-ipfs-api"
9)
10
11type votePacket struct {
12 PID uint
13 Nullifier string
14 Vote string
15 Franchise string
16}
17
18func publish(object []byte) (string) {
19 sh := shell.NewShell("localhost:5001")
20 cid, err := sh.Add(bytes.NewBuffer(object))
21 if err != nil {
22 fmt.Fprintf(os.Stderr, "error: %s", err)
23 os.Exit(1)
24 }
25 return cid
26}
27
28func pin(path string) {
29 sh := shell.NewShell("localhost:5001")
30 err := sh.Pin(path)
31 if err != nil{
32 fmt.Fprintf(os.Stderr, "error: %s", err)
33 os.Exit(1)
34 }
35}
36
37
38func retrieve(hash string) ([]byte){
39 sh := shell.NewShell("localhost:5001")
40 reader, err := sh.Cat(hash)
41 if err != nil {
42 fmt.Fprintf(os.Stderr, "error: %s", err)
43 os.Exit(1)
44 }
45 content, err := ioutil.ReadAll(reader)
46 if err != nil {
47 fmt.Fprintf(os.Stderr, "error: %s", err)
48 os.Exit(1)
49 }
50 return content
51}