Search in sources :

Example 46 with ECKey

use of org.ethereum.crypto.ECKey in project rskj by rsksmart.

the class TxBuilderEx method simulateTxs.

public void simulateTxs() {
    final byte[] privateKeyBytes = HashUtil.keccak256(config.simulateTxsExAccountSeed().getBytes(StandardCharsets.UTF_8));
    final ECKey key = ECKey.fromPrivate(privateKeyBytes);
    RskAddress addr = new RskAddress(key.getAddress());
    final Account targetAcc = new Account(new ECKey(Utils.getRandom()));
    final String targetAddress = targetAcc.getAddress().toString();
    final Account target2Acc = new Account(new ECKey(Utils.getRandom()));
    final String target2Address = target2Acc.getAddress().toString();
    logger.trace("Accounts {} {} {}", addr, targetAddress, target2Address);
    new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(60000);
            } catch (InterruptedException e) {
                logger.error("Interrupted", e);
            }
            while (nodeBlockProcessor.hasBetterBlockToSync()) {
                try {
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                    logger.error("Interrupted", e);
                }
            }
            for (int k = 0; k < 6; k++) {
                try {
                    Thread.sleep(20000);
                } catch (InterruptedException e) {
                    logger.error("Interrupted", e);
                }
                AccountState fromAccountState = repository.getAccountState(addr);
                Transaction tx = createNewTransaction(privateKeyBytes, targetAddress, BigInteger.valueOf(config.simulateTxsExFounding()), fromAccountState.getNonce());
                sendTransaction(tx);
                logger.trace("Funding tx {} nonce {}", 10000000000L, getNonce(tx));
            }
            try {
                Thread.sleep(60000);
            } catch (InterruptedException e) {
                logger.error("Interrupted", e);
            }
            BigInteger nonce = BigInteger.ZERO;
            int value = 1;
            BigInteger lastNonce;
            while (!stop) {
                Transaction tx = createNewTransaction(targetAcc.getEcKey().getPrivKeyBytes(), target2Address, BigInteger.valueOf(value), nonce);
                sendTransaction(tx);
                logger.trace("Send tx value {} nonce {}", value, getNonce(tx));
                value += 2;
                lastNonce = nonce;
                nonce = nonce.add(BigInteger.ONE);
                try {
                    SecureRandom r = new SecureRandom();
                    Thread.sleep(10000 + (long) r.nextInt(20000));
                    Repository prepository = transactionPool.getRepository();
                    AccountState accountState;
                    accountState = prepository.getAccountState(targetAcc.getAddress());
                    BigInteger accnonce = accountState.getNonce();
                    if (accnonce.compareTo(lastNonce) < 0) {
                        tx = createNewTransaction(targetAcc.getEcKey().getPrivKeyBytes(), target2Address, BigInteger.valueOf(accnonce.intValue() * 2 + 1), accnonce);
                        logger.trace("Resend tx value {} nonce {}", accnonce.intValue() * 2 + 1, getNonce(tx));
                        sendTransaction(tx);
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }.start();
}
Also used : SecureRandom(java.security.SecureRandom) ECKey(org.ethereum.crypto.ECKey) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger)

Example 47 with ECKey

use of org.ethereum.crypto.ECKey in project rskj by rsksmart.

the class PeerDiscoveryMessage method getKey.

public ECKey getKey() {
    byte[] r = new byte[32];
    byte[] s = new byte[32];
    byte v = signature[64];
    if (v == 1) {
        v = 28;
    }
    if (v == 0) {
        v = 27;
    }
    System.arraycopy(signature, 0, r, 0, 32);
    System.arraycopy(signature, 32, s, 0, 32);
    byte[] msgHash = keccak256(wire, 97, wire.length - 97);
    ECKey outKey = null;
    try {
        outKey = ECKey.signatureToKey(msgHash, ECKey.ECDSASignature.fromComponents(r, s, v).toBase64());
    } catch (SignatureException e) {
        logger.error("Error generating key from message", e);
    }
    return outKey;
}
Also used : ECKey(org.ethereum.crypto.ECKey) SignatureException(java.security.SignatureException)

Example 48 with ECKey

use of org.ethereum.crypto.ECKey in project rskj by rsksmart.

the class SyncProcessorTest method createAccount.

