Search in sources :

Example 26 with Result

use of io.nuls.kernel.model.Result in project nuls by nuls-io.

the class AccountServiceImpl method removeAccount.

@Override
public Result removeAccount(String address, String password) {
    if (!AddressTool.validAddress(address)) {
        return Result.getFailed(AccountErrorCode.ADDRESS_ERROR);
    }
    Account account = getAccountByAddress(address);
    if (account == null) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
    }
    // 加过密(有密码)并且没有解锁, 就验证密码 Already encrypted(Added password) and did not unlock, verify password
    if (account.isEncrypted() && account.isLocked()) {
        if (!account.validatePassword(password)) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG);
        }
    }
    Result result = accountStorageService.removeAccount(account.getAddress());
    if (result.isFailed()) {
        return result;
    }
    accountLedgerService.deleteUnconfirmedTx(account.getAddress().getAddressBytes());
    accountCacheService.localAccountMaps.remove(account.getAddress().getBase58());
    return Result.getSuccess().setData(true);
}
Also used : Result(io.nuls.kernel.model.Result)

Example 27 with Result

use of io.nuls.kernel.model.Result 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 Result

use of io.nuls.kernel.model.Result 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 Result

use of io.nuls.kernel.model.Result 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();
    }
}
Also used : Script(io.nuls.kernel.script.Script) Address(io.nuls.kernel.model.Address) 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)

Example 30 with Result

use of io.nuls.kernel.model.Result in project nuls by nuls-io.

the class AccountServiceImpl method isEncrypted.

@Override
public Result isEncrypted(String address) {
    if (!AddressTool.validAddress(address)) {
        return Result.getFailed(AccountErrorCode.ADDRESS_ERROR);
    }
    Account account = getAccountByAddress(address);
    if (null == account) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
    }
    Result result = new Result();
    boolean rs = account.isEncrypted();
    result.setSuccess(true);
    result.setData(rs);
    return result;
}
Also used : Result(io.nuls.kernel.model.Result)

Aggregations

Result (io.nuls.kernel.model.Result)70 NulsException (io.nuls.kernel.exception.NulsException)16 IOException (java.io.IOException)15 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)12 ValidateResult (io.nuls.kernel.validate.ValidateResult)11 AccountPo (io.nuls.account.storage.po.AccountPo)7 RpcClientResult (io.nuls.kernel.model.RpcClientResult)7 ArrayList (java.util.ArrayList)7 Account (io.nuls.account.model.Account)6 NulsDigestData (io.nuls.kernel.model.NulsDigestData)5 Test (org.junit.Test)5 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)4 BatchOperation (io.nuls.db.service.BatchOperation)4 Address (io.nuls.kernel.model.Address)4 Block (io.nuls.kernel.model.Block)4 BlockHeader (io.nuls.kernel.model.BlockHeader)4 Transaction (io.nuls.kernel.model.Transaction)4 Node (io.nuls.network.model.Node)4 NotFound (io.nuls.protocol.model.NotFound)4 ApiOperation (io.swagger.annotations.ApiOperation)4