use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeUtilsTest method testIsMigrationTx.
@Test
public void testIsMigrationTx() {
BridgeRegTestConstants bridgeConstants = BridgeRegTestConstants.getInstance();
NetworkParameters parameters = bridgeConstants.getBtcParams();
Context btcContext = new Context(parameters);
List<BtcECKey> activeFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
Federation activeFederation = new Federation(activeFederationKeys, Instant.ofEpochMilli(2000L), 2L, parameters);
List<BtcECKey> retiringFederationKeys = 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 retiringFederation = new Federation(retiringFederationKeys, Instant.ofEpochMilli(1000L), 1L, parameters);
Address activeFederationAddress = activeFederation.getAddress();
BtcTransaction migrationTx = new BtcTransaction(parameters);
migrationTx.addOutput(Coin.COIN, activeFederationAddress);
TransactionInput migrationTxInput = new TransactionInput(parameters, migrationTx, new byte[] {}, new TransactionOutPoint(parameters, 0, Sha256Hash.ZERO_HASH));
migrationTx.addInput(migrationTxInput);
signWithNecessaryKeys(retiringFederation, retiringFederationKeys, migrationTxInput, migrationTx, bridgeConstants);
assertThat(BridgeUtils.isMigrationTx(migrationTx, activeFederation, retiringFederation, btcContext, bridgeConstants), is(true));
BtcTransaction toActiveFederationTx = new BtcTransaction(parameters);
toActiveFederationTx.addOutput(Coin.COIN, activeFederationAddress);
toActiveFederationTx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertThat(BridgeUtils.isMigrationTx(toActiveFederationTx, activeFederation, retiringFederation, btcContext, bridgeConstants), is(false));
Address randomAddress = Address.fromBase58(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), "n3PLxDiwWqa5uH7fSbHCxS6VAjD9Y7Rwkj");
BtcTransaction fromRetiringFederationTx = new BtcTransaction(parameters);
fromRetiringFederationTx.addOutput(Coin.COIN, randomAddress);
TransactionInput fromRetiringFederationTxInput = new TransactionInput(parameters, fromRetiringFederationTx, new byte[] {}, new TransactionOutPoint(parameters, 0, Sha256Hash.ZERO_HASH));
fromRetiringFederationTx.addInput(fromRetiringFederationTxInput);
signWithNecessaryKeys(retiringFederation, retiringFederationKeys, fromRetiringFederationTxInput, fromRetiringFederationTx, bridgeConstants);
assertThat(BridgeUtils.isMigrationTx(fromRetiringFederationTx, activeFederation, retiringFederation, btcContext, bridgeConstants), is(false));
assertThat(BridgeUtils.isMigrationTx(migrationTx, activeFederation, null, btcContext, bridgeConstants), is(false));
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeUtilsTest method testIsLock.
@Test
public void testIsLock() throws Exception {
// Lock is for the genesis federation ATM
NetworkParameters params = RegTestParams.get();
Context btcContext = new Context(params);
BridgeRegTestConstants bridgeConstants = BridgeRegTestConstants.getInstance();
Federation federation = bridgeConstants.getGenesisFederation();
Wallet wallet = new BridgeBtcWallet(btcContext, Arrays.asList(federation));
wallet.addWatchedAddress(federation.getAddress(), federation.getCreationTime().toEpochMilli());
Address address = federation.getAddress();
// Tx sending less than 1 btc to the federation, not a lock tx
BtcTransaction tx = new BtcTransaction(params);
tx.addOutput(Coin.CENT, address);
tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertFalse(BridgeUtils.isLockTx(tx, federation, btcContext, bridgeConstants));
// Tx sending 1 btc to the federation, but also spending from the federation addres, the typical release tx, not a lock tx.
BtcTransaction tx2 = new BtcTransaction(params);
tx2.addOutput(Coin.COIN, address);
TransactionInput txIn = new TransactionInput(params, tx2, new byte[] {}, new TransactionOutPoint(params, 0, Sha256Hash.ZERO_HASH));
tx2.addInput(txIn);
signWithNecessaryKeys(bridgeConstants.getGenesisFederation(), bridgeConstants.getFederatorPrivateKeys(), txIn, tx2, bridgeConstants);
assertFalse(BridgeUtils.isLockTx(tx2, federation, btcContext, bridgeConstants));
// Tx sending 1 btc to the federation, is a lock tx
BtcTransaction tx3 = new BtcTransaction(params);
tx3.addOutput(Coin.COIN, address);
tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertTrue(BridgeUtils.isLockTx(tx3, federation, btcContext, bridgeConstants));
// Tx sending 50 btc to the federation, is a lock tx
BtcTransaction tx4 = new BtcTransaction(params);
tx4.addOutput(Coin.FIFTY_COINS, address);
tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertTrue(BridgeUtils.isLockTx(tx4, federation, btcContext, bridgeConstants));
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class AddSignatureTest method getSignaturesFor.
private List<byte[]> getSignaturesFor(BtcTransaction tx, BtcECKey key) {
List<byte[]> signatures = new ArrayList<>();
int inputIndex = 0;
for (TransactionInput txIn : tx.getInputs()) {
Script inputScript = txIn.getScriptSig();
List<ScriptChunk> chunks = inputScript.getChunks();
byte[] program = chunks.get(chunks.size() - 1).data;
Script redeemScript = new Script(program);
Sha256Hash sighash = tx.hashForSignature(inputIndex, redeemScript, BtcTransaction.SigHash.ALL, false);
BtcECKey.ECDSASignature sig = key.sign(sighash);
signatures.add(sig.encodeToDER());
inputIndex++;
}
return signatures;
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class AddSignatureTest method signInputsWith.
private void signInputsWith(BtcTransaction tx, BtcECKey key) {
List<byte[]> signatures = getSignaturesFor(tx, key);
for (int i = 0; i < tx.getInputs().size(); i++) {
TransactionInput input = tx.getInput(i);
Script inputScript = input.getScriptSig();
List<ScriptChunk> chunks = inputScript.getChunks();
byte[] program = chunks.get(chunks.size() - 1).data;
Script redeemScript = new Script(program);
Sha256Hash sighash = tx.hashForSignature(i, redeemScript, BtcTransaction.SigHash.ALL, false);
int sigIndex = inputScript.getSigInsertionIndex(sighash, key);
BtcECKey.ECDSASignature sig = BtcECKey.ECDSASignature.decodeFromDER(signatures.get(i));
TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
inputScript = ScriptBuilder.updateScriptWithSignature(inputScript, txSig.encodeToBitcoin(), sigIndex, 1, 1);
input.setScriptSig(inputScript);
}
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class UpdateCollectionsTest method updateCollections_buildReleaseTxs.
private void updateCollections_buildReleaseTxs(ExecutionStats stats, int numCases) throws IOException {
final int minUTXOs = 1;
final int maxUTXOs = 1000;
final int minMilliBtc = 1;
final int maxMilliBtc = 1000;
final int minReleaseRequests = 1;
final int maxReleaseRequests = 100;
final int minMilliReleaseBtc = 10;
final int maxMilliReleaseBtc = 2000;
final NetworkParameters parameters = NetworkParameters.fromID(NetworkParameters.ID_REGTEST);
BridgeStorageProviderInitializer storageInitializer = (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
Random rnd = new Random();
List<UTXO> utxos;
ReleaseRequestQueue queue;
try {
utxos = provider.getNewFederationBtcUTXOs();
} catch (Exception e) {
throw new RuntimeException("Unable to gather active federation btc utxos");
}
try {
queue = provider.getReleaseRequestQueue();
} catch (Exception e) {
throw new RuntimeException("Unable to gather release request queue");
}
// Generate some utxos
int numUTXOs = Helper.randomInRange(minUTXOs, maxUTXOs);
Script federationScript = BridgeRegTestConstants.getInstance().getGenesisFederation().getP2SHScript();
for (int i = 0; i < numUTXOs; i++) {
Sha256Hash hash = Sha256Hash.wrap(HashUtil.sha256(BigInteger.valueOf(rnd.nextLong()).toByteArray()));
Coin value = Coin.MILLICOIN.multiply(Helper.randomInRange(minMilliBtc, maxMilliBtc));
utxos.add(new UTXO(hash, 0, value, 1, false, federationScript));
}
// Generate some release requests to process
for (int i = 0; i < Helper.randomInRange(minReleaseRequests, maxReleaseRequests); i++) {
Coin value = Coin.MILLICOIN.multiply(Helper.randomInRange(minMilliReleaseBtc, maxMilliReleaseBtc));
queue.add(new BtcECKey().toAddress(parameters), value);
}
};
final byte[] updateCollectionsEncoded = Bridge.UPDATE_COLLECTIONS.encode();
ABIEncoder abiEncoder = (int executionIndex) -> updateCollectionsEncoded;
executeAndAverage("updateCollections-releaseRequests", numCases, abiEncoder, storageInitializer, Helper.getZeroValueRandomSenderTxBuilder(), Helper.getRandomHeightProvider(10), stats);
}
Aggregations