use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.
the class BridgeUtilsTest method isInputSignedByThisFederator_isSigned.
@Test
public void isInputSignedByThisFederator_isSigned() {
// 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);
BtcECKey.ECDSASignature sig = federator1Key.sign(sighash);
TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
byte[] txSigEncoded = txSig.encodeToBitcoin();
int sigIndex = inputScript.getSigInsertionIndex(sighash, federator1Key);
inputScript = ScriptBuilder.updateScriptWithSignature(inputScript, txSigEncoded, sigIndex, 1, 1);
txInput.setScriptSig(inputScript);
// Act
boolean isSigned = BridgeUtils.isInputSignedByThisFederator(federator1Key, sighash, txInput);
// Assert
Assert.assertTrue(isSigned);
}
use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.
the class BridgeUtilsTest method signWithNKeys.
private void signWithNKeys(Federation federation, Script federationRedeemScript, List<BtcECKey> privateKeys, TransactionInput txIn, BtcTransaction tx, int numberOfSignatures) {
Script scriptPubKey = federation.getP2SHScript();
RedeemData redeemData = RedeemData.of(federation.getBtcPublicKeys(), federationRedeemScript);
Script inputScript = scriptPubKey.createEmptyInputScript(redeemData.keys.get(0), redeemData.redeemScript);
txIn.setScriptSig(inputScript);
Sha256Hash sighash = tx.hashForSignature(0, federationRedeemScript, BtcTransaction.SigHash.ALL, false);
for (int i = 0; i < numberOfSignatures; i++) {
inputScript = signWithOneKey(federation, privateKeys, inputScript, sighash, i);
}
txIn.setScriptSig(inputScript);
}
use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.
the class RepositoryBtcBlockStoreWithCacheTest method checkDifferentInstancesWithSameRepoHaveSameContentTest.
@Test
public void checkDifferentInstancesWithSameRepoHaveSameContentTest() throws Exception {
// This Is how I produced RepositoryBlockStore_data.ser. I had a bitcoind in regtest with 613 blocks + genesis block
// NetworkParameters params = RegTestParams.get();
// Context context = new Context(params);
// Wallet wallet = new Wallet(context);
// BlockStore store = new SPVBlockStore(params, new File("spvBlockstore"));
// AbstractBlockChain chain = new BlockChain(context, wallet, store);
// PeerGroup peerGroup = new PeerGroup(context, chain);
// peerGroup.start();
// final DownloadProgressTracker listener = new DownloadProgressTracker();
// peerGroup.startBlockChainDownload(listener);
// listener.await();
// peerGroup.stop();
// StoredBlock storedBlock = chain.getChainHead();
// FileOutputStream fos = new FileOutputStream("RepositoryBlockStore_data.ser");
// ObjectOutputStream oos = new ObjectOutputStream(fos);
// for (int i = 0; i < 614; i++) {
// Triple<byte[], BigInteger , Integer> tripleStoredBlock = new ImmutableTriple<>(storedBlock.getHeader().bitcoinSerialize(), storedBlock.getChainWork(), storedBlock.getHeight());
// oos.writeObject(tripleStoredBlock);
// storedBlock = store.get(storedBlock.getHeader().getPrevBlockHash());
// }
// oos.close();
// Read original store
InputStream fileInputStream = ClassLoader.getSystemResourceAsStream("peg/RepositoryBlockStore_data.ser");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Repository repository = createRepository();
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams());
BtcBlockStoreWithCache store = btcBlockStoreFactory.newInstance(repository, bridgeConstants, mock(BridgeStorageProvider.class), mock(ActivationConfig.ForBlock.class));
for (int i = 0; i < 614; i++) {
Triple<byte[], BigInteger, Integer> tripleStoredBlock = (Triple<byte[], BigInteger, Integer>) objectInputStream.readObject();
BtcBlock header = RegTestParams.get().getDefaultSerializer().makeBlock(tripleStoredBlock.getLeft());
StoredBlock storedBlock = new StoredBlock(header, tripleStoredBlock.getMiddle(), tripleStoredBlock.getRight());
if (i == 0) {
store.setChainHead(storedBlock);
}
store.put(storedBlock);
}
// Create a new instance of the store
BtcBlockStoreWithCache store2 = btcBlockStoreFactory.newInstance(repository, bridgeConstants, mock(BridgeStorageProvider.class), mock(ActivationConfig.ForBlock.class));
// Check a specific block that used to fail when we had a bug
assertEquals(store.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")), store2.get(Sha256Hash.wrap("373941fe83961cf70e181e468abc5f9f7cc440c711c3d06948fa66f3912ed27a")));
// Check new instance content is identical to the original one
StoredBlock storedBlock = store.getChainHead();
StoredBlock storedBlock2 = store2.getChainHead();
int headHeight = storedBlock.getHeight();
for (int i = 0; i < headHeight; i++) {
assertNotNull(storedBlock);
assertEquals(storedBlock, storedBlock2);
Sha256Hash prevBlockHash = storedBlock.getHeader().getPrevBlockHash();
storedBlock = store.get(prevBlockHash);
storedBlock2 = store2.get(prevBlockHash);
}
}
use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.
the class BridgeUtilsTest method isInputSignedByThisFederator_isSignedByAnotherFederator.
@Test
public void isInputSignedByThisFederator_isSignedByAnotherFederator() {
// 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);
BtcECKey.ECDSASignature sig = federator1Key.sign(sighash);
TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
byte[] txSigEncoded = txSig.encodeToBitcoin();
int sigIndex = inputScript.getSigInsertionIndex(sighash, federator1Key);
inputScript = ScriptBuilder.updateScriptWithSignature(inputScript, txSigEncoded, sigIndex, 1, 1);
txInput.setScriptSig(inputScript);
// Act
boolean isSigned = BridgeUtils.isInputSignedByThisFederator(federator2Key, sighash, txInput);
// Assert
Assert.assertFalse(isSigned);
}
use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.
the class LockTest method isBtcTxHashAlreadyProcessed_no.
private void isBtcTxHashAlreadyProcessed_no(int times, ExecutionStats stats) throws VMException {
Sha256Hash hash = Sha256Hash.of(BigInteger.valueOf(new Random().nextLong()).toByteArray());
ABIEncoder abiEncoder = (int executionIndex) -> Bridge.IS_BTC_TX_HASH_ALREADY_PROCESSED.encode(new Object[] { ByteUtil.toHexString(hash.getBytes()) });
executeAndAverage("isBtcTxHashAlreadyProcessed-no", times, abiEncoder, buildInitializer(), Helper.getZeroValueRandomSenderTxBuilder(), Helper.getRandomHeightProvider(10), stats);
}
Aggregations