buildAndSignIfNeeded method

Future<AuthorizationTuple?> buildAndSignIfNeeded({
  1. required Signer signer,
  2. Executor? executor,
})

Builds and signs a new authorization tuple only if the EOA is not already delegated to the configured implementation.

This method:

  1. Checks the current delegation state using isDelegatedTo.
  2. If the EOA already delegates to Eip7702Context.delegateAddress, returns null.
  3. Otherwise, invokes buildAndSign to produce a new AuthorizationTuple.

This helper is typically used when preparing a type 0x04 EIP-7702 transaction to ensure that delegation is applied only when necessary.

Example:

final tuple = await builder.buildAndSignIfNeeded(signer: signer);
if (tuple == null) {
  print('Delegation already active; no authorization needed.');
}

See also:

Implementation

Future<AuthorizationTuple?> buildAndSignIfNeeded({
  required Signer signer,
  Executor? executor,
}) async {
  final alreadyDelegating = await isDelegatedTo(
    signer.ethPrivateKey.address,
    ctx.delegateAddress,
  );
  if (alreadyDelegating) return null;
  return buildAndSign(signer: signer, executor: executor);
}