-
Notifications
You must be signed in to change notification settings - Fork 16
/
multisig.go
212 lines (171 loc) · 4.64 KB
/
multisig.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package keys
import (
"bytes"
"encoding/json"
"github.com/Oneledger/protocol/utils"
"github.com/pkg/errors"
)
type MultiSigner interface {
//Initialize the MultiSig
// "msg" that need to be sign by this multisig
// "threshold" used for threshold signature representing the minimal signatures for the multisig to be valid
// "signers" are the expected signers that need to sign for the message, the signers will be sorted in the slice.
Init(msg []byte, threshold int, signers []Address) error
// Add a signature to the MultiSig. Return error if the signature not satisfy:
// - Valid s.PubKey in signature
// - PubKey's address doesn't match signer address of s.Index
// - Signed info is not the signature for message in the MultiSig
AddSignature(s Signature) error
// check MultiSig contains enough signature with threshold
IsValid() bool
//// Return the threshold for MultiSig
//// threshold is between (0, len(Signer)]
//// when threshold = len(Signer): need all signers to sign
//Threshold() int
//
//// Return the expected signers for the MultiSig
//Signers() []Address
//
//// Return the expected signing message for the MultiSig
//Message() []byte
//
//// Return the address for the MultiSig, which act as unique identifier of MultiSig
//// The address should only depend on (msg, m, signers) the signatures should not influence the address
//Address() Address
// Return the bytes for the MultiSig,
// the Bytes should include all info
Bytes() []byte
// Set MultiSig from []byte
FromBytes(b []byte) error
GetSignerIndex(Address) (int, error)
GetSignatures() []Signature
}
type Signature struct {
Index int `json:"index"` //index in the MultiSig
PubKey PublicKey `json:"pubkey"`
Signed []byte `json:"signed"`
}
var _ MultiSigner = &MultiSig{}
type MultiSig struct {
// message that need to be sign by this multisig
Msg []byte
//used for threshold signature where M represent the minimal signatures for the multisig to be valid
// M is between (0, len(Signer)]
// when M = len(Signer): need all signers to sign
M int
//the expected signers that need to sign for the message, the signers will be sorted in the slice.
Signers []Address
//the collection of signatures for signers, the index of signatures should match the index of signers. this should
// be empty when created and before add Signature
Signatures []Signature
}
func (m *MultiSig) Init(msg []byte, threshold int, signers []Address) error {
if msg == nil {
return ErrMissMsg
}
if signers == nil {
return ErrMissSigners
}
if threshold < 0 || threshold > len(signers) {
return ErrInvalidThreshold
}
*m = MultiSig{
Msg: msg,
M: threshold,
Signers: signers,
Signatures: make([]Signature, len(signers)),
}
return nil
}
func (m *MultiSig) AddSignature(s Signature) error {
h, err := s.PubKey.GetHandler()
if err != nil {
return errors.Wrap(err, "failed to add")
}
if !bytes.Equal(m.Signers[s.Index], h.Address().Bytes()) {
return ErrNotExpectedSigner
}
if !h.VerifyBytes(m.Msg, s.Signed) {
return ErrInvalidSignedMsg
}
m.Signatures[s.Index] = s
return nil
}
func (m MultiSig) IsValid() bool {
cnt := 0
for _, item := range m.Signatures {
if item.Signed != nil {
cnt++
}
}
if cnt < m.M {
return false
}
return true
}
func (m MultiSig) Address() Address {
s := &MultiSig{m.Msg, m.M, m.Signers, nil}
b := s.Bytes()
return utils.Hash(b)
}
func (m MultiSig) HasAddressSigned(addr Address) bool {
index := len(m.Signers) + 100
for i := range m.Signers {
if bytes.Equal(m.Signers[i], addr) {
index = i
break
}
}
if index > len(m.Signers) {
return false
}
if len(m.Signatures[index].Signed) == 0 {
return false
}
return true
}
//func (m MultiSig) Threshold() int {
// return m.M
//}
//
//func (m MultiSig) Signers() []Address {
// return m.Signers
//}
//
//func (m MultiSig) Message() []byte {
// return m.Msg
//}
func (m MultiSig) Bytes() []byte {
signatures := m.Signatures
m.Signatures = make([]Signature, 0)
for _, item := range signatures {
if item.Signed != nil {
m.Signatures = append(m.Signatures, item)
}
}
b, _ := json.Marshal(m)
return b
}
func (m *MultiSig) FromBytes(b []byte) error {
err := json.Unmarshal(b, m)
if err != nil {
return err
}
signatures := m.Signatures
m.Signatures = make([]Signature, len(m.Signers))
for i, item := range signatures {
m.Signatures[i] = item
}
return nil
}
func (m *MultiSig) GetSignerIndex(ad Address) (int, error) {
for i := range m.Signers {
if bytes.Equal(m.Signers[i], ad) {
return i, nil
}
}
return 0, errors.New("address not found")
}
func (m *MultiSig) GetSignatures() []Signature {
return m.Signatures
}