You don't store strings in an OP_RETURN output, you store bytes so forget about base58 or any other string encoding that reduces the size but adds extra overhead (version byte, checksum, etc.).
You store raw bytes in an OP_RETURN. If you want to do something not standardized then you should defined your own standard meaning you should do something like this:
- Define a standard where the bytes inside the OP_RETURN include a version + data
-- The version tells the address/output-script-type
-- The data is the hash/pubkey used in the address that can have arbitrary length.
- You can skip the push size byte.
For example (version):
0 -> P2PKH
1 -> P2SH
3 -> P2WPKH
4 -> P2WSH
5 -> P2TR
So your OP_RETURN output script for a P2WSH would look like this:
6a045bf5847136b36d23da9a706a5ad4d8eaa20a265a6b11db87d251d7c1779386fc
When decoding this you see the 0x04 and realize the address type should be P2WSH so you need 32 bytes (no need for an extra push byte 0x20) so you read the next 32 bytes (0x5bf5847136b36d23da9a706a5ad4d8eaa20a265a6b11db87d251d7c1779386fc) and use that to encode a P2WSH bech32 address by appending the version byte (0) and adding the computed checksum.
To encode, you just take these steps in reverse. You have the address and decide the version based on that (eg. 0 for P2PKH) then decode the address using the respective encoder (eg. base58) and extract the data part (eg. 20 byte hash) and put it in your custom OP_RETURN output.
This method is obviously scalable since you can always add more versions like version 6 being the next SegWit version that may come after Taproot some day.
This method is also as compressed as it gets. For example a normal P2PKH output takes up 25 bytes while this would take 22 bytes.