SealdSdk constructor

SealdSdk({
  1. required String apiURL,
  2. required String appId,
  3. int keySize = 4096,
  4. String databasePath = "",
  5. Uint8List? databaseEncryptionKey,
  6. Duration encryptionSessionCacheTTL = Duration.zero,
  7. int logLevel = 0,
  8. bool logNoColor = false,
  9. String instanceName = "",
  10. RootIsolateToken? rootIsolateToken,
})

Creates a new instance of SealdSdk.

apiURL - The Seald server for this instance to use. This value is given on your Seald dashboard. appId - The ID given by the Seald server to your app. This value is given on your Seald dashboard. keySize - The Asymmetric key size for newly generated keys. Defaults to 4096. Warning: for security, it is extremely not recommended to lower this value. For advanced use only. databasePath - The path where to store the local Seald database. Defaults to an empty string. databaseEncryptionKey - The encryption key with which to encrypt the local Seald database. This MUST be a cryptographically random buffer of 64 bytes. Defaults to an empty string. encryptionSessionCacheTTL - The duration of cache lifetime. -1 to cache forever. 0 for no cache. Defaults to 0. logLevel - The minimum level of logs you want. All logs of this level or above will be displayed. -1: Trace; 0: Debug; 1: Info; 2: Warn; 3: Error; 4: Fatal; 5: Panic; 6: NoLevel; 7: Disabled. Defaults to 0. logNoColor - Whether to disable colors in the log output. true to disable colors, false to enable colors. Defaults to false. instanceName - An arbitrary name to give to this Seald instance. Can be useful for debugging when multiple instances are running in parallel, as it is added to logs. Defaults to an empty string. rootIsolateToken - The rootIsolateToken that you can retrieve from the root isolate with ServicesBinding.rootIsolateToken.

Implementation

SealdSdk(
    {required String apiURL,
    required String appId,
    int keySize = 4096,
    String databasePath = "",
    Uint8List? databaseEncryptionKey,
    Duration encryptionSessionCacheTTL = Duration.zero,
    int logLevel = 0,
    bool logNoColor = false,
    String instanceName = "",
    RootIsolateToken? rootIsolateToken}) {
  if (rootIsolateToken != null) {
    _rootIsolateToken = rootIsolateToken;
  } else if (ServicesBinding.rootIsolateToken != null) {
    _rootIsolateToken = ServicesBinding.rootIsolateToken!;
  } else {
    throw SealdException(
        code: "REQUIRES_ROOT_ISOLATE_TOKEN",
        id: "FLUTTER_REQUIRES_ROOT_ISOLATE_TOKEN",
        description:
            "The Seald SDK for Flutter must be instantiated from the root isolate, or `rootIsolateToken` must me passed.");
  }

  final Pointer<NativeSealdInitializeOptions> initOpts =
      calloc<NativeSealdInitializeOptions>();
  final String platform = "c-flutter-${Platform.operatingSystem}";

  // Dart FFI forces us to copy the data from Uint8List to a newly allocated Pointer<Uint8>
  final Pointer<Uint8> nativeDatabaseEncryptionKey =
      databaseEncryptionKey != null
          ? calloc<Uint8>(databaseEncryptionKey.length)
          : nullptr;
  final pointerListDatabaseEncryptionKey = databaseEncryptionKey != null
      ? nativeDatabaseEncryptionKey.asTypedList(databaseEncryptionKey.length)
      : null;
  pointerListDatabaseEncryptionKey?.setAll(0, databaseEncryptionKey!);

  initOpts.ref
    ..ApiURL = apiURL.toNativeUtf8()
    ..AppId = appId.toNativeUtf8()
    ..KeySize = keySize
    ..DatabasePath = databasePath.toNativeUtf8()
    ..DatabaseEncryptionKey = nativeDatabaseEncryptionKey
    ..DatabaseEncryptionKeyLen = databaseEncryptionKey?.length ?? 0
    ..EncryptionSessionCacheTTL = encryptionSessionCacheTTL.inMilliseconds
    ..LogLevel = logLevel
    ..LogNoColor = logNoColor ? 1 : 0
    ..InstanceName = instanceName.toNativeUtf8()
    ..Platform = platform.toNativeUtf8();
  _keySize = keySize;

  final Pointer<Pointer<NativeSealdSdk>> result =
      calloc<Pointer<NativeSealdSdk>>();
  final Pointer<Pointer<NativeSealdError>> err =
      calloc<Pointer<NativeSealdError>>();

  final int resultCode = _bindings.SealdSdk_Initialize(initOpts, result, err);

  calloc.free(initOpts.ref.ApiURL);
  calloc.free(initOpts.ref.AppId);
  calloc.free(initOpts.ref.DatabasePath);
  calloc.free(initOpts.ref.DatabaseEncryptionKey);
  calloc.free(initOpts.ref.InstanceName);
  calloc.free(initOpts.ref.Platform);
  calloc.free(initOpts);

  if (resultCode != 0) {
    calloc.free(result);
    throw SealdException._fromCPtr(err);
  } else {
    _ptr = _TransferablePointer<NativeSealdSdk>(result.value);
    calloc.free(result);
    calloc.free(err);
  }
}