createAccount method

Future<AuthInfo> createAccount(
  1. String password, {
  2. Key? dscKey,
  3. Key? pskKey,
})

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.
  • dscKey - An optional pregenerated key for the Device Shared Key.
  • pskKey - An optional pregenerated key for the Password Secured Key.
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<AuthInfo> createAccount(String password,
    {e.Key? dscKey, e.Key? pskKey}) async {
  if (!MotorFlutter.isReady) {
    throw Exception('MotorFlutter is not initialized');
  }
  final dscBz = dscKey?.bytes ?? e.Key.fromSecureRandom(32).bytes;
  final pskBz = pskKey?.bytes ?? e.Key.fromSecureRandom(32).bytes;

  final resp = await MotorFlutterPlatform.instance
      .createAccountWithKeys(CreateAccountWithKeysRequest(
    password: password,
    aesDscKey: dscBz,
    aesPskKey: pskBz,
  ));
  if (resp == null) {
    throw UnmarshalException<CreateAccountResponse>();
  }

  address.value = resp.address;
  didDocument.value = resp.whoIs.didDocument;
  authorized.value = true;

  // Only store keys if generated by Plugin not provided
  if (dscKey == null && pskKey == null) {
    await writeKeysForAddr(dscBz, pskBz, resp.address);
  }

  return AuthInfo(
    did: resp.whoIs.didDocument.id,
    aesDscKey: dscBz,
    aesPskKey: pskBz,
    address: resp.whoIs.owner,
  );
}