use of co.rsk.bitcoinj.core.PartialMerkleTree in project rskj by rsksmart.
the class GenesisMerkleProofValidator method isValid.
@Override
public boolean isValid(Sha256Hash expectedRoot, Sha256Hash coinbaseHash) {
PartialMerkleTree merkleTree = new PartialMerkleTree(bitcoinNetworkParameters, pmtSerialized, 0);
List<Sha256Hash> txHashes = new ArrayList<>();
Sha256Hash root = merkleTree.getTxnHashAndMerkleRoot(txHashes);
return root.equals(expectedRoot) && txHashes.contains(coinbaseHash);
}
use of co.rsk.bitcoinj.core.PartialMerkleTree in project rskj by rsksmart.
the class BridgeUtilsTest method calculateMerkleRoot_hashes_in_pmt.
@Test
public void calculateMerkleRoot_hashes_in_pmt() {
BtcTransaction tx = new BtcTransaction(networkParameters);
byte[] bits = new byte[1];
bits[0] = 0x5;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(Sha256Hash.ZERO_HASH);
hashes.add(tx.getHash());
PartialMerkleTree pmt = new PartialMerkleTree(networkParameters, bits, hashes, 2);
Sha256Hash merkleRoot = BridgeUtils.calculateMerkleRoot(networkParameters, pmt.bitcoinSerialize(), tx.getHash());
Assert.assertNotNull(merkleRoot);
}
use of co.rsk.bitcoinj.core.PartialMerkleTree in project rskj by rsksmart.
the class BridgeUtilsTest method calculateMerkleRoot_hashes_not_in_pmt.
@Test
public void calculateMerkleRoot_hashes_not_in_pmt() {
byte[] bits = new byte[1];
bits[0] = 0x01;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(PegTestUtils.createHash(2));
BtcTransaction tx = new BtcTransaction(networkParameters);
PartialMerkleTree pmt = new PartialMerkleTree(networkParameters, bits, hashes, 1);
Assert.assertNull(BridgeUtils.calculateMerkleRoot(networkParameters, pmt.bitcoinSerialize(), tx.getHash()));
}
use of co.rsk.bitcoinj.core.PartialMerkleTree in project rskj by rsksmart.
the class BridgeSupport method registerBtcCoinbaseTransaction.
public void registerBtcCoinbaseTransaction(byte[] btcTxSerialized, Sha256Hash blockHash, byte[] pmtSerialized, Sha256Hash witnessMerkleRoot, byte[] witnessReservedValue) throws VMException {
Context.propagate(btcContext);
try {
this.ensureBtcBlockStore();
} catch (BlockStoreException | IOException e) {
logger.warn("Exception in registerBtcCoinbaseTransaction", e);
throw new VMException("Exception in registerBtcCoinbaseTransaction", e);
}
Sha256Hash btcTxHash = BtcTransactionFormatUtils.calculateBtcTxHash(btcTxSerialized);
if (witnessReservedValue.length != 32) {
logger.warn("[btcTx:{}] WitnessResevedValue length can't be different than 32 bytes", btcTxHash);
throw new BridgeIllegalArgumentException("WitnessResevedValue length can't be different than 32 bytes");
}
if (!PartialMerkleTreeFormatUtils.hasExpectedSize(pmtSerialized)) {
logger.warn("[btcTx:{}] PartialMerkleTree doesn't have expected size", btcTxHash);
throw new BridgeIllegalArgumentException("PartialMerkleTree doesn't have expected size");
}
Sha256Hash merkleRoot;
try {
PartialMerkleTree pmt = new PartialMerkleTree(bridgeConstants.getBtcParams(), pmtSerialized, 0);
List<Sha256Hash> hashesInPmt = new ArrayList<>();
merkleRoot = pmt.getTxnHashAndMerkleRoot(hashesInPmt);
if (!hashesInPmt.contains(btcTxHash)) {
logger.warn("Supplied Btc Tx {} is not in the supplied partial merkle tree", btcTxHash);
return;
}
} catch (VerificationException e) {
logger.warn("[btcTx:{}] PartialMerkleTree could not be parsed", btcTxHash);
throw new BridgeIllegalArgumentException(String.format("PartialMerkleTree could not be parsed %s", ByteUtil.toHexString(pmtSerialized)), e);
}
// Check merkle root equals btc block merkle root at the specified height in the btc best chain
// Btc blockstore is available since we've already queried the best chain height
StoredBlock storedBlock = btcBlockStore.getFromCache(blockHash);
if (storedBlock == null) {
logger.warn("[btcTx:{}] Block not registered", btcTxHash);
throw new BridgeIllegalArgumentException(String.format("Block not registered %s", blockHash.toString()));
}
BtcBlock blockHeader = storedBlock.getHeader();
if (!blockHeader.getMerkleRoot().equals(merkleRoot)) {
String panicMessage = String.format("Btc Tx %s Supplied merkle root %s does not match block's merkle root %s", btcTxHash.toString(), merkleRoot, blockHeader.getMerkleRoot());
logger.warn(panicMessage);
panicProcessor.panic("btclock", panicMessage);
return;
}
BtcTransaction btcTx = new BtcTransaction(bridgeConstants.getBtcParams(), btcTxSerialized);
btcTx.verify();
Sha256Hash witnessCommitment = Sha256Hash.twiceOf(witnessMerkleRoot.getReversedBytes(), witnessReservedValue);
if (!witnessCommitment.equals(btcTx.findWitnessCommitment())) {
logger.warn("[btcTx:{}] WitnessCommitment does not match", btcTxHash);
throw new BridgeIllegalArgumentException("WitnessCommitment does not match");
}
CoinbaseInformation coinbaseInformation = new CoinbaseInformation(witnessMerkleRoot);
provider.setCoinbaseInformation(blockHeader.getHash(), coinbaseInformation);
logger.warn("[btcTx:{}] Registered coinbase information", btcTxHash);
}
Aggregations