Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse a bcs serialized RawTransaction and return important fields #1

Open
ankushagarwal opened this issue Dec 7, 2021 · 2 comments

Comments

@ankushagarwal
Copy link
Contributor

ankushagarwal commented Dec 7, 2021

When a RawTransaction is being signed on the ledger nano device, we want to display relevant transaction fields on the display so that the user can confirm them before signing.

Source of RawTransaction struct : https://github.com/diem/diem/blob/0bf8a9702c847bb9741b3b4747ed4e0ca5f1a827/types/src/transaction/mod.rs#L67

Fields that we want to extract:

  • sequence_number: u64
  • payload: TransactionPayload
    • If payload is of type ScriptFunction(ScriptFunction), parse
      • module: ModuleId,
      • function: Identifier,
      • ty_args: Vec,
      • args: Vec<Vec>,

Example serialized (hex-encoded) RawTransaction in bcs is:

000000000000000000000000000000016d0500000000000003000000000000000000000000000000010e5061796d656e74536372697074731a706565725f746f5f706565725f776974685f6d657461646174610107000000000000000000000000000000010358555303585553000410000000000000000000000000000000dd0801000000000000000100010048010000000000009b0300000000000003585553287412000000000004

The code for constructing this RawTransaction object and serializing it is here : https://github.com/ankushagarwal/libra/blob/test-txn-serialize/api/src/tests/transactions_test.rs#L1701

We need to take this serialized RawTransaction in our app and write a function which can extract the fields mentioned above

Step 1

Write a function in rust-app/src/utils.rs which takes as an input the raw_txn as a byte array (&[u8]) and parses returns the u64 sequence number.

Sample fn signature : fn seq_num(raw_txn: &[u8]) -> Result<u64, u8>

@lei2022
Copy link
Contributor

lei2022 commented Dec 9, 2021

@ankushagarwal : to confirm on the input parameter, raw_txn. It is a byte array([u8]), rather than string(https://github.com/ankushagarwal/libra/blob/test-txn-serialize/api/src/tests/transactions_test.rs#L1701) or the RawTransaction object.

The byte array is after we apply bcs::to_bytes() on the RawTransaction object, so using the above example(transactions_test.rs#L1701), the array is following. Thank you for confirming!

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 109, 5, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 14, 80, 97, 121, 109, 101, 110, 116, 83, 99, 114, 105, 112, 116, 115, 26, 112, 101, 101, 114, 95, 116, 111, 95, 112, 101, 101, 114, 95, 119, 105, 116, 104, 95, 109, 101, 116, 97, 100, 97, 116, 97, 1, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 88, 85, 83, 3, 88, 85, 83, 0, 4, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 221, 8, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 72, 1, 0, 0, 0, 0, 0, 0, 155, 3, 0, 0, 0, 0, 0, 0, 3, 88, 85, 83, 40, 116, 18, 0, 0, 0, 0, 0, 4]

@ankushagarwal
Copy link
Contributor Author

Yeah it will be a byte array. The string is a hex-encoding of the same byte array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants