Skip to content

Instantly share code, notes, and snippets.

@jbenet
Last active June 18, 2021 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbenet/31eebd5d452ead8aef655134020fa79f to your computer and use it in GitHub Desktop.
Save jbenet/31eebd5d452ead8aef655134020fa79f to your computer and use it in GitHub Desktop.
Sending a tx in filecoin w/ extra data

Messages in Filecoin allow sending arbitrary values through the params field.

Encoding data into params

  • data to send through transactions should be base64 encoded when using the message sending APIs.
  • note: you'll be sending to method: 0, which should be the default in your tools. method: 0 will ignore the params.

lotus-shed example

  • assuming you have a configured lotus and lotus-shed
  • assuming the data you want to send is aGVsbG8= (base64. or in hex: 68656c6c6f)
./lotus-shed rpc MpoolPushMessage '[{"From":"f3u27vs6zd3eesaevkawq4wo3emr45vu7cipjkbi5zgafko32aoebqikw7ohgsirdsenvsp73z6doogpoinwhq", "To": "f3wnmzhu5iln7shuawotkmbaejf6oabp7dits4t4p6zvwxeomqgewjpmp4ho4oyuxuga2gzwe7eu2nmw4prsjq", "Value": "1", "Params": "aGVsbG8="}, {"MaxFee": "70000000000000000"}]'

{"jsonrpc":"2.0","result":{"Message":{"Version":0,"To":"f3wnmzhu5iln7shuawotkmbaejf6oabp7dits4t4p6zvwxeomqgewjpmp4ho4oyuxuga2gzwe7eu2nmw4prsjq","From":"f3u27vs6zd3eesaevkawq4wo3emr45vu7cipjkbi5zgafko32aoebqikw7ohgsirdsenvsp73z6doogpoinwhq","Nonce":834,"Value":"1","GasLimit":592085,"GasFeeCap":"10511865265","GasPremium":"99612","Method":0,"Params":"aGVsbG8=","CID":{"/":"bafy2bzacedtrbj3j2kr7jjlpselfilae634rhfn6k7qmllfsyyfqnnbmqory6"}},"Signature":{"Type":2,"Data":"kFXCBa52x4QMamFDEfn36IqGCo+Zbst+3nnj6N+zuHq/8KsZZW5VykMw4ChhioxSCs9+u44yinnnsYxYdWcpvqgh2oQXvdFvabqoluavh8bhZl6OgbD6p9dS60zzNwtK"},"CID":{"/":"bafy2bzacedtrbj3j2kr7jjlpselfilae634rhfn6k7qmllfsyyfqnnbmqory6"}},"id":0}

lotus API example

  • TODO: show an example here of signing the message outside, and using MPoolPush

filecoin.js example

  • using filecoin.js, assuming you have set it up (see their docs)
  • you should be able to do something like this:
const msgVals = {
    To: <to-addr>,
    From: <from-addr>,
    Value: new BigNumber(<amount>),
    Params: <base64-encoded-params>, // todo: check if this is base64 encoded string, or a Buffer
} 

const message = await walletProvider.createMessage(msgVals);
const signedMessage = await walletProvider.signMessage(message);
const msgCid = await walletProvider.sendSignedMessage(signedMessage);
  • TODO: write an example w/ js-lotus-client

Hosted services

there are a bunch of filecoin node hosted services. see:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment