// Decode unpacks an encoded HopPayload from the passed reader into the target
// HopPayload.
Decode unpacks an encoded HopPayload from the passed reader into the target
HopPayload.
// Decode unpacks an encoded HopPayload from the passed reader into the target // HopPayload .
func (hp *HopPayload) Decode(r io.Reader) error {
	bufReader := bufio.NewReader(r)
	peekByte, err := bufReader.Peek(1)
	if err != nil {
		return err
	}
	var payloadSize uint32
	switch int(peekByte[0]) {
	case 0x00:
		payloadSize = LegacyHopDataSize - HMACSize
		hp.Type = PayloadLegacy
	default:
		var b [8]byte
		varInt, err := ReadVarInt(bufReader, &b)
		if err != nil {
			return err
		}
		payloadSize = uint32(varInt)
		hp.Type = PayloadTLV
	}
	 TODO(roasbeef): can avoid all these copies
	hp.Payload = make([]byte, payloadSize)
	if _, err := io.ReadFull(bufReader, hp.Payload[:]); err != nil {
		return err
	}
	if _, err := io.ReadFull(bufReader, hp.HMAC[:]); err != nil {
		return err
	}
	return nil
}
func ( hp * HopPayload ) Decode ( r io . Reader ) error { bufReader := bufio . NewReader ( r ) peekByte , err := bufReader . Peek ( 1 ) if err != nil { return err } var payloadSize uint32 switch int ( peekByte [ 0 ] ) { case 0x00 : payloadSize = LegacyHopDataSize - HMACSize hp . Type = PayloadLegacy default : var b [ 8 ] byte varInt , err := ReadVarInt ( bufReader , & b ) if err != nil { return err } payloadSize = uint32 ( varInt ) hp . Type = PayloadTLV } hp . Payload = make ( [ ] byte , payloadSize ) if _ , err := io . ReadFull ( bufReader , hp . Payload [ : ] ) ; err != nil { return err } if _ , err := io . ReadFull ( bufReader , hp . HMAC [ : ] ) ; err != nil { return err } return nil }
Decode unpacks an encoded HopPayload from the passed reader into the target
HopPayload.
Decode unpacks an encoded HopPayload from the passed reader into the target HopPayload .
// In order to properly parse the payload, we'll need to check the // first byte. We'll use a bufio reader to peek at it without consuming // it from the buffer. // If the first byte is a zero (the realm), then this is the normal // payload. // Our size is just the payload, without the HMAC. This means // that this is the legacy payload type. // Otherwise, this is the new TLV based payload type, so we'll // extract the payload length encoded as a var-int. // Now that we know the payload size, we'll create a  new buffer to // read it out in full. // // TODO(roasbeef): can avoid all these copies