Search in sources :

Example 21 with ECKey

use of org.aion.crypto.ECKey in project aion by aionnetwork.

the class Keystore method getKey.

public static ECKey getKey(String _address, String _password) {
    if (_address.startsWith("0x")) {
        _address = _address.substring(2);
    }
    ECKey key = null;
    List<File> files = getFiles();
    for (File file : files) {
        if (file.getName().contains(_address)) {
            try {
                byte[] content = Files.readAllBytes(file.toPath());
                key = KeystoreFormat.fromKeystore(content, _password);
            } catch (IOException e) {
                key = null;
            }
            break;
        }
    }
    return key;
}
Also used : ECKey(org.aion.crypto.ECKey) IOException(java.io.IOException) File(java.io.File)

Example 22 with ECKey

use of org.aion.crypto.ECKey in project aion by aionnetwork.

the class ApiWeb3Aion method eth_sign.

public Object eth_sign(String _address, String _message) {
    Address address = Address.wrap(_address);
    ECKey key = getAccountKey(address.toString());
    if (key == null)
        return null;
    // Message starts with Unicode Character 'END OF MEDIUM' (U+0019)
    String message = "\u0019Aion Signed Message:\n" + _message.length() + _message;
    byte[] messageHash = HashUtil.keccak256(message.getBytes());
    String signature = TypeConverter.toJsonHex(key.sign(messageHash).getSignature());
    return signature;
}
Also used : Address(org.aion.base.type.Address) ECKey(org.aion.crypto.ECKey) ByteUtil.toHexString(org.aion.base.util.ByteUtil.toHexString)

Example 23 with ECKey

use of org.aion.crypto.ECKey in project aion by aionnetwork.

the class ApiWeb3Aion method eth_sendTransaction.

public Object eth_sendTransaction(JSONObject _tx) {
    ArgTxCall txParams = ArgTxCall.fromJSON(_tx, getRecommendedNrgPrice(), getDefaultNrgLimit());
    // check for unlocked account
    Address address = txParams.getFrom();
    ECKey key = getAccountKey(address.toString());
    // TODO: send json-rpc error code + message
    if (key == null)
        return null;
    if (txParams != null) {
        byte[] response = sendTransaction(txParams);
        return TypeConverter.toJsonHex(response);
    }
    return null;
}
Also used : Address(org.aion.base.type.Address) ECKey(org.aion.crypto.ECKey)

Example 24 with ECKey

use of org.aion.crypto.ECKey in project aion by aionnetwork.

the class TxnPoolTest method benchmarkSnapshot.

@Test
public /* 100K new transactions in pool around 1200ms (cold-call)
     */
void benchmarkSnapshot() {
    Properties config = new Properties();
    config.put("txn-timeout", "100");
    TxPoolA0<ITransaction> tp = new TxPoolA0<>(config);
    List<ITransaction> txnl = new ArrayList<>();
    int cnt = 10000;
    for (ECKey aKey1 : key) {
        Address acc = Address.wrap(aKey1.getAddress());
        for (int i = 0; i < cnt; i++) {
            ITransaction txn = new AionTransaction(BigInteger.valueOf(i).toByteArray(), acc, Address.wrap("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 10000L, 1L);
            ((AionTransaction) txn).sign(aKey1);
            txn.setNrgConsume(100L);
            txnl.add(txn);
        }
    }
    tp.add(txnl);
    assertTrue(tp.size() == cnt * key.size());
    // sort the inserted txs
    long start = System.currentTimeMillis();
    tp.snapshot();
    System.out.println("time spent: " + (System.currentTimeMillis() - start) + " ms.");
    for (ECKey aKey : key) {
        List<BigInteger> nl = tp.getNonceList(Address.wrap(aKey.getAddress()));
        for (int i = 0; i < cnt; i++) {
            assertTrue(nl.get(i).equals(BigInteger.valueOf(i)));
        }
    }
}
Also used : Address(org.aion.base.type.Address) ITransaction(org.aion.base.type.ITransaction) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction) TxPoolA0(org.aion.txpool.zero.TxPoolA0) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 25 with ECKey

use of org.aion.crypto.ECKey in project aion by aionnetwork.

the class TxnPoolTest method benchmarkSnapshot3.

@Test
public /* 1M new transactions with 10000 accounts (100 txs per account)in pool snapshot around 10s (cold-call)
       gen new txns 55s (spent a lot of time to sign tx)
       put txns into pool 2.5s
       snapshot txn 5s
     */
void benchmarkSnapshot3() {
    Properties config = new Properties();
    config.put("txn-timeout", "100");
    TxPoolA0<ITransaction> tp = new TxPoolA0<>(config);
    List<ITransaction> txnl = new ArrayList<>();
    int cnt = 100;
    System.out.println("Gen new transactions --");
    long start = System.currentTimeMillis();
    for (ECKey aKey21 : key2) {
        Address acc = Address.wrap(aKey21.getAddress());
        for (int i = 0; i < cnt; i++) {
            ITransaction txn = new AionTransaction(BigInteger.valueOf(i).toByteArray(), acc, Address.wrap("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 10000L, 1L);
            ((AionTransaction) txn).sign(aKey21);
            txn.setNrgConsume(100L);
            txnl.add(txn);
        }
    }
    System.out.println("time spent: " + (System.currentTimeMillis() - start) + " ms.");
    System.out.println("Adding transactions into pool--");
    start = System.currentTimeMillis();
    tp.add(txnl);
    System.out.println("time spent: " + (System.currentTimeMillis() - start) + " ms.");
    assertTrue(tp.size() == cnt * key2.size());
    // sort the inserted txs
    System.out.println("Snapshoting --");
    start = System.currentTimeMillis();
    tp.snapshot();
    System.out.println("time spent: " + (System.currentTimeMillis() - start) + " ms.");
    for (ECKey aKey2 : key2) {
        List<BigInteger> nl = tp.getNonceList(Address.wrap(aKey2.getAddress()));
        for (int i = 0; i < cnt; i++) {
            assertTrue(nl.get(i).equals(BigInteger.valueOf(i)));
        }
    }
}
Also used : Address(org.aion.base.type.Address) ITransaction(org.aion.base.type.ITransaction) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction) TxPoolA0(org.aion.txpool.zero.TxPoolA0) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Aggregations

ECKey (org.aion.crypto.ECKey)29 Test (org.junit.Test)17 Address (org.aion.base.type.Address)13 AionTransaction (org.aion.zero.types.AionTransaction)12 BigInteger (java.math.BigInteger)10 TxPoolA0 (org.aion.txpool.zero.TxPoolA0)7 AionBlock (org.aion.zero.impl.types.AionBlock)6 ITransaction (org.aion.base.type.ITransaction)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 StandaloneBlockchain (org.aion.zero.impl.StandaloneBlockchain)4 BlockPropagationHandler (org.aion.zero.impl.sync.handler.BlockPropagationHandler)4 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ImportResult (org.aion.mcf.core.ImportResult)2 FileOutputStream (java.io.FileOutputStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 FileAttribute (java.nio.file.attribute.FileAttribute)1