-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
102 lines (89 loc) · 2.38 KB
/
input.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
//Date: 2017 Q4
//Email: ali.mashatan@gmail.com
//Author: Ali Mashatan
package GoBigChainDBDriver
import (
gcc "github.com/Mashatan/go-cryptoconditions"
"golang.org/x/crypto/ed25519"
)
type input struct {
inputItems []*inputItem
}
func (in *input) Generate(removeNull bool) []JsonObj {
arr := []JsonObj{}
for _, item := range in.inputItems {
if !removeNull || item != nil {
arr = append(arr, item.Generate(removeNull))
}
}
return arr
}
func (in *input) Sign(message []byte) error {
for _, item := range in.inputItems {
item.Sign(message)
}
return nil
}
func (in *input) Add(publicKey *[]PublicKey, privateKey *[]PrivateKey, fulfill *JsonObj) {
it := NewInputItem()
it.ownerBefores = publicKey
it.ownerPrivates = privateKey
it.ownerSignatures = nil
it.fulfills = fulfill
in.inputItems = append(in.inputItems, &it)
}
type inputItem struct {
ccEd gcc.Ed25519Sha256
ownerBefores *[]PublicKey
ownerPrivates *[]PrivateKey
ownerSignatures *[]Signature
fulfills *JsonObj `json:"fulfills,omitempty"`
}
func NewInputItem() inputItem {
in := inputItem{}
in.ownerBefores = nil
in.ownerPrivates = nil
in.ownerSignatures = nil
in.fulfills = nil
return in
}
func (i *inputItem) Generate(removeNull bool) JsonObj {
var publicList []string
for _, pk := range *i.ownerBefores {
publicList = append(publicList, pk.String())
}
ii := JsonObj{}
fulfilment := i.creatFulfillment()
//if !removeNull || fulfilment != nil {
ii["fulfillment"] = fulfilment
//}
//if !removeNull || i.fulfills != nil {
ii["fulfills"] = i.fulfills
//}
if !removeNull || len(publicList) != 0 {
ii["owners_before"] = publicList
}
return ii
}
func (i *inputItem) Sign(message []byte) (Signature, error) {
if i.ownerPrivates == nil {
return nil, nil
}
priv := (*i.ownerPrivates)[0]
println("*\n message: ", string(message), "\n*\n")
sgn := ed25519.Sign(ed25519.PrivateKey(priv), message)
if i.ownerSignatures == nil {
i.ownerSignatures = new([]Signature)
*(i.ownerSignatures) = append(*(i.ownerSignatures), sgn)
}
return sgn, nil
}
func (i *inputItem) creatFulfillment() *string {
if i.ownerSignatures == nil {
return nil
}
ee, _ := gcc.NewEd25519Sha256((*i.ownerBefores)[0], (*i.ownerSignatures)[0])
tt, _ := ee.Encode()
ss := string(Base64UrlEncode(tt))
return &ss
}