Search in sources :

Example 26 with ECKey

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

the class BlockTest method testParseRemascTransaction.

@Test
public void testParseRemascTransaction() {
    List<Transaction> txs = new ArrayList<>();
    Transaction txNotToRemasc = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ONE.toByteArray(), BigInteger.valueOf(21000).toByteArray(), new ECKey().getAddress(), BigInteger.valueOf(1000).toByteArray(), null);
    txNotToRemasc.sign(new ECKey().getPrivKeyBytes());
    txs.add(txNotToRemasc);
    Transaction txToRemascThatIsNotTheLatestTx = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ONE.toByteArray(), BigInteger.valueOf(21000).toByteArray(), PrecompiledContracts.REMASC_ADDR.getBytes(), BigInteger.valueOf(1000).toByteArray(), null);
    txToRemascThatIsNotTheLatestTx.sign(new ECKey().getPrivKeyBytes());
    txs.add(txToRemascThatIsNotTheLatestTx);
    Transaction remascTx = new RemascTransaction(1);
    txs.add(remascTx);
    Block block = new Block(// parent hash
    PegTestUtils.createHash3().getBytes(), // uncle hash
    EMPTY_LIST_HASH, // coinbase
    TestUtils.randomAddress().getBytes(), // logs bloom
    new Bloom().getData(), // difficulty
    BigInteger.ONE.toByteArray(), 1, // gasLimit
    BigInteger.valueOf(4000000).toByteArray(), // gasUsed
    3000000, // timestamp
    100, // extraData
    new byte[0], // mixHash
    new byte[0], // provisory nonce
    new byte[] { 0 }, // receipts root
    HashUtil.EMPTY_TRIE_HASH, // transaction root
    BlockChainImpl.calcTxTrie(txs), // EMPTY_TRIE_HASH,   // state root
    HashUtil.EMPTY_TRIE_HASH, // transaction list
    txs, // uncle list
    null, BigInteger.TEN.toByteArray(), Coin.ZERO);
    Block parsedBlock = new Block(block.getEncoded());
    Assert.assertEquals(ImmutableTransaction.class, parsedBlock.getTransactionsList().get(0).getClass());
    Assert.assertEquals(ImmutableTransaction.class, parsedBlock.getTransactionsList().get(1).getClass());
    Assert.assertEquals(RemascTransaction.class, parsedBlock.getTransactionsList().get(2).getClass());
}
Also used : RemascTransaction(co.rsk.remasc.RemascTransaction) RemascTransaction(co.rsk.remasc.RemascTransaction) ArrayList(java.util.ArrayList) ECKey(org.ethereum.crypto.ECKey) Test(org.junit.Test)

Example 27 with ECKey

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

the class TransactionTest method test2.

@Test
public /* achieve public key of the sender */
void test2() throws Exception {
    if (config.getBlockchainConfig().getCommonConstants().getChainId() != 0)
        return;
    // cat --> 79b08ad8787060333663d19704909ee7b1903e58
    // cow --> cd2a3d9f938e13cd947ec05abc7fe734df8dd826
    BigInteger value = new BigInteger("1000000000000000000000");
    byte[] privKey = HashUtil.keccak256("cat".getBytes());
    ECKey ecKey = ECKey.fromPrivate(privKey);
    byte[] senderPrivKey = HashUtil.keccak256("cow".getBytes());
    byte[] gasPrice = Hex.decode("09184e72a000");
    byte[] gas = Hex.decode("4255");
    // Tn (nonce); Tp(pgas); Tg(gaslimi); Tt(value); Tv(value); Ti(sender);  Tw; Tr; Ts
    Transaction tx = new Transaction(null, gasPrice, gas, ecKey.getAddress(), value.toByteArray(), null);
    tx.sign(senderPrivKey);
    System.out.println("v\t\t\t: " + Hex.toHexString(new byte[] { tx.getSignature().v }));
    System.out.println("r\t\t\t: " + Hex.toHexString(BigIntegers.asUnsignedByteArray(tx.getSignature().r)));
    System.out.println("s\t\t\t: " + Hex.toHexString(BigIntegers.asUnsignedByteArray(tx.getSignature().s)));
    System.out.println("RLP encoded tx\t\t: " + Hex.toHexString(tx.getEncoded()));
    // retrieve the signer/sender of the transaction
    ECKey key = ECKey.signatureToKey(tx.getHash().getBytes(), tx.getSignature().toBase64());
    System.out.println("Tx unsigned RLP\t\t: " + Hex.toHexString(tx.getEncodedRaw()));
    System.out.println("Tx signed   RLP\t\t: " + Hex.toHexString(tx.getEncoded()));
    System.out.println("Signature public key\t: " + Hex.toHexString(key.getPubKey()));
    System.out.println("Sender is\t\t: " + Hex.toHexString(key.getAddress()));
    assertEquals("cd2a3d9f938e13cd947ec05abc7fe734df8dd826", Hex.toHexString(key.getAddress()));
    System.out.println(tx.toString());
}
Also used : BigInteger(java.math.BigInteger) ECKey(org.ethereum.crypto.ECKey) Test(org.junit.Test)

Example 28 with ECKey

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

the class TransactionTest method test3.

