encodeMultiEd25519Key static method

List<int> encodeMultiEd25519Key(
  1. List<Ed25519PublicKey> publicKeys,
  2. int threshold
)

encode Multi ED25519 public keys to MultiEd25519 address

Implementation

static List<int> encodeMultiEd25519Key(
  List<Ed25519PublicKey> publicKeys,
  int threshold,
) {
  try {
    final keys = publicKeys.toSet();
    if (keys.length != publicKeys.length) {
      throw AddressConverterException.addressKeyValidationFailed(
        reason: "Duplicate public key detected.",
        network: "Aptos",
      );
    }
    if (publicKeys.length < AptosAddrConst.minPublicKeys ||
        publicKeys.length > AptosAddrConst.maximumPublicKeys) {
      throw AddressConverterException.addressKeyValidationFailed(
        network: "Aptos",
        reason:
            "The number of public keys provided is invalid. It must be between ${AptosAddrConst.minPublicKeys} and ${AptosAddrConst.maximumPublicKeys}.",
      );
    }
    if (threshold < AptosAddrConst.minthreshold ||
        threshold > publicKeys.length) {
      throw AddressConverterException.addressKeyValidationFailed(
        network: "Aptos",
        reason:
            "Invalid threshold. The threshold must be between ${AptosAddrConst.minthreshold} and the number of provided public keys (${publicKeys.length}).",
      );
    }
    final keyBytes = [
      ...publicKeys.map((e) => e.compressed.sublist(1)).expand((e) => e),
      threshold,
    ];
    return hashKeyBytes(
      bytes: keyBytes,
      scheme: AptosAddrConst.multiEd25519AddressFlag,
    );
  } on AddressConverterException {
    rethrow;
  } catch (e) {
    throw AddressConverterException.addressKeyValidationFailed(
      network: "Aptos",
      details: {"error": e.toString()},
    );
  }
}