-
Notifications
You must be signed in to change notification settings - Fork 92
/
signatureblock.go
134 lines (112 loc) · 3.08 KB
/
signatureblock.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package factoid
import (
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
)
/**************************************
* ISign
*
* Interface for RCB Signatures
*
* The signature block holds the signatures that validate one of the RCBs.
* Each signature has an index, so if the RCD is a multisig, you can know
* how to apply the signatures to the addresses in the RCD.
**************************************/
type SignatureBlock struct {
Signatures []interfaces.ISignature `json:"signatures"`
}
var _ interfaces.ISignatureBlock = (*SignatureBlock)(nil)
func (b *SignatureBlock) IsSameAs(s interfaces.ISignatureBlock) bool {
if s == nil {
return b == nil
}
sigs := s.GetSignatures()
if len(b.Signatures) != len(sigs) {
return false
}
for i := range b.Signatures {
if b.Signatures[i].IsSameAs(sigs[i]) == false {
return false
}
}
return true
}
func (b SignatureBlock) UnmarshalBinary(data []byte) error {
_, err := b.UnmarshalBinaryData(data)
return err
}
func (e *SignatureBlock) JSONByte() ([]byte, error) {
return primitives.EncodeJSON(e)
}
func (e *SignatureBlock) JSONString() (string, error) {
return primitives.EncodeJSONString(e)
}
func (b SignatureBlock) String() string {
txt, err := b.CustomMarshalText()
if err != nil {
return "<error>"
}
return string(txt)
}
func (s *SignatureBlock) AddSignature(sig interfaces.ISignature) {
if len(s.Signatures) > 0 {
s.Signatures[0] = sig
} else {
s.Signatures = append(s.Signatures, sig)
}
}
func (s SignatureBlock) GetSignature(index int) interfaces.ISignature {
if len(s.Signatures) <= index {
return nil
}
return s.Signatures[index]
}
func (s SignatureBlock) GetSignatures() []interfaces.ISignature {
if s.Signatures == nil {
s.Signatures = make([]interfaces.ISignature, 1, 1)
s.Signatures[0] = new(FactoidSignature)
}
return s.Signatures
}
func (a SignatureBlock) MarshalBinary() ([]byte, error) {
buf := primitives.NewBuffer(nil)
for _, sig := range a.GetSignatures() {
err := buf.PushBinaryMarshallable(sig)
if err != nil {
return nil, err
}
}
return buf.DeepCopyBytes(), nil
}
func (s SignatureBlock) CustomMarshalText() ([]byte, error) {
var out primitives.Buffer
out.WriteString("Signature Block: \n")
for _, sig := range s.Signatures {
out.WriteString(" signature: ")
txt, err := sig.CustomMarshalText()
if err != nil {
return nil, err
}
out.Write(txt)
out.WriteString("\n ")
}
return out.DeepCopyBytes(), nil
}
func (s *SignatureBlock) UnmarshalBinaryData(data []byte) ([]byte, error) {
buf := primitives.NewBuffer(data)
s.Signatures = make([]interfaces.ISignature, 1)
s.Signatures[0] = new(FactoidSignature)
err := buf.PopBinaryMarshallable(s.Signatures[0])
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
func NewSingleSignatureBlock(priv, data []byte) *SignatureBlock {
s := new(SignatureBlock)
s.AddSignature(NewED25519Signature(priv, data))
return s
}