use of org.aion.crypto.ECKey in project aion by aionnetwork.
the class ApiAion method estimateNrg.
// TODO: refactor these ad-hoc transaction creations - violates DRY and is messy
public long estimateNrg(ArgTxCall _params) {
if (_params == null) {
throw new NullPointerException();
}
Address from = _params.getFrom();
if (from.equals(Address.EMPTY_ADDRESS())) {
LOG.error("<send-transaction msg=invalid-from-address>");
return -1L;
}
ECKey key = this.getAccountKey(from.toString());
if (key == null) {
LOG.error("<send-transaction msg=account-not-found>");
return -1L;
}
try {
// Transaction is executed as local transaction, no need to retrieve the real nonce.
byte[] nonce = BigInteger.ZERO.toByteArray();
AionTransaction tx = new AionTransaction(nonce, _params.getTo(), _params.getValue().toByteArray(), _params.getData(), _params.getNrg(), _params.getNrgPrice());
tx.sign(key);
return this.ac.estimateTxNrg(tx, this.ac.getAionHub().getBlockchain().getBestBlock());
} catch (Exception ex) {
return -1L;
}
}
use of org.aion.crypto.ECKey in project aion by aionnetwork.
the class ApiAion method createContract.
public ContractCreateResult createContract(ArgTxCall _params) {
Address from = _params.getFrom();
if (from == null || from.equals(Address.EMPTY_ADDRESS())) {
return null;
}
ECKey key = this.getAccountKey(from.toString());
if (key == null) {
LOG.debug("ApiAion.createContract - null key");
return null;
} else {
try {
synchronized (pendingState) {
byte[] nonce = !(_params.getNonce().equals(BigInteger.ZERO)) ? _params.getNonce().toByteArray() : pendingState.bestNonce(Address.wrap(key.getAddress())).toByteArray();
AionTransaction tx = new AionTransaction(nonce, from, null, _params.getValue().toByteArray(), _params.getData(), _params.getNrg(), _params.getNrgPrice());
tx.sign(key);
pendingState.addPendingTransaction(tx);
ContractCreateResult c = new ContractCreateResult();
c.address = tx.getContractAddress();
c.transId = tx.getHash();
return c;
}
} catch (Exception ex) {
LOG.error("ApiAion.createContract - exception: [{}]", ex.getMessage());
return null;
}
}
}
use of org.aion.crypto.ECKey in project aion by aionnetwork.
the class KeystoreTest method keyCreateAndRetrieve.
@Test
public void keyCreateAndRetrieve() {
String password = randomPassword(10);
String address = Keystore.create(password);
assertNotNull(address);
assertEquals(address.length(), 2 + 64);
System.out.println("new addr: " + address);
ECKey key = Keystore.getKey(address, password);
assertNotNull(key);
}
use of org.aion.crypto.ECKey in project aion by aionnetwork.
the class AccountManager method unlockAccount.
public boolean unlockAccount(Address _address, String _password, int _timeout) {
int timeout = UNLOCK_DEFAULT;
if (_timeout > UNLOCK_MAX) {
timeout = UNLOCK_MAX;
} else if (_timeout > 0) {
timeout = _timeout;
}
ECKey key = Keystore.getKey(_address.toString(), _password);
if (Optional.ofNullable(key).isPresent()) {
Account acc = inst.accounts.get(_address);
long t = Instant.now().getEpochSecond() + timeout;
if (Optional.ofNullable(acc).isPresent()) {
acc.updateTimeout(t);
} else {
Account a = new Account(key, t);
inst.accounts.put(_address, a);
}
LOGGER.debug("<unlock-success addr={}>", _address);
return true;
} else {
LOGGER.debug("<unlock-fail addr={}>", _address);
return false;
}
}
use of org.aion.crypto.ECKey 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