createAccount method Null safety

Future<WhoIs> createAccount(
  1. String password,
  2. {HandleKeysCallback? onKeysGenerated}
)

{@subCategory Creating a New Account}

Creates a new Account with the given password. This process generates a two random 32 byte keys and stores them in the keychain during production and in the temporary storage during development. Returns CreateAccountResponse if the account is created successfully.

Parameters

  • password - The password used to encrypt the keys in the keychain.
  • onKeysGenerated - A callback function that is triggered when the keys are generated. This is useful for storing the keys in a secure location. (optional)
final res = await MotorFlutter.to.createAccount('terrible-password-123');
if (res == null) {
    throw Exception('Account creation failed');
}
print('Account created successfully: ${res.address}');

Next Steps

  • Login with the newly created account using login
  • Issue payments to the account using sendTokens
  • Buy a .snr/ subdomain to simplify your account address using buyAlias
  • ADR-1

Implementation

Future<WhoIs> createAccount(String password, {HandleKeysCallback? onKeysGenerated}) async {
  if (!MotorFlutter.isReady) {}
  final dscKey = e.Key.fromSecureRandom(32);
  final pskKey = e.Key.fromSecureRandom(32);
  onKeysGenerated?.call(dscKey.bytes, pskKey.bytes);

  final resp = await MotorFlutterPlatform.instance.createAccountWithKeys(CreateAccountWithKeysRequest(
    password: password,
    aesDscKey: dscKey.bytes,
    aesPskKey: pskKey.bytes,
  ));
  if (resp == null) {
    throw UnmarshalException<CreateAccountResponse>();
  }
  address.value = resp.address;
  didDocument.value = resp.whoIs.didDocument;
  authorized.value = true;
  await writeKeysForAddr(dscKey.bytes, pskKey.bytes, resp.address);
  return resp.whoIs;
}