Search in sources :

Example 26 with NulsException

use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.

the class AccountServiceImpl method getMultiSigAccountList.

/**
 * 获取所有账户集合
 * Query all account collections.
 *
 * @return account list of all accounts.
 */
@Override
public Result<List<MultiSigAccount>> getMultiSigAccountList() {
    List<byte[]> list = this.multiSigAccountStorageService.getAccountList().getData();
    if (null == list) {
        return Result.getFailed(KernelErrorCode.DATA_NOT_FOUND);
    }
    List<MultiSigAccount> accountList = new ArrayList<>();
    for (byte[] bytes : list) {
        MultiSigAccount account = new MultiSigAccount();
        try {
            account.parse(new NulsByteBuffer(bytes, 0));
        } catch (NulsException e) {
            Log.error(e);
        }
        accountList.add(account);
    }
    List<AliasPo> aliasList = aliasStorageService.getAliasList().getData();
    for (AliasPo aliasPo : aliasList) {
        if (aliasPo.getAddress()[2] != NulsContext.P2SH_ADDRESS_TYPE) {
            continue;
        }
        for (MultiSigAccount multiSigAccount : accountList) {
            if (Arrays.equals(aliasPo.getAddress(), multiSigAccount.getAddress().getAddressBytes())) {
                multiSigAccount.setAlias(aliasPo.getAlias());
                break;
            }
        }
    }
    return new Result<List<MultiSigAccount>>().setData(accountList);
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) AliasPo(io.nuls.account.storage.po.AliasPo) NulsByteBuffer(io.nuls.kernel.utils.NulsByteBuffer)

Example 27 with NulsException

use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.

the class AccountServiceImpl method importAccountFormKeyStore.

@Override
public Result<Account> importAccountFormKeyStore(AccountKeyStore keyStore, String password) {
    if (null == keyStore || StringUtils.isBlank(keyStore.getAddress())) {
        return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
    }
    if (!AddressTool.validAddress(keyStore.getAddress())) {
        return Result.getFailed(AccountErrorCode.ADDRESS_ERROR);
    }
    Account account;
    byte[] priKey = null;
    if (null != keyStore.getPrikey() && keyStore.getPrikey().length > 0) {
        if (!ECKey.isValidPrivteHex(Hex.encode(keyStore.getPrikey()))) {
            return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
        }
        priKey = keyStore.getPrikey();
        try {
            account = AccountTool.createAccount(Hex.encode(priKey));
        } catch (NulsException e) {
            return Result.getFailed(e.getErrorCode());
        }
        // 如果私钥生成的地址和keystore的地址不相符,说明私钥错误
        if (!account.getAddress().getBase58().equals(keyStore.getAddress())) {
            return Result.getFailed(AccountErrorCode.PRIVATE_KEY_WRONG);
        }
    } else if (null == keyStore.getPrikey() && null != keyStore.getEncryptedPrivateKey()) {
        if (!StringUtils.validPassword(password)) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
        }
        try {
            priKey = AESEncrypt.decrypt(Hex.decode(keyStore.getEncryptedPrivateKey()), password);
            account = AccountTool.createAccount(Hex.encode(priKey));
        } catch (CryptoException e) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
        } catch (NulsException e) {
            return Result.getFailed(e.getErrorCode());
        }
        // 如果私钥生成的地址和keystore的地址不相符,说明密码错误
        if (!account.getAddress().getBase58().equals(keyStore.getAddress())) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
        }
    } else {
        return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
    }
    Alias aliasDb = null;
    if (StringUtils.isNotBlank(keyStore.getAlias())) {
        aliasDb = aliasService.getAlias(keyStore.getAlias());
    }
    if (null != aliasDb && AddressTool.getStringAddressByBytes(aliasDb.getAddress()).equals(account.getAddress().toString())) {
        account.setAlias(aliasDb.getAlias());
    } else {
        List<AliasPo> list = aliasStorageService.getAliasList().getData();
        for (AliasPo aliasPo : list) {
            // 如果全网别名中的地址有和当前导入的账户地址相同,则赋值别名到账户中
            if (AddressTool.getStringAddressByBytes(aliasPo.getAddress()).equals(account.getAddress().toString())) {
                account.setAlias(aliasPo.getAlias());
                break;
            }
        }
    }
    if (StringUtils.validPassword(password)) {
        try {
            account.encrypt(password);
        } catch (NulsException e) {
            Log.error(e);
            return Result.getFailed(e.getErrorCode());
        }
    }
    account.setOk(false);
    AccountPo po = new AccountPo(account);
    Result result = accountStorageService.saveAccount(po);
    if (result.isFailed()) {
        return result;
    }
    accountCacheService.localAccountMaps.put(account.getAddress().getBase58(), account);
    TaskManager.asynExecuteRunnable(new Runnable() {

        @Override
        public void run() {
            String address = account.getAddress().getBase58();
            Result res = accountLedgerService.importLedger(address);
            if (res.isFailed()) {
                AccountServiceImpl.this.removeAccount(address, password);
            } else {
                AccountServiceImpl.this.finishImport(account);
            }
        }
    });
    return Result.getSuccess().setData(account);
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) AccountPo(io.nuls.account.storage.po.AccountPo) AliasPo(io.nuls.account.storage.po.AliasPo) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) Result(io.nuls.kernel.model.Result)

