keychainAddresses method

Future<List<String>> keychainAddresses(
  1. String endpoint,
  2. Uint8List pubkey
)

Retrieves the list of keychain addresses associated with the given public key.

  • endpoint: The API endpoint to query.
  • pubkey: The public key as a Uint8List.

Returns a list of keychain addresses as hexadecimal strings.

Implementation

Future<List<String>> keychainAddresses(
  String endpoint,
  Uint8List pubkey,
) async {
  // We hardcode the curve ID (0 = ed25519 by default)
  final genesisAddress = await _findKeychainGenesisAddress(endpoint, pubkey);
  if (genesisAddress == null || genesisAddress.isEmpty) {
    return [];
  }

  final transaction = await _searchKeychain(endpoint, genesisAddress);
  if (transaction == null ||
      transaction.data == null ||
      transaction.data!.content == null) {
    return [];
  }

  final publicKeys = <String>[];
  final json = jsonDecode(transaction.data!.content!);

  if (json.containsKey('verificationMethod')) {
    for (final method in json['verificationMethod']) {
      if (method is Map<String, dynamic> &&
          method.containsKey('publicKeyJwk')) {
        final publicKeyJwk = method['publicKeyJwk'];
        if (publicKeyJwk is Map<String, dynamic> &&
            publicKeyJwk.containsKey('x')) {
          try {
            final publicKey = jwkToKey(publicKeyJwk);
            publicKeys.add(uint8ListToHex(publicKey));
          } catch (e) {
            _logger.severe(
              'Error converting JWK to public key: $e',
            );
          }
        }
      }
    }
  }

  final addressList = <String>[];
  for (final publicKey in publicKeys) {
    addressList.add(
      '00${uint8ListToHex(hash(hexToUint8List(publicKey)))}',
    );
  }

  return addressList;
}