3

Given a transaction in its serialized format, how do you parse it to get the destination, the amount, and the other fields?

I am not asking for library or RPC endpoint to achieve this but for an explanation of how each field is encoded in the serialized raw hex.

Here is an example of a raw hex:

008f1d96e2783258ff663f03dacfe946c026a5d194c73d1987b3da73fadea7d46c008cb5baedee4dc3ec261dfcf57a9600bb0a8e26c0f00bdd85a0018452ac02e0a712000153957451d3cc83a71e26b65ea2391a1b16713d2d009595facf847a72b4c3fe231c0e4185e68e9b2875aa3c639382c86bcf0af23699f47fe66a6550ade936a5b59d5919ad20703885750314e0c368b277de39e7d10a
2
+50

The structure of an encoded operation group is the following:

<branch : 32 bytes>
<tag#0 : nat> | <content#0>
... 
<tag#n : nat> | <content#n>
<signature (optional) : 64 bytes>

Extracting branch:

  1. Append 0x0134 prefix
  2. Base58 encode with checksum

Extracting signature:

  1. Append 0x04822b prefix
  2. Base58 encode with checksum

Operation tag by kind (as of Carthage):

  • 0 for endorsement
  • 5 for proposal
  • 6 for ballot
  • 1 for seed_nonce_revelation
  • 2 for double_endorsement_evidence
  • 3 for double_baking_evidence
  • 4 for activate_account
  • 107 for reveal
  • 108 for transaction
  • 109 for origination
  • 110 for delegation

Content encoding depends on the operation kind, in your question you were likely referring to "transaction", so let's take it as an example:

<source : key_hash>
<fee : nat>
<counter : nat>
<gas_limit : nat>
<storage_limit : nat>
<amount : nat>
<destination : address>
<has_parameters? : bool>
<parameters (optional)>

"has_parameters" flag is False if at least one of the conditions is met:

  • Transaction content does not contain "parameters" field
  • "parameters" field has value {"entrypoint": "default", "value": {"prim": "Unit"}}

Parameters are encoded as follows:

<entrypoint tag : 1 byte> | <entrypoint name (optional)>
<encoded value length : 4 bytes, big endian> | <value : micheline>

Entrypoint tag depends on the entrypoint name (can be reserved):

  • 0x00 for default
  • 0x01 for root
  • 0x02 for do
  • 0x03 for set_delegate
  • 0x04 for remove_delegate
  • 0xff otherwise

Entrypoint name is encoded as <encoded name length : 1 byte> | <utf-8 encoded name>

Out of scope of this answer:

  • Forging primitives: nat, key_hash, address, bool
  • Forging Micheline values

Breakdown for the sample opg hex:

branch 
008f1d96e2783258ff663f03dacfe946c026a5d194c73d1987b3da73fadea7d4
BKiXcfN1ZTXnNNbTWSRArSWzVFc6om7radWq5mTqGX6rY4P2Uhe

tag
6c
transaction

source
008cb5baedee4dc3ec261dfcf57a9600bb0a8e26c0
tz1YU2zoyCkXPKEA4jknSpCpMs7yUndVNe3S

fee
f00b
1520

counter
dd85a001
2622173

gas_limit
8452
10500

storage_limit
ac02
300

amount
e0a712
300000

destination
000153957451d3cc83a71e26b65ea2391a1b16713d2d
tz2FwBnXhuXvPAUcr1aF3uX84Z6JELxrdYxD

has_parameters
00
false

signature
9595facf847a72b4c3fe231c0e4185e68e9b2875aa3c639382c86bcf0af23699f47fe66a6550ade936a5b59d5919ad20703885750314e0c368b277de39e7d10a
sighZMqWz5G8drK1VTsmTnQBFEQ9kxQQxL88NFh8UaqDEJ3R3mzgR3g81azadZ9saPwsWga3kEPsyfbzrXm6ueuDvx3pQ5Q9
| improve this answer | |
  • This is great. Thank you. I believe the raw hex from my question is a "transaction" operation. Can you decode that in your answer to show what values the different fields have for this example? – Thorkil Værge Oct 18 at 13:13
  • Thanks for the decoding of the example. Bounty awarded. – Thorkil Værge Oct 21 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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