private static Account createAccount(String seed) {
    byte[] privateKeyBytes = HashUtil.keccak256(seed.getBytes());
    ECKey key = ECKey.fromPrivate(privateKeyBytes);
    Account account = new Account(key);
    return account;
}
Also used : ECKey(org.ethereum.crypto.ECKey)

Example 49 with ECKey

use of org.ethereum.crypto.ECKey in project rskj by rsksmart.

the class EncryptionHandshake method makeAuthInitiate.

AuthResponseMessage makeAuthInitiate(AuthInitiateMessage initiate, ECKey key) {
    initiatorNonce = initiate.nonce;
    remotePublicKey = initiate.publicKey;
    BigInteger secretScalar = remotePublicKey.multiply(key.getPrivKey()).normalize().getXCoord().toBigInteger();
    byte[] token = ByteUtil.bigIntegerToBytes(secretScalar, NONCE_SIZE);
    byte[] signed = xor(token, initiatorNonce);
    ECKey ephemeral = ECKey.recoverFromSignature(recIdFromSignatureV(initiate.signature.v), initiate.signature, signed, false);
    if (ephemeral == null) {
        throw new RuntimeException("failed to recover signatue from message");
    }
    remoteEphemeralKey = ephemeral.getPubKeyPoint();
    AuthResponseMessage response = new AuthResponseMessage();
    response.isTokenUsed = initiate.isTokenUsed;
    response.ephemeralPublicKey = ephemeralKey.getPubKeyPoint();
    response.nonce = responderNonce;
    return response;
}
Also used : BigInteger(java.math.BigInteger) ECKey(org.ethereum.crypto.ECKey)

Example 50 with ECKey

use of org.ethereum.crypto.ECKey in project rskj by rsksmart.

the class PacketDecoderTest method decode.

@Test
public void decode() throws Exception {
    ECKey key1 = ECKey.fromPrivate(Hex.decode(KEY_1)).decompress();
    String check = UUID.randomUUID().toString();
    ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
    PacketDecoder decoder = new PacketDecoder();
    // Decode Ping Message
    PingPeerMessage nodeMessage = PingPeerMessage.create("localhost", 44035, check, key1);
    InetSocketAddress sender = new InetSocketAddress("localhost", 44035);
    this.assertDecodedMessage(decoder.decodeMessage(ctx, nodeMessage.getPacket(), sender), sender, DiscoveryMessageType.PING);
    // Decode Pong Message
    PongPeerMessage pongPeerMessage = PongPeerMessage.create("localhost", 44036, check, key1);
    sender = new InetSocketAddress("localhost", 44036);
    this.assertDecodedMessage(decoder.decodeMessage(ctx, pongPeerMessage.getPacket(), sender), sender, DiscoveryMessageType.PONG);
    // Decode Find Node Message
    FindNodePeerMessage findNodePeerMessage = FindNodePeerMessage.create(key1.getNodeId(), check, key1);
    sender = new InetSocketAddress("localhost", 44037);
    this.assertDecodedMessage(decoder.decodeMessage(ctx, findNodePeerMessage.getPacket(), sender), sender, DiscoveryMessageType.FIND_NODE);
    // Decode Neighbors Message
    NeighborsPeerMessage neighborsPeerMessage = NeighborsPeerMessage.create(new ArrayList<>(), check, key1);
    sender = new InetSocketAddress("localhost", 44038);
    this.assertDecodedMessage(decoder.decodeMessage(ctx, neighborsPeerMessage.getPacket(), sender), sender, DiscoveryMessageType.NEIGHBORS);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ECKey(org.ethereum.crypto.ECKey) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Test(org.junit.Test)

Aggregations

ECKey (org.ethereum.crypto.ECKey)76 Test (org.junit.Test)43 BigInteger (java.math.BigInteger)17 NodeDistanceTable (co.rsk.net.discovery.table.NodeDistanceTable)10 ArrayList (java.util.ArrayList)10 Node (org.ethereum.net.rlpx.Node)10 InetSocketAddress (java.net.InetSocketAddress)9 Transaction (org.ethereum.core.Transaction)8 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)7 RepositoryImpl (co.rsk.db.RepositoryImpl)7 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)7 Channel (io.netty.channel.Channel)7 org.ethereum.core (org.ethereum.core)7 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)6 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)4 Account (org.ethereum.core.Account)4 RskAddress (co.rsk.core.RskAddress)3