use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class PeginInstructionsVersion1Test method parseAdditionalData_invalidAddressType.
@Test(expected = PeginInstructionsParseException.class)
public void parseAdditionalData_invalidAddressType() throws PeginInstructionsParseException {
// Arrange
BtcECKey key = new BtcECKey();
Address btcRefundAddress = key.toAddress(params);
Script opReturnScript = PegTestUtils.createOpReturnScriptForRsk(1, new RskAddress(new byte[20]), Optional.of(btcRefundAddress));
byte[] opReturnData = opReturnScript.getChunks().get(1).data;
// Change the byte containing the address type
opReturnData[25] = 9;
// Act
PeginInstructionsVersion1 peginInstructionsVersion1 = new PeginInstructionsVersion1(params);
peginInstructionsVersion1.parseAdditionalData(opReturnData);
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeSupportTest method getTransactionType_sentFromOldFed_afterRskip199_migration_tx.
@Test
public void getTransactionType_sentFromOldFed_afterRskip199_migration_tx() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP199)).thenReturn(true);
BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, mock(BridgeStorageProvider.class), activations);
int multisigSigners = 2;
Federation activeFederation = bridgeConstants.getGenesisFederation();
List<BtcECKey> oldFederationPrivateKeys = REGTEST_OLD_FEDERATION_PRIVATE_KEYS;
Script redeemScript = ScriptBuilder.createRedeemScript(multisigSigners, oldFederationPrivateKeys);
Script outputScript = ScriptBuilder.createP2SHOutputScript(redeemScript);
Address sender = Address.fromP2SHScript(btcParams, outputScript);
Assert.assertEquals(bridgeConstants.getOldFederationAddress(), sender.toBase58());
// Create a tx from the old fed address to the active fed
BtcTransaction tx = new BtcTransaction(btcParams);
tx.addOutput(Coin.COIN, activeFederation.getAddress());
tx.addInput(Sha256Hash.ZERO_HASH, 0, redeemScript);
Script inputScript = outputScript.createEmptyInputScript(null, redeemScript);
tx.getInput(0).setScriptSig(inputScript);
Sha256Hash sighash = tx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
for (int i = 0; i < multisigSigners; i++) {
BtcECKey privateKey = oldFederationPrivateKeys.get(i);
BtcECKey publicKey = BtcECKey.fromPublicOnly(privateKey.getPubKeyPoint().getEncoded(true));
BtcECKey.ECDSASignature sig = privateKey.sign(sighash);
TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
int sigIndex = inputScript.getSigInsertionIndex(sighash, publicKey);
inputScript = ScriptBuilder.updateScriptWithSignature(inputScript, txSig.encodeToBitcoin(), sigIndex, 1, 1);
}
tx.getInput(0).setScriptSig(inputScript);
Assert.assertEquals(TxType.MIGRATION, bridgeSupport.getTransactionType(tx));
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransaction_sending_segwit_tx_twice_locks_just_once.
@Test
public void registerBtcTransaction_sending_segwit_tx_twice_locks_just_once() throws BlockStoreException, IOException, BridgeIllegalArgumentException {
ActivationConfig.ForBlock mockedActivations = mock(ActivationConfig.ForBlock.class);
when(mockedActivations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
BtcTransaction txWithWitness = new BtcTransaction(btcParams);
// first input spends P2PKH
BtcECKey srcKey1 = new BtcECKey();
txWithWitness.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey1));
// second input spends P2SH-P2PWKH (actually, just has a witness doesn't matter if it truly spends a witness for the test's sake)
txWithWitness.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
TransactionWitness txWit = new TransactionWitness(1);
txWit.setPush(0, new byte[] {});
txWithWitness.setWitness(0, txWit);
List<BtcECKey> fedKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")));
Federation fed = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(fedKeys), Instant.ofEpochMilli(1000L), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
txWithWitness.addOutput(Coin.COIN.multiply(5), fed.getAddress());
// Create the pmt without witness and calculate the block merkle root
byte[] bits = new byte[1];
bits[0] = 0x3f;
PartialMerkleTree pmtWithoutWitness = new PartialMerkleTree(btcParams, bits, Collections.singletonList(txWithWitness.getHash()), 1);
Sha256Hash merkleRoot = pmtWithoutWitness.getTxnHashAndMerkleRoot(new ArrayList<>());
PartialMerkleTree pmtWithWitness = new PartialMerkleTree(btcParams, bits, Collections.singletonList(txWithWitness.getHash(true)), 1);
Sha256Hash witnessMerkleRoot = pmtWithWitness.getTxnHashAndMerkleRoot(new ArrayList<>());
co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), merkleRoot, 1, 1, 1, new ArrayList<>());
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
when(provider.getNewFederation()).thenReturn(fed);
when(provider.getCoinbaseInformation(registerHeader.getHash())).thenReturn(new CoinbaseInformation(witnessMerkleRoot));
when(provider.getLockWhitelist()).thenReturn(new LockWhitelist(new HashMap<>(), 0));
when(provider.getLockingCap()).thenReturn(Coin.FIFTY_COINS);
// mock an actual store for the processed txs
HashMap<Sha256Hash, Long> processedTxs = new HashMap<>();
doAnswer(a -> {
processedTxs.put(a.getArgument(0), a.getArgument(1));
return null;
}).when(provider).setHeightBtcTxhashAlreadyProcessed(any(), anyLong());
doAnswer(a -> Optional.ofNullable(processedTxs.get(a.getArgument(0)))).when(provider).getHeightIfBtcTxhashIsAlreadyProcessed(any());
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
when(mockFactory.newInstance(any(), any(), any(), any())).thenReturn(btcBlockStore);
Block executionBlock = mock(Block.class);
when(executionBlock.getNumber()).thenReturn(666L);
BridgeSupport bridgeSupport = bridgeSupportBuilder.withBridgeConstants(bridgeConstants).withProvider(provider).withBtcLockSenderProvider(new BtcLockSenderProvider()).withPeginInstructionsProvider(new PeginInstructionsProvider()).withExecutionBlock(executionBlock).withBtcBlockStoreFactory(mockFactory).withActivations(mockedActivations).build();
int height = 30;
mockChainOfStoredBlocks(btcBlockStore, registerHeader, 35, height);
// Tx is locked
bridgeSupport.registerBtcTransaction(mock(Transaction.class), txWithWitness.bitcoinSerialize(), height, pmtWithWitness.bitcoinSerialize());
verify(provider, never()).setHeightBtcTxhashAlreadyProcessed(txWithWitness.getHash(true), executionBlock.getNumber());
verify(provider, times(1)).setHeightBtcTxhashAlreadyProcessed(txWithWitness.getHash(false), executionBlock.getNumber());
BtcTransaction txWithoutWitness = new BtcTransaction(btcParams, txWithWitness.bitcoinSerialize());
txWithoutWitness.setWitness(0, null);
assertFalse(txWithoutWitness.hasWitness());
// Tx is NOT locked again!
bridgeSupport.registerBtcTransaction(mock(Transaction.class), txWithoutWitness.bitcoinSerialize(), height, pmtWithoutWitness.bitcoinSerialize());
verify(provider, times(1)).setHeightBtcTxhashAlreadyProcessed(txWithoutWitness.getHash(), executionBlock.getNumber());
assertNotEquals(txWithWitness.getHash(true), txWithoutWitness.getHash());
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransaction_accepts_lock_tx_without_witness_after_rskip_143_activation.
@Test
public void registerBtcTransaction_accepts_lock_tx_without_witness_after_rskip_143_activation() throws BlockStoreException, IOException, BridgeIllegalArgumentException {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
Federation federation1 = getFederation(bridgeConstants);
Repository repository = createRepository();
BtcTransaction tx1 = new BtcTransaction(btcParams);
BtcECKey srcKey1 = new BtcECKey();
ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
Address btcAddress = srcKey1.toAddress(btcParams);
RskAddress rskAddress = new RskAddress(key.getAddress());
Coin amountToLock = Coin.COIN.multiply(10);
tx1.addOutput(amountToLock, federation1.getAddress());
tx1.addInput(PegTestUtils.createHash(1), 0, new Script(new byte[] {}));
byte[] bits = new byte[1];
bits[0] = 0x3f;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(tx1.getHash());
PartialMerkleTree pmtWithoutWitness = new PartialMerkleTree(btcParams, bits, hashes, 1);
List<Sha256Hash> hashlist = new ArrayList<>();
Sha256Hash blockMerkleRoot = pmtWithoutWitness.getTxnHashAndMerkleRoot(hashlist);
co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), blockMerkleRoot, 1, 1, 1, new ArrayList<>());
int height = 50;
StoredBlock block = new StoredBlock(registerHeader, new BigInteger("0"), height);
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
co.rsk.bitcoinj.core.BtcBlock headBlock = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(2), Sha256Hash.of(new byte[] { 1 }), 1, 1, 1, new ArrayList<>());
StoredBlock chainHead = new StoredBlock(headBlock, new BigInteger("0"), 132);
when(btcBlockStore.getChainHead()).thenReturn(chainHead);
when(btcBlockStore.getStoredBlockAtMainChainHeight(block.getHeight())).thenReturn(block);
BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
when(mockFactory.newInstance(any(), any(), any(), any())).thenReturn(btcBlockStore);
BridgeStorageProvider provider = new BridgeStorageProvider(repository, contractAddress, bridgeConstants, activations);
provider.setNewFederation(federation1);
// Whitelist the addresses
LockWhitelist whitelist = provider.getLockWhitelist();
whitelist.put(btcAddress, new OneOffWhiteListEntry(btcAddress, Coin.COIN.multiply(10)));
BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, getBtcLockSenderProvider(TxSenderAddressType.P2PKH, btcAddress, rskAddress), new PeginInstructionsProvider(), mock(Block.class), mockFactory, activations);
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx1.bitcoinSerialize(), height, pmtWithoutWitness.bitcoinSerialize());
co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(amountToLock);
Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(rskAddress));
Assert.assertEquals(1, provider.getNewFederationBtcUTXOs().size());
Assert.assertEquals(amountToLock, provider.getNewFederationBtcUTXOs().get(0).getValue());
Assert.assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
Assert.assertEquals(0, provider.getReleaseTransactionSet().getEntries().size());
Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
Assert.assertTrue(provider.getHeightIfBtcTxhashIsAlreadyProcessed(tx1.getHash(true)).isPresent());
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransaction_accepts_lock_tx_with_witness_after_rskip_143_activation.
@Test
public void registerBtcTransaction_accepts_lock_tx_with_witness_after_rskip_143_activation() throws BlockStoreException, IOException, BridgeIllegalArgumentException {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
Federation federation1 = getFederation(bridgeConstants);
Repository repository = createRepository();
BtcTransaction tx1 = new BtcTransaction(btcParams);
BtcECKey srcKey1 = new BtcECKey();
ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
Address btcAddress = srcKey1.toAddress(btcParams);
RskAddress rskAddress = new RskAddress(key.getAddress());
Coin amountToLock = Coin.COIN.multiply(10);
tx1.addOutput(amountToLock, federation1.getAddress());
tx1.addInput(PegTestUtils.createHash(1), 0, new Script(new byte[] {}));
TransactionWitness txWit = new TransactionWitness(1);
txWit.setPush(0, new byte[] {});
tx1.setWitness(0, txWit);
byte[] bits = new byte[1];
bits[0] = 0x3f;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(tx1.getHash());
PartialMerkleTree pmtWithoutWitness = new PartialMerkleTree(btcParams, bits, hashes, 1);
List<Sha256Hash> hashlist = new ArrayList<>();
Sha256Hash blockMerkleRoot = pmtWithoutWitness.getTxnHashAndMerkleRoot(hashlist);
co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), blockMerkleRoot, 1, 1, 1, new ArrayList<>());
List<Sha256Hash> hashes2 = new ArrayList<>();
hashes2.add(tx1.getHash(true));
PartialMerkleTree pmtWithWitness = new PartialMerkleTree(btcParams, bits, hashes2, 1);
List<Sha256Hash> hashlist2 = new ArrayList<>();
Sha256Hash witnessMerkleRoot = pmtWithWitness.getTxnHashAndMerkleRoot(hashlist2);
int height = 50;
StoredBlock block = new StoredBlock(registerHeader, new BigInteger("0"), height);
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
co.rsk.bitcoinj.core.BtcBlock headBlock = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(2), Sha256Hash.of(new byte[] { 1 }), 1, 1, 1, new ArrayList<>());
StoredBlock chainHead = new StoredBlock(headBlock, new BigInteger("0"), 132);
when(btcBlockStore.getChainHead()).thenReturn(chainHead);
when(btcBlockStore.getStoredBlockAtMainChainHeight(block.getHeight())).thenReturn(block);
BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
when(mockFactory.newInstance(any(), any(), any(), any())).thenReturn(btcBlockStore);
BridgeStorageProvider provider = new BridgeStorageProvider(repository, contractAddress, bridgeConstants, activations);
provider.setNewFederation(federation1);
// Whitelist the addresses
LockWhitelist whitelist = provider.getLockWhitelist();
whitelist.put(btcAddress, new OneOffWhiteListEntry(btcAddress, Coin.COIN.multiply(10)));
BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, getBtcLockSenderProvider(TxSenderAddressType.P2SHP2WPKH, btcAddress, rskAddress), new PeginInstructionsProvider(), mock(Block.class), mockFactory, activations);
CoinbaseInformation coinbaseInformation = new CoinbaseInformation(witnessMerkleRoot);
provider.setCoinbaseInformation(registerHeader.getHash(), coinbaseInformation);
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx1.bitcoinSerialize(), height, pmtWithWitness.bitcoinSerialize());
co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(amountToLock);
Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(rskAddress));
Assert.assertEquals(1, provider.getNewFederationBtcUTXOs().size());
Assert.assertEquals(amountToLock, provider.getNewFederationBtcUTXOs().get(0).getValue());
Assert.assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
Assert.assertEquals(0, provider.getReleaseTransactionSet().getEntries().size());
Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
Assert.assertTrue(provider.getHeightIfBtcTxhashIsAlreadyProcessed(tx1.getHash(false)).isPresent());
}
Aggregations