use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class AccountServiceImpl method createMultiAccount.
@Override
public Result<Address> createMultiAccount(List<String> pubkeys, int m) {
locker.lock();
try {
Script redeemScript = ScriptBuilder.createNulsRedeemScript(m, pubkeys);
Address address = new Address(NulsContext.getInstance().getDefaultChainId(), NulsContext.P2SH_ADDRESS_TYPE, SerializeUtils.sha256hash160(redeemScript.getProgram()));
MultiSigAccount account = new MultiSigAccount();
account.setAddress(address);
account.setM(m);
account.addPubkeys(pubkeys);
Result result = this.multiSigAccountStorageService.saveAccount(account.getAddress(), account.serialize());
if (result.isFailed()) {
return result;
}
return result.setData(account);
} catch (Exception e) {
Log.error(e);
throw new NulsRuntimeException(KernelErrorCode.FAILED);
} finally {
locker.unlock();
}
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class AccountServiceImpl method createAccount.
@Override
public Result<List<Account>> createAccount(int count, String password) {
if (count <= 0 || count > AccountTool.CREATE_MAX_SIZE) {
return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
}
if (StringUtils.isNotBlank(password) && !StringUtils.validPassword(password)) {
return Result.getFailed(AccountErrorCode.PASSWORD_FORMAT_WRONG);
}
locker.lock();
try {
List<Account> accounts = new ArrayList<>();
List<AccountPo> accountPos = new ArrayList<>();
for (int i = 0; i < count; i++) {
Account account = AccountTool.createAccount();
if (StringUtils.isNotBlank(password)) {
account.encrypt(password);
}
accounts.add(account);
AccountPo po = new AccountPo(account);
accountPos.add(po);
}
Result result = accountStorageService.saveAccountList(accountPos);
if (result.isFailed()) {
return result;
}
for (Account account : accounts) {
accountCacheService.localAccountMaps.put(account.getAddress().getBase58(), account);
}
return Result.getSuccess().setData(accounts);
} catch (Exception e) {
Log.error(e);
throw new NulsRuntimeException(KernelErrorCode.FAILED);
} finally {
locker.unlock();
}
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class LocalUtxoServiceImpl method unlockCoinData.
@Override
public Result<List<byte[]>> unlockCoinData(Transaction tx, long newLockTime) {
List<byte[]> addresses = new ArrayList<>();
CoinData coinData = tx.getCoinData();
if (coinData != null) {
List<Coin> tos = coinData.getTo();
Coin to;
for (int i = 0, length = tos.size(); i < length; i++) {
to = tos.get(i);
if (to.getLockTime() == -1L) {
Coin needUnLockUtxoNew = new Coin(to.getOwner(), to.getNa(), newLockTime);
needUnLockUtxoNew.setFrom(to.getFrom());
try {
byte[] outKey = ArraysTool.concatenate(tx.getHash().serialize(), new VarInt(i).encode());
saveUTXO(outKey, needUnLockUtxoNew.serialize());
addresses.add(to.getAddress());
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
// todo , think about weather to add a transaction history
break;
}
}
}
return Result.getSuccess().setData(addresses);
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class LocalUtxoServiceImpl method rollbackUnlockTxCoinData.
@Override
public Result<List<byte[]>> rollbackUnlockTxCoinData(Transaction tx) {
List<byte[]> addresses = new ArrayList<>();
CoinData coinData = tx.getCoinData();
if (coinData != null) {
// lock utxo - to
List<Coin> tos = coinData.getTo();
for (int i = 0, length = tos.size(); i < length; i++) {
Coin to = tos.get(i);
if (to.getLockTime() == -1L) {
try {
byte[] outKey = ArraysTool.concatenate(tx.getHash().serialize(), new VarInt(i).encode());
saveUTXO(outKey, to.serialize());
addresses.add(to.getAddress());
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
break;
}
}
}
return Result.getSuccess().setData(addresses);
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class LocalUtxoServiceImpl method saveUtxoForAccount.
@Override
public Result saveUtxoForAccount(Transaction tx, List<byte[]> addressesList) {
if (tx == null || addressesList == null || addressesList.size() == 0) {
return Result.getFailed(KernelErrorCode.NULL_PARAMETER);
}
CoinData coinData = tx.getCoinData();
if (coinData != null) {
// delete - from
List<Coin> froms = coinData.getFrom();
List<byte[]> fromsList = new ArrayList<>();
byte[] fromSource;
byte[] utxoFromSource;
byte[] fromIndex;
Transaction sourceTx;
Coin fromOfFromCoin;
for (Coin from : froms) {
fromSource = from.getOwner();
fromOfFromCoin = from.getFrom();
if (fromOfFromCoin == null) {
utxoFromSource = new byte[tx.getHash().size()];
fromIndex = new byte[fromSource.length - utxoFromSource.length];
System.arraycopy(fromSource, 0, utxoFromSource, 0, tx.getHash().size());
System.arraycopy(fromSource, tx.getHash().size(), fromIndex, 0, fromIndex.length);
try {
sourceTx = ledgerService.getTx(NulsDigestData.fromDigestHex(Hex.encode(utxoFromSource)));
} catch (Exception e) {
continue;
}
if (sourceTx == null) {
return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
}
int index = (int) new VarInt(fromIndex, 0).value;
fromOfFromCoin = sourceTx.getCoinData().getTo().get(index);
from.setFrom(fromOfFromCoin);
}
if (fromOfFromCoin == null) {
Log.warn("from coin not found!");
continue;
}
byte[] toAddress = fromOfFromCoin.getAddress();
boolean addressIsMatch = false;
for (byte[] addresses : addressesList) {
if (Arrays.equals(toAddress, addresses)) {
addressIsMatch = true;
break;
}
}
if (!addressIsMatch) {
continue;
}
fromsList.add(fromSource);
}
// save utxo - to
List<Coin> tos = coinData.getTo();
List<Entry<byte[], byte[]>> toList = new ArrayList<>();
byte[] txHashBytes = null;
try {
txHashBytes = tx.getHash().serialize();
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
byte[] outKey;
Coin to;
byte[] toAddress;
for (int i = 0, length = tos.size(); i < length; i++) {
to = tos.get(i);
toAddress = to.getAddress();
boolean addressIsMatch = false;
for (byte[] addresses : addressesList) {
if (Arrays.equals(toAddress, addresses)) {
addressIsMatch = true;
break;
}
}
if (!addressIsMatch) {
continue;
}
try {
outKey = ArraysTool.concatenate(txHashBytes, new VarInt(i).encode());
toList.add(new Entry<>(outKey, to.serialize()));
} catch (IOException e) {
Log.error(e);
}
}
Result result = localUtxoStorageService.batchSaveAndDeleteUTXO(toList, fromsList);
if (result.isFailed() || result.getData() == null || (int) result.getData() != toList.size() + fromsList.size()) {
return Result.getFailed();
}
}
return Result.getSuccess();
}
Aggregations