getDelegatedImpl method
Reads the on-chain bytecode of an externally owned account (EOA) and extracts the delegated implementation address if the account has been upgraded via EIP-7702.
This method checks whether the account code matches the minimal delegation stub:
0xEF 0x01 0x00 || <20-byte implementation address>
If the code matches this format, the function returns the final
20 bytes, representing the implementation address that the EOA is
currently delegated to.
Otherwise, null is returned.
A valid delegation stub must satisfy:
- non-empty code
- code length of exactly
23bytes - first three bytes equal to
0xEF 0x01 0x00 - remaining 20 bytes represent the delegated address
Example:
final impl = await getDelegatedImpl(myEoa);
if (impl != null) {
print('EOA delegates to ${bytesToHex(impl)}');
}
See also:
- isDelegatedTo – checks if an EOA is delegated to a specific address.
- https://eips.ethereum.org/EIPS/eip-7702
Implementation
Future<Uint8List?> getDelegatedImpl(EthereumAddress eoa) async {
const List<int> kStubPrefix = [0xEF, 0x01, 0x00];
final code = await ctx.web3Client.getCode(eoa);
if (code.isEmpty || code.length != 23) return null;
for (var i = 0; i < kStubPrefix.length; i++) {
if (code[i] != kStubPrefix[i]) return null;
}
return code.sublist(code.length - 20);
}