All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.dl.client.tool.LedgerValidation Maven / Gradle / Ivy

/*
 * This file is part of the Scalar DL client SDK.
 * Copyright (c) 2019 Scalar, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 *
 * You can be released from the requirements of the license by purchasing
 * a commercial license. For more information, please contact Scalar, Inc.
 */
package com.scalar.dl.client.tool;

import com.scalar.dl.client.config.ClientConfig;
import com.scalar.dl.client.exception.ClientException;
import com.scalar.dl.client.service.ClientService;
import com.scalar.dl.client.service.ClientServiceFactory;
import com.scalar.dl.ledger.asset.AssetProof;
import com.scalar.dl.ledger.model.LedgerValidationResult;
import java.io.File;
import java.util.Base64;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonValue;
import picocli.CommandLine;
import picocli.CommandLine.Command;

@Command(name = "validate-ledger", description = "Validate a specified asset in a ledger.")
public class LedgerValidation implements Callable {

  @CommandLine.Option(
      names = {"--properties", "--config"},
      required = true,
      paramLabel = "PROPERTIES_FILE",
      description = "A configuration file in properties format.")
  private String properties;

  @CommandLine.Option(
      names = {"--asset-id"},
      required = true,
      paramLabel = "ASSET_ID",
      description =
          "The ID (and the ages) of an asset. Format: 'asset_id' or 'asset_id,start_age,end_age'")
  private List assetIds;

  @CommandLine.Option(
      names = {"-h", "--help"},
      usageHelp = true,
      description = "display the help message.")
  boolean helpRequested;

  public static void main(String[] args) {
    int exitCode = new CommandLine(new LedgerValidation()).execute(args);
    System.exit(exitCode);
  }

  @Override
  public Integer call() throws Exception {
    ClientServiceFactory factory = new ClientServiceFactory(new ClientConfig(new File(properties)));

    try (ClientService service = factory.getClientService()) {
      assetIds.forEach(
          assetId -> {
            LedgerValidationResult result;
            String[] idAndAges = assetId.split(",");
            if (idAndAges.length != 3) {
              result = service.validateLedger(idAndAges[0]);
            } else {
              result =
                  service.validateLedger(
                      idAndAges[0], Integer.parseInt(idAndAges[1]), Integer.parseInt(idAndAges[2]));
            }
            JsonObject json =
                Json.createObjectBuilder()
                    .add(Common.STATUS_CODE_KEY, result.getCode().toString())
                    .add("Ledger", getProof(result.getProof()))
                    .add("Auditor", getProof(result.getAuditorProof()))
                    .add(Common.ERROR_MESSAGE_KEY, JsonValue.NULL)
                    .build();
            Common.printJson(json);
          });
      return 0;
    } catch (ClientException e) {
      Common.printError(e);
      return 1;
    }
  }

  private JsonValue getProof(Optional proof) {
    if (!proof.isPresent()) {
      return JsonValue.NULL;
    }

    AssetProof p = proof.get();
    return Json.createObjectBuilder()
        .add("id", p.getId())
        .add("age", p.getAge())
        .add("nonce", p.getNonce())
        .add("hash", Base64.getEncoder().encodeToString(p.getHash()))
        .add("signature", Base64.getEncoder().encodeToString(p.getSignature()))
        .build();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy