Note that while your question is interesting as a learning exercise, writing messages into the blockchain in this way is not recommended as you are increasing the UTXO set. You are encouraged to use 'OP_RETURN' instead. There is a related thread to your question, but it does not seem to focus on the specific aim of creating an address with a specific message in itThere is a thread to which your question is related, but it does not seem to focus on the aim of creating an address with a specific message in it.
A bitcoin address (of type 'P2PKH', i.e. 'pay to public key hash') is simply a 25 bytes number, where the leading byte is 0x00
and the last four bytes (low order bytes) constitute some checksum which guarantees the address has no 'typing' error. Specifically, the last four bytes of the address are the first four bytes of the hash (specifically the double Sha256 hash) of the first 21 bytes (highest order) of the address. So in big-endian representation, a bitcoin address looks as follows:
As it turns out, since you are not worriedconcerned about recovering any fund sent to your address, you have complete freedom on how to choose the 20 bytes (normally those 20 bytes would be some hash of a valid public key). The only constraintconstraints you have isare the leading byte 0x00
and the last 4 bytes which will depend on how you chose the 20 bytes. Now suppose you wish to send the message "HelloWorldHowAreYouIamOk"
Now suppose you wish to send the message "HelloWorldHowAreYouIamOk".
One naive approach consists in focusing on the number 1HeLLoWorLdHowAreYouiAmok111 (expressed in base 58) which is of length 21 bytes with a leading 0x00
byte, calculate the hash (double Sha256) of these 21 bytes, keep the first 4 bytes of the hash and append these 4 bytes to the 21 bytes. However, the operation of appending 4 bytes to the right means you are effectively multiplying your 21 bytes number by 256^4 and then add the 4 bytes number. This will completely mess up the Base58 digits of your initial number.
So what you need to do instead is focus on the 25 bytes number 1HeLLoWorLdHowAreYouiAmok111111111 (expressed in Base58, add enough padding so your number is 25 bytes), keep the high order 21 bytes of this number, compute the double hash of these 21 bytes and append the first 4 bytes of the hash to the 21 bytes. In our example, we obtain 1HeLLoWorLdHowAreYouiAmojzzzzu7udH.
As you can see, this is not quite what we were hoping, as the 'k' Base58 digit of 'iAmok' has been replaced by 'j', due to the interference of the last 4 bytes. We should have chosen a shorter message.
I attach some java code to illustrate the procedure: