Search in sources :

Example 31 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class AionTransactionTest method testSerializationZero.

@Test
public void testSerializationZero() {
    byte[] nonce = RandomUtils.nextBytes(16);
    Address to = Address.wrap(RandomUtils.nextBytes(32));
    byte[] value = RandomUtils.nextBytes(16);
    byte[] data = RandomUtils.nextBytes(64);
    long nrg = 0;
    long nrgPrice = 0;
    byte type = 0;
    AionTransaction tx = new AionTransaction(nonce, to, value, data, nrg, nrgPrice, type);
    tx.sign(ECKeyFac.inst().create());
    AionTransaction tx2 = new AionTransaction(tx.getEncoded());
    assertTransactionEquals(tx, tx2);
}
Also used : Address(org.aion.base.type.Address) AionTransaction(org.aion.zero.types.AionTransaction) Test(org.junit.Test)

Example 32 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class AionTransactionTest method testTransactionCost2.

@Test
public void testTransactionCost2() {
    byte[] nonce = DataWord.ONE.getData();
    byte[] from = RandomUtils.nextBytes(Address.ADDRESS_LEN);
    Address to = Address.EMPTY_ADDRESS();
    byte[] value = DataWord.ONE.getData();
    byte[] data = RandomUtils.nextBytes(128);
    long nrg = new DataWord(1000L).longValue();
    long nrgPrice = DataWord.ONE.longValue();
    AionTransaction tx = new AionTransaction(nonce, to, value, data, nrg, nrgPrice);
    long expected = 200000 + 21000;
    for (byte b : data) {
        expected += (b == 0) ? 4 : 64;
    }
    assertEquals(expected, tx.transactionCost(1));
}
Also used : Address(org.aion.base.type.Address) DataWord(org.aion.mcf.vm.types.DataWord) AionTransaction(org.aion.zero.types.AionTransaction) Test(org.junit.Test)

Example 33 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class ArgFltr method fromJSON.

// implements validation of JSONObject received from user to create new filter (web3)
// https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newfilter
public static ArgFltr fromJSON(final JSONObject json) {
    try {
        String fromBlock = json.optString("fromBlock", "latest");
        String toBlock = json.optString("toBlock", "latest");
        // user can choose to pass in either one address, or an array of addresses
        List<byte[]> address = new ArrayList<>();
        if (!json.isNull("address")) {
            JSONArray addressList = json.optJSONArray("address");
            if (addressList == null) {
                // let the address constructor do the validation on the string
                // it will throw if user passes invalid address
                address.add((new Address(json.optString("address", null))).toBytes());
            } else {
                for (int i = 0; i < addressList.length(); i++) {
                    address.add((new Address(addressList.optString(i, null))).toBytes());
                }
            }
        }
        /*
                Topic Parsing Rules:
                -------------------
                Topics are order-dependent. A transaction with a log with topics [A, B] will be matched by
                the following topic filters:

                + [] "anything"
                + [A] "A in first position (and anything after)"
                + [null, B] "anything in first position AND B in second position (and anything after)"
                + [A, B] "A in first position AND B in second position (and anything after)"
                + [[A, B], [A, B]] "(A OR B) in first position AND (A OR B) in second position (and anything after)"
            */
        List<byte[][]> topics = new ArrayList<>();
        JSONArray topicList = json.optJSONArray("topics");
        if (topicList != null) {
            for (Object topic : topicList) {
                if (topic == null || topic.equals(null)) {
                    topics.add((byte[][]) null);
                } else if (topic instanceof String) {
                    byte[][] data = new byte[1][];
                    data[0] = ArgFltr.parseTopic((String) topic);
                    topics.add(data);
                } else if (topic instanceof JSONArray) {
                    // at this point, we should have only strings in this array.
                    // invalid input if assumption not valid
                    JSONArray subTopicList = (JSONArray) topic;
                    List<byte[]> t = new ArrayList<>();
                    for (int i = 0; i < subTopicList.length(); i++) {
                        // ok to throw since, OR clause should not have any nulls
                        String topicString = subTopicList.getString(i);
                        t.add(parseTopic(topicString));
                    }
                    topics.add(t.toArray(new byte[0][]));
                }
            // else just ignore the json node.
            }
        }
        return new ArgFltr(fromBlock, toBlock, address, topics);
    } catch (Exception ex) {
        return null;
    }
}
Also used : Address(org.aion.base.type.Address) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject)

Example 34 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class ArgTxCall method fromJSON.

