use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransaction_accepts_lock_tx_version1_after_rskip_170_activation.
@Test
public void registerBtcTransaction_accepts_lock_tx_version1_after_rskip_170_activation() throws BlockStoreException, IOException, PeginInstructionsException, BridgeIllegalArgumentException {
// Arrange
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP170)).thenReturn(true);
Federation federation1 = getFederation(bridgeConstants);
Repository repository = createRepository();
repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
BtcECKey srcKey1 = new BtcECKey();
ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
Address btcAddressFromBtcLockSender = srcKey1.toAddress(btcParams);
RskAddress rskDerivedAddress = new RskAddress(key.getAddress());
RskAddress rskDestinationAddress = new RskAddress(new byte[20]);
Coin amountToLock = Coin.COIN.multiply(10);
BtcTransaction tx1 = new BtcTransaction(btcParams);
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);
BtcLockSenderProvider btcLockSenderProvider = getBtcLockSenderProvider(TxSenderAddressType.P2PKH, btcAddressFromBtcLockSender, rskDerivedAddress);
PeginInstructionsProvider peginInstructionsProvider = getPeginInstructionsProviderForVersion1(rskDestinationAddress, Optional.empty());
BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, btcLockSenderProvider, peginInstructionsProvider, mock(Block.class), mockFactory, activations);
// Act
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx1.bitcoinSerialize(), height, pmtWithoutWitness.bitcoinSerialize());
// Assert
co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(amountToLock);
Assert.assertEquals(co.rsk.core.Coin.ZERO, repository.getBalance(rskDerivedAddress));
Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(rskDestinationAddress));
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 getTransactionType_sentFromP2SH_afterRskip199_pegin_tx.
@Test
public void getTransactionType_sentFromP2SH_afterRskip199_pegin_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> privateKeys = Arrays.asList(new BtcECKey(), new BtcECKey(), new BtcECKey());
Script redeemScript = ScriptBuilder.createRedeemScript(multisigSigners, privateKeys);
Script outputScript = ScriptBuilder.createP2SHOutputScript(redeemScript);
Address sender = Address.fromP2SHScript(btcParams, outputScript);
Assert.assertNotEquals(bridgeConstants.getOldFederationAddress(), sender.toBase58());
// Create a tx from a p2sh multisig 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 = privateKeys.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.PEGIN, bridgeSupport.getTransactionType(tx));
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeSupportTest method registerBtcTransaction_ignores_pegin_instructions_before_rskip_170_activation.
@Test
public void registerBtcTransaction_ignores_pegin_instructions_before_rskip_170_activation() throws BlockStoreException, IOException, PeginInstructionsException, BridgeIllegalArgumentException {
// Arrange
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP170)).thenReturn(false);
Federation federation1 = getFederation(bridgeConstants);
Repository repository = createRepository();
BtcECKey srcKey1 = new BtcECKey();
ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
Address btcAddressFromBtcLockSender = srcKey1.toAddress(btcParams);
RskAddress rskDerivedAddress = new RskAddress(key.getAddress());
RskAddress rskDestinationAddress = new RskAddress(new byte[20]);
Coin amountToLock = Coin.COIN.multiply(10);
BtcTransaction tx1 = new BtcTransaction(btcParams);
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(btcAddressFromBtcLockSender, new OneOffWhiteListEntry(btcAddressFromBtcLockSender, amountToLock));
BtcLockSenderProvider btcLockSenderProvider = getBtcLockSenderProvider(TxSenderAddressType.P2PKH, btcAddressFromBtcLockSender, rskDerivedAddress);
PeginInstructionsProvider peginInstructionsProvider = getPeginInstructionsProviderForVersion1(rskDestinationAddress, Optional.empty());
BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, btcLockSenderProvider, peginInstructionsProvider, mock(Block.class), mockFactory, activations);
// Act
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx1.bitcoinSerialize(), height, pmtWithoutWitness.bitcoinSerialize());
// Assert
co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(amountToLock);
Assert.assertEquals(co.rsk.core.Coin.ZERO, repository.getBalance(rskDestinationAddress));
Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(rskDerivedAddress));
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 getTransactionType_sentFromOldFed_beforeRskip199_pegin_tx.
@Test
public void getTransactionType_sentFromOldFed_beforeRskip199_pegin_tx() {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP199)).thenReturn(false);
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.PEGIN, bridgeSupport.getTransactionType(tx));
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeSupportTest method getTransactionType_migration_tx.
@Test
public void getTransactionType_migration_tx() {
FederationSupport mockFederationSupport = mock(FederationSupport.class);
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
BridgeSupport bridgeSupport = new BridgeSupport(bridgeConstants, provider, mock(BridgeEventLogger.class), new BtcLockSenderProvider(), new PeginInstructionsProvider(), mock(Repository.class), mock(Block.class), new Context(bridgeConstants.getBtcParams()), mockFederationSupport, null, mock(ActivationConfig.ForBlock.class));
Federation retiringFederation = bridgeConstants.getGenesisFederation();
List<BtcECKey> retiringFederationPrivateKeys = BridgeRegTestConstants.REGTEST_FEDERATION_PRIVATE_KEYS;
List<BtcECKey> activeFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
Federation activeFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(1000L), 1L, btcParams);
when(mockFederationSupport.getActiveFederation()).thenReturn(activeFederation);
when(mockFederationSupport.getRetiringFederation()).thenReturn(retiringFederation);
BtcTransaction migrationTx = new BtcTransaction(btcParams);
migrationTx.addOutput(Coin.COIN, activeFederation.getAddress());
TransactionInput migrationTxInput = new TransactionInput(btcParams, null, new byte[] {}, new TransactionOutPoint(btcParams, 0, Sha256Hash.ZERO_HASH));
migrationTx.addInput(migrationTxInput);
// Sign it using the Federation members
Script redeemScript = createBaseRedeemScriptThatSpendsFromTheFederation(retiringFederation);
Script inputScript = createBaseInputScriptThatSpendsFromTheFederation(retiringFederation);
migrationTxInput.setScriptSig(inputScript);
Sha256Hash sighash = migrationTx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
for (int i = 0; i < retiringFederation.getNumberOfSignaturesRequired(); i++) {
BtcECKey federatorPrivKey = retiringFederationPrivateKeys.get(i);
BtcECKey federatorPublicKey = retiringFederation.getBtcPublicKeys().get(i);
BtcECKey.ECDSASignature sig = federatorPrivKey.sign(sighash);
TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
int sigIndex = inputScript.getSigInsertionIndex(sighash, federatorPublicKey);
inputScript = ScriptBuilder.updateScriptWithSignature(inputScript, txSig.encodeToBitcoin(), sigIndex, 1, 1);
}
migrationTxInput.setScriptSig(inputScript);
Assert.assertEquals(TxType.MIGRATION, bridgeSupport.getTransactionType(migrationTx));
when(mockFederationSupport.getRetiringFederation()).thenReturn(null);
when(provider.getLastRetiredFederationP2SHScript()).thenReturn(Optional.of(retiringFederation.getP2SHScript()));
Assert.assertEquals(TxType.MIGRATION, bridgeSupport.getTransactionType(migrationTx));
}
Aggregations