isValidKeyEncoding static method

bool isValidKeyEncoding(
  1. List<int> keyBytes,
  2. CryptoKeyEncoding encoding
)
override

Checks whether keyBytes has a valid length for the given encoding.

This check validates only the key length and does not verify whether keyBytes represents a valid public key.

Implementation

static bool isValidKeyEncoding(
  List<int> keyBytes,
  CryptoKeyEncoding encoding,
) {
  switch (encoding) {
    case CryptoKeyEncoding.base:
      final layout = LayoutConst.struct([
        LayoutConst.fixedBlobN(
          EcdsaKeysConst.pubKeyCompressedByteLen,
          property: "agg",
        ),
        LayoutConst.optional(
          LayoutConst.vec(
            LayoutConst.fixedBlobN(
              EcdsaKeysConst.pubKeyCompressedByteLen,
              property: "keys",
            ),
          ),
        ),
      ]);
      try {
        final decode = layout.deserialize(keyBytes);
        final keys = decode.value.valueAsList<List?>("keys");
        return decode.consumed == keyBytes.length &&
            (keys == null ||
                keys.length >= MuSig2Constants.minimumRequiredKey);
      } catch (_) {
        return false;
      }

    case CryptoKeyEncoding.uncompressed:
      return false;
  }
}