|
17 | 17 | import net.minecraft.util.ExtraCodecs; |
18 | 18 |
|
19 | 19 | import javax.annotation.ParametersAreNonnullByDefault; |
| 20 | +import java.security.interfaces.EdECPrivateKey; |
20 | 21 | import java.security.interfaces.EdECPublicKey; |
21 | 22 | import java.time.Instant; |
22 | 23 | import java.time.temporal.ChronoUnit; |
|
31 | 32 | public final class MUARecord { |
32 | 33 | public static final Codec<MUARecord> CODEC; |
33 | 34 | public static final StreamCodec<ByteBuf, MUARecord> STREAM_CODEC; |
34 | | - public static final StreamCodec<ByteBuf, Pair<GameProfile, MUAUser>> STREAM_CODEC_PART; |
| 35 | + public static final StreamCodec<ByteBuf, Pair<GameProfile, User>> STREAM_CODEC_PART; |
35 | 36 |
|
36 | 37 | static { |
37 | 38 | CODEC = RecordCodecBuilder.create(builder -> builder.group( |
38 | 39 | RecordCodecBuilder.<GameProfile>mapCodec(b -> b.group( |
39 | 40 | UUIDUtil.AUTHLIB_CODEC.fieldOf("id").forGetter(GameProfile::getId), |
40 | 41 | ExtraCodecs.PLAYER_NAME.fieldOf("name").forGetter(GameProfile::getName)) |
41 | 42 | .apply(b, GameProfile::new)).forGetter(MUARecord::getProfile), |
42 | | - MUAUser.CODEC.fieldOf("mua").forGetter(MUARecord::getUser), |
| 43 | + User.CODEC.fieldOf("mua").forGetter(MUARecord::getUser), |
43 | 44 | Codec.list(SignEntry.CODEC).fieldOf("signatures").forGetter(MUARecord::getSignatures)) |
44 | 45 | .apply(builder, MUARecord::new)); |
45 | 46 | STREAM_CODEC = StreamCodec.composite( |
46 | | - ByteBufCodecs.GAME_PROFILE, MUARecord::getProfile, MUAUser.STREAM_CODEC, MUARecord::getUser, |
| 47 | + ByteBufCodecs.GAME_PROFILE, MUARecord::getProfile, User.STREAM_CODEC, MUARecord::getUser, |
47 | 48 | SignEntry.STREAM_CODEC.apply(ByteBufCodecs.list()), MUARecord::getSignatures, MUARecord::new); |
48 | 49 | STREAM_CODEC_PART = StreamCodec.composite( |
49 | | - ByteBufCodecs.GAME_PROFILE, Pair::getFirst, MUAUser.STREAM_CODEC, Pair::getSecond, Pair::of); |
| 50 | + ByteBufCodecs.GAME_PROFILE, Pair::getFirst, User.STREAM_CODEC, Pair::getSecond, Pair::of); |
50 | 51 | } |
51 | 52 |
|
52 | | - private final MUAUser user; |
| 53 | + private final User user; |
53 | 54 | private final GameProfile profile; |
54 | 55 | private final List<SignEntry> signatures; |
55 | 56 |
|
56 | | - public MUARecord(GameProfile profile, MUAUser user, Collection<? extends SignEntry> signatures) { |
| 57 | + public MUARecord(GameProfile profile, User user, Collection<? extends SignEntry> signatures) { |
57 | 58 | this.user = user; |
58 | 59 | this.profile = profile; |
59 | 60 | this.signatures = List.copyOf(signatures); |
60 | 61 | } |
61 | 62 |
|
62 | | - public MUAUser getUser() { |
| 63 | + public User getUser() { |
63 | 64 | return this.user; |
64 | 65 | } |
65 | 66 |
|
@@ -163,4 +164,29 @@ public HashCode getSignature() { |
163 | 164 | return this.signature; |
164 | 165 | } |
165 | 166 | } |
| 167 | + |
| 168 | + @FieldsAreNonnullByDefault |
| 169 | + @MethodsReturnNonnullByDefault |
| 170 | + @ParametersAreNonnullByDefault |
| 171 | + public record User(String sub, String nickname, String email) { |
| 172 | + public static final Codec<User> CODEC; |
| 173 | + public static final StreamCodec<ByteBuf, User> STREAM_CODEC; |
| 174 | + |
| 175 | + static { |
| 176 | + CODEC = RecordCodecBuilder.create(builder -> builder.group( |
| 177 | + Codec.STRING.fieldOf("sub").forGetter(User::sub), |
| 178 | + Codec.STRING.fieldOf("nickname").forGetter(User::nickname), |
| 179 | + Codec.STRING.fieldOf("email").forGetter(User::email)).apply(builder, User::new)); |
| 180 | + STREAM_CODEC = StreamCodec.composite( |
| 181 | + ByteBufCodecs.STRING_UTF8, User::sub, |
| 182 | + ByteBufCodecs.STRING_UTF8, User::nickname, |
| 183 | + ByteBufCodecs.STRING_UTF8, User::email, User::new); |
| 184 | + } |
| 185 | + |
| 186 | + public MUARecord sign(GameProfile profile, Instant expire, Pair<EdECPublicKey, EdECPrivateKey> keys) { |
| 187 | + var keyBytes = Ed25519.serialize(keys.getFirst()); |
| 188 | + var signature = Ed25519.sign(keys.getSecond(), expire, Pair.of(profile, this), STREAM_CODEC_PART); |
| 189 | + return new MUARecord(profile, this, List.of(new SignEntry(keyBytes, expire, signature))); |
| 190 | + } |
| 191 | + } |
166 | 192 | } |
0 commit comments