// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

syntax = "proto3";

package soma.rpc;

import "soma/rpc/signature_scheme.proto";

// A signature from a user.
message UserSignature {


  // The signature scheme of this signature.
  optional SignatureScheme scheme = 1;

  oneof signature {
    // Simple signature if scheme is ed25519.
    SimpleSignature simple = 2;

    // The multisig aggregated signature if scheme is `MULTISIG`.
    MultisigAggregatedSignature multisig = 3;
  }
}

// Either an ed25519, secp256k1 or secp256r1 signature
message SimpleSignature {
  // The signature scheme of this signature.
  optional SignatureScheme scheme = 1;

  // Signature bytes
  optional bytes signature = 2;

  // Public key bytes
  optional bytes public_key = 3;
}

// Set of valid public keys for multisig committee members.
message MultisigMemberPublicKey {
  // The signature scheme of this public key.
  optional SignatureScheme scheme = 1;

  // Public key bytes if scheme is ed25519.
  optional bytes public_key = 2;
}

// A member in a multisig committee.
message MultisigMember {
  // The public key of the committee member.
  optional MultisigMemberPublicKey public_key = 1;
  // The weight of this member's signature.
  optional uint32 weight = 2;
}

// A multisig committee.
message MultisigCommittee {
  // A list of committee members and their corresponding weight.
  repeated MultisigMember members = 1;
  // The threshold of signatures needed to validate a signature from
  // this committee.
  optional uint32 threshold = 2;
}

// Aggregated signature from members of a multisig committee.
message MultisigAggregatedSignature {
  // The plain signatures encoded with signature scheme.
  //
  // The signatures must be in the same order as they are listed in the committee.
  repeated MultisigMemberSignature signatures = 1;

  // Bitmap indicating which committee members contributed to the
  // signature.
  optional uint32 bitmap = 2;
 
  // The committee to use to validate this signature.
  optional MultisigCommittee committee = 3;
}

// A signature from a member of a multisig committee.
message MultisigMemberSignature {
  // The signature scheme of this signature.
  optional SignatureScheme scheme = 1;

  // Signature bytes if scheme is ed25519.
  optional bytes signature = 2;
}

// The validator set for a particular epoch.
message ValidatorCommittee {
  // The epoch where this committee governs.
  optional uint64 epoch = 1;

  // The committee members.
  repeated ValidatorCommitteeMember members = 2;
}
// A member of a validator committee with full authority information.
message ValidatorCommitteeMember {
  // The BLS12381 public key bytes (becomes AuthorityName)
  optional bytes authority_key = 1;

  // Voting weight this validator possesses.
  optional uint64 weight = 2;

  // Network metadata for this validator
  optional ValidatorNetworkMetadata network_metadata = 3;
}

// Network and protocol information for a validator
message ValidatorNetworkMetadata {
  // Multiaddr for consensus communication (as string)
  optional string consensus_address = 1;

  // Hostname for metrics and logging
  optional string hostname = 2;

  // Ed25519 protocol public key bytes
  optional bytes protocol_key = 3;

  // Ed25519 network public key bytes
  optional bytes network_key = 4;
}

/// An aggregated signature from multiple validators.
message ValidatorAggregatedSignature {
  // The epoch when this signature was produced.
  //
  // This can be used to lookup the `ValidatorCommittee` from this epoch
  // to verify this signature.
  optional uint64 epoch = 1;

  // The 48-byte Bls12381 aggregated signature.
  optional bytes signature = 2;

  // Bitmap indicating which members of the committee contributed to
  // this signature.
  repeated uint32 bitmap = 3;
}