ecEncryptServiceSeed method

({List<AuthorizedKey> authorizedPublicKeys, Uint8List secret}) ecEncryptServiceSeed(
  1. String service,
  2. List<String> publicKeys, {
  3. String pathSuffix = '',
})

Implementation

({Uint8List secret, List<AuthorizedKey> authorizedPublicKeys})
    ecEncryptServiceSeed(
  String service,
  List<String> publicKeys, {
  String pathSuffix = '',
}) {
  if (services[service] == null) {
    throw Exception(
      "Service doesn't exist in the keychain",
    );
  }

  final serviceSelected = services[service];

  if (isPathWithIndex(serviceSelected!.derivationPath)) {
    throw Exception(
      'Service should have a derivation path without index (removing the last "/0")',
    );
  }

  final extendedSeed = deriveServiceSeed(
    seed,
    serviceSelected.derivationPath,
    0,
    pathSuffix: pathSuffix,
  );

  final aesKey = generateRandomAESKey();

  final secret = crypto.aesEncrypt(extendedSeed, aesKey);

  final authorizedPublicKeys = <AuthorizedKey>[];
  for (final key in publicKeys) {
    authorizedPublicKeys.add(
      AuthorizedKey(
        encryptedSecretKey: uint8ListToHex(crypto.ecEncrypt(aesKey, key)),
        publicKey: key,
      ),
    );
  }
  return (secret: secret, authorizedPublicKeys: authorizedPublicKeys);
}