-
Notifications
You must be signed in to change notification settings - Fork 16
/
btc_multisig.go
199 lines (153 loc) · 3.31 KB
/
btc_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
/*
*/
package keys
import (
"bytes"
"encoding/json"
"fmt"
"github.com/btcsuite/btcutil/base58"
"github.com/pkg/errors"
"github.com/Oneledger/protocol/utils"
)
type BTCMultiSig struct {
Msg []byte `json:"msg"`
M int `json:"m"`
Signers []Address `json:"signers"`
Signatures []BTCSignature `json:"signatures"`
}
type BTCSignature struct {
Index int `json:"index"`
Address Address `json:"address"` // this should be a compressed public key
Sign []byte `json:"sign"`
}
func NewBTCMultiSig(msg []byte, m int, signers []Address) (*BTCMultiSig, error) {
/*
if msg == nil {
return nil, ErrMissMsg
}
*/
if signers == nil {
return nil, ErrMissSigners
}
if m < 0 || m > len(signers) {
return nil, ErrInvalidThreshold
}
return &BTCMultiSig{
Msg: msg,
M: m,
Signers: signers,
Signatures: make([]BTCSignature, len(signers)),
}, nil
}
func (m *BTCMultiSig) AddSignature(s *BTCSignature) error {
if !bytes.Equal(s.Address, m.Signers[s.Index]) {
return ErrNotExpectedSigner
}
// TODO verify the signature using btc libs
m.Signatures[s.Index] = *s
return nil
}
func (m BTCMultiSig) IsValid() bool {
cnt := 0
for _, item := range m.Signatures {
if item.Sign != nil {
cnt++
}
}
if cnt < m.M {
return false
}
return true
}
func (m BTCMultiSig) IsCancel() bool {
cnt := 0
for _, item := range m.Signatures {
if item.Sign != nil {
cnt++
}
}
if cnt < m.M {
return false
}
return true
}
func (m BTCMultiSig) Address() Address {
s := &BTCMultiSig{m.Msg, m.M, m.Signers, nil}
b := s.Bytes()
return utils.Hash(b)
}
func (m BTCMultiSig) Bytes() []byte {
signatures := m.Signatures
m.Signatures = make([]BTCSignature, 0)
for _, item := range signatures {
if item.Sign != nil {
m.Signatures = append(m.Signatures, item)
}
}
b, _ := json.Marshal(m)
return b
}
func (m BTCMultiSig) HasAddressSigned(addr Address) bool {
index := len(m.Signers) + 100
for i := range m.Signers {
if bytes.Equal(addr, m.Signers[i]) {
index = i
break
}
}
if index >= len(m.Signatures) {
return false
}
if len(m.Signatures[index].Sign) == 0 {
return false
}
return true
}
func (m *BTCMultiSig) FromBytes(b []byte) error {
err := json.Unmarshal(b, m)
if err != nil {
return err
}
signatures := m.Signatures
m.Signatures = make([]BTCSignature, len(m.Signers))
for _, item := range signatures {
m.Signatures[item.Index] = item
}
return nil
}
func (m *BTCMultiSig) GetSignerIndex(addr Address) (int, error) {
for i := range m.Signers {
if bytes.Equal(addr, m.Signers[i]) {
return i, nil
}
}
return 0, errors.New("address not found")
}
func (m *BTCMultiSig) GetSignatures() []BTCSignature {
return m.Signatures
}
func (m *BTCMultiSig) GetSignaturesInOrder() [][]byte {
tempSignatures := make([][]byte, len(m.Signers))
for _, s := range m.Signatures {
if len(s.Sign) == 0 {
continue
}
index := s.Index
tempSignatures[index] = s.Sign
fmt.Println(s.Index, base58.Encode(s.Address))
}
signatures := make([][]byte, 0, len(m.Signers))
for _, sig := range tempSignatures {
if len(sig) == 0 {
continue
}
signatures = append(signatures, sig)
}
return signatures
}
func (m *BTCMultiSig) Marshal() ([]byte, error) {
return m.Bytes(), nil
}
func (m *BTCMultiSig) Unmarshal(b []byte) error {
return m.FromBytes(b)
}