rotateAuthKeyEd25519 method

Future rotateAuthKeyEd25519(
  1. AptosAccount forAccount,
  2. Uint8List toPrivateKeyBytes
)

Rotate an account's auth key. After rotation, only the new private key can be used to sign txns for the account. WARNING: You must create a new instance of AptosAccount after using this function.

Implementation

Future<dynamic> rotateAuthKeyEd25519(
  AptosAccount forAccount,
  Uint8List toPrivateKeyBytes,
) async {
  final accountInfo = await getAccount(forAccount.address);
  final sequenceNumber = accountInfo.sequenceNumber;
  final authKey = accountInfo.authenticationKey;

  final helperAccount = AptosAccount(toPrivateKeyBytes);

  final challenge = RotationProofChallenge(
    AccountAddress.coreCodeAddress(),
    "account",
    "RotationProofChallenge",
    BigInt.parse(sequenceNumber),
    AccountAddress.fromHex(forAccount.address),
    AccountAddress(HexString(authKey).toUint8Array()),
    helperAccount.pubKey().toUint8Array(),
  );

  final challengeBytes = bcsToBytes(challenge);

  final proofSignedByCurrentPrivateKey = forAccount.signBuffer(challengeBytes);

  final proofSignedByNewPrivateKey = helperAccount.signBuffer(challengeBytes);

  final payload = TransactionPayloadEntryFunction(
    EntryFunction.natural(
      "0x1::account",
      "rotate_authentication_key",
      [],
      [
        bcsSerializeU8(0), // ed25519 scheme
        bcsSerializeBytes(forAccount.pubKey().toUint8Array()),
        bcsSerializeU8(0), // ed25519 scheme
        bcsSerializeBytes(helperAccount.pubKey().toUint8Array()),
        bcsSerializeBytes(proofSignedByCurrentPrivateKey.toUint8Array()),
        bcsSerializeBytes(proofSignedByNewPrivateKey.toUint8Array()),
      ],
    ),
  );

  final rawTransaction = await generateRawTransaction(forAccount.address, payload);
  final bcsTxn = AptosClient.generateBCSTransaction(forAccount, rawTransaction);
  return submitSignedBCSTransaction(bcsTxn);
}