-
Notifications
You must be signed in to change notification settings - Fork 19
/
multisig.go
207 lines (186 loc) · 4.99 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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package types
import (
"encoding/hex"
"fmt"
"strconv"
"code.vegaprotocol.io/vega/libs/crypto"
vgproto "code.vegaprotocol.io/vega/protos/vega"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
)
type SignerEventKind = eventspb.ERC20MultiSigSignerEvent_Type
const (
SignerEventKindAdded SignerEventKind = eventspb.ERC20MultiSigSignerEvent_TYPE_ADDED
SignerEventKindRemoved = eventspb.ERC20MultiSigSignerEvent_TYPE_REMOVED
)
type SignerEvent struct {
BlockNumber, LogIndex uint64
TxHash string
ID string
Address string
Nonce string
BlockTime int64
Kind SignerEventKind
}
func (s SignerEvent) Hash() string {
var kind string
switch s.Kind {
case SignerEventKindAdded:
kind = "signer_added"
case SignerEventKindRemoved:
kind = "signer_removed"
}
bn, li := strconv.FormatUint(s.BlockNumber, 10), strconv.FormatUint(s.LogIndex, 10)
return hex.EncodeToString(
crypto.Hash(
[]byte(bn + li + s.TxHash + s.Address + s.Nonce + kind),
),
)
}
func SignerEventFromSignerAddedProto(
s *vgproto.ERC20SignerAdded,
blockNumber, logIndex uint64,
txhash, id string,
) (*SignerEvent, error) {
return &SignerEvent{
ID: id,
BlockNumber: blockNumber,
LogIndex: logIndex,
TxHash: txhash,
Address: crypto.EthereumChecksumAddress(s.NewSigner),
Nonce: s.Nonce,
Kind: SignerEventKindAdded,
BlockTime: s.BlockTime,
}, nil
}
func SignerEventFromEventProto(
event *eventspb.ERC20MultiSigSignerEvent,
) *SignerEvent {
return &SignerEvent{
ID: event.Id,
BlockNumber: event.BlockNumber,
LogIndex: event.LogIndex,
TxHash: event.TxHash,
Address: crypto.EthereumChecksumAddress(event.Signer),
Nonce: event.Nonce,
Kind: event.Type,
BlockTime: event.BlockTime,
}
}
func (s *SignerEvent) IntoProto() *eventspb.ERC20MultiSigSignerEvent {
return &eventspb.ERC20MultiSigSignerEvent{
Id: s.ID,
Type: s.Kind,
Nonce: s.Nonce,
Signer: s.Address,
BlockTime: s.BlockTime,
TxHash: s.TxHash,
BlockNumber: s.BlockNumber,
LogIndex: s.LogIndex,
}
}
func (s *SignerEvent) String() string {
return fmt.Sprintf(
"blockNumber(%v) txHash(%s) ID(%s) address(%s) nonce(%s) blockTime(%v) kind(%s) ",
s.BlockNumber,
s.TxHash,
s.ID,
s.Address,
s.Nonce,
s.BlockTime,
s.Kind.String(),
)
}
func SignerEventFromSignerRemovedProto(
s *vgproto.ERC20SignerRemoved,
blockNumber, logIndex uint64,
txhash, id string,
) (*SignerEvent, error) {
return &SignerEvent{
ID: id,
BlockNumber: blockNumber,
LogIndex: logIndex,
TxHash: txhash,
Address: crypto.EthereumChecksumAddress(s.OldSigner),
Nonce: s.Nonce,
Kind: SignerEventKindRemoved,
BlockTime: s.BlockTime,
}, nil
}
type SignerThresholdSetEvent struct {
BlockNumber, LogIndex uint64
TxHash string
ID string
Threshold uint32
Nonce string
BlockTime int64
}
func (s SignerThresholdSetEvent) Hash() string {
bn, li := strconv.FormatUint(s.BlockNumber, 10), strconv.FormatUint(s.LogIndex, 10)
return hex.EncodeToString(
crypto.Hash(
[]byte(bn + li + s.TxHash + s.Nonce),
),
)
}
func SignerThresholdSetEventFromProto(
s *vgproto.ERC20ThresholdSet,
blockNumber, logIndex uint64,
txhash, id string,
) (*SignerThresholdSetEvent, error) {
return &SignerThresholdSetEvent{
ID: id,
BlockNumber: blockNumber,
LogIndex: logIndex,
TxHash: txhash,
Threshold: s.NewThreshold,
Nonce: s.Nonce,
BlockTime: s.BlockTime,
}, nil
}
func SignerThresholdSetEventFromEventProto(
event *eventspb.ERC20MultiSigThresholdSetEvent,
) *SignerThresholdSetEvent {
return &SignerThresholdSetEvent{
ID: event.Id,
BlockNumber: event.BlockNumber,
LogIndex: event.LogIndex,
TxHash: event.TxHash,
Threshold: event.NewThreshold,
Nonce: event.Nonce,
BlockTime: event.BlockTime,
}
}
func (s *SignerThresholdSetEvent) IntoProto() *eventspb.ERC20MultiSigThresholdSetEvent {
return &eventspb.ERC20MultiSigThresholdSetEvent{
Id: s.ID,
NewThreshold: s.Threshold,
Nonce: s.Nonce,
BlockTime: s.BlockTime,
TxHash: s.TxHash,
BlockNumber: s.BlockNumber,
LogIndex: s.LogIndex,
}
}
func (s *SignerThresholdSetEvent) String() string {
return fmt.Sprintf(
"ID(%s) blockNumber(%v) logIndex(%v) txHash(%s) threshold(%v) nonce(%s) blockTime(%v)",
s.ID,
s.BlockNumber,
s.LogIndex,
s.TxHash,
s.Threshold,
s.Nonce,
s.BlockTime,
)
}