Search in sources :

Example 36 with RLPList

use of org.ethereum.util.RLPList in project rskj by rsksmart.

the class BridgeSerializationUtils method deserializeMapOfHashesToLong.

public static Map<Sha256Hash, Long> deserializeMapOfHashesToLong(byte[] data) {
    Map<Sha256Hash, Long> map = new HashMap<>();
    if (data == null || data.length == 0) {
        return map;
    }
    RLPList rlpList = (RLPList) RLP.decode2(data).get(0);
    // List size must be even - key, value pairs expected in sequence
    if (rlpList.size() % 2 != 0) {
        throw new RuntimeException("deserializeMapOfHashesToLong: expected an even number of entries, but odd given");
    }
    int numEntries = rlpList.size() / 2;
    for (int k = 0; k < numEntries; k++) {
        Sha256Hash hash = Sha256Hash.wrap(rlpList.get(k * 2).getRLPData());
        Long number = BigIntegers.fromUnsignedByteArray(rlpList.get(k * 2 + 1).getRLPData()).longValue();
        map.put(hash, number);
    }
    return map;
}
Also used : RLPList(org.ethereum.util.RLPList)

Example 37 with RLPList

use of org.ethereum.util.RLPList in project rskj by rsksmart.

the class ProofOfWorkRule method validFallbackBlockSignature.

public static boolean validFallbackBlockSignature(Constants constants, BlockHeader header, byte[] signatureBytesRLP) {
    if (header.getBitcoinMergedMiningCoinbaseTransaction() != null) {
        return false;
    }
    if (header.getBitcoinMergedMiningMerkleProof() != null) {
        return false;
    }
    byte[] fallbackMiningPubKeyBytes;
    boolean isEvenBlockNumber = header.getNumber() % 2 == 0;
    if (isEvenBlockNumber) {
        fallbackMiningPubKeyBytes = constants.getFallbackMiningPubKey0();
    } else {
        fallbackMiningPubKeyBytes = constants.getFallbackMiningPubKey1();
    }
    ECKey fallbackMiningPubKey = ECKey.fromPublicOnly(fallbackMiningPubKeyBytes);
    List<RLPElement> signatureRLP = (RLPList) RLP.decode2(signatureBytesRLP).get(0);
    if (signatureRLP.size() != 3) {
        return false;
    }
    byte[] v = signatureRLP.get(0).getRLPData();
    byte[] r = signatureRLP.get(1).getRLPData();
    byte[] s = signatureRLP.get(2).getRLPData();
    ECKey.ECDSASignature signature = ECKey.ECDSASignature.fromComponents(r, s, v[0]);
    return fallbackMiningPubKey.verify(header.getHashForMergedMining(), signature);
}
Also used : RLPElement(org.ethereum.util.RLPElement) ECKey(org.ethereum.crypto.ECKey) RLPList(org.ethereum.util.RLPList)

Example 38 with RLPList

use of org.ethereum.util.RLPList in project rskj by rsksmart.

the class BridgeSupportTest method addSignatureMultipleInputsPartiallyValid.

