replaceDeviceKey method

  1. @override
Future<Transaction> replaceDeviceKey(
  1. AccountKeyPair account,
  2. AccountKeyPair newKey,
  3. Map<RecoveryServerKey, RecoveryServerSigning> serverAuth, {
  4. AccountKeyPair? lostKey,
  5. AccountKeyPair? sponsorAddress,
})
override

Replace lost device key with a new key @account target account @newKey a key to replace the lost key with @serverAuth list of servers to use @lostKey (optional) lost device key. If not specified, try to deduce key from account signers list @sponsorAddress (optional) sponsor address of the transaction. Please note that not all SEP-30 servers support signing sponsored transactions.

Implementation

@override
Future<flutter_sdk.Transaction> replaceDeviceKey(
    AccountKeyPair account,
    AccountKeyPair newKey,
    Map<RecoveryServerKey, RecoveryServerSigning> serverAuth,
    {AccountKeyPair? lostKey,
    AccountKeyPair? sponsorAddress}) async {
  var sdk = flutter_sdk.StellarSDK(stellar.horizonUrl);
  flutter_sdk.AccountResponse? stellarAccount;
  try {
    stellarAccount = await sdk.accounts.account(account.address);
  } catch (e) {
    if (e is flutter_sdk.ErrorResponse) {
      if (e.code != 404) {
        throw HorizonRequestFailedException(e);
      } else {
        throw ValidationException("Account doesn't exist");
      }
    } else {
      rethrow;
    }
  }

  flutter_sdk.AccountResponse? sponsorAcc;
  if (sponsorAddress != null) {
    try {
      sponsorAcc = await sdk.accounts.account(sponsorAddress.address);
    } catch (e) {
      if (e is flutter_sdk.ErrorResponse) {
        if (e.code != 404) {
          throw HorizonRequestFailedException(e);
        } else {
          throw ValidationException("Sponsor account dose not exist");
        }
      } else {
        rethrow;
      }
    }
  }

  AccountKeyPair? lost;
  int? weight;
  if (lostKey != null) {
    lost = lostKey;
    for (flutter_sdk.Signer signer in stellarAccount.signers) {
      if (signer.key == lostKey.address) {
        weight = signer.weight;
        break;
      }
    }
    if (weight == null) {
      throw ValidationException("Lost key doesn't belong to the account");
    }
  } else {
    flutter_sdk.Signer deduced = _deduceKey(stellarAccount, serverAuth);
    lost = PublicKeyPair(flutter_sdk.KeyPair.fromAccountId(deduced.key));
    weight = deduced.weight;
  }

  flutter_sdk.TransactionBuilder txBuilder =
      flutter_sdk.TransactionBuilder(sponsorAcc ?? stellarAccount);
  if (sponsorAddress != null) {
    flutter_sdk.BeginSponsoringFutureReservesOperationBuilder
        beginSponsoringBuilder =
        flutter_sdk.BeginSponsoringFutureReservesOperationBuilder(
            stellarAccount.accountId);
    beginSponsoringBuilder.setSourceAccount(sponsorAddress.address);
    txBuilder.addOperation(beginSponsoringBuilder.build());
  }
  flutter_sdk.SetOptionsOperationBuilder setOpR =
      flutter_sdk.SetOptionsOperationBuilder();
  setOpR.setSourceAccount(stellarAccount.accountId);
  flutter_sdk.XdrSignerKey sLostKey = flutter_sdk.XdrSignerKey(
      flutter_sdk.XdrSignerKeyType.SIGNER_KEY_TYPE_ED25519);
  sLostKey.ed25519 = flutter_sdk.KeyPair.fromAccountId(lost.address)
      .xdrPublicKey
      .getEd25519();
  setOpR.setSigner(sLostKey, 0); // remove
  txBuilder.addOperation(setOpR.build());

  flutter_sdk.SetOptionsOperationBuilder setOpA =
      flutter_sdk.SetOptionsOperationBuilder();
  setOpA.setSourceAccount(stellarAccount.accountId);
  flutter_sdk.XdrSignerKey sNewKey = flutter_sdk.XdrSignerKey(
      flutter_sdk.XdrSignerKeyType.SIGNER_KEY_TYPE_ED25519);
  sNewKey.ed25519 = flutter_sdk.KeyPair.fromAccountId(newKey.address)
      .xdrPublicKey
      .getEd25519();
  setOpA.setSigner(sNewKey, weight); // add
  txBuilder.addOperation(setOpA.build());

  if (sponsorAddress != null) {
    flutter_sdk.EndSponsoringFutureReservesOperationBuilder
        endSponsorshipBuilder =
        flutter_sdk.EndSponsoringFutureReservesOperationBuilder();
    endSponsorshipBuilder.setSourceAccount(stellarAccount.accountId);
    txBuilder.addOperation(endSponsorshipBuilder.build());
  }

  flutter_sdk.Transaction tx = txBuilder.build();
  return await signWithRecoveryServers(tx, account, serverAuth);
}