decodeKeychain function

Keychain decodeKeychain(
  1. Uint8List binary
)

Implementation

Keychain decodeKeychain(Uint8List binary) {
  int pos = 0;
  final int version =
      binary.sublist(pos, 4).buffer.asByteData().getInt32(0).toInt();
  pos = pos + 4;
  final int seedSize = binary.sublist(pos, pos + 1)[0].toInt();
  pos++;
  final Uint8List seed = binary.sublist(pos, pos + seedSize);
  pos = pos + seedSize;
  final int nbServices = binary.sublist(pos, pos + 1)[0].toInt();
  pos++;

  final Keychain keychain = Keychain(seed, version: version);
  for (int i = 0; i < nbServices; i++) {
    final int serviceNameLength = binary.sublist(pos, pos + 1)[0].toInt();
    pos++;
    final Uint8List serviceName = binary.sublist(pos, pos + serviceNameLength);
    pos = pos + serviceNameLength;
    final int derivationPathLength = binary.sublist(pos, pos + 1)[0].toInt();
    pos++;
    final Uint8List derivationPath =
        binary.sublist(pos, pos + derivationPathLength);
    pos = pos + derivationPathLength;
    final int curveId = binary.sublist(pos, pos + 1)[0].toInt();
    pos++;
    final int hashAlgoId = binary.sublist(pos, pos + 1)[0].toInt();
    pos++;

    keychain.addService(utf8.decode(serviceName), utf8.decode(derivationPath),
        curve: crypto.idToCurve(curveId),
        hashAlgo: crypto.idToHashAlgo(hashAlgoId));
  }
  return keychain;
}