@Test
public void addSignatureMultipleInputsPartiallyValid() throws Exception {
    // Federation is the genesis federation ATM
    Federation federation = bridgeConstants.getGenesisFederation();
    Repository repository = new RepositoryImpl(config);
    final Keccak256 keccak256 = PegTestUtils.createHash3();
    Repository track = repository.startTracking();
    BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants);
    BtcTransaction prevTx1 = new BtcTransaction(btcParams);
    TransactionOutput prevOut1 = new TransactionOutput(btcParams, prevTx1, Coin.FIFTY_COINS, federation.getAddress());
    prevTx1.addOutput(prevOut1);
    BtcTransaction prevTx2 = new BtcTransaction(btcParams);
    TransactionOutput prevOut2 = new TransactionOutput(btcParams, prevTx1, Coin.FIFTY_COINS, federation.getAddress());
    prevTx2.addOutput(prevOut2);
    BtcTransaction prevTx3 = new BtcTransaction(btcParams);
    TransactionOutput prevOut3 = new TransactionOutput(btcParams, prevTx1, Coin.FIFTY_COINS, federation.getAddress());
    prevTx3.addOutput(prevOut3);
    BtcTransaction t = new BtcTransaction(btcParams);
    TransactionOutput output = new TransactionOutput(btcParams, t, Coin.COIN, new BtcECKey().toAddress(btcParams));
    t.addOutput(output);
    t.addInput(prevOut1).setScriptSig(PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation));
    t.addInput(prevOut2).setScriptSig(PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation));
    t.addInput(prevOut3).setScriptSig(PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation));
    provider.getRskTxsWaitingForSignatures().put(keccak256, t);
    provider.save();
    track.commit();
    track = repository.startTracking();
    List<LogInfo> logs = new ArrayList<>();
    BridgeEventLogger eventLogger = new BridgeEventLoggerImpl(bridgeConstants, logs);
    BridgeSupport bridgeSupport = new BridgeSupport(config, track, eventLogger, contractAddress, (Block) null);
    // Generate valid signatures for inputs
    List<byte[]> derEncodedSigsFirstFed = new ArrayList<>();
    List<byte[]> derEncodedSigsSecondFed = new ArrayList<>();
    BtcECKey privateKeyOfFirstFed = ((BridgeRegTestConstants) bridgeConstants).getFederatorPrivateKeys().get(0);
    BtcECKey privateKeyOfSecondFed = ((BridgeRegTestConstants) bridgeConstants).getFederatorPrivateKeys().get(1);
    BtcECKey.ECDSASignature lastSig = null;
    for (int i = 0; i < 3; i++) {
        Script inputScript = t.getInput(i).getScriptSig();
        List<ScriptChunk> chunks = inputScript.getChunks();
        byte[] program = chunks.get(chunks.size() - 1).data;
        Script redeemScript = new Script(program);
        Sha256Hash sighash = t.hashForSignature(i, redeemScript, BtcTransaction.SigHash.ALL, false);
        // Sign the last input with a random key
        // but keep the good signature for a subsequent call
        BtcECKey.ECDSASignature sig = privateKeyOfFirstFed.sign(sighash);
        if (i == 2) {
            lastSig = sig;
            sig = new BtcECKey().sign(sighash);
        }
        derEncodedSigsFirstFed.add(sig.encodeToDER());
        derEncodedSigsSecondFed.add(privateKeyOfSecondFed.sign(sighash).encodeToDER());
    }
    // Sign with two valid signatuers and one invalid signature
    bridgeSupport.addSignature(1, findPublicKeySignedBy(federation.getPublicKeys(), privateKeyOfFirstFed), derEncodedSigsFirstFed, keccak256.getBytes());
    bridgeSupport.save();
    track.commit();
    // Sign with two valid signatuers and one malformed signature
    byte[] malformedSignature = new byte[lastSig.encodeToDER().length];
    for (int i = 0; i < malformedSignature.length; i++) {
        malformedSignature[i] = (byte) i;
    }
    derEncodedSigsFirstFed.set(2, malformedSignature);
    bridgeSupport.addSignature(1, findPublicKeySignedBy(federation.getPublicKeys(), privateKeyOfFirstFed), derEncodedSigsFirstFed, keccak256.getBytes());
    bridgeSupport.save();
    track.commit();
    // Sign with fully valid signatures for same federator
    derEncodedSigsFirstFed.set(2, lastSig.encodeToDER());
    bridgeSupport.addSignature(1, findPublicKeySignedBy(federation.getPublicKeys(), privateKeyOfFirstFed), derEncodedSigsFirstFed, keccak256.getBytes());
    bridgeSupport.save();
    track.commit();
    // Sign with second federation
    bridgeSupport.addSignature(1, findPublicKeySignedBy(federation.getPublicKeys(), privateKeyOfSecondFed), derEncodedSigsSecondFed, keccak256.getBytes());
    bridgeSupport.save();
    track.commit();
    provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants);
    Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
    Assert.assertThat(logs, is(not(empty())));
    Assert.assertThat(logs, hasSize(5));
    LogInfo releaseTxEvent = logs.get(4);
    Assert.assertThat(releaseTxEvent.getTopics(), hasSize(1));
    Assert.assertThat(releaseTxEvent.getTopics(), hasItem(Bridge.RELEASE_BTC_TOPIC));
    BtcTransaction releaseTx = new BtcTransaction(bridgeConstants.getBtcParams(), ((RLPList) RLP.decode2(releaseTxEvent.getData()).get(0)).get(1).getRLPData());
    // Verify all inputs fully signed
    for (int i = 0; i < releaseTx.getInputs().size(); i++) {
        Script retrievedScriptSig = releaseTx.getInput(i).getScriptSig();
        Assert.assertEquals(4, retrievedScriptSig.getChunks().size());
        Assert.assertEquals(true, retrievedScriptSig.getChunks().get(1).data.length > 0);
        Assert.assertEquals(true, retrievedScriptSig.getChunks().get(2).data.length > 0);
    }
}
Also used : BridgeEventLoggerImpl(co.rsk.peg.utils.BridgeEventLoggerImpl) Script(co.rsk.bitcoinj.script.Script) LogInfo(org.ethereum.vm.LogInfo) Keccak256(co.rsk.crypto.Keccak256) ScriptChunk(co.rsk.bitcoinj.script.ScriptChunk) RLPList(org.ethereum.util.RLPList) RepositoryImpl(co.rsk.db.RepositoryImpl) BridgeEventLogger(co.rsk.peg.utils.BridgeEventLogger) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 39 with RLPList

use of org.ethereum.util.RLPList in project rskj by rsksmart.

the class NeighborsPeerMessage method parse.

@Override
public final void parse(byte[] data) {
    RLPList list = (RLPList) RLP.decode2OneItem(data, 0);
    RLPList nodesRLP = (RLPList) list.get(0);
    nodes = new ArrayList<>();
    for (int i = 0; i < nodesRLP.size(); ++i) {
        RLPList nodeRLP = (RLPList) nodesRLP.get(i);
        Node node = new Node(nodeRLP.getRLPData());
        nodes.add(node);
    }
    RLPItem chk = (RLPItem) list.get(1);
    this.messageId = new String(chk.getRLPData(), Charset.forName("UTF-8"));
}
Also used : RLPItem(org.ethereum.util.RLPItem) Node(org.ethereum.net.rlpx.Node) RLPList(org.ethereum.util.RLPList)

Example 40 with RLPList

use of org.ethereum.util.RLPList in project rskj by rsksmart.

the class PongPeerMessage method parse.

@Override
public final void parse(byte[] data) {
    RLPList dataList = (RLPList) RLP.decode2OneItem(data, 0);
    RLPList fromList = (RLPList) dataList.get(1);
    byte[] ipB = fromList.get(0).getRLPData();
    this.host = new String(ipB, Charset.forName("UTF-8"));
    this.port = ByteUtil.byteArrayToInt(fromList.get(1).getRLPData());
    RLPItem chk = (RLPItem) dataList.get(2);
    this.messageId = new String(chk.getRLPData(), Charset.forName("UTF-8"));
}
Also used : RLPItem(org.ethereum.util.RLPItem) RLPList(org.ethereum.util.RLPList)

Aggregations

RLPList (org.ethereum.util.RLPList)60 RLPElement (org.ethereum.util.RLPElement)19 Test (org.junit.Test)13 Keccak256 (co.rsk.crypto.Keccak256)8 RskAddress (co.rsk.core.RskAddress)7 BigInteger (java.math.BigInteger)5 RLPItem (org.ethereum.util.RLPItem)5 LogInfo (org.ethereum.vm.LogInfo)5 Script (co.rsk.bitcoinj.script.Script)3 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)3 Coin (co.rsk.core.Coin)3 RepositoryImpl (co.rsk.db.RepositoryImpl)3 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)3 BridgeEventLoggerImpl (co.rsk.peg.utils.BridgeEventLoggerImpl)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2