SHA256
193 lines
8.0 KiB
Java
193 lines
8.0 KiB
Java
package blockchain.body;
|
|
|
|
import blockchain.MsgSubType;
|
|
import blockchain.CoreRecord;
|
|
|
|
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;
|
|
|
|
/**
|
|
* UserParamBody — type=4, ver=1 (в документации: USER_PROFILE).
|
|
*
|
|
* subType (в заголовке блока):
|
|
* 1 = TEXT_TEXT
|
|
*
|
|
* USER_PROFILE имеет единственную фиксированную линию, поэтому lineCode в body не хранится.
|
|
* bodyBytes (BigEndian):
|
|
* [4] prevProfileBlockNumber — GLOBAL blockNumber предыдущего USER_PROFILE-блока
|
|
* [32] prevProfileBlockHash32 — hash32 предыдущего USER_PROFILE-блока
|
|
* [4] profileSequence — порядковый номер изменения профиля (1..N)
|
|
*
|
|
* [2] keyLenBytes (uint16)
|
|
* [N] keyUtf8
|
|
*
|
|
* [2] valueLenBytes (uint16)
|
|
* [M] valueUtf8
|
|
*/
|
|
public final class UserParamBody implements BodyRecord, BodyHasLine {
|
|
|
|
public static final short TYPE = 4;
|
|
public static final short VER = 1;
|
|
|
|
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
|
|
|
private final boolean contentOnly;
|
|
public final short subType;
|
|
public final short version;
|
|
|
|
public final int prevProfileBlockNumber;
|
|
public final byte[] prevProfileBlockHash32;
|
|
public final int profileSequence;
|
|
|
|
public final String paramKey;
|
|
public final String paramValue;
|
|
|
|
public UserParamBody(short subType, short version, byte[] bodyBytes) {
|
|
this(subType, version, bodyBytes, null);
|
|
}
|
|
|
|
/** Разбор CONTENT: линия берётся из CORE, порядковый номер не подписан. */
|
|
public UserParamBody(short subType, short version, byte[] bodyBytes, CoreRecord.Line line) {
|
|
this.contentOnly = line != null;
|
|
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
|
|
|
this.subType = subType;
|
|
this.version = version;
|
|
|
|
if ((this.version & 0xFFFF) != (VER & 0xFFFF)) {
|
|
throw new IllegalArgumentException("UserParamBody version must be 1, got=" + (this.version & 0xFFFF));
|
|
}
|
|
if ((this.subType & 0xFFFF) != (MsgSubType.USER_PARAM_TEXT_TEXT & 0xFFFF)) {
|
|
throw new IllegalArgumentException("Bad UserParam subType: " + (this.subType & 0xFFFF));
|
|
}
|
|
|
|
// prevGlobal(4)+prevHash(32)+seq(4) + keyLen(2)+key(1) + valLen(2)+val(1)
|
|
if (bodyBytes.length < (4 + 32 + 4 + 2 + 1 + 2 + 1) - (contentOnly ? 40 : 0)) {
|
|
throw new IllegalArgumentException("UserParamBody too short");
|
|
}
|
|
|
|
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
|
|
|
if (contentOnly) {
|
|
this.prevProfileBlockNumber = line.prevLineBlockNumber();
|
|
this.prevProfileBlockHash32 = line.prevLineCoreHash32();
|
|
this.profileSequence = BodyHasLine.UNRESOLVED_SEQUENCE;
|
|
} else {
|
|
this.prevProfileBlockNumber = bb.getInt();
|
|
this.prevProfileBlockHash32 = new byte[32];
|
|
bb.get(this.prevProfileBlockHash32);
|
|
this.profileSequence = bb.getInt();
|
|
}
|
|
|
|
int keyLen = Short.toUnsignedInt(bb.getShort());
|
|
if (keyLen <= 0) throw new IllegalArgumentException("paramKeyLen is 0");
|
|
if (bb.remaining() < keyLen + 2) throw new IllegalArgumentException("UserParam key payload too short");
|
|
|
|
byte[] keyBytes = new byte[keyLen];
|
|
bb.get(keyBytes);
|
|
|
|
int valLen = Short.toUnsignedInt(bb.getShort());
|
|
if (valLen <= 0) throw new IllegalArgumentException("paramValueLen is 0");
|
|
if (bb.remaining() < valLen) throw new IllegalArgumentException("UserParam value payload too short");
|
|
|
|
byte[] valBytes = new byte[valLen];
|
|
bb.get(valBytes);
|
|
|
|
if (bb.remaining() != 0) throw new IllegalArgumentException("Unexpected tail bytes, remaining=" + bb.remaining());
|
|
|
|
this.paramKey = strictUtf8(keyBytes, "paramKey");
|
|
this.paramValue = strictUtf8(valBytes, "paramValue");
|
|
|
|
if (this.paramKey.isBlank()) throw new IllegalArgumentException("paramKey is blank");
|
|
if (this.paramValue.isBlank()) throw new IllegalArgumentException("paramValue is blank");
|
|
}
|
|
|
|
public UserParamBody(int prevProfileBlockNumber,
|
|
byte[] prevProfileBlockHash32,
|
|
int profileSequence,
|
|
String paramKey,
|
|
String paramValue) {
|
|
this.contentOnly = false;
|
|
|
|
Objects.requireNonNull(paramKey, "paramKey == null");
|
|
Objects.requireNonNull(paramValue, "paramValue == null");
|
|
|
|
this.subType = MsgSubType.USER_PARAM_TEXT_TEXT;
|
|
this.version = VER;
|
|
|
|
this.prevProfileBlockNumber = prevProfileBlockNumber;
|
|
this.prevProfileBlockHash32 = (prevProfileBlockHash32 == null ? new byte[32] : Arrays.copyOf(prevProfileBlockHash32, 32));
|
|
this.profileSequence = profileSequence;
|
|
|
|
if (paramKey.isBlank()) throw new IllegalArgumentException("paramKey is blank");
|
|
if (paramValue.isBlank()) throw new IllegalArgumentException("paramValue is blank");
|
|
|
|
this.paramKey = paramKey;
|
|
this.paramValue = paramValue;
|
|
}
|
|
|
|
@Override
|
|
public UserParamBody check() {
|
|
if ((subType & 0xFFFF) != (MsgSubType.USER_PARAM_TEXT_TEXT & 0xFFFF)) {
|
|
throw new IllegalArgumentException("Bad UserParam subType: " + (subType & 0xFFFF));
|
|
}
|
|
if (prevProfileBlockNumber < 0) throw new IllegalArgumentException("prevProfileBlockNumber must be >=0");
|
|
if (prevProfileBlockHash32 == null || prevProfileBlockHash32.length != 32) throw new IllegalArgumentException("prevProfileBlockHash32 invalid");
|
|
if (!contentOnly && profileSequence <= 0) throw new IllegalArgumentException("profileSequence must be >=1");
|
|
|
|
if (paramKey == null || paramKey.isBlank()) throw new IllegalArgumentException("paramKey is blank");
|
|
if (paramValue == null || paramValue.isBlank()) throw new IllegalArgumentException("paramValue is blank");
|
|
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public byte[] toBytes() {
|
|
check();
|
|
byte[] keyUtf8 = paramKey.getBytes(StandardCharsets.UTF_8);
|
|
byte[] valUtf8 = paramValue.getBytes(StandardCharsets.UTF_8);
|
|
|
|
if (keyUtf8.length == 0 || keyUtf8.length > 65535) throw new IllegalArgumentException("paramKey utf8 len must be 1..65535");
|
|
if (valUtf8.length == 0 || valUtf8.length > 65535) throw new IllegalArgumentException("paramValue utf8 len must be 1..65535");
|
|
|
|
int cap = (contentOnly ? 0 : 40)
|
|
+ 2 + keyUtf8.length
|
|
+ 2 + valUtf8.length;
|
|
|
|
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
|
|
if (!contentOnly) {
|
|
bb.putInt(prevProfileBlockNumber);
|
|
bb.put(prevProfileBlockHash32);
|
|
bb.putInt(profileSequence);
|
|
}
|
|
bb.putShort((short) keyUtf8.length);
|
|
bb.put(keyUtf8);
|
|
bb.putShort((short) valUtf8.length);
|
|
bb.put(valUtf8);
|
|
return bb.array();
|
|
}
|
|
|
|
private static String strictUtf8(byte[] bytes, String fieldName) {
|
|
var decoder = StandardCharsets.UTF_8.newDecoder()
|
|
.onMalformedInput(CodingErrorAction.REPORT)
|
|
.onUnmappableCharacter(CodingErrorAction.REPORT);
|
|
|
|
try {
|
|
return decoder.decode(ByteBuffer.wrap(bytes)).toString();
|
|
} catch (CharacterCodingException e) {
|
|
throw new IllegalArgumentException(fieldName + " is not valid UTF-8", e);
|
|
}
|
|
}
|
|
|
|
/* USER_PROFILE имеет единственную линию, логический lineCode всегда 0. */
|
|
@Override public int lineCode() { return 0; }
|
|
@Override public int prevLineBlockGlobalNumber() { return prevProfileBlockNumber; }
|
|
@Override public byte[] prevLineBlockHash32() { return Arrays.copyOf(prevProfileBlockHash32, 32); }
|
|
@Override public int lineSeq() { return profileSequence; }
|
|
}
|