encodeKey method

  1. @override
String encodeKey(
  1. List<int> pubKey, [
  2. Map<String, dynamic> kwargs = const {}
])
override

Blockchain address encoder for Ada Shelley addresses. This encoder is used to create addresses based on public and private key pairs.

Implementation

@override
String encodeKey(List<int> pubKey, [Map<String, dynamic> kwargs = const {}]) {
  /// Validate the provided address arguments.
  AddrKeyValidator.validateAddressArgs<List<int>>(kwargs, "pub_skey");

  /// Extract the public spending key (pub_skey) from the arguments.
  final List<int> pubSkey = kwargs["pub_skey"];

  /// Determine the network tag, defaulting to mainnet if not specified.
  final netTag = kwargs["net_tag"] ?? AdaShelleyAddrNetworkTags.mainnet;

  /// Check if the provided network tag is a valid enum value.
  if (netTag is! AdaShelleyAddrNetworkTags) {
    throw ArgumentException(
        'Address type is not an enumerative of AdaShelleyAddrNetworkTags');
  }

  /// Validate and retrieve public keys.
  final pubKeyObj = AddrKeyValidator.validateAndGetEd25519Key(pubKey);
  final pubSkeyObj = AddrKeyValidator.validateAndGetEd25519Key(pubSkey);

  // Compute key hashes for public spending and public delegation keys.
  final pubKeyHash =
      _AdaShelleyAddrUtils.keyHash(pubKeyObj.compressed.sublist(1));
  final pubSkeyHash =
      _AdaShelleyAddrUtils.keyHash(pubSkeyObj.compressed.sublist(1));

  /// Encode the address prefix using the header type and network tag.
  final prefixByte = _AdaShelleyAddrUtils.encodePrefix(
      AdaShelleyAddrHeaderTypes.payment, netTag.value);

  /// Create the final address by combining prefix, public key hashes, and spending key hash.
  return Bech32Encoder.encode(
      AdaShelleyAddrConst.networkTagToAddrHrp[netTag]!,
      List<int>.from([...prefixByte, ...pubKeyHash, ...pubSkeyHash]));
}