signTransaction method

Future<Uint8List> signTransaction(
  1. Envelope envelope
)

Implementation

Future<Uint8List> signTransaction(Envelope envelope) async {
  var env = envelope;
  final address = await signer.getAddress();

  if (env.nonce == BigInt.zero) {
    final nonce = await reader.getTransactionCount(address);
    env = Envelope(
      kind: env.kind,
      to: env.to,
      value: env.value,
      data: env.data,
      gasLimit: env.gasLimit,
      nonce: nonce,
      chainId: env.chainId,
      gasPrice: env.gasPrice,
      maxFeePerGas: env.maxFeePerGas,
      maxPriorityFeePerGas: env.maxPriorityFeePerGas,
      maxFeePerBlobGas: env.maxFeePerBlobGas,
      blobVersionedHashes: env.blobVersionedHashes,
      blobs: env.blobs,
      accessList: env.accessList,
      authorizationList: env.authorizationList,
    );
  }

  if (env.chainId == BigInt.one) {
    final chainId = await reader.getChainId();
    env = Envelope(
      kind: env.kind,
      to: env.to,
      value: env.value,
      data: env.data,
      gasLimit: env.gasLimit,
      nonce: env.nonce,
      chainId: chainId,
      gasPrice: env.gasPrice,
      maxFeePerGas: env.maxFeePerGas,
      maxPriorityFeePerGas: env.maxPriorityFeePerGas,
      maxFeePerBlobGas: env.maxFeePerBlobGas,
      blobVersionedHashes: env.blobVersionedHashes,
      blobs: env.blobs,
      accessList: env.accessList,
      authorizationList: env.authorizationList,
    );
  }

  if (env.gasLimit == BigInt.from(21000) && env.data.isNotEmpty) {
    final toHex = env.to != null ? '0x${hexEncode(env.to!)}' : null;
    final estimate = await reader.estimateGas({
      'from': address.toString(),
      if (toHex != null) 'to': toHex,
      'value': '0x${env.value.toRadixString(16)}',
      'data': '0x${hexEncode(env.data)}',
    });
    env = Envelope(
      kind: env.kind,
      to: env.to,
      value: env.value,
      data: env.data,
      gasLimit: estimate,
      nonce: env.nonce,
      chainId: env.chainId,
      gasPrice: env.gasPrice,
      maxFeePerGas: env.maxFeePerGas,
      maxPriorityFeePerGas: env.maxPriorityFeePerGas,
      maxFeePerBlobGas: env.maxFeePerBlobGas,
      blobVersionedHashes: env.blobVersionedHashes,
      blobs: env.blobs,
      accessList: env.accessList,
      authorizationList: env.authorizationList,
    );
  }

  return signer.signTransaction(env);
}