saveIdentityFromRawKeys method

String saveIdentityFromRawKeys(
  1. String userId,
  2. String rawStorageKey,
  3. Uint8List rawEncryptionKey,
  4. Uint8List identity,
)

Save the given identity for the given userId, encrypted with the given raw keys.

userId - The ID of the user. rawStorageKey - The key under which identity keys are stored. This MUST be a secret known only to this user of your app, and never to other users, as learning it will allow deleting the stored identities. Useful to change if you want to store multiple identities for the same userId. Allowed characters : A-Za-z0-9+/=-_@.. Max length is 256 characters. rawEncryptionKey - The raw encryption key used to encrypt / decrypt the stored identity keys. This MUST be a cryptographically random buffer of 64 bytes. identity - The identity to save.

Returns the SSKS ID of the stored identity, which can be used by your backend to manage it.

Implementation

String saveIdentityFromRawKeys(String userId, String rawStorageKey,
    Uint8List rawEncryptionKey, Uint8List identity) {
  final Pointer<Utf8> nativeUserId = userId.toNativeUtf8();
  final Pointer<Utf8> nativeRawStorageKey = rawStorageKey.toNativeUtf8();
  // Dart FFI forces us to copy the data from Uint8List to a newly allocated Pointer<Uint8>
  final Pointer<Uint8> nativeRawEncryptionKey =
      calloc<Uint8>(rawEncryptionKey.length);
  final pointerListRawEncryptionKey =
      nativeRawEncryptionKey.asTypedList(rawEncryptionKey.length);
  pointerListRawEncryptionKey.setAll(0, rawEncryptionKey);
  final Pointer<Uint8> nativeIdentity = calloc<Uint8>(identity.length);
  final pointerListIdentity = nativeIdentity.asTypedList(identity.length);
  pointerListIdentity.setAll(0, identity);
  final Pointer<Pointer<Utf8>> result = calloc<Pointer<Utf8>>();
  final Pointer<Pointer<NativeSealdError>> err =
      calloc<Pointer<NativeSealdError>>();

  final int resultCode =
      _bindings.SealdSsksPasswordPlugin_SaveIdentityFromRawKeys(
          _ptr.pointer(),
          nativeUserId,
          nativeRawStorageKey,
          nativeRawEncryptionKey,
          rawEncryptionKey.length,
          nativeIdentity,
          identity.length,
          result,
          err);

  calloc.free(nativeUserId);
  calloc.free(nativeRawStorageKey);
  calloc.free(nativeRawEncryptionKey);
  calloc.free(nativeIdentity);

  if (resultCode != 0) {
    throw SealdException._fromCPtr(err);
  } else {
    final String res = result.value.toDartString();
    calloc.free(result.value);
    calloc.free(result);
    calloc.free(err);
    return res;
  }
}