-
Notifications
You must be signed in to change notification settings - Fork 31
/
multisig.go
174 lines (156 loc) · 5.43 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
package multisig
import (
fmt "fmt"
occrypto "github.com/Finschia/ostracon/crypto"
"github.com/Finschia/finschia-sdk/codec/types"
cryptotypes "github.com/Finschia/finschia-sdk/crypto/types"
multisigtypes "github.com/Finschia/finschia-sdk/crypto/types/multisig"
"github.com/Finschia/finschia-sdk/types/tx/signing"
)
var (
_ multisigtypes.PubKey = &LegacyAminoPubKey{}
_ types.UnpackInterfacesMessage = &LegacyAminoPubKey{}
)
// NewLegacyAminoPubKey returns a new LegacyAminoPubKey.
// Multisig can be constructed with multiple same keys - it will increase the power of
// the owner of that key (he will still need to add multiple signatures in the right order).
// Panics if len(pubKeys) < k or 0 >= k.
func NewLegacyAminoPubKey(threshold int, pubKeys []cryptotypes.PubKey) *LegacyAminoPubKey {
if threshold <= 0 {
panic("threshold k of n multisignature: k <= 0")
}
if len(pubKeys) < threshold {
panic("threshold k of n multisignature: len(pubKeys) < k")
}
anyPubKeys, err := packPubKeys(pubKeys)
if err != nil {
panic(err)
}
return &LegacyAminoPubKey{Threshold: uint32(threshold), PubKeys: anyPubKeys}
}
// Address implements cryptotypes.PubKey Address method
func (m *LegacyAminoPubKey) Address() cryptotypes.Address {
return occrypto.AddressHash(m.Bytes())
}
// Bytes returns the proto encoded version of the LegacyAminoPubKey
func (m *LegacyAminoPubKey) Bytes() []byte {
return AminoCdc.MustMarshal(m)
}
// VerifyMultisignature implements the multisigtypes.PubKey VerifyMultisignature method.
// The signatures must be added in an order corresponding to the public keys order in
// LegacyAminoPubKey. It's OK to have multiple same keys in the multisig - it will increase
// the power of the owner of that key - in that case the signer will still need to append
// multiple same signatures in the right order.
func (m *LegacyAminoPubKey) VerifyMultisignature(getSignBytes multisigtypes.GetSignBytesFunc, sig *signing.MultiSignatureData) error {
bitarray := sig.BitArray
sigs := sig.Signatures
size := bitarray.Count()
pubKeys := m.GetPubKeys()
// ensure bit array is the correct size
if len(pubKeys) != size {
return fmt.Errorf("bit array size is incorrect, expecting: %d", len(pubKeys))
}
// ensure size of signature list
if len(sigs) < int(m.Threshold) || len(sigs) > size {
return fmt.Errorf("signature size is incorrect %d", len(sigs))
}
// ensure at least k signatures are set
if bitarray.NumTrueBitsBefore(size) < int(m.Threshold) {
return fmt.Errorf("not enough signatures set, have %d, expected %d", bitarray.NumTrueBitsBefore(size), int(m.Threshold))
}
// index in the list of signatures which we are concerned with.
sigIndex := 0
for i := 0; i < size; i++ {
if bitarray.GetIndex(i) {
si := sig.Signatures[sigIndex]
switch si := si.(type) {
case *signing.SingleSignatureData:
msg, err := getSignBytes(si.SignMode)
if err != nil {
return err
}
if !pubKeys[i].VerifySignature(msg, si.Signature) {
return fmt.Errorf("unable to verify signature at index %d", i)
}
case *signing.MultiSignatureData:
nestedMultisigPk, ok := pubKeys[i].(multisigtypes.PubKey)
if !ok {
return fmt.Errorf("unable to parse pubkey of index %d", i)
}
if err := nestedMultisigPk.VerifyMultisignature(getSignBytes, si); err != nil {
return err
}
default:
return fmt.Errorf("improper signature data type for index %d", sigIndex)
}
sigIndex++
}
}
return nil
}
// VerifySignature implements cryptotypes.PubKey VerifySignature method,
// it panics because it can't handle MultiSignatureData
// cf. https://github.com/cosmos/cosmos-sdk/issues/7109#issuecomment-686329936
func (m *LegacyAminoPubKey) VerifySignature(msg []byte, sig []byte) bool {
panic("not implemented")
}
// GetPubKeys implements the PubKey.GetPubKeys method
func (m *LegacyAminoPubKey) GetPubKeys() []cryptotypes.PubKey {
if m != nil {
pubKeys := make([]cryptotypes.PubKey, len(m.PubKeys))
for i := 0; i < len(m.PubKeys); i++ {
pubKeys[i] = m.PubKeys[i].GetCachedValue().(cryptotypes.PubKey)
}
return pubKeys
}
return nil
}
// Equals returns true if m and other both have the same number of keys, and
// all constituent keys are the same, and in the same order.
func (m *LegacyAminoPubKey) Equals(key cryptotypes.PubKey) bool {
otherKey, ok := key.(multisigtypes.PubKey)
if !ok {
return false
}
pubKeys := m.GetPubKeys()
otherPubKeys := otherKey.GetPubKeys()
if m.GetThreshold() != otherKey.GetThreshold() || len(pubKeys) != len(otherPubKeys) {
return false
}
for i := 0; i < len(pubKeys); i++ {
if !pubKeys[i].Equals(otherPubKeys[i]) {
return false
}
}
return true
}
// GetThreshold implements the PubKey.GetThreshold method
func (m *LegacyAminoPubKey) GetThreshold() uint {
return uint(m.Threshold)
}
// Type returns multisig type
func (m *LegacyAminoPubKey) Type() string {
return "PubKeyMultisigThreshold"
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (m *LegacyAminoPubKey) UnpackInterfaces(unpacker types.AnyUnpacker) error {
for _, any := range m.PubKeys {
var pk cryptotypes.PubKey
err := unpacker.UnpackAny(any, &pk)
if err != nil {
return err
}
}
return nil
}
func packPubKeys(pubKeys []cryptotypes.PubKey) ([]*types.Any, error) {
anyPubKeys := make([]*types.Any, len(pubKeys))
for i := 0; i < len(pubKeys); i++ {
any, err := types.NewAnyWithValue(pubKeys[i])
if err != nil {
return nil, err
}
anyPubKeys[i] = any
}
return anyPubKeys, nil
}