package blockchain; import blockchain.body.BodyRecord; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.time.Instant; import java.util.Arrays; import java.util.Objects; /** * SHiNE user block, Frame v1, carried as the data payload of an ANS-104 DataItem. * * The constructor accepts the complete serialized ANS-104 DataItem because this is * exactly what is stored in PostgreSQL and exchanged between servers. * * SHiNE Frame v1 (all integer fields BigEndian): * [2] frameCode = 0x0001 * [32] prevHash32 = SHA-256(Frame v1 bytes of previous SHiNE block) * [32] back32Hash32 = SHA-256(Frame v1 bytes of block N-32), or zero for N<32 * [4] blockSize = exact Frame v1 byte length * [4] blockNumber * [8] timestamp unix seconds * [2] type * [2] subType * [2] version * [N] bodyBytes * * The user signature is NOT stored in this frame. It is the single Ed25519/Solana * signature of the surrounding ANS-104 DataItem. */ public final class BchBlockEntry { public static final int HASH_LEN = 32; public static final int FRAME_CODE_V1 = 0x0001; public static final int MAX_BLOCK_FULL_BYTES = Ans104DataItem.MAX_DATA_ITEM_BYTES; public static final long MAX_FUTURE_SECONDS = 60; public static final int FRAME_HEADER_SIZE = 2 + 32 + 32 + 4 + 4 + 8 + 2 + 2 + 2; public final int frameCode; public final byte[] prevHash32; public final byte[] back32Hash32; public final int blockSize; public final int blockNumber; public final long timestamp; public final short type; public final short subType; public final short version; public final byte[] bodyBytes; public final BodyRecord body; private final Ans104DataItem dataItem; private final byte[] frameBytes; private final byte[] hash32; /** Parse a complete serialized ANS-104 DataItem containing one SHiNE Frame v1. */ public BchBlockEntry(byte[] fullDataItemBytes) { Objects.requireNonNull(fullDataItemBytes, "fullDataItemBytes == null"); this.dataItem = new Ans104DataItem(fullDataItemBytes); this.frameBytes = dataItem.data(); if (frameBytes.length < FRAME_HEADER_SIZE) { throw new IllegalArgumentException("SHiNE Frame v1 too short: " + frameBytes.length); } ByteBuffer bb = ByteBuffer.wrap(frameBytes).order(ByteOrder.BIG_ENDIAN); this.frameCode = Short.toUnsignedInt(bb.getShort()); if (frameCode != FRAME_CODE_V1) { throw new IllegalArgumentException(String.format("Bad SHiNE frameCode: 0x%04X (expected 0x%04X)", frameCode, FRAME_CODE_V1)); } this.prevHash32 = new byte[32]; bb.get(prevHash32); this.back32Hash32 = new byte[32]; bb.get(back32Hash32); this.blockSize = bb.getInt(); if (blockSize != frameBytes.length || blockSize < FRAME_HEADER_SIZE) { throw new IllegalArgumentException("SHiNE blockSize mismatch: declared=" + blockSize + " actual=" + frameBytes.length); } this.blockNumber = bb.getInt(); if (blockNumber < 0) throw new IllegalArgumentException("blockNumber < 0: " + blockNumber); this.timestamp = bb.getLong(); long now = Instant.now().getEpochSecond(); if (timestamp > now + MAX_FUTURE_SECONDS) { throw new IllegalArgumentException("timestamp is too far in future: ts=" + timestamp + " now=" + now); } this.type = bb.getShort(); this.subType = bb.getShort(); this.version = bb.getShort(); this.bodyBytes = new byte[bb.remaining()]; bb.get(bodyBytes); this.body = BodyRecordParser.parse(type, subType, version, bodyBytes); this.hash32 = BchCryptoVerifier.sha256(frameBytes); } public Ans104DataItem getDataItem() { return dataItem; } public byte[] getFrameBytes() { return Arrays.copyOf(frameBytes, frameBytes.length); } /** Kept as an alias for code/docs that called the old field preimage. */ public byte[] getPreimageBytes() { return getFrameBytes(); } public byte[] getSignature64() { return dataItem.signature64(); } public byte[] getOwner32() { return dataItem.owner32(); } public byte[] getDataItemId32() { return dataItem.id32(); } public byte[] getHash32() { return Arrays.copyOf(hash32, hash32.length); } /** Full serialized ANS-104 DataItem. */ public byte[] toBytes() { return dataItem.rawBytes(); } @Override public String toString() { return "BchBlockEntry{" + "frameCode=0x" + Integer.toHexString(frameCode) + ", blockNumber=" + blockNumber + ", timestamp=" + timestamp + ", type=" + (type & 0xffff) + ", subType=" + (subType & 0xffff) + ", version=" + (version & 0xffff) + ", bodyLen=" + bodyBytes.length + ", dataItemId=" + toHex(getDataItemId32()) + ", blockHash=" + toHex(hash32) + '}'; } private static String toHex(byte[] bytes) { char[] h = "0123456789abcdef".toCharArray(); char[] out = new char[bytes.length * 2]; for (int i = 0; i < bytes.length; i++) { int v = bytes[i] & 0xff; out[i * 2] = h[v >>> 4]; out[i * 2 + 1] = h[v & 15]; } return new String(out); } }