29

How can I send a message to another Ethereum address? And how would that user receiving know that its a message? Is there an Ethereum wallet that will show that I sent a message to that address? And what is the max size I can send?

5 Answers 5

28

So it is only possible to send HEX data in the data field. This will not be a standard transaction but will require you to use geth, MyCrypto advanced settings on the send page, or another client that allows you to access and edit the data field.

  1. Google and visit an ascii->hex site and type your message.

  2. Copy the hexadecimal output, and remove all the spaces in that hexadecimal output. Then paste it into the data field.

  3. On etherscan, you can click the "convert to ascii" at the bottom of any TX that has data. Not all data is ascii data though, so it will be gibberish sometimes.

  4. I sent a demo here. Under "payload", you can see the HEX data on the left and the ASCII on the right.


As euro10 pointed out in the comments (but it's a bit hard to read), you can actually use built-in ascii -> hex if you are using geth:

eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(1‌​00,'finney'),data:web3.toHex('John Doe sent you a message')})

7
  • 5
    you can use the built-in ascii to hex should you use geth : eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(100,'finney'),data:web3.toHex('John Doe sent you a message')}) you can see here etherchain.org/tx/…
    – euri10
    Mar 31, 2016 at 6:39
  • 1
    It's much easier than this. In the Mist browser, you can just copy and paste your hex string there. It recalculates gas, etc. Rapid Tables is a good site for converting text to hex. This is basically what I did here: medium.com/@tjayrush/… Aug 29, 2016 at 16:21
  • 2
    Must I use ASCII? UTF-8? Dec 26, 2016 at 0:59
  • 2
    How can I receive message? Any sample js code? Jul 12, 2017 at 15:22
  • @tayvano, Can you please provide sample code to read the message? Oct 30, 2017 at 2:17
9

By sending a message, I assume you mean you want to send some form of a text based message to another user...

  • You can send a message as part of the data field in a transaction.
  • For the user to know you actually sent them a message, you could use the web3.js api to listen and extract messages from a transaction. You'd then have to decode it to ascii to make it human readable.
  • I'm not aware of a wallet that will show you sent a specific message to an address, but a wallet such as Mist will show you sent a transaction to a given address
  • I believe there is a max size for transaction (which would include your message). I'm not positive, but I think it's currently around 80kb...?

And alternative to the approach above is to use a contract to post an event to the blockchain. A client can then listen for these specific events again using the web3.js api.

6

Here's how you would do it with the web3 api.

const Utils = require('web3-utils');
let txTransfer = {};
txTransfer.from = from.address;
txTransfer.to = to.address;
txTransfer.gas = GasLimit;
txTransfer.value = amount;
txTransfer.data = Utils.toHex('free text data');
web3.eth.sendTransaction(txTransfer);

I have a working example on the blockchain at this transaction 0x99f537b788c1e0c9513735c644921ffa423f8bd20ce45165403e8f12942aaca8. The ascii data in the transaction was the same JavaScript used to send the transaction.

4

web3js v1.x.x

const txParams = {
  nonce: nonce,
  gasPrice: gasPrice,
  gasLimit: gasLimit,
  to: to,
  value: value,
  data : web3.utils.toHex('rockstar blockchain developer'),
  // EIP 155 chainId - mainnet: 1, ropsten: 3
  chainId: 4
}

const tx = new ethereumTx(txParams);

tx.sign(privateKey);
const serializedTx = '0x'+tx.serialize().toString('hex')

console.log("serializedTx" ,serializedTx );
web3.eth.sendSignedTransaction(serializedTx);

Tx hash of successful example on rinkeBy test net 0xbc5ef9dc8da24c90d5910d93419f9746f3fe318cb2f2b5e7c873e64264bfcf53

2
  • Honest question, why use sendSignedTransaction instead of just sendTransaction? What benefits does the signed one have?
    – adrianmcli
    Jul 27, 2018 at 16:40
  • It's more flexible. There are some benefits to signing with a private key not held by web3. For example you could load the private key from a brainwallet or generated in an offline way (through dice rolls) Dec 19, 2018 at 4:52
0

You use Whisper (https://github.com/ethereum/go-ethereum/wiki/Whisper)


Solution using Web3.py:

I have followed following ssh-mode code.


receiver.py that runs on node-1:

from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://localhost:8545'))
from web3.shh import Shh
Shh.attach(web3, "shh")

import time, sys;

from hexbytes import (
    HexBytes,
)

kId = web3.shh.newKeyPair()
receiver_pub = web3.shh.getPublicKey(kId)
print('receiverPubK: ' + receiver_pub);

topic = '0x07678231'

shh_filter = web3.shh.newMessageFilter({
    'privateKeyID': kId,
    'topics': [topic]
})

input("Press Enter to continue...");

received_messages = [];
received_messages = shh_filter.get_new_entries()
print(len(received_messages)) # Returns '0'
print(web3.shh.info.memory)  # Returns '0'

Output:

receiverPubK: 0x04226d96bf9857ac0ba429c1e8b480a2811ce47cb526dbd3829d7586e5cae740198ba291f3eca0f279f82db8a136be90ea9ec629ed6cd1d45cc7f873159811757d
Press Enter to continue...

After Node_1's messages are sent; I have copied printed receiverPublicKey into receiver_pub variable on the following code. I run following code on Node 2. Node 2 sends a message to the network. Later I press enter on the Node_1 and Node_1 prints the message

from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://localhost:8545'))
from web3.shh import Shh
Shh.attach(web3, "shh")

import time, sys;

from hexbytes import (
    HexBytes,
)

receiver_pub='0x04226d96bf9857ac0ba429c1e8b480a2811ce47cb526dbd3829d7586e5cae740198ba291f3eca0f279f82db8a136be90ea9ec629ed6cd1'; # obtained from node_1 and assigned here.

topic = '0x07678231'
payloads = [web3.toHex(text="test message :)"), web3.toHex(text="2nd test message")]

web3.shh.post({
        'powTarget': 2.5,
        'powTime': 2,
        'ttl': 60,
        'payload': payloads[0],
        'topic': topic,
        'pubKey': receiver_pub
})    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.