encodeKey method

  1. @override
String encodeKey(
  1. List<int> pubKey, {
  2. XtzAddrPrefixes? addressPrefix,
})
override

Encodes a public key or address bytes into a blockchain address.

Throws AddressConverterException on invalid input.

Implementation

@override
String encodeKey(List<int> pubKey, {XtzAddrPrefixes? addressPrefix}) {
  addressPrefix = AddrKeyValidator.getAddrArg<XtzAddrPrefixes>(
    addressPrefix,
    'addressPrefix',
  );
  if (!addressPrefix.isImplicit) {
    throw CoreErrors.addressValidationFailed(
      reason:
          'Cannot derive ${addressPrefix.name} from a public key '
          '(only tz1–tz4).',
    );
  }
  final List<int> keyBytes;
  switch (addressPrefix) {
    case XtzAddrPrefixes.tz1:
      keyBytes = AddrKeyValidator.validateAndGetEd25519KeyBytes(pubKey);
    case XtzAddrPrefixes.tz2:
      keyBytes = AddrKeyValidator.validateAndGetSecp256k1Key(
        pubKey,
      ).toBytes(mode: CryptoKeyEncoding.base);
    case XtzAddrPrefixes.tz3:
      keyBytes = AddrKeyValidator.validateAndGetNist256p1Key(
        pubKey,
      ).toBytes(mode: CryptoKeyEncoding.base);
    case XtzAddrPrefixes.tz4:
      keyBytes = AddrKeyValidator.validateAndGetBlsG1PublicKey(
        pubKey,
      ).toBytes(mode: CryptoKeyEncoding.base);
    case XtzAddrPrefixes.tz5:
      keyBytes = AddrKeyValidator.validateAndGetMLDSA44PubKey(
        pubKey,
      ).toBytes(mode: CryptoKeyEncoding.base);
    case XtzAddrPrefixes.kt1:
    case XtzAddrPrefixes.sr1:
    case XtzAddrPrefixes.epx1:
      throw CoreErrors.addressValidationFailed(reason: 'Unreachable');
  }
  final blakeBytes = QuickCrypto.blake2b160Hash(keyBytes);
  return Base58Encoder.encodeWithChecksum([
    ...addressPrefix.prefix,
    ...blakeBytes,
  ]);
}