Files
SHiNE-server/SHiNE-server/shine-server-blockchain/src/main/java/blockchain/BchCryptoVerifier.java
T

35 lines
1.5 KiB
Java

package blockchain;
import java.security.MessageDigest;
import java.util.Objects;
/** Cryptographic checks for SHiNE Frame v1 + surrounding ANS-104 DataItem. */
public final class BchCryptoVerifier {
private BchCryptoVerifier() {}
public static byte[] sha256(byte[] data) {
Objects.requireNonNull(data, "data == null");
try { return MessageDigest.getInstance("SHA-256").digest(data); }
catch (Exception e) { throw new IllegalStateException("SHA-256 unavailable", e); }
}
/**
* Verifies that the ANS-104 owner is the current blockchain key and that the
* ANS-104 Ed25519 signature is valid. The SHiNE block hash itself remains
* SHA-256(Frame v1 bytes) and is intentionally independent of Arweave.
*/
public static boolean verifyBlock(BchBlockEntry block, byte[] publicKey32) {
Objects.requireNonNull(block, "block == null");
Objects.requireNonNull(publicKey32, "publicKey32 == null");
if (publicKey32.length != 32) throw new IllegalArgumentException("publicKey32 != 32");
if (block.isCoreContent()) {
try {
CoreContentTransport.verifyPair(block.getCoreDataItem(), block.getDataItemId32(),
block.getContentDataItem(), publicKey32, java.time.Instant.now().getEpochSecond());
return true;
} catch (IllegalArgumentException e) { return false; }
}
return block.getDataItem().ownerEquals(publicKey32) && block.getDataItem().verifySignature();
}
}