use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.
the class BridgeUtilsTest method getRegularPegoutTxSize_has_proper_calculations.
@Test
public void getRegularPegoutTxSize_has_proper_calculations() {
BtcECKey key1 = new BtcECKey();
BtcECKey key2 = new BtcECKey();
BtcECKey key3 = new BtcECKey();
List<BtcECKey> keys = Arrays.asList(key1, key2, key3);
Federation fed = new Federation(FederationMember.getFederationMembersFromKeys(keys), Instant.now(), 0, networkParameters);
// Create a pegout tx with two inputs and two outputs
int inputs = 2;
BtcTransaction pegoutTx = createPegOutTx(Collections.emptyList(), inputs, fed, false);
for (int inputIndex = 0; inputIndex < inputs; inputIndex++) {
Script inputScript = pegoutTx.getInput(inputIndex).getScriptSig();
Sha256Hash sighash = pegoutTx.hashForSignature(inputIndex, fed.getRedeemScript(), BtcTransaction.SigHash.ALL, false);
for (int keyIndex = 0; keyIndex < keys.size() - 1; keyIndex++) {
BtcECKey key = keys.get(keyIndex);
BtcECKey.ECDSASignature sig = key.sign(sighash);
TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
byte[] txSigEncoded = txSig.encodeToBitcoin();
int sigIndex = inputScript.getSigInsertionIndex(sighash, key);
inputScript = ScriptBuilder.updateScriptWithSignature(inputScript, txSigEncoded, sigIndex, 1, 1);
pegoutTx.getInput(inputIndex).setScriptSig(inputScript);
}
}
int pegoutTxSize = BridgeUtils.getRegularPegoutTxSize(fed);
// The difference between the calculated size and a real tx size should be smaller than 10% in any direction
int difference = pegoutTx.bitcoinSerialize().length - pegoutTxSize;
double tolerance = pegoutTxSize * .1;
assertTrue(difference < tolerance && difference > -tolerance);
}
use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.
the class BridgeUtilsTest method isInputSignedByThisFederator_notSigned.
@Test
public void isInputSignedByThisFederator_notSigned() {
// Arrange
BtcECKey federator1Key = new BtcECKey();
BtcECKey federator2Key = new BtcECKey();
Federation federation = new Federation(FederationMember.getFederationMembersFromKeys(Arrays.asList(federator1Key, federator2Key)), Instant.now(), 0, networkParameters);
// Create a tx from the Fed to a random btc address
BtcTransaction tx = new BtcTransaction(networkParameters);
TransactionInput txInput = new TransactionInput(networkParameters, tx, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
// Create script to be signed by federation members
Script inputScript = PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation);
txInput.setScriptSig(inputScript);
tx.addInput(txInput);
List<ScriptChunk> chunks = inputScript.getChunks();
byte[] program = chunks.get(chunks.size() - 1).data;
Script redeemScript = new Script(program);
Sha256Hash sighash = tx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
// Act
boolean isSigned = BridgeUtils.isInputSignedByThisFederator(federator1Key, sighash, txInput);
// Assert
Assert.assertFalse(isSigned);
}
use of co.rsk.bitcoinj.core.Sha256Hash 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.Sha256Hash 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.Sha256Hash 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);
}
Aggregations