SHA256
Добавлена доработка сценариев ротации ключей, закрытия сессий и повторного шифрования DM. Версия пока не проверена полностью, но ожидается рабочей.
188 lines
9.6 KiB
Java
188 lines
9.6 KiB
Java
package blockchain.body;
|
|
|
|
import blockchain.MsgSubType;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.ByteOrder;
|
|
import java.nio.charset.CharacterCodingException;
|
|
import java.nio.charset.CodingErrorAction;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Arrays;
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* Line-based TEXT body.
|
|
*
|
|
* POST / CHANNEL_META / ENTRYPOINT / EXERCISE / SERVICE / COURSE:
|
|
* line fields + text
|
|
*
|
|
* EDIT_POST (own-chain compact target):
|
|
* line fields + [4] targetBlockNumber + [32] targetHash + text
|
|
*
|
|
* REPOST (external target):
|
|
* line fields + [1]loginLen + login + [2]forkNumber + [4]blockNumber + [32]hash + text
|
|
*/
|
|
public final class TextLineBody implements BodyRecord, BodyHasLine, BodyHasTarget {
|
|
public static final short TYPE = 1;
|
|
public static final short VER = 1;
|
|
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
|
|
|
public final short subType;
|
|
public final short version;
|
|
public final int lineCode;
|
|
public final int prevLineNumber;
|
|
public final byte[] prevLineHash32;
|
|
public final int thisLineNumber;
|
|
public final String toLogin;
|
|
public final Integer toForkNumber;
|
|
public final Integer toBlockGlobalNumber;
|
|
public final byte[] toBlockHash32;
|
|
public final String message;
|
|
|
|
public TextLineBody(short subType, short version, byte[] bodyBytes) {
|
|
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
|
this.subType = subType;
|
|
this.version = version;
|
|
if ((version & 0xFFFF) != 1 || !isSupportedSubType(subType)) throw new IllegalArgumentException("Bad TextLineBody header");
|
|
|
|
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
|
BodyTargetSupport.ensureMin(bb, 4 + 4 + 32 + 4 + 2, "TextLineBody too short");
|
|
this.lineCode = bb.getInt();
|
|
this.prevLineNumber = bb.getInt();
|
|
this.prevLineHash32 = new byte[32]; bb.get(this.prevLineHash32);
|
|
this.thisLineNumber = bb.getInt();
|
|
|
|
int st = subType & 0xFFFF;
|
|
if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
|
BodyTargetSupport.ensureMin(bb, 4 + 32 + 2, "EDIT_POST target missing");
|
|
this.toLogin = null;
|
|
this.toForkNumber = null;
|
|
this.toBlockGlobalNumber = bb.getInt();
|
|
this.toBlockHash32 = new byte[32]; bb.get(this.toBlockHash32);
|
|
BodyTargetSupport.validateCompact(this.toBlockGlobalNumber, this.toBlockHash32, "EDIT_POST");
|
|
} else if (st == (MsgSubType.TEXT_REPOST & 0xFFFF)) {
|
|
BodyTargetSupport.ExternalTarget t = BodyTargetSupport.readExternal(bb, "REPOST", 2);
|
|
this.toLogin = t.login();
|
|
this.toForkNumber = t.forkNumber();
|
|
this.toBlockGlobalNumber = t.blockNumber();
|
|
this.toBlockHash32 = t.blockHash32();
|
|
} else {
|
|
this.toLogin = null;
|
|
this.toForkNumber = null;
|
|
this.toBlockGlobalNumber = null;
|
|
this.toBlockHash32 = null;
|
|
}
|
|
|
|
boolean allowEmpty = st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF) || st == (MsgSubType.TEXT_CHANNEL_META & 0xFFFF) || st == (MsgSubType.TEXT_CHANNEL_DELETE & 0xFFFF);
|
|
this.message = readText(bb, allowEmpty);
|
|
if (bb.hasRemaining()) throw new IllegalArgumentException("Unexpected TextLineBody tail");
|
|
}
|
|
|
|
public TextLineBody(int lineCode,
|
|
int prevLineNumber,
|
|
byte[] prevLineHash32,
|
|
int thisLineNumber,
|
|
short subType,
|
|
String toLogin,
|
|
Integer toForkNumber,
|
|
Integer toBlockGlobalNumber,
|
|
byte[] toBlockHash32,
|
|
String message) {
|
|
Objects.requireNonNull(message, "message == null");
|
|
if (!isSupportedSubType(subType)) throw new IllegalArgumentException("Bad TextLineBody subtype");
|
|
if (lineCode < 0) throw new IllegalArgumentException("lineCode < 0");
|
|
|
|
this.subType = subType;
|
|
this.version = VER;
|
|
this.lineCode = lineCode;
|
|
this.prevLineNumber = prevLineNumber;
|
|
this.prevLineHash32 = prevLineHash32 == null ? new byte[32] : Arrays.copyOf(prevLineHash32, 32);
|
|
this.thisLineNumber = thisLineNumber;
|
|
this.toLogin = toLogin;
|
|
this.toForkNumber = toForkNumber;
|
|
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
|
this.toBlockHash32 = toBlockHash32 == null ? null : Arrays.copyOf(toBlockHash32, 32);
|
|
this.message = message;
|
|
check();
|
|
}
|
|
|
|
@Override public TextLineBody check() {
|
|
if (!isSupportedSubType(subType)) throw new IllegalArgumentException("Bad TextLineBody subtype");
|
|
if (lineCode < 0) throw new IllegalArgumentException("lineCode < 0");
|
|
if (prevLineHash32 == null || prevLineHash32.length != 32) throw new IllegalArgumentException("prevLineHash32 invalid");
|
|
|
|
int st = subType & 0xFFFF;
|
|
if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
|
if (toLogin != null || toForkNumber != null) throw new IllegalArgumentException("EDIT_POST must not contain login/fork target");
|
|
BodyTargetSupport.validateCompact(toBlockGlobalNumber, toBlockHash32, "EDIT_POST");
|
|
if (message == null) throw new IllegalArgumentException("EDIT_POST message is null");
|
|
} else if (st == (MsgSubType.TEXT_REPOST & 0xFFFF)) {
|
|
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REPOST");
|
|
if (message == null || message.isBlank()) throw new IllegalArgumentException("REPOST message is blank");
|
|
} else {
|
|
if (toLogin != null || toForkNumber != null || toBlockGlobalNumber != null || toBlockHash32 != null) {
|
|
throw new IllegalArgumentException("Non-target text body must not contain target fields");
|
|
}
|
|
if (st == (MsgSubType.TEXT_CHANNEL_META & 0xFFFF)) {
|
|
if (message == null) throw new IllegalArgumentException("CHANNEL_META message is null");
|
|
} else if (st == (MsgSubType.TEXT_CHANNEL_DELETE & 0xFFFF)) {
|
|
if (message == null || !message.isEmpty()) throw new IllegalArgumentException("CHANNEL_DELETE message must be empty");
|
|
} else if (message == null || message.isBlank()) {
|
|
throw new IllegalArgumentException("Text message is blank");
|
|
}
|
|
}
|
|
|
|
if (message.getBytes(StandardCharsets.UTF_8).length > 65535) throw new IllegalArgumentException("Text too long");
|
|
return this;
|
|
}
|
|
|
|
@Override public byte[] toBytes() {
|
|
check();
|
|
byte[] text = message.getBytes(StandardCharsets.UTF_8);
|
|
int st = subType & 0xFFFF;
|
|
int targetSize = 0;
|
|
if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) targetSize = 4 + 32;
|
|
else if (st == (MsgSubType.TEXT_REPOST & 0xFFFF)) targetSize = BodyTargetSupport.externalSize(toLogin);
|
|
|
|
ByteBuffer bb = ByteBuffer.allocate(4 + 4 + 32 + 4 + targetSize + 2 + text.length).order(ByteOrder.BIG_ENDIAN);
|
|
bb.putInt(lineCode).putInt(prevLineNumber).put(prevLineHash32).putInt(thisLineNumber);
|
|
if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
|
bb.putInt(toBlockGlobalNumber).put(toBlockHash32);
|
|
} else if (st == (MsgSubType.TEXT_REPOST & 0xFFFF)) {
|
|
BodyTargetSupport.writeExternal(bb, toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REPOST");
|
|
}
|
|
bb.putShort((short) text.length).put(text);
|
|
return bb.array();
|
|
}
|
|
|
|
@Override public int lineCode() { return lineCode; }
|
|
@Override public int prevLineBlockGlobalNumber() { return prevLineNumber; }
|
|
@Override public byte[] prevLineBlockHash32() { return Arrays.copyOf(prevLineHash32, 32); }
|
|
@Override public int lineSeq() { return thisLineNumber; }
|
|
@Override public String toLogin() { return toLogin; }
|
|
@Override public Integer toForkNumber() { return toForkNumber; }
|
|
@Override public Integer toBlockGlobalNumber() { return toBlockGlobalNumber; }
|
|
@Override public byte[] toBlockHashBytes() { return toBlockHash32 == null ? null : Arrays.copyOf(toBlockHash32, 32); }
|
|
public boolean isEditPost() { return (subType & 0xFFFF) == (MsgSubType.TEXT_EDIT_POST & 0xFFFF); }
|
|
|
|
private static boolean isSupportedSubType(short subType) {
|
|
int st = subType & 0xFFFF;
|
|
return st == (MsgSubType.TEXT_POST & 0xFFFF) || st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)
|
|
|| st == (MsgSubType.TEXT_REPOST & 0xFFFF) || st == (MsgSubType.TEXT_CHANNEL_META & 0xFFFF)
|
|
|| st == (MsgSubType.TEXT_CHANNEL_DELETE & 0xFFFF)
|
|
|| st == (MsgSubType.TEXT_ENTRYPOINT & 0xFFFF) || st == (MsgSubType.TEXT_EXERCISE & 0xFFFF)
|
|
|| st == (MsgSubType.TEXT_SERVICE & 0xFFFF) || st == (MsgSubType.TEXT_COURSE & 0xFFFF);
|
|
}
|
|
|
|
private static String readText(ByteBuffer bb, boolean allowEmpty) {
|
|
if (bb.remaining() < 2) throw new IllegalArgumentException("Text length missing");
|
|
int len = Short.toUnsignedInt(bb.getShort());
|
|
if (len == 0) { if (allowEmpty) return ""; throw new IllegalArgumentException("Text is empty"); }
|
|
if (bb.remaining() < len) throw new IllegalArgumentException("Text payload too short");
|
|
byte[] bytes = new byte[len]; bb.get(bytes);
|
|
var dec = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
|
|
try { String s = dec.decode(ByteBuffer.wrap(bytes)).toString(); if (!allowEmpty && s.isBlank()) throw new IllegalArgumentException("Text blank"); return s; }
|
|
catch (CharacterCodingException e) { throw new IllegalArgumentException("Text invalid UTF-8", e); }
|
|
}
|
|
}
|