github.com/aergoio/aergo@v1.3.1/p2p/p2pcommon/msgid.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package p2pcommon 7 8 import ( 9 "fmt" 10 "github.com/gofrs/uuid" 11 ) 12 13 // MsgID is 14 type MsgID [IDLength]byte 15 16 // NewMsgID return random id 17 func NewMsgID() (m MsgID) { 18 uid := uuid.Must(uuid.NewV4()) 19 return MsgID(uid) 20 } 21 22 var ( 23 EmptyID = MsgID(uuid.Nil) 24 ) 25 26 func ParseBytesToMsgID(b []byte) (MsgID, error) { 27 var m MsgID 28 if b == nil || len(b) != IDLength { 29 return m, fmt.Errorf("wrong format") 30 } 31 copy(m[:], b) 32 return m, nil 33 } 34 35 // MustParseBytes return MsgID from byte slice 36 func MustParseBytes(b []byte) MsgID { 37 if m, err := ParseBytesToMsgID(b); err == nil { 38 return m 39 } else { 40 panic(err) 41 } 42 } 43 44 func (id MsgID) UUID() uuid.UUID { 45 return uuid.UUID(id) 46 } 47 48 func (id MsgID) String() string { 49 return id.UUID().String() 50 }