@Test
public /* achieve public key of the sender when signature manually set */
void test3() throws Exception {
    if (config.getBlockchainConfig().getCommonConstants().getChainId() != 0)
        return;
    // cat --> 79b08ad8787060333663d19704909ee7b1903e58
    // cow --> cd2a3d9f938e13cd947ec05abc7fe734df8dd826
    BigInteger value = new BigInteger("1000000000000000000000");
    byte[] privKey = HashUtil.keccak256("cat".getBytes());
    ECKey ecKey = ECKey.fromPrivate(privKey);
    byte[] senderPrivKey = HashUtil.keccak256("cow".getBytes());
    byte[] gasPrice = Hex.decode("09184e72a000");
    byte[] gas = Hex.decode("4255");
    // Tn (nonce); Tp(pgas); Tg(gaslimi); Tt(value); Tv(value); Ti(sender);  Tw; Tr; Ts
    Transaction tx = new Transaction(null, gasPrice, gas, ecKey.getAddress(), value.toByteArray(), null);
    Keccak256 hash = tx.getRawHash();
    ECKey.ECDSASignature signature = ECKey.fromPrivate(senderPrivKey).sign(hash.getBytes());
    tx.setSignature(signature);
    System.out.println("v\t\t\t: " + Hex.toHexString(new byte[] { tx.getSignature().v }));
    System.out.println("r\t\t\t: " + Hex.toHexString(BigIntegers.asUnsignedByteArray(tx.getSignature().r)));
    System.out.println("s\t\t\t: " + Hex.toHexString(BigIntegers.asUnsignedByteArray(tx.getSignature().s)));
    System.out.println("RLP encoded tx\t\t: " + Hex.toHexString(tx.getEncoded()));
    // retrieve the signer/sender of the transaction
    ECKey key = ECKey.signatureToKey(tx.getHash().getBytes(), tx.getSignature().toBase64());
    System.out.println("Tx unsigned RLP\t\t: " + Hex.toHexString(tx.getEncodedRaw()));
    System.out.println("Tx signed   RLP\t\t: " + Hex.toHexString(tx.getEncoded()));
    System.out.println("Signature public key\t: " + Hex.toHexString(key.getPubKey()));
    System.out.println("Sender is\t\t: " + Hex.toHexString(key.getAddress()));
    assertEquals("cd2a3d9f938e13cd947ec05abc7fe734df8dd826", Hex.toHexString(key.getAddress()));
    System.out.println(tx.toString());
}
Also used : BigInteger(java.math.BigInteger) ECKey(org.ethereum.crypto.ECKey) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 29 with ECKey

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

the class BlockExecutorTest method createAccount.

public 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 30 with ECKey

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

the class MinerServerImpl method generateFallbackBlock.

@Override
public boolean generateFallbackBlock() {
    Block newBlock;
    synchronized (lock) {
        if (latestBlock == null) {
            return false;
        }
        // Iterate and find a block that can be privately mined.
        Block workingBlock = latestBlock;
        newBlock = workingBlock.cloneBlock();
    }
    boolean isEvenBlockNumber = (newBlock.getNumber() % 2) == 0;
    String path = config.fallbackMiningKeysDir();
    if (privKey0 == null) {
        privKey0 = readFromFile(new File(path, "privkey0.bin"));
    }
    if (privKey1 == null) {
        privKey1 = readFromFile(new File(path, "privkey1.bin"));
    }
    if (!isEvenBlockNumber && privKey1 == null) {
        return false;
    }
    if (isEvenBlockNumber && privKey0 == null) {
        return false;
    }
    ECKey privateKey;
    if (isEvenBlockNumber) {
        privateKey = ECKey.fromPrivate(privKey0);
    } else {
        privateKey = ECKey.fromPrivate(privKey1);
    }
    // 
    // Set the timestamp now to control mining interval better
    // 
    BlockHeader newHeader = newBlock.getHeader();
    newHeader.setTimestamp(this.getCurrentTimeInSeconds());
    Block parentBlock = blockchain.getBlockByHash(newHeader.getParentHash().getBytes());
    newHeader.setDifficulty(difficultyCalculator.calcDifficulty(newHeader, parentBlock.getHeader()));
    // fallback mining marker
    newBlock.setExtraData(new byte[] { 42 });
    byte[] signature = fallbackSign(newBlock.getHashForMergedMining(), privateKey);
    newBlock.setBitcoinMergedMiningHeader(signature);
    newBlock.seal();
    if (!isValid(newBlock)) {
        String message = "Invalid block supplied by miner: " + newBlock.getShortHash() + " " + newBlock.getShortHashForMergedMining() + " at height " + newBlock.getNumber();
        logger.error(message);
        return false;
    } else {
        // never reuse if block is valid
        latestBlock = null;
        ImportResult importResult = ethereum.addNewMinedBlock(newBlock);
        fallbackBlocksGenerated++;
        logger.info("Mined block import result is {}: {} {} at height {}", importResult, newBlock.getShortHash(), newBlock.getShortHashForMergedMining(), newBlock.getNumber());
        return importResult.isSuccessful();
    }
}
Also used : ECKey(org.ethereum.crypto.ECKey) File(java.io.File)

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