func DeserializeMessages(msgs []byte) []*BitMessage { iterator := 0 var vec []*BitMessage for len(msgs[iterator:]) >= 20 { msgtype := MessageType(msgs[iterator+4 : iterator+16]) Payloadbyte := msgs[iterator+16 : iterator+20] Payload := int(mymath.HexRev2Big(Payloadbyte).Int64()) if msgtype == VERSION || msgtype == VERACK { vec = append(vec, DecodeMessage(msgs[iterator:iterator+20+Payload])) iterator = iterator + 20 + Payload } else { vec = append(vec, DecodeMessage(msgs[iterator:iterator+24+Payload])) iterator = iterator + 24 + Payload } } /*answer:=make([]BitMessage, vec.Len()) for i:=0;i<vec.Len();i++{ answer[i]=vec.At(i).(BitMessage) }*/ return vec }
func DecodeMessage(msg []byte) *BitMessage { bm := new(BitMessage) if len(msg) < 20 { return nil } for i := 0; i < 4; i++ { bm.Magic[i] = msg[0+i] bm.Length[i] = msg[16+i] } for i := 0; i < 12; i++ { bm.Command[i] = msg[4+i] } msgtype := MessageType(msg[4:16]) Payloadbyte := msg[16:20] Payload := int(mymath.HexRev2Big(Payloadbyte).Int64()) if msgtype == VERSION || msgtype == VERACK { bm.Payload = nil if len(msg) < 20+Payload { return nil } else { if msgtype == VERSION { bm.SetPayloadByte(msg[20 : 20+Payload]) } else if msgtype == VERACK { } } } else { if len(msg) < 24+Payload { return nil } else { for i := 0; i < 4; i++ { bm.Checksum[i] = msg[20+i] } bm.SetPayloadByte(msg[24 : 24+Payload]) } } return bm }