Search in sources :

Example 1 with AccountKeyStoreDto

use of io.nuls.account.rpc.model.AccountKeyStoreDto in project nuls by nuls-io.

the class ImportByKeyStoreProcessor method getAccountKeystoreDto.

/**
 * 根据文件地址获取AccountKeystoreDto对象
 * Gets the AccountKeystoreDto object based on the file address
 * @param path
 * @return
 */
private Result<AccountKeyStoreDto> getAccountKeystoreDto(String path) {
    File file = null;
    try {
        file = new File(URLDecoder.decode(path, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        Log.error(e);
    }
    if (null != file && file.isFile()) {
        StringBuilder ks = new StringBuilder();
        BufferedReader bufferedReader = null;
        String str;
        try {
            bufferedReader = new BufferedReader(new FileReader(file));
            while ((str = bufferedReader.readLine()) != null) {
                if (!str.isEmpty()) {
                    ks.append(str);
                }
            }
            AccountKeyStoreDto accountKeyStoreDto = JSONUtils.json2pojo(ks.toString(), AccountKeyStoreDto.class);
            return Result.getSuccess().setData(accountKeyStoreDto);
        } catch (FileNotFoundException e) {
            return Result.getFailed(AccountErrorCode.ACCOUNTKEYSTORE_FILE_NOT_EXIST);
        } catch (IOException e) {
            return Result.getFailed(AccountErrorCode.ACCOUNTKEYSTORE_FILE_DAMAGED);
        } catch (Exception e) {
            return Result.getFailed(AccountErrorCode.ACCOUNTKEYSTORE_FILE_DAMAGED);
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    Log.error(e);
                }
            }
        }
    }
    return Result.getFailed(AccountErrorCode.ACCOUNTKEYSTORE_FILE_NOT_EXIST);
}
Also used : AccountKeyStoreDto(io.nuls.account.rpc.model.AccountKeyStoreDto)

Example 2 with AccountKeyStoreDto

use of io.nuls.account.rpc.model.AccountKeyStoreDto in project nuls by nuls-io.

the class ImportByKeyStoreProcessor method execute.

@Override
public CommandResult execute(String[] args) {
    String path = args[1];
    String password = CommandHelper.getPwdOptional();
    Result rs = getAccountKeystoreDto(path);
    if (rs.isFailed()) {
        return CommandResult.getFailed(rs.getMsg());
    }
    AccountKeyStoreDto accountKeyStoreDto = (AccountKeyStoreDto) rs.getData();
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("accountKeyStoreDto", accountKeyStoreDto);
    parameters.put("password", password);
    parameters.put("overwrite", false);
    RpcClientResult result = restFul.post("/account/import", parameters);
    if (result.isFailed()) {
        return CommandResult.getFailed(result);
    }
    return CommandResult.getResult(CommandResult.dataTransformValue(result));
}
Also used : HashMap(java.util.HashMap) AccountKeyStoreDto(io.nuls.account.rpc.model.AccountKeyStoreDto) RpcClientResult(io.nuls.kernel.model.RpcClientResult) CommandResult(io.nuls.kernel.model.CommandResult) RpcClientResult(io.nuls.kernel.model.RpcClientResult) Result(io.nuls.kernel.model.Result)

Aggregations

AccountKeyStoreDto (io.nuls.account.rpc.model.AccountKeyStoreDto)2 CommandResult (io.nuls.kernel.model.CommandResult)1 Result (io.nuls.kernel.model.Result)1 RpcClientResult (io.nuls.kernel.model.RpcClientResult)1 HashMap (java.util.HashMap)1