-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecord.go
113 lines (96 loc) · 2.61 KB
/
record.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
package iprs_record
import (
"context"
"time"
ld "github.com/dirkmc/go-iprs/ipld"
rsp "github.com/dirkmc/go-iprs/path"
node "gx/ipfs/QmNwUEK7QbwSqyKBu3mMtToo8SUc6wQJ7gdZq4gGGJqfnf/go-ipld-format"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
)
const PublishPutValTimeout = time.Second * 10
var log = logging.Logger("iprs.record")
type RecordValidation interface {
ValidationType() ld.IprsValidationType
// Return the validation data for the record
Validation() (interface{}, error)
// Get any nodes of data required for validation
Nodes() ([]node.Node, error)
}
type RecordChecker interface {
// Validates that the record has not expired etc
ValidateRecord(ctx context.Context, iprsKey rsp.IprsPath, record *Record) error
// Selects the best (most valid) record
SelectRecord(recs []*Record) (int, error)
}
type RecordSigner interface {
// Get the base IPRS path, eg /iprs/<certificate cid>/id
BasePath(id string) (rsp.IprsPath, error)
VerificationType() ld.IprsVerificationType
// Return the verification data for the record
Verification() (interface{}, error)
// Get any nodes of data required for verification
// eg public key, certificate etc
Nodes() ([]node.Node, error)
// Publish any data required for verification to the network
// eg public key, certificate etc
//PublishVerification(ctx context.Context, iprsKey rsp.IprsPath, entry *pb.IprsEntry) error
SignRecord([]byte) ([]byte, error)
}
type RecordVerifier interface {
// Verifies cryptographic signatures etc
VerifyRecord(ctx context.Context, iprsKey rsp.IprsPath, record *Record) error
}
type Record struct {
ld.Node
nodes []node.Node
}
func NewRecord(vl RecordValidation, s RecordSigner, val []byte) (*Record, error) {
vfn, err := s.Verification()
if err != nil {
return nil, err
}
vdn, err := vl.Validation()
if err != nil {
return nil, err
}
validity := &ld.Validity{
VerificationType: s.VerificationType(),
Verification: vfn,
ValidationType: vl.ValidationType(),
Validation: vdn,
}
signable, err := dataForSig(val, validity)
if err != nil {
return nil, err
}
sig, err := s.SignRecord(signable)
if err != nil {
return nil, err
}
n, err := ld.NewIprsNode(val, validity, sig)
if err != nil {
return nil, err
}
sb, err := s.Nodes()
if err != nil {
return nil, err
}
vb, err := vl.Nodes()
if err != nil {
return nil, err
}
nodes := append(sb, vb...)
return &Record{
Node: *n,
nodes: nodes,
}, nil
}
func NewRecordFromNode(n *ld.Node) *Record {
return &Record{
Node: *n,
nodes: []node.Node{},
}
}
func (r *Record) DependencyNodes() []node.Node {
return r.nodes
}