-
Notifications
You must be signed in to change notification settings - Fork 0
/
invariable.go
89 lines (74 loc) · 1.74 KB
/
invariable.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
package kt
import (
"encoding/json"
"github.com/hootuu/tome/bk/bid"
"github.com/hootuu/tome/ki"
"github.com/hootuu/tome/vn"
"github.com/hootuu/utils/errors"
)
type Invariable interface {
GetType() Type
GetVersion() Version
GetVN() vn.ID
GetSignature() *Signature
SetSignature(signature *Signature)
Signing() *Signing
}
func InvariableSign(inv Invariable, pri ki.PRI) *errors.Error {
sign := NewSignature()
err := sign.sign(inv, pri)
if err != nil {
return err
}
return nil
}
func InvariableVerify(inv Invariable) *errors.Error {
return inv.GetSignature().verify(inv)
}
func InvariableMarshal(inv Invariable) ([]byte, *errors.Error) {
bytes, nErr := json.Marshal(inv)
if nErr != nil {
return nil, errors.Verify("invalid invariable: " + nErr.Error())
}
return bytes, nil
}
type Lead struct {
Type Type `json:"t"`
Version Version `json:"v"`
Invariable bid.BID `json:"f"`
}
func (l *Lead) S() string {
return l.Type.S() + ":" + l.Version.S() + "/" + l.Invariable.S()
}
type Template struct {
Type Type `json:"t"`
Version Version `json:"v"`
Vn vn.ID `json:"vn"`
Signature *Signature `json:"s"`
}
func InvariableOf(data []byte) (*Template, *errors.Error) {
var tpl Template
nErr := json.Unmarshal(data, &tpl)
if nErr != nil {
return nil, errors.Verify("invalid invariable: " + nErr.Error())
}
return &tpl, nil
}
func (t *Template) GetType() Type {
return t.Type
}
func (t *Template) GetVersion() Version {
return t.Version
}
func (t *Template) GetVN() vn.ID {
return t.Vn
}
func (t *Template) GetSignature() *Signature {
return t.Signature
}
func (t *Template) SetSignature(signature *Signature) {
t.Signature = signature
}
func (t *Template) Signing() *Signing {
return NewSigning()
}