Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

add cleos convert pack_transaction and unpack_transaction #5018

Closed

Conversation

eosbase
Copy link

@eosbase eosbase commented Aug 3, 2018

I needed a way to pack and unpack transactions from shell scripts and found this easiest to do.

Would something like this be accepted ? Or are any changes required ?

Thanks, Leon

--

for example pack:

# cleos convert pack_transaction '{
  "expiration": "2018-08-02T20:24:36",
  "ref_block_num": 14207,
  "ref_block_prefix": 1438248607,
  "max_net_usage_words": 0,
  "max_cpu_usage_ms": 0,
  "delay_sec": 0,
  "context_free_actions": [],
  "actions": [{
      "account": "eosio",
      "name": "newaccount",
      "authorization": [{
          "actor": "eosio",
          "permission": "active"
        }
      ],
      "data": "0000000000ea305500a6823403ea30550100000001000240cc0bf90a5656c8bb81f0eb86f49f89613c5cd988c018715d4646c6bd0ad3d8010000000100000001000240cc0bf90a5656c8bb81f0eb86f49f89613c5cd988c018715d4646c6bd0ad3d801000000"
    }
  ],
  "transaction_extensions": []
}'

{
  "signatures": [],
  "compression": "none",
  "packed_context_free_data": "",
  "packed_trx": "8468635b7f379feeb95500000000010000000000ea305500409e9a2264b89a010000000000ea305500000000a8ed3232660000000000ea305500a6823403ea30550100000001000240cc0bf90a5656c8bb81f0eb86f49f89613c5cd988c018715d4646c6bd0ad3d8010000000100000001000240cc0bf90a5656c8bb81f0eb86f49f89613c5cd988c018715d4646c6bd0ad3d80100000000"
}

and unpack:

# cleos convert unpack_transaction '{
  "signatures": [
    "SIG_K1_KmRbWahefwxs6uyCGNR6wNRjw7cntEeFQhNCbyg8S92Kbp7zdSSVGTD2QS7pNVWgcU126zpxaBp9CwUxFpRwSnfkjd46bS"
  ],
  "compression": "none",
  "packed_context_free_data": "",
  "packed_trx": "8468635b7f379feeb95500000000010000000000ea305500409e9a2264b89a010000000000ea305500000000a8ed3232660000000000ea305500a6823403ea30550100000001000240cc0bf90a5656c8bb81f0eb86f49f89613c5cd988c018715d4646c6bd0ad3d8010000000100000001000240cc0bf90a5656c8bb81f0eb86f49f89613c5cd988c018715d4646c6bd0ad3d80100000000"
}'

{
  "expiration": "2018-08-02T20:24:36",
  "ref_block_num": 14207,
  "ref_block_prefix": 1438248607,
  "max_net_usage_words": 0,
  "max_cpu_usage_ms": 0,
  "delay_sec": 0,
  "context_free_actions": [],
  "actions": [{
      "account": "eosio",
      "name": "newaccount",
      "authorization": [{
          "actor": "eosio",
          "permission": "active"
        }
      ],
      "data": "0000000000ea305500a6823403ea30550100000001000240cc0bf90a5656c8bb81f0eb86f49f89613c5cd988c018715d4646c6bd0ad3d8010000000100000001000240cc0bf90a5656c8bb81f0eb86f49f89613c5cd988c018715d4646c6bd0ad3d801000000"
    }
  ],
  "transaction_extensions": []
}

Copy link
Contributor

@heifner heifner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be useful. Thanks. Can you rebase to develop branch and use the new variant_to_bin there to also support passing in unpacked action data with your transaction and also support unpacking of action data. Seems like that should be optional so that if someone wants to avoid interacting with an active nodeos they can as long as they pack that action data like in your examples.

@eosbase
Copy link
Author

eosbase commented Aug 3, 2018

thanks, I will do that!

@eosbase eosbase force-pushed the add_cleos_convert_pack_unpack_transaction branch from 206a0eb to ef60b93 Compare August 5, 2018 23:36
@eosbase eosbase force-pushed the add_cleos_convert_pack_unpack_transaction branch from ef60b93 to 4e9965d Compare August 7, 2018 15:37
@eosbase eosbase changed the base branch from master to develop August 7, 2018 15:37
std::tie( it, std::ignore ) = abi_cache.emplace( account, result["abi"].as_blob().data );
//we also received result["wasm"], but we don't use it
}
const std::vector<char>& abi_v = it->second;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above should be moved to its own function to avoid duplication and to use same abi_cache for variant_to_bin and bin_to_variant

@@ -1725,6 +1752,86 @@ int main( int argc, char** argv ) {
// create account
auto createAccount = create_account_subcommand( create, true /*simple*/ );

// convert subcommand
auto convert = app.add_subcommand("convert", localized("Pack and unpack transactions"), false); // TODO also add converting action args based on abi from here ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To address your TODO, I do think having the ability to specify an abi file to use would be nice. In that case it should NOT connect to nodeos or need to.

std::vector<char> trx_blob(trx_hex.size()/2);
fc::from_hex(trx_hex, trx_blob.data(), trx_blob.size());
transaction unpacked_trx = fc::raw::unpack<transaction>(trx_blob);
// TODO would it be nice to put the unpacked_trx actions into the trx actions in a separate unpacked_data field or somesuch ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove TODO, that would require api changes.

std::cout << fc::json::to_pretty_string(unpacked_action_data) << std::endl;
}
} else {
// TODO should the "sigantures" and "context_free_data" fields be copied from trx_var into the action field(s) ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, signatures and context_free_data should be added to the signed transaction if available.

bool unpack_action_data_flag = false;
auto unpack_transaction = convert->add_subcommand("unpack_transaction", localized("From packed to plain signed json form"));
unpack_transaction->add_option("transaction", packed_transaction_json, localized("The packed transaction json (string containing packed_trx and optionally compression fields)"))->required();
unpack_transaction->add_flag("--unpack-action-data", unpack_action_data_flag, localized("Upack all action datas within transaction and only return those, needs interaction with nodeos"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why anyone would use --unpack-action-data. What is the use case? I think it is fine, just not sure anyone would use it.

@heifner
Copy link
Contributor

heifner commented Aug 14, 2018

Replaced by #5225

@heifner heifner closed this Aug 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants