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