Search in sources :

Example 1 with AccountPo

use of io.nuls.account.storage.po.AccountPo 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 2 with AccountPo

use of io.nuls.account.storage.po.AccountPo 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 3 with AccountPo

use of io.nuls.account.storage.po.AccountPo in project nuls by nuls-io.

the class AccountServiceImpl method finishImport.

private void finishImport(Account account) {
    account.setOk(true);
    accountStorageService.saveAccount(new AccountPo(account));
    accountCacheService.localAccountMaps.put(account.getAddress().getBase58(), account);
}
Also used : AccountPo(io.nuls.account.storage.po.AccountPo)

Example 4 with AccountPo

use of io.nuls.account.storage.po.AccountPo 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 5 with AccountPo

use of io.nuls.account.storage.po.AccountPo 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();
    }
}
Also used : AccountPo(io.nuls.account.storage.po.AccountPo) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException) Result(io.nuls.kernel.model.Result)

Aggregations

AccountPo (io.nuls.account.storage.po.AccountPo)12 NulsException (io.nuls.kernel.exception.NulsException)8 Result (io.nuls.kernel.model.Result)7 Account (io.nuls.account.model.Account)5 AliasPo (io.nuls.account.storage.po.AliasPo)4 IOException (java.io.IOException)3 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)2 MultiSigAccount (io.nuls.account.model.MultiSigAccount)2 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)2 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)1 Address (io.nuls.kernel.model.Address)1 SignatureException (java.security.SignatureException)1