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();
}
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;
}
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;
}
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;
}
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);
}
Aggregations