public static ArgTxCall fromJSON(final JSONObject _jsonObj, long nrgRecommended, long defaultNrgLimit) {
    try {
        Address from = Address.wrap(ByteUtil.hexStringToBytes(_jsonObj.optString("from", "")));
        Address to = Address.wrap(ByteUtil.hexStringToBytes(_jsonObj.optString("to", "")));
        byte[] data = ByteUtil.hexStringToBytes(_jsonObj.optString("data", ""));
        String nonceStr = _jsonObj.optString("nonce", "0x0");
        String valueStr = _jsonObj.optString("value", "0x0");
        BigInteger nonce = nonceStr.indexOf("0x") >= 0 ? TypeConverter.StringHexToBigInteger(nonceStr) : TypeConverter.StringNumberAsBigInt(nonceStr);
        BigInteger value = valueStr.indexOf("0x") >= 0 ? TypeConverter.StringHexToBigInteger(valueStr) : TypeConverter.StringNumberAsBigInt(valueStr);
        String nrgStr = _jsonObj.optString("gas", null);
        String nrgPriceStr = _jsonObj.optString("gasPrice", null);
        long nrg = defaultNrgLimit;
        if (nrgStr != null)
            nrg = nrgStr.indexOf("0x") >= 0 ? TypeConverter.StringHexToBigInteger(nrgStr).longValue() : TypeConverter.StringNumberAsBigInt(nrgStr).longValue();
        long nrgPrice = nrgRecommended;
        if (nrgPriceStr != null)
            nrgPrice = nrgPriceStr.indexOf("0x") >= 0 ? TypeConverter.StringHexToBigInteger(nrgPriceStr).longValue() : TypeConverter.StringNumberAsBigInt(nrgPriceStr).longValue();
        return new ArgTxCall(from, to, data, nonce, value, nrg, nrgPrice);
    } catch (Exception ex) {
        return null;
    }
}
Also used : Address(org.aion.base.type.Address) BigInteger(java.math.BigInteger)

Example 35 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class Keystore method backupAccount.

public static Map<Address, ByteArrayWrapper> backupAccount(Map<Address, String> account) {
    if (account == null) {
        throw new NullPointerException();
    }
    List<File> files = getFiles();
    if (files == null) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("No key file been stored in the kernel.");
        }
        return new java.util.HashMap<>();
    }
    List<File> matchedFile = files.parallelStream().filter(file -> account.entrySet().parallelStream().filter(ac -> file.getName().contains(ac.getKey().toString())).findFirst().isPresent()).collect(Collectors.toList());
    Map<Address, ByteArrayWrapper> res = new HashMap<>();
    for (File file : matchedFile) {
        try {
            String[] frags = file.getName().split("--");
            if (frags.length == 3) {
                Address addr = Address.wrap(frags[2]);
                byte[] content = Files.readAllBytes(file.toPath());
                String pw = account.get(addr);
                if (pw != null) {
                    ECKey key = KeystoreFormat.fromKeystore(content, pw);
                    if (key != null) {
                        res.put(addr, ByteArrayWrapper.wrap(content));
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return res;
}
Also used : AionLoggerFactory(org.aion.log.AionLoggerFactory) java.util(java.util) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) ECKeyFac(org.aion.crypto.ECKeyFac) Logger(org.slf4j.Logger) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) FileAttribute(java.nio.file.attribute.FileAttribute) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) File(java.io.File) Address(org.aion.base.type.Address) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) LogEnum(org.aion.log.LogEnum) org.aion.base.util(org.aion.base.util) Paths(java.nio.file.Paths) Path(java.nio.file.Path) ECKey(org.aion.crypto.ECKey) DateFormat(java.text.DateFormat) Address(org.aion.base.type.Address) HashMap(java.util.HashMap) ECKey(org.aion.crypto.ECKey) IOException(java.io.IOException) File(java.io.File)

Aggregations

Address (org.aion.base.type.Address)61 Test (org.junit.Test)34 BigInteger (java.math.BigInteger)29 AionTransaction (org.aion.zero.types.AionTransaction)23 ITransaction (org.aion.base.type.ITransaction)14 ECKey (org.aion.crypto.ECKey)13 DataWord (org.aion.mcf.vm.types.DataWord)11 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)10 TxPoolA0 (org.aion.txpool.zero.TxPoolA0)9 IRepositoryCache (org.aion.base.db.IRepositoryCache)8 HashMap (java.util.HashMap)6 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)5 ByteUtil.toHexString (org.aion.base.util.ByteUtil.toHexString)5 AccountState (org.aion.mcf.core.AccountState)5 AionBlock (org.aion.zero.impl.types.AionBlock)5 ArrayList (java.util.ArrayList)4 ImportResult (org.aion.mcf.core.ImportResult)4 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 IRepository (org.aion.base.db.IRepository)3 AionContractDetailsImpl (org.aion.zero.db.AionContractDetailsImpl)3