Example 28 with NulsException

use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.

the class AccountServiceImpl method importAccount.

@Override
public Result<Account> importAccount(String prikey, String password) {
    if (!ECKey.isValidPrivteHex(prikey)) {
        return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
    }
    Account account;
    try {
        account = AccountTool.createAccount(prikey);
    } catch (NulsException e) {
        return Result.getFailed(AccountErrorCode.PRIVATE_KEY_WRONG);
    }
    if (StringUtils.validPassword(password)) {
        try {
            account.encrypt(password);
        } catch (NulsException e) {
            Log.error(e);
            return Result.getFailed(e.getErrorCode());
        }
    }
    // 扫所全网别名对比地址符合就设置
    // String alias = null;
    Account acc = getAccountByAddress(account.getAddress().toString());
    if (null == acc) {
        List<AliasPo> list = aliasStorageService.getAliasList().getData();
        for (AliasPo aliasPo : list) {
            // 如果全网别名中的地址有和当前导入的账户地址相同,则赋值别名到账户中
            if (AddressTool.getStringAddressByBytes(aliasPo.getAddress()).equals(account.getAddress().toString())) {
                account.setAlias(aliasPo.getAlias());
                break;
            }
        }
    } else {
        account.setAlias(acc.getAlias());
    }
    // Result res = accountLedgerService.importLedgerByAddress(account.getAddress().getBase58());
    // if (res.isFailed()) {
    // return res;
    // }
    account.setOk(false);
    AccountPo po = new AccountPo(account);
    Result result = accountStorageService.saveAccount(po);
    if (result.isFailed()) {
        return result;
    }
    accountCacheService.localAccountMaps.put(account.getAddress().getBase58(), account);
    TaskManager.asynExecuteRunnable(new Runnable() {

        @Override
        public void run() {
            String address = account.getAddress().getBase58();
            Result res = accountLedgerService.importLedger(address);
            if (res.isFailed()) {
                AccountServiceImpl.this.removeAccount(address, password);
            } else {
                AccountServiceImpl.this.finishImport(account);
            }
        }
    });
    return Result.getSuccess().setData(account);
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) AccountPo(io.nuls.account.storage.po.AccountPo) AliasPo(io.nuls.account.storage.po.AliasPo) Result(io.nuls.kernel.model.Result)

Example 29 with NulsException

use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.

the class AccountServiceImpl method updatePasswordByAccountKeyStore.

