encodeKeyWithInfo method

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

Encodes an Ada Byron Legacy address with the provided public key, chain code, and optional HD path information.

The pubKey parameter is the public key to be encoded. The optional kwargs parameter is a map of additional arguments, including:

  • "hd_path": A string or Bip32Path specifying the hierarchical deterministic (HD) path.
  • "chain_code": A bytes or Bip32ChainCode representing the chain code.
  • "hd_path_key": An optional bytes for the HD path key (must be 32 bytes).

Returns a ADAByronAddr representing the encoded Ada Byron Legacy address.

Throws an ArgumentException if the provided HD path, chain code, or HD path key is invalid.

Implementation

ADAByronAddr encodeKeyWithInfo(List<int> pubKey,
    [Map<String, dynamic> kwargs = const {}]) {
  /// Determine the network tag, defaulting to mainnet if not specified.
  final netTag = kwargs["net_tag"] ?? ADANetwork.mainnet;

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

  Bip32Path hdPath;
  if (kwargs["hd_path"] is String) {
    hdPath = Bip32PathParser.parse(kwargs["hd_path"]);
  } else {
    if (kwargs["hd_path"] is! Bip32Path) {
      throw const AddressConverterException(
          "hd path must be string or Bip32Path");
    }
    hdPath = kwargs["hd_path"];
  }

  List<int> chainCodeBytes;

  if (kwargs["chain_code"] is List<int>) {
    chainCodeBytes = kwargs["chain_code"];
  } else {
    if (kwargs["chain_code"] is! Bip32ChainCode) {
      throw const AddressConverterException(
          "chain code must be bytes or Bip32ChainCode");
    }
    chainCodeBytes = (kwargs["chain_code"] as Bip32ChainCode).toBytes();
  }
  List<int> hdPathKeyBytes;
  if (kwargs["hd_path_key"] is! List<int>) {
    throw const AddressConverterException("hd path key must be bytes");
  }
  hdPathKeyBytes = kwargs["hd_path_key"];
  if (hdPathKeyBytes.length != QuickCrypto.chacha20Polu1305Keysize) {
    throw const AddressConverterException(
        "HD path key shall be ${QuickCrypto.chacha20Polu1305Keysize}-byte long");
  }
  final pubKeyBytes =
      AddrKeyValidator.validateAndGetEd25519Key(pubKey).compressed;
  return _AdaByronAddrUtils.encodeKey(
      pubKeyBytes, chainCodeBytes, ADAByronAddrTypes.publicKey,
      hdPathEncBytes: _AdaByronAddrHdPath.encrypt(hdPath, hdPathKeyBytes),
      networkMagic: netTag.protocolMagic);
}