| 1 | package net.minecraft.network.chat;
|
| 2 |
|
| 3 | import com.google.common.primitives.Ints;
|
| 4 | import com.google.common.primitives.Longs;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.MapCodec;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 8 | import io.netty.buffer.ByteBuf;
|
| 9 | import java.nio.charset.StandardCharsets;
|
| 10 | import java.security.SignatureException;
|
| 11 | import java.time.Instant;
|
| 12 | import java.util.Optional;
|
| 13 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 14 | import net.minecraft.network.codec.StreamCodec;
|
| 15 | import net.minecraft.util.ExtraCodecs;
|
| 16 | import net.minecraft.util.SignatureUpdater;
|
| 17 |
|
| 18 | public record SignedMessageBody(String content, Instant timeStamp, long salt, LastSeenMessages lastSeen) {
|
| 19 | public static final MapCodec<SignedMessageBody> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 20 | i -> i.group(
|
| 21 | Codec.STRING.fieldOf("content").forGetter(SignedMessageBody::content),
|
| 22 | ExtraCodecs.INSTANT_ISO8601.fieldOf("time_stamp").forGetter(SignedMessageBody::timeStamp),
|
| 23 | Codec.LONG.fieldOf("salt").forGetter(SignedMessageBody::salt),
|
| 24 | LastSeenMessages.CODEC.optionalFieldOf("last_seen", LastSeenMessages.EMPTY).forGetter(SignedMessageBody::lastSeen)
|
| 25 | )
|
| 26 | .apply(i, SignedMessageBody::new)
|
| 27 | );
|
| 28 |
|
| 29 | public static SignedMessageBody unsigned(final String content) {
|
| 30 | return new SignedMessageBody(content, Instant.now(), 0L, LastSeenMessages.EMPTY);
|
| 31 | }
|
| 32 |
|
| 33 | public void updateSignature(final SignatureUpdater.Output output) throws SignatureException {
|
| 34 | output.update(Longs.toByteArray(this.salt));
|
| 35 | output.update(Longs.toByteArray(this.timeStamp.getEpochSecond()));
|
| 36 | byte[] contentBytes = this.content.getBytes(StandardCharsets.UTF_8);
|
| 37 | output.update(Ints.toByteArray(contentBytes.length));
|
| 38 | output.update(contentBytes);
|
| 39 | this.lastSeen.updateSignature(output);
|
| 40 | }
|
| 41 |
|
| 42 | public SignedMessageBody.Packed pack(final MessageSignatureCache cache) {
|
| 43 | return new SignedMessageBody.Packed(this.content, this.timeStamp, this.salt, this.lastSeen.pack(cache));
|
| 44 | }
|
| 45 |
|
| 46 | public record Packed(String content, Instant timeStamp, long salt, LastSeenMessages.Packed lastSeen) {
|
| 47 | public static final StreamCodec<ByteBuf, SignedMessageBody.Packed> STREAM_CODEC = StreamCodec.composite(
|
| 48 | ByteBufCodecs.stringUtf8(256),
|
| 49 | SignedMessageBody.Packed::content,
|
| 50 | ByteBufCodecs.INSTANT,
|
| 51 | SignedMessageBody.Packed::timeStamp,
|
| 52 | ByteBufCodecs.LONG,
|
| 53 | SignedMessageBody.Packed::salt,
|
| 54 | LastSeenMessages.Packed.STREAM_CODEC,
|
| 55 | SignedMessageBody.Packed::lastSeen,
|
| 56 | SignedMessageBody.Packed::new
|
| 57 | );
|
| 58 |
|
| 59 | public Optional<SignedMessageBody> unpack(final MessageSignatureCache cache) {
|
| 60 | return this.lastSeen.unpack(cache).map(lastSeen -> new SignedMessageBody(this.content, this.timeStamp, this.salt, lastSeen));
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 |
|