@Override
public Result<Account> updatePasswordByAccountKeyStore(AccountKeyStore keyStore, String password) {
    AssertUtil.canNotEmpty(keyStore, AccountErrorCode.PARAMETER_ERROR.getMsg());
    AssertUtil.canNotEmpty(keyStore.getAddress(), AccountErrorCode.PARAMETER_ERROR.getMsg());
    AssertUtil.canNotEmpty(password, AccountErrorCode.PARAMETER_ERROR.getMsg());
    Account account;
    byte[] priKey = null;
    if (null != keyStore.getPrikey() && keyStore.getPrikey().length > 0) {
        if (!ECKey.isValidPrivteHex(Hex.encode(keyStore.getPrikey()))) {
            return Result.getFailed(AccountErrorCode.PARAMETER_ERROR);
        }
        priKey = keyStore.getPrikey();
        try {
            account = AccountTool.createAccount(Hex.encode(priKey));
        } catch (NulsException e) {
            return Result.getFailed(AccountErrorCode.FAILED);
        }
    } else {
        try {
            account = AccountTool.createAccount();
        } catch (NulsException e) {
            return Result.getFailed(AccountErrorCode.FAILED);
        }
        account.setAddress(new Address(keyStore.getAddress()));
    }
    try {
        account.encrypt(password);
    } catch (NulsException e) {
        Log.error(e);
        return Result.getFailed(e.getErrorCode());
    }
    if (StringUtils.isNotBlank(keyStore.getAlias())) {
        Alias aliasDb = aliasService.getAlias(keyStore.getAlias());
        if (null != aliasDb && account.getAddress().toString().equals(AddressTool.getStringAddressByBytes(aliasDb.getAddress()))) {
            account.setAlias(aliasDb.getAlias());
        } else {
            List<AliasPo> list = aliasStorageService.getAliasList().getData();
            for (AliasPo aliasPo : list) {
                // 如果全网别名中的地址有和当前导入的账户地址相同,则赋值别名到账户中
                if (AddressTool.getStringAddressByBytes(aliasPo.getAddress()).equals(account.getAddress().toString())) {
                    account.setAlias(aliasPo.getAlias());
                    break;
                }
            }
        }
    }
    account.setOk(false);
    AccountPo po = new AccountPo(account);
    Result result = accountStorageService.saveAccount(po);
    if (result.isFailed()) {
        return result;
    }
    accountCacheService.localAccountMaps.put(account.getAddress().getBase58(), account);
    TaskManager.asynExecuteRunnable(new Runnable() {

        @Override
        public void run() {
            String address = account.getAddress().getBase58();
            Result res = accountLedgerService.importLedger(address);
            if (res.isFailed()) {
                AccountServiceImpl.this.removeAccount(address, password);
            } else {
                AccountServiceImpl.this.finishImport(account);
            }
        }
    });
    return Result.getSuccess().setData(account);
}
Also used : Address(io.nuls.kernel.model.Address) NulsException(io.nuls.kernel.exception.NulsException) AccountPo(io.nuls.account.storage.po.AccountPo) AliasPo(io.nuls.account.storage.po.AliasPo) Result(io.nuls.kernel.model.Result)

Example 30 with NulsException

use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.

the class AccountBaseService method getAllPrivateKey.

/**
 * 获取所有本地账户账户私钥,必须保证所有账户密码一致,
 * 如果本地账户中的密码不一致,将返回错误信息
 * Get the all local private keys
 *
 * @param password
 * @return
 */
public Result getAllPrivateKey(String password) {
    Collection<Account> localAccountList = accountService.getAccountList().getData();
    if (localAccountList == null || localAccountList.isEmpty()) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
    }
    if (StringUtils.isNotBlank(password) && !StringUtils.validPassword(password)) {
        return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
    }
    List<String> list = new ArrayList<>();
    for (Account account : localAccountList) {
        if (account.isEncrypted()) {
            if (StringUtils.isBlank(password)) {
                // 如果有账户是加密的,但是没有传密码,则返回错误信息;
                return Result.getFailed(AccountErrorCode.HAVE_ENCRYPTED_ACCOUNT);
            }
            try {
                byte[] priKeyBytes = account.getPriKey(password);
                list.add(Hex.encode(priKeyBytes));
            } catch (NulsException e) {
                return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
            }
        } else {
            if (StringUtils.isNotBlank(password)) {
                // 账户集合中有未加密账户,但是参数传了密码
                return Result.getFailed(AccountErrorCode.HAVE_UNENCRYPTED_ACCOUNT);
            }
            list.add(Hex.encode(account.getPriKey()));
        }
    }
    Map<String, List<String>> map = new HashMap<>();
    map.put("value", list);
    return Result.getSuccess().setData(map);
}
Also used : Account(io.nuls.account.model.Account) NulsException(io.nuls.kernel.exception.NulsException)

Aggregations

NulsException (io.nuls.kernel.exception.NulsException)109 IOException (java.io.IOException)35 Account (io.nuls.account.model.Account)25 ValidateResult (io.nuls.kernel.validate.ValidateResult)23 ECKey (io.nuls.core.tools.crypto.ECKey)20 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)18 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)13 Coin (io.nuls.kernel.model.Coin)12 MultiSigAccount (io.nuls.account.model.MultiSigAccount)11 ContractResult (io.nuls.contract.dto.ContractResult)9 NulsByteBuffer (io.nuls.kernel.utils.NulsByteBuffer)9 ArrayList (java.util.ArrayList)9 TransactionSignature (io.nuls.kernel.script.TransactionSignature)8 BigInteger (java.math.BigInteger)8 TransactionDataResult (io.nuls.account.ledger.model.TransactionDataResult)7 AccountPo (io.nuls.account.storage.po.AccountPo)7 AliasPo (io.nuls.account.storage.po.AliasPo)7 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)7