code.vegaprotocol.io/vega@v0.79.0/datanode/entities/utils.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 entities 17 18 import ( 19 "encoding/base64" 20 "encoding/hex" 21 "fmt" 22 "strings" 23 "time" 24 25 "github.com/jackc/pgtype" 26 ) 27 28 type VegaPublicKey string 29 30 func (pk *VegaPublicKey) Bytes() ([]byte, error) { 31 strPK := pk.String() 32 33 bytes, err := hex.DecodeString(strPK) 34 if err != nil { 35 return nil, fmt.Errorf("decoding '%v': %w", pk.String(), ErrInvalidID) 36 } 37 return bytes, nil 38 } 39 40 func (pk *VegaPublicKey) Error() error { 41 _, err := pk.Bytes() 42 return err 43 } 44 45 func (pk *VegaPublicKey) String() string { 46 return string(*pk) 47 } 48 49 func (pk VegaPublicKey) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) { 50 bytes, err := pk.Bytes() 51 if err != nil { 52 return buf, err 53 } 54 return append(buf, bytes...), nil 55 } 56 57 func (pk *VegaPublicKey) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error { 58 strPK := hex.EncodeToString(src) 59 60 *pk = VegaPublicKey(strPK) 61 return nil 62 } 63 64 type TendermintPublicKey string 65 66 func (pk *TendermintPublicKey) Bytes() ([]byte, error) { 67 strPK := pk.String() 68 69 bytes, err := base64.StdEncoding.DecodeString(strPK) 70 if err != nil { 71 return nil, fmt.Errorf("decoding '%v': %w", pk.String(), ErrInvalidID) 72 } 73 return bytes, nil 74 } 75 76 func (pk *TendermintPublicKey) Error() error { 77 _, err := pk.Bytes() 78 return err 79 } 80 81 func (pk *TendermintPublicKey) String() string { 82 return string(*pk) 83 } 84 85 func (pk TendermintPublicKey) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) { 86 bytes, err := pk.Bytes() 87 if err != nil { 88 return buf, err 89 } 90 return append(buf, bytes...), nil 91 } 92 93 func (pk *TendermintPublicKey) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error { 94 strPK := base64.StdEncoding.EncodeToString(src) 95 96 *pk = TendermintPublicKey(strPK) 97 return nil 98 } 99 100 type EthereumAddress string 101 102 func (addr *EthereumAddress) Bytes() ([]byte, error) { 103 strAddr := addr.String() 104 105 if !strings.HasPrefix(strAddr, "0x") { 106 return nil, fmt.Errorf("invalid '%v': %w", addr.String(), ErrInvalidID) 107 } 108 109 bytes, err := hex.DecodeString(strAddr[2:]) 110 if err != nil { 111 return nil, fmt.Errorf("decoding '%v': %w", addr.String(), ErrInvalidID) 112 } 113 return bytes, nil 114 } 115 116 func (addr *EthereumAddress) Error() error { 117 _, err := addr.Bytes() 118 return err 119 } 120 121 func (addr *EthereumAddress) String() string { 122 return string(*addr) 123 } 124 125 func (addr EthereumAddress) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) { 126 bytes, err := addr.Bytes() 127 if err != nil { 128 return buf, err 129 } 130 return append(buf, bytes...), nil 131 } 132 133 func (addr *EthereumAddress) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error { 134 strAddr := "0x" + hex.EncodeToString(src) 135 136 *addr = EthereumAddress(strAddr) 137 return nil 138 } 139 140 type TxHash string 141 142 func (h *TxHash) Bytes() ([]byte, error) { 143 strPK := h.String() 144 145 bytes, err := hex.DecodeString(strPK) 146 if err != nil { 147 return nil, fmt.Errorf("decoding '%v': %w", h.String(), ErrInvalidID) 148 } 149 return bytes, nil 150 } 151 152 func (h *TxHash) Error() error { 153 _, err := h.Bytes() 154 return err 155 } 156 157 func (h *TxHash) String() string { 158 return string(*h) 159 } 160 161 func (h TxHash) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) { 162 bytes, err := h.Bytes() 163 if err != nil { 164 return buf, err 165 } 166 return append(buf, bytes...), nil 167 } 168 169 func (h *TxHash) DecodeBinary(ci *pgtype.ConnInfo, src []byte) error { 170 *h = TxHash(hex.EncodeToString(src)) 171 return nil 172 } 173 174 // NanosToPostgresTimestamp postgres stores timestamps in microsecond resolution. 175 func NanosToPostgresTimestamp(nanos int64) time.Time { 176 return time.Unix(0, nanos).Truncate(time.Microsecond) 177 }