Calldata Coders & Helpers
calldata - Calldata.encode / Calldata.decode using utf8_to_hex and hex_to_utf8 helpers and more for inscriptions / inscribes for ethereum & co
What's Ethscription Calldata - Ethereum Inscription / Inscribe?!
See Introducing Ethscriptions - A new way of creating and sharing digital artifacts on the Ethereum blockchain using transaction calldata »
Usage
Let's start with the Calldata encode/decode helpers.
In an inscribe the data gets encoded in the the calldata (hexdata notes) of a transaction.
The calldata is a binary blob.
Note: This gem uses hexstrings for binary blobs.
Option 1: Use the "low-level" global utf8_to_hex
and hex_to_utf8
helper methods:
require 'calldata'
hex = utf8_to_hex( "Hello, World!" )
utf8 = hex_to_utf8( "48656c6c6f2c20576f726c6421" )
hex = utf8_to_hex( "data:image/png;base64,..." )
utf8 = hex_to_utf8( "646174613a696d6167652f706e673b6261736536342c2e2e2e" )
Option 2: Use the Calldata
module helper methods encode/encode_utf8
and decode/decode_hex
:
hex = Calldata.encode( "Hello, World!" )
utf8 = Calldata.decode( "0x48656c6c6f2c20576f726c6421" )
hex = Calldata.encode( "data:image/png;base64,..." )
utf8 = Calldata.decode( "0x646174613a696d6167652f706e673b6261736536342c2e2e2e" )
Note: The hex_to_utf8
helper (incl. Calldata.decode
)
works with or without leading 0x
in hexstrings.
Data URIs / URLs
Let's continue with the built-in data uri/url machinery.
A valid inscribe must use a valid data uri/url in the calldata.
The "useless" (null) minimum is:
data_uri = "data:,"
Calldata.valid_data?( data_uri )
data = Calldata.parse_data( data_uri )
data.type
data.text
Let's try the (genesis) inscribe no. 0:
data_uri = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHV..."
Calldata.valid_data?( data_uri )
data = Calldata.parse_data( data_uri )
data.type
data.blob
data.write( "0.jpeg" )
and voila!

Let's try the inscribe no. 15:
data_uri = "data:image/png;base64,/9j/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAAB..."
Calldata.valid_data?( data_uri )
data = Calldata.parse_data( data_uri )
data.type
data.blob
data.write( "15.png" )
and voila!

Let's try a (structured) protocol message inscribe:
data_uri = %Q<data:,{"p":"erc-20","op":"mint","tick":"eths","id":"16888","amt":"1000"}>
Calldata.valid_data?( data_uri )
data = Calldata.parse_data( data_uri )
data.type
data['p']
data['op']
data['tick']
data['id']
data['amt']
or use the convenience all-in-one parse_hex
helper:
hex = "0x646174613a2c7b2270223a226572632d3230222c226f70223a226d696e74222c227469636b223a2265746873222c226964223a223136383838222c22616d74223a2231303030227d"
data = Calldata.parse_hex( hex )
data.type
data['p']
data['op']
data['tick']
data['id']
data['amt']
That's it for now.
Bonus - More Blockchain (Crypto) Tools, Libraries & Scripts In Ruby
See /blockchain
at the ruby code commons (rubycocos) org.
Join us in the 0xCompute discord (chat server)
(or in the more general Ethscription discord).
Yes you can.
Your questions and commentary welcome.
Or post them over at the Help & Support page. Thanks.