All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jd.blockchain.ledger.MerkleProofVerifier Maven / Gradle / Ivy

There is a newer version: 1.6.5.RELEASE
Show newest version
package com.jd.blockchain.ledger;

import com.jd.blockchain.crypto.Crypto;
import com.jd.blockchain.crypto.HashDigest;
import com.jd.blockchain.crypto.HashFunction;

import utils.io.BytesUtils;

public class MerkleProofVerifier {

	public static boolean verify(MerkleProof proof) {
		HashDigest parentHash = proof.getRootHash();
		MerkleProofLevel[] levels = proof.getProofLevels();
		HashDigest[] childHashs;
		for (MerkleProofLevel level : levels) {
			childHashs = level.getHashNodes();
			if (!verify(parentHash, childHashs)) {
				return false;
			}
			parentHash = childHashs[level.getProofPoint()];
		}
		
		return parentHash.equals(proof.getDataHash());
	}

	private static boolean verify(HashDigest parentHash, HashDigest[] childHashs) {
		HashFunction hashFunc = Crypto.getHashFunction(parentHash.getAlgorithm());
		byte[] bytes = BytesUtils.concat(childHashs);
		HashDigest hash = hashFunc.hash(bytes);
		return hash.equals(parentHash);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy