use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class PeginInstructionsProviderTest method extractOpReturnData_opReturnWithDataLengthShorterThanExpected.
@Test(expected = NoOpReturnException.class)
public void extractOpReturnData_opReturnWithDataLengthShorterThanExpected() throws PeginInstructionsException {
// Arrange
Script opReturnScript = ScriptBuilder.createOpReturnScript("1".getBytes());
BtcTransaction btcTransaction = new BtcTransaction(params);
btcTransaction.addOutput(Coin.ZERO, opReturnScript);
// Act
PeginInstructionsProvider.extractOpReturnData(btcTransaction);
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class PeginInstructionsProviderTest method buildPeginInstructions_v1_withBtcrefundAddress.
@Test
public void buildPeginInstructions_v1_withBtcrefundAddress() throws Exception {
// Arrange
int protocolVersion = 1;
BtcECKey key = new BtcECKey();
RskAddress rskDestinationAddress = new RskAddress(ECKey.fromPublicOnly(key.getPubKey()).getAddress());
Address btcRefundAddress = key.toAddress(params);
Script opReturnScript = PegTestUtils.createOpReturnScriptForRsk(protocolVersion, rskDestinationAddress, Optional.of(btcRefundAddress));
BtcTransaction btcTransaction = new BtcTransaction(params);
btcTransaction.addOutput(Coin.ZERO, opReturnScript);
// Act
PeginInstructionsProvider peginInstructionsProvider = new PeginInstructionsProvider();
Optional<PeginInstructions> peginInstructions = peginInstructionsProvider.buildPeginInstructions(btcTransaction);
// Assert
Assert.assertTrue(peginInstructions.isPresent());
Assert.assertEquals(protocolVersion, peginInstructions.get().getProtocolVersion());
Assert.assertEquals(rskDestinationAddress, peginInstructions.get().getRskDestinationAddress());
PeginInstructionsVersion1 peginInstructionsVersion1 = (PeginInstructionsVersion1) peginInstructions.get();
Assert.assertTrue(peginInstructionsVersion1.getBtcRefundAddress().isPresent());
Assert.assertEquals(btcRefundAddress, peginInstructionsVersion1.getBtcRefundAddress().get());
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class FastBridgeCompatibleBtcWalletWithStorageTest method findRedeemDataFromScriptHash_with_fastBridgeInformation_in_storage.
@Test
public void findRedeemDataFromScriptHash_with_fastBridgeInformation_in_storage() {
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
Keccak256 derivationArgumentsHash = PegTestUtils.createHash3(1);
Script fastBridgeRedeemScript = FastBridgeRedeemScriptParser.createMultiSigFastBridgeRedeemScript(federation.getRedeemScript(), Sha256Hash.wrap(derivationArgumentsHash.getBytes()));
Script p2SHOutputScript = ScriptBuilder.createP2SHOutputScript(fastBridgeRedeemScript);
byte[] fastBridgeFederationP2SH = p2SHOutputScript.getPubKeyHash();
FastBridgeFederationInformation fastBridgeFederationInformation = new FastBridgeFederationInformation(derivationArgumentsHash, federation.getP2SHScript().getPubKeyHash(), fastBridgeFederationP2SH);
when(provider.getFastBridgeFederationInformation(fastBridgeFederationP2SH)).thenReturn(Optional.of(fastBridgeFederationInformation));
FastBridgeCompatibleBtcWalletWithStorage fastBridgeCompatibleBtcWalletWithStorage = new FastBridgeCompatibleBtcWalletWithStorage(mock(Context.class), federationList, provider);
RedeemData redeemData = fastBridgeCompatibleBtcWalletWithStorage.findRedeemDataFromScriptHash(fastBridgeFederationP2SH);
Assert.assertNotNull(redeemData);
Assert.assertEquals(fastBridgeRedeemScript, redeemData.redeemScript);
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeUtilsTest method assertIsWatching.
private void assertIsWatching(Address address, Wallet wallet, NetworkParameters parameters) {
List<Script> watchedScripts = wallet.getWatchedScripts();
Assert.assertEquals(1, watchedScripts.size());
Script watchedScript = watchedScripts.get(0);
Assert.assertTrue(watchedScript.isPayToScriptHash());
Assert.assertEquals(address.toString(), watchedScript.getToAddress(parameters).toString());
}
use of co.rsk.bitcoinj.script.Script 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);
}
Aggregations