createAccount method Null safety
- String password,
- {HandleKeysCallback? onKeysGenerated}
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)
Example
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<CreateAccountResponse?> createAccount(String password, {HandleKeysCallback? onKeysGenerated}) async {
final dscKey = e.Key.fromSecureRandom(32);
final pskKey = e.Key.fromSecureRandom(32);
if (onKeysGenerated != null) {
onKeysGenerated(dscKey.bytes, pskKey.bytes);
}
final resp = await MotorFlutterPlatform.instance.createAccountWithKeys(CreateAccountWithKeysRequest(
password: password,
aesDscKey: dscKey.bytes,
aesPskKey: pskKey.bytes,
));
if (resp != null) {
address.value = resp.address;
didDocument.value = resp.whoIs.didDocument;
authorized.value = true;
await writeKeysForAddr(dscKey.bytes, pskKey.bytes, resp.address);
return resp.toDefaultResponse();
}
return null;
}