SHA256
238 lines
13 KiB
Java
238 lines
13 KiB
Java
package blockchain.body;
|
|
|
|
import utils.config.ShineSignatureConstants;
|
|
|
|
import java.nio.ByteBuffer;
|
|
import java.nio.ByteOrder;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Arrays;
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* HeaderBody — TECH type=0, subType=0; version=1 legacy / version=2 license / version=3 identity.
|
|
*
|
|
* bodyBytes (BigEndian):
|
|
* [TAG_LEN] tag ASCII
|
|
* [1] loginLength (uint8)
|
|
* [N] login UTF-8
|
|
* [1] accountType (v3 only, 0 alternative / 1 primary)
|
|
* [2+N] previous accounts CSV (v3 primary only, u16 length)
|
|
* [1] licenseLength (uint8, version>=2)
|
|
* [M] license UTF-8 (version>=2)
|
|
* Historical v2: fixed ASCII "license:CC0-1.0" instead of length + text.
|
|
* [32] initialBlockchainKey32
|
|
*
|
|
* initialBlockchainKey32 is the public blockchain-signing key that created fork #1.
|
|
* It is historical identity metadata and MUST remain byte-for-byte unchanged when a
|
|
* preserved prefix (including block #0) is republished into later forks.
|
|
*/
|
|
public final class HeaderBody implements BodyRecord {
|
|
public static final short TYPE = 0;
|
|
public static final short VER = 1; // Legacy Frame v1 and new CONTENT v1 (without CC0)
|
|
public static final short VER_LICENSE = 2;
|
|
public static final short VER_IDENTITY = 3;
|
|
public static final int ALTERNATIVE = 0, PRIMARY = 1;
|
|
public static final short VER_CC0 = VER_LICENSE; // historical source compatibility
|
|
public static final String LICENSE_CC0 = "CC0-1.0";
|
|
public static final String LICENSE_FIELD_CC0 = "license:" + LICENSE_CC0;
|
|
private static final byte[] LICENSE_BYTES = LICENSE_FIELD_CC0.getBytes(StandardCharsets.US_ASCII);
|
|
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
|
public static final short SUBTYPE_COMPAT = 0;
|
|
public static final String TAG = ShineSignatureConstants.BLOCKCHAIN_HEADER_TAG;
|
|
|
|
private static final byte[] TAG_ASCII = TAG.getBytes(StandardCharsets.US_ASCII);
|
|
private static final int TAG_LEN = TAG_ASCII.length;
|
|
|
|
public final short subType;
|
|
public final short version;
|
|
public final String tag;
|
|
public final String login;
|
|
public final byte[] initialBlockchainKey32;
|
|
public final Integer accountType; // null for legacy HEADER
|
|
public final java.util.List<String> previousAccounts;
|
|
public final String license; // null for legacy HEADER v1
|
|
private final boolean legacyCc0Encoding;
|
|
|
|
public HeaderBody(short subType, short version, byte[] bodyBytes) {
|
|
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
|
this.subType = subType;
|
|
this.version = version;
|
|
if ((subType & 0xFFFF) != 0) throw new IllegalArgumentException("HeaderBody subType must be 0");
|
|
if (version != VER && version != VER_CC0 && version != VER_IDENTITY) throw new IllegalArgumentException("HeaderBody version must be 1, 2 or 3");
|
|
if (bodyBytes.length < TAG_LEN + 1 + 1 + 32 + (version == VER_LICENSE ? 1 : 0))
|
|
throw new IllegalArgumentException("HeaderBody too short");
|
|
|
|
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
|
byte[] tagBytes = new byte[TAG_LEN];
|
|
bb.get(tagBytes);
|
|
String parsedTag = new String(tagBytes, StandardCharsets.US_ASCII);
|
|
if (!TAG.equals(parsedTag)) throw new IllegalArgumentException("Bad tag: " + parsedTag);
|
|
this.tag = parsedTag;
|
|
|
|
int loginLen = Byte.toUnsignedInt(bb.get());
|
|
if (loginLen <= 0 || bb.remaining() < loginLen + 32 + (version == VER_LICENSE ? 1 : 0))
|
|
throw new IllegalArgumentException("Bad HeaderBody login/key/license length");
|
|
byte[] loginBytes = new byte[loginLen];
|
|
bb.get(loginBytes);
|
|
this.login = BodyTargetSupport.decodeStrictUtf8(loginBytes, "HeaderBody login");
|
|
if (version == VER_IDENTITY) {
|
|
if (bb.remaining() < 34) throw new IllegalArgumentException("Truncated HEADER identity");
|
|
this.accountType = Byte.toUnsignedInt(bb.get());
|
|
if (accountType == PRIMARY) {
|
|
if (bb.remaining() < 35) throw new IllegalArgumentException("Truncated HEADER previous accounts");
|
|
int size = Short.toUnsignedInt(bb.getShort());
|
|
if (size > bb.remaining() - 33) throw new IllegalArgumentException("Bad HEADER previous accounts length");
|
|
byte[] previous = new byte[size]; bb.get(previous);
|
|
String csv = BodyTargetSupport.decodeStrictUtf8(previous, "HEADER previous accounts");
|
|
this.previousAccounts = csv.isEmpty() ? java.util.List.of() : java.util.List.of(csv.split(",", -1));
|
|
} else {
|
|
this.previousAccounts = java.util.List.of();
|
|
}
|
|
int size = Byte.toUnsignedInt(bb.get());
|
|
if (bb.remaining() != size + 32) throw new IllegalArgumentException("Bad HEADER license length");
|
|
byte[] field = new byte[size]; bb.get(field);
|
|
this.license = BodyTargetSupport.decodeStrictUtf8(field, "HEADER license");
|
|
this.legacyCc0Encoding = false;
|
|
} else if (version == VER_LICENSE) {
|
|
this.accountType = null;
|
|
this.previousAccounts = java.util.List.of();
|
|
int fieldLength = bb.remaining() - 32;
|
|
if (fieldLength > 256) throw new IllegalArgumentException("HEADER license too long");
|
|
byte[] field = new byte[fieldLength];
|
|
bb.get(field);
|
|
legacyCc0Encoding = Arrays.equals(field, LICENSE_BYTES);
|
|
if (legacyCc0Encoding) {
|
|
this.license = LICENSE_CC0;
|
|
} else {
|
|
int length = Byte.toUnsignedInt(field[0]);
|
|
if (fieldLength != 1 + length) throw new IllegalArgumentException("Bad HEADER license length");
|
|
this.license = BodyTargetSupport.decodeStrictUtf8(Arrays.copyOfRange(field, 1, fieldLength), "HEADER license");
|
|
}
|
|
} else {
|
|
this.accountType = null;
|
|
this.previousAccounts = java.util.List.of();
|
|
legacyCc0Encoding = false;
|
|
this.license = null;
|
|
}
|
|
this.initialBlockchainKey32 = new byte[32];
|
|
bb.get(this.initialBlockchainKey32);
|
|
requireNonZeroKey(this.initialBlockchainKey32);
|
|
if (bb.hasRemaining()) throw new IllegalArgumentException("Unexpected HeaderBody tail");
|
|
}
|
|
|
|
// Legacy constructor is kept for historical fixtures and importing v1 blocks.
|
|
public HeaderBody(String login, byte[] initialBlockchainKey32) {
|
|
this(login, initialBlockchainKey32, VER, null, false);
|
|
}
|
|
|
|
/** HEADER для CORE v2 / CONTENT v1: сохраняет login и начальный ключ, без поля лицензии. */
|
|
public static HeaderBody forCoreContent(String login, byte[] initialBlockchainKey32) {
|
|
return new HeaderBody(login, initialBlockchainKey32, VER, null, false);
|
|
}
|
|
|
|
public static HeaderBody licensedCc0(String login, byte[] initialBlockchainKey32) {
|
|
return new HeaderBody(login, initialBlockchainKey32, VER_LICENSE, LICENSE_CC0, true);
|
|
}
|
|
|
|
/** Новый HEADER v2: произвольная строка лицензии, включая пустую. */
|
|
public static HeaderBody licensed(String login, byte[] initialBlockchainKey32, String license) {
|
|
return new HeaderBody(login, initialBlockchainKey32, VER_LICENSE, Objects.requireNonNull(license), false);
|
|
}
|
|
|
|
public static HeaderBody identity(String login, byte[] initialBlockchainKey32, String license,
|
|
int accountType, java.util.List<String> previousAccounts) {
|
|
return new HeaderBody(login, initialBlockchainKey32, VER_IDENTITY, license, false, accountType, previousAccounts);
|
|
}
|
|
|
|
private HeaderBody(String login, byte[] initialBlockchainKey32, short version, String license, boolean legacyCc0Encoding) {
|
|
this(login, initialBlockchainKey32, version, license, legacyCc0Encoding, null, java.util.List.of());
|
|
}
|
|
|
|
private HeaderBody(String login, byte[] initialBlockchainKey32, short version, String license, boolean legacyCc0Encoding,
|
|
Integer accountType, java.util.List<String> previousAccounts) {
|
|
Objects.requireNonNull(login, "login == null");
|
|
Objects.requireNonNull(initialBlockchainKey32, "initialBlockchainKey32 == null");
|
|
this.subType = SUBTYPE_COMPAT;
|
|
this.version = version;
|
|
this.tag = TAG;
|
|
this.login = login;
|
|
this.initialBlockchainKey32 = Arrays.copyOf(initialBlockchainKey32, initialBlockchainKey32.length);
|
|
this.license = license;
|
|
this.accountType = accountType;
|
|
this.previousAccounts = java.util.List.copyOf(previousAccounts);
|
|
this.legacyCc0Encoding = legacyCc0Encoding;
|
|
check();
|
|
}
|
|
|
|
@Override
|
|
public HeaderBody check() {
|
|
if ((subType & 0xFFFF) != 0) throw new IllegalArgumentException("HeaderBody subType must be 0");
|
|
if (version != VER && version != VER_CC0 && version != VER_IDENTITY) throw new IllegalArgumentException("Bad HeaderBody version");
|
|
if (version != VER && (license == null || !StandardCharsets.UTF_8.newEncoder().canEncode(license)
|
|
|| license.getBytes(StandardCharsets.UTF_8).length > 255))
|
|
throw new IllegalArgumentException("HEADER license must be 0..255 UTF-8 bytes");
|
|
if (version == VER && license != null) throw new IllegalArgumentException("Legacy HEADER must not contain a license");
|
|
if (login == null || login.isBlank()) throw new IllegalArgumentException("Login is blank");
|
|
byte[] loginUtf8 = login.getBytes(StandardCharsets.UTF_8);
|
|
// В HEADER хранится неизменяемое написание зарегистрированного login@namespace.
|
|
// Namespace остаётся lowercase, регистр локальной части сохраняется байт-в-байт.
|
|
boolean legacyLogin = login.matches("^[A-Za-z0-9_]+$");
|
|
boolean namespacedLogin = loginUtf8.length <= 30 && login.matches("^[A-Za-z0-9_]+@[a-z0-9_]+$");
|
|
if (!legacyLogin && !namespacedLogin) {
|
|
throw new IllegalArgumentException("Login must be legacy [A-Za-z0-9_]+ or registered-case login@namespace (max 30 bytes)");
|
|
}
|
|
if (loginUtf8.length == 0 || loginUtf8.length > 255) throw new IllegalArgumentException("Login utf8 len must be 1..255");
|
|
if (version == VER_IDENTITY) {
|
|
if (accountType == null || (accountType != ALTERNATIVE && accountType != PRIMARY))
|
|
throw new IllegalArgumentException("Bad HEADER account type");
|
|
if (previousAccounts.size() > 10 || (accountType == ALTERNATIVE && !previousAccounts.isEmpty()))
|
|
throw new IllegalArgumentException("Bad HEADER previous account count");
|
|
java.util.Set<String> seen = new java.util.HashSet<>();
|
|
seen.add(login.toLowerCase(java.util.Locale.ROOT));
|
|
for (String prev : previousAccounts) {
|
|
if (!(prev.matches("^[A-Za-z0-9_]{1,255}$") || (prev.length() <= 30 && prev.matches("^[A-Za-z0-9_]+@[a-z0-9_]+$")))
|
|
|| !seen.add(prev.toLowerCase(java.util.Locale.ROOT)))
|
|
throw new IllegalArgumentException("Bad, duplicate or self HEADER previous account");
|
|
}
|
|
}
|
|
requireNonZeroKey(initialBlockchainKey32);
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public byte[] toBytes() {
|
|
check();
|
|
byte[] loginUtf8 = login.getBytes(StandardCharsets.UTF_8);
|
|
byte[] licenseUtf8 = version != VER ? license.getBytes(StandardCharsets.UTF_8) : new byte[0];
|
|
int licenseSize = version != VER ? (legacyCc0Encoding ? LICENSE_BYTES.length : 1 + licenseUtf8.length) : 0;
|
|
byte[] previous = String.join(",", previousAccounts).getBytes(StandardCharsets.UTF_8);
|
|
int identitySize = version == VER_IDENTITY ? 1 + (accountType == PRIMARY ? 2 + previous.length : 0) : 0;
|
|
ByteBuffer bb = ByteBuffer.allocate(TAG_LEN + 1 + loginUtf8.length + 32 + licenseSize + identitySize).order(ByteOrder.BIG_ENDIAN);
|
|
bb.put(TAG_ASCII);
|
|
bb.put((byte) loginUtf8.length);
|
|
bb.put(loginUtf8);
|
|
if (version == VER_IDENTITY) {
|
|
bb.put((byte)(int)accountType);
|
|
if (accountType == PRIMARY) bb.putShort((short)previous.length).put(previous);
|
|
}
|
|
if (version != VER) {
|
|
if (legacyCc0Encoding) bb.put(LICENSE_BYTES);
|
|
else bb.put((byte) licenseUtf8.length).put(licenseUtf8);
|
|
}
|
|
bb.put(initialBlockchainKey32);
|
|
return bb.array();
|
|
}
|
|
|
|
private static void requireNonZeroKey(byte[] key) {
|
|
if (key == null || key.length != 32) throw new IllegalArgumentException("initialBlockchainKey32 must be 32 bytes");
|
|
boolean any = false;
|
|
for (byte b : key) any |= b != 0;
|
|
if (!any) throw new IllegalArgumentException("initialBlockchainKey32 must not be zero");
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "HeaderBody{version=" + version + ", login='" + login + "', license=" + license + ", initialBlockchainKey32=<32 bytes>}";
|
|
}
|
|
}
|