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

com.scalar.dl.client.util.RequestSigner Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
/*
 * 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.util;

import com.google.inject.Inject;
import com.google.protobuf.ByteString;
import com.scalar.dl.ledger.crypto.SignatureSigner;
import com.scalar.dl.rpc.AssetProofRetrievalRequest;
import com.scalar.dl.rpc.ContractExecutionRequest;
import com.scalar.dl.rpc.ContractRegistrationRequest;
import com.scalar.dl.rpc.ContractsListingRequest;
import com.scalar.dl.rpc.ExecutionAbortRequest;
import com.scalar.dl.rpc.LedgerValidationRequest;
import com.scalar.dl.rpc.LedgersValidationRequest;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import javax.annotation.concurrent.Immutable;

@Immutable
public class RequestSigner {
  private final SignatureSigner signer;

  @Inject
  public RequestSigner(SignatureSigner signer) {
    this.signer = signer;
  }

  public ContractRegistrationRequest.Builder sign(ContractRegistrationRequest.Builder builder) {
    ByteBuffer buffer =
        ByteBuffer.allocate(
            builder.getContractId().getBytes(StandardCharsets.UTF_8).length
                + builder.getContractBinaryName().getBytes(StandardCharsets.UTF_8).length
                + builder.getContractByteCode().size()
                + builder.getContractProperties().getBytes(StandardCharsets.UTF_8).length
                + builder.getCertHolderId().getBytes(StandardCharsets.UTF_8).length
                + Integer.BYTES);

    buffer.put(builder.getContractId().getBytes(StandardCharsets.UTF_8));
    buffer.put(builder.getContractBinaryName().getBytes(StandardCharsets.UTF_8));
    buffer.put(builder.getContractByteCode().toByteArray());
    buffer.put(builder.getContractProperties().getBytes(StandardCharsets.UTF_8));
    buffer.put(builder.getCertHolderId().getBytes(StandardCharsets.UTF_8));
    buffer.putInt(builder.getCertVersion());
    buffer.rewind();

    byte[] signature = signer.sign(buffer.array());
    return builder.setSignature(ByteString.copyFrom(signature));
  }

  public ContractsListingRequest.Builder sign(ContractsListingRequest.Builder builder) {
    ByteBuffer buffer =
        ByteBuffer.allocate(
            builder.getContractId().getBytes(StandardCharsets.UTF_8).length
                + builder.getCertHolderId().getBytes(StandardCharsets.UTF_8).length
                + Integer.BYTES);

    buffer.put(builder.getContractId().getBytes(StandardCharsets.UTF_8));
    buffer.put(builder.getCertHolderId().getBytes(StandardCharsets.UTF_8));
    buffer.putInt(builder.getCertVersion());
    buffer.rewind();

    byte[] signature = signer.sign(buffer.array());
    return builder.setSignature(ByteString.copyFrom(signature));
  }

  public ContractExecutionRequest.Builder sign(ContractExecutionRequest.Builder builder) {
    ByteBuffer buffer =
        ByteBuffer.allocate(
            builder.getContractId().getBytes(StandardCharsets.UTF_8).length
                + builder.getContractArgument().getBytes(StandardCharsets.UTF_8).length
                + builder.getCertHolderId().getBytes(StandardCharsets.UTF_8).length
                + Integer.BYTES);

    buffer.put(builder.getContractId().getBytes(StandardCharsets.UTF_8));
    buffer.put(builder.getContractArgument().getBytes(StandardCharsets.UTF_8));
    buffer.put(builder.getCertHolderId().getBytes(StandardCharsets.UTF_8));
    buffer.putInt(builder.getCertVersion());
    buffer.rewind();

    byte[] signature = signer.sign(buffer.array());
    return builder.setSignature(ByteString.copyFrom(signature));
  }

  public LedgerValidationRequest.Builder sign(LedgerValidationRequest.Builder builder) {
    ByteBuffer buffer =
        ByteBuffer.allocate(
            builder.getAssetId().getBytes(StandardCharsets.UTF_8).length
                + Integer.BYTES
                + Integer.BYTES
                + builder.getCertHolderId().getBytes(StandardCharsets.UTF_8).length
                + Integer.BYTES);

    buffer.put(builder.getAssetId().getBytes(StandardCharsets.UTF_8));
    buffer.putInt(builder.getStartAge());
    buffer.putInt(builder.getEndAge());
    buffer.put(builder.getCertHolderId().getBytes(StandardCharsets.UTF_8));
    buffer.putInt(builder.getCertVersion());
    buffer.rewind();

    byte[] signature = signer.sign(buffer.array());
    return builder.setSignature(ByteString.copyFrom(signature));
  }

  public LedgersValidationRequest.Builder sign(LedgersValidationRequest.Builder builder) {
    ByteBuffer buffer =
        ByteBuffer.allocate(
            builder.getAssetId().getBytes(StandardCharsets.UTF_8).length
                + builder.getCertHolderId().getBytes(StandardCharsets.UTF_8).length
                + Integer.BYTES);

    buffer.put(builder.getAssetId().getBytes(StandardCharsets.UTF_8));
    buffer.put(builder.getCertHolderId().getBytes(StandardCharsets.UTF_8));
    buffer.putInt(builder.getCertVersion());
    buffer.rewind();

    byte[] signature = signer.sign(buffer.array());
    return builder.setSignature(ByteString.copyFrom(signature));
  }

  public AssetProofRetrievalRequest.Builder sign(AssetProofRetrievalRequest.Builder builder) {
    ByteBuffer buffer =
        ByteBuffer.allocate(
            builder.getAssetId().getBytes(StandardCharsets.UTF_8).length
                + Integer.BYTES
                + builder.getCertHolderId().getBytes(StandardCharsets.UTF_8).length
                + Integer.BYTES);

    buffer.put(builder.getAssetId().getBytes(StandardCharsets.UTF_8));
    buffer.putInt(builder.getAge());
    buffer.put(builder.getCertHolderId().getBytes(StandardCharsets.UTF_8));
    buffer.putInt(builder.getCertVersion());
    buffer.rewind();

    byte[] signature = signer.sign(buffer.array());
    return builder.setSignature(ByteString.copyFrom(signature));
  }

  public ExecutionAbortRequest.Builder sign(ExecutionAbortRequest.Builder builder) {
    ByteBuffer buffer =
        ByteBuffer.allocate(
            builder.getNonce().getBytes(StandardCharsets.UTF_8).length
                + builder.getCertHolderId().getBytes(StandardCharsets.UTF_8).length
                + Integer.BYTES);

    buffer.put(builder.getNonce().getBytes(StandardCharsets.UTF_8));
    buffer.put(builder.getCertHolderId().getBytes(StandardCharsets.UTF_8));
    buffer.putInt(builder.getCertVersion());
    buffer.rewind();

    byte[] signature = signer.sign(buffer.array());
    return builder.setSignature(ByteString.copyFrom(signature));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy