-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
90 lines (74 loc) · 2.13 KB
/
data.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
package main
import (
"context"
"errors"
"time"
"gx/ipfs/QmQa2wf1sLFKkjHCVEbna8y5qhdMjL8vtTJSAc48vZGTer/go-ipfs/core/coreunix"
"github.com/fatih/structs"
)
const IPFSTimeoutDuration = 30 * time.Second
// IPFSObj is an abstraction to deal with objects / blobs from IPFS;
// also does signing and verification
type IPFSObj struct {
// Hash is the hash address given by IPFS
Hash string
// Key is the PublicKey of the signing user
Key string
// Data is an opaque field to dump the payload in
Data map[string]interface{}
// Signature used to verify this object was indeed sent by
// user with Key
Signature string
}
// NewIPFSObj is a generalized helper function to created signed
// IPFSObj and add it to the IPFS network
func NewIPFSObj(node *Node, user *User, data interface{}) (*IPFSObj, error) {
obj := &IPFSObj{Key: user.PubKeyRaw}
obj.Data = structs.New(data).Map()
var err error
obj.Signature, err = Sign(user, obj.Data)
if err != nil {
return nil, err
}
// Add to IPFS Node Repository
hash, err := coreunix.Add(node.IpfsNode, ToJSONReader(obj))
if err != nil {
return nil, err
}
obj.Hash = hash
return obj, nil
}
// RiggedError is thrown if the signature of a post does not match
// with public key identity, means someone tries to impersonate
// someone else
var RiggedError = errors.New("This object got rigged")
// GetIPFSObj retrieves data from IPFS and verifies the signature. If
// the signature does not match, it means that someone pretends to
// send content under someone else's identity, which will throw a
// RiggedError
//
// @TODO this smells like refactoring since it uses global variable
// MyNode
func GetIPFSObj(hash string) (*IPFSObj, error) {
ctx, cancel := context.WithTimeout(context.Background(), IPFSTimeoutDuration)
defer cancel()
r, err := coreunix.Cat(ctx, MyNode.IpfsNode, hash)
if err != nil {
return nil, err
}
obj := &IPFSObj{}
err = FromJSONReader(r, obj)
if err != nil {
return nil, err
}
ok, err := Verify(obj)
if err != nil {
return nil, err
}
if !ok {
return nil, RiggedError
}
// Set this as the hash is not stored inside IPFS blob
obj.Hash = hash
return obj, nil
}