code.vegaprotocol.io/vega@v0.79.0/core/types/multisig.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package types 17 18 import ( 19 "encoding/hex" 20 "fmt" 21 "strconv" 22 23 "code.vegaprotocol.io/vega/libs/crypto" 24 vgproto "code.vegaprotocol.io/vega/protos/vega" 25 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 26 ) 27 28 type SignerEventKind = eventspb.ERC20MultiSigSignerEvent_Type 29 30 const ( 31 SignerEventKindAdded SignerEventKind = eventspb.ERC20MultiSigSignerEvent_TYPE_ADDED 32 SignerEventKindRemoved = eventspb.ERC20MultiSigSignerEvent_TYPE_REMOVED 33 ) 34 35 type SignerEvent struct { 36 BlockNumber, LogIndex uint64 37 TxHash string 38 39 ID string 40 Address string 41 Nonce string 42 BlockTime int64 43 ChainID string 44 45 Kind SignerEventKind 46 } 47 48 func (s SignerEvent) Hash() string { 49 var kind string 50 switch s.Kind { 51 case SignerEventKindAdded: 52 kind = "signer_added" 53 case SignerEventKindRemoved: 54 kind = "signer_removed" 55 } 56 bn, li := strconv.FormatUint(s.BlockNumber, 10), strconv.FormatUint(s.LogIndex, 10) 57 return hex.EncodeToString( 58 crypto.Hash( 59 []byte(bn + li + s.TxHash + s.Address + s.Nonce + kind), 60 ), 61 ) 62 } 63 64 func SignerEventFromSignerAddedProto( 65 s *vgproto.ERC20SignerAdded, 66 blockNumber, logIndex uint64, 67 txhash, id, chainID string, 68 ) (*SignerEvent, error) { 69 return &SignerEvent{ 70 ID: id, 71 BlockNumber: blockNumber, 72 LogIndex: logIndex, 73 TxHash: txhash, 74 Address: crypto.EthereumChecksumAddress(s.NewSigner), 75 Nonce: s.Nonce, 76 Kind: SignerEventKindAdded, 77 BlockTime: s.BlockTime, 78 ChainID: chainID, 79 }, nil 80 } 81 82 func SignerEventFromEventProto( 83 event *eventspb.ERC20MultiSigSignerEvent, 84 ) *SignerEvent { 85 return &SignerEvent{ 86 ID: event.Id, 87 BlockNumber: event.BlockNumber, 88 LogIndex: event.LogIndex, 89 TxHash: event.TxHash, 90 Address: crypto.EthereumChecksumAddress(event.Signer), 91 Nonce: event.Nonce, 92 Kind: event.Type, 93 BlockTime: event.BlockTime, 94 ChainID: event.ChainId, 95 } 96 } 97 98 func (s *SignerEvent) IntoProto() *eventspb.ERC20MultiSigSignerEvent { 99 return &eventspb.ERC20MultiSigSignerEvent{ 100 Id: s.ID, 101 Type: s.Kind, 102 Nonce: s.Nonce, 103 Signer: s.Address, 104 BlockTime: s.BlockTime, 105 TxHash: s.TxHash, 106 BlockNumber: s.BlockNumber, 107 LogIndex: s.LogIndex, 108 ChainId: s.ChainID, 109 } 110 } 111 112 func (s *SignerEvent) String() string { 113 return fmt.Sprintf( 114 "blockNumber(%v) txHash(%s) ID(%s) address(%s) nonce(%s) blockTime(%v) kind(%s) chainID(%s)", 115 s.BlockNumber, 116 s.TxHash, 117 s.ID, 118 s.Address, 119 s.Nonce, 120 s.BlockTime, 121 s.Kind.String(), 122 s.ChainID, 123 ) 124 } 125 126 func SignerEventFromSignerRemovedProto( 127 s *vgproto.ERC20SignerRemoved, 128 blockNumber, logIndex uint64, 129 txhash, id, chainID string, 130 ) (*SignerEvent, error) { 131 return &SignerEvent{ 132 ID: id, 133 BlockNumber: blockNumber, 134 LogIndex: logIndex, 135 TxHash: txhash, 136 Address: crypto.EthereumChecksumAddress(s.OldSigner), 137 Nonce: s.Nonce, 138 Kind: SignerEventKindRemoved, 139 BlockTime: s.BlockTime, 140 ChainID: chainID, 141 }, nil 142 } 143 144 type SignerThresholdSetEvent struct { 145 BlockNumber, LogIndex uint64 146 TxHash string 147 148 ID string 149 Threshold uint32 150 Nonce string 151 BlockTime int64 152 ChainID string 153 } 154 155 func (s SignerThresholdSetEvent) Hash() string { 156 bn, li := strconv.FormatUint(s.BlockNumber, 10), strconv.FormatUint(s.LogIndex, 10) 157 return hex.EncodeToString( 158 crypto.Hash( 159 []byte(bn + li + s.TxHash + s.Nonce), 160 ), 161 ) 162 } 163 164 func SignerThresholdSetEventFromProto( 165 s *vgproto.ERC20ThresholdSet, 166 blockNumber, logIndex uint64, 167 txhash, id, chainID string, 168 ) (*SignerThresholdSetEvent, error) { 169 return &SignerThresholdSetEvent{ 170 ID: id, 171 BlockNumber: blockNumber, 172 LogIndex: logIndex, 173 TxHash: txhash, 174 Threshold: s.NewThreshold, 175 Nonce: s.Nonce, 176 BlockTime: s.BlockTime, 177 ChainID: chainID, 178 }, nil 179 } 180 181 func SignerThresholdSetEventFromEventProto( 182 event *eventspb.ERC20MultiSigThresholdSetEvent, 183 ) *SignerThresholdSetEvent { 184 return &SignerThresholdSetEvent{ 185 ID: event.Id, 186 BlockNumber: event.BlockNumber, 187 LogIndex: event.LogIndex, 188 TxHash: event.TxHash, 189 Threshold: event.NewThreshold, 190 Nonce: event.Nonce, 191 BlockTime: event.BlockTime, 192 ChainID: event.ChainId, 193 } 194 } 195 196 func (s *SignerThresholdSetEvent) IntoProto() *eventspb.ERC20MultiSigThresholdSetEvent { 197 return &eventspb.ERC20MultiSigThresholdSetEvent{ 198 Id: s.ID, 199 NewThreshold: s.Threshold, 200 Nonce: s.Nonce, 201 BlockTime: s.BlockTime, 202 TxHash: s.TxHash, 203 BlockNumber: s.BlockNumber, 204 LogIndex: s.LogIndex, 205 ChainId: s.ChainID, 206 } 207 } 208 209 func (s *SignerThresholdSetEvent) String() string { 210 return fmt.Sprintf( 211 "ID(%s) blockNumber(%v) logIndex(%v) txHash(%s) threshold(%v) nonce(%s) blockTime(%v) chainID(%s)", 212 s.ID, 213 s.BlockNumber, 214 s.LogIndex, 215 s.TxHash, 216 s.Threshold, 217 s.Nonce, 218 s.BlockTime, 219 s.ChainID, 220 ) 221 }