computeKeys method

MoneroComputeKey computeKeys(
  1. int minorIndex,
  2. int majorIndex
)

Compute the subaddress keys based on minor and major indexes.

This method calculates Monero subaddress keys using the provided minorIndex and majorIdx. If the indexes are out of valid range, it throws an ArgumentException. It returns a tuple of subaddress public spend key and subaddress public view key.

Implementation

MoneroComputeKey computeKeys(int minorIndex, int majorIndex) {
  if (minorIndex < 0 || minorIndex > MoneroSubaddressConst.subaddrMaxIdx) {
    throw ArgumentException('Invalid minor index ($minorIndex)');
  }
  if (majorIndex < 0 || majorIndex > MoneroSubaddressConst.subaddrMaxIdx) {
    throw ArgumentException('Invalid major index ($majorIndex)');
  }

  if (minorIndex == 0 && majorIndex == 0) {
    return MoneroComputeKey(
        pubSKey: pubSKey, pubVKey: pubVKey, privateKey: privVKey);
  }

  List<int> majorIdxBytes = IntUtils.toBytes(majorIndex,
      length: MoneroSubaddressConst.subaddrIdxByteLen,
      byteOrder: Endian.little);
  List<int> minorIdxBytes = IntUtils.toBytes(minorIndex,
      length: MoneroSubaddressConst.subaddrIdxByteLen,
      byteOrder: Endian.little);

  List<int> privVKeyBytes = privVKey.raw;

  List<int> mBytes = QuickCrypto.keccack256Hash(List<int>.from([
    ...MoneroSubaddressConst.subaddrPrefix,
    ...privVKeyBytes,
    ...majorIdxBytes,
    ...minorIdxBytes
  ]));
  List<int> secretKey = Ed25519Utils.scalarReduce(mBytes);
  BigInt mInt = BigintUtils.fromBytes(secretKey, byteOrder: Endian.little);
  final newPoint = pubSKey.point + (Curves.generatorED25519 * mInt);

  MoneroPublicKey subaddrPubSKey = MoneroPublicKey.fromPoint(newPoint);
  MoneroPublicKey subaddrPubVKey = MoneroPublicKey.fromPoint(
      (subaddrPubSKey.point *
          BigintUtils.fromBytes(privVKey.raw, byteOrder: Endian.little)));
  final sKey = MoneroPrivateKey.fromBytes(secretKey);

  return MoneroComputeKey(
      pubSKey: subaddrPubSKey, pubVKey: subaddrPubVKey, privateKey: sKey);
}