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

Merkle tree #99

Merged
merged 18 commits into from
Oct 20, 2022
Merged

Merkle tree #99

merged 18 commits into from
Oct 20, 2022

Conversation

relatko
Copy link
Contributor

@relatko relatko commented Jul 26, 2022

Adds Merkle tree support

@pgebheim
Copy link

pgebheim commented Aug 5, 2022

How does this relate to #96 ?

@relatko
Copy link
Contributor Author

relatko commented Aug 8, 2022

@pgebheim This is a very basic implementation of #96. At the moment it supports 7^4 transactions (exponent is quite easy to change in the future) and avoids any optimizations and complications.

@pgebheim
Copy link

pgebheim commented Aug 8, 2022

Thanks!

@pgebheim pgebheim added this to the v0.10.2 milestone Sep 7, 2022
js/src/helperV2.js Outdated Show resolved Hide resolved
validateCryptoOptions(cryptoOptions);

const getVersionResponse = await this.getVersion();
const pathSerializationVersion = (compareVersion(getVersionResponse, 0, 9, 12) <= 0) ? 0 : 1;

return this.signGetChunks(path, cryptoOptions, pathSerializationVersion, message).then((chunks) => {
// APDU call sequence without Merkle trees
if (compareVersion(getVersionResponse, 0, 10, 3) <= 0) {
Copy link

Choose a reason for hiding this comment

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

Maybe the compareVersion function can be renamed so that the name would indicate what it does (e.g. isVersionAtLeast or something). It's unnecessarily hard to understand what <= 0 means for its result.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is quite standard three-way comparison. Maybe slightly less readable but slightly more flexible. I think I will keep it now, but I will consider this change when there is more backward compatibility code.

js/src/index.js Outdated Show resolved Hide resolved
js/src/index.js Outdated
@@ -98,6 +100,31 @@ export default class FlowApp {
return FlowApp.prepareChunks(serializedPath, message);
}

async signGetChunksv2(path, options, pathSerializationVersion, message, scriptHash) {
Copy link

Choose a reason for hiding this comment

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

I don't understand this function. E.g.

  1. what are basic chunks vs. other chunks, or actually what are chunks?
  2. why is it async? all of the code seems synchronous in the end (or I have overlooked something)
  3. since there are no types, code like scriptHash.slice(0, 16) is incomprehensible to me; perhaps we can extract the operation ".slice(0,16)" into a function with a meaningful name

js/src/helperV2.js Show resolved Hide resolved
docs/APDUSPEC.md Outdated Show resolved Hide resolved
docs/APDUSPEC.md Show resolved Hide resolved
Copy link

@janmazak janmazak left a comment

Choose a reason for hiding this comment

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

Looks OK, some suggestions for improvements in the comments.

return false;
case 4:
if (validateStoredTxMetadataMerkleTreeLevel(&(G_io_apdu_buffer[OFFSET_DATA]), rx - OFFSET_DATA) != PARSER_OK) {
THROW(APDU_CODE_DATA_INVALID);
Copy link

Choose a reason for hiding this comment

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

It would be safest to reset the proof state before THROW. The same in case 5 and possibly elsewhere (e.g. if it is possible to send APDU 1 before APDU 0).

app/src/common/app_main.c Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved

1. Add/modify the manifest files in `transaction_metadata` directory, modify the script to include new files.
2. Copy the resulting `txMerkleTree.js` file to `js/src`. Re-build js and re-install speculos (to include newly build js).
3. Take the top level hash from `txMerkleTree.js` and move it to `merkleTreeRoot` variable of `app/src/tx_metadata.c` (you need to split the hex into C array of uint8_t's).
Copy link

Choose a reason for hiding this comment

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

This is annoying and error-prone. It would be better if an appropriate C source file was generated by some script and included in tx_metadata.c (e.g. as with https://github.com/vacuumlabs/ledger-app-cardano-shelley/tree/develop/tokenRegistry).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now, I do not want to introduce anything elaborate and I want to work hands on for some time. As the comment bellow suggest, changes will be necessary very soon (backward compatibility, scaling up the processes 100s of transactions). When I will have enough information I will definitely automate this but as of now I am not sure how do this in such a way that I will not have to rework the solution soon.
Errors should be easily detected by tests, so I am not that worried about this.

README.md Outdated Show resolved Hide resolved
app/src/tx_metadata.c Outdated Show resolved Hide resolved
app/src/tx_metadata.c Outdated Show resolved Hide resolved
app/src/tx_metadata.c Outdated Show resolved Hide resolved
return PARSER_METADATA_ERROR;
}

//validate that current hash is in the list
Copy link

Choose a reason for hiding this comment

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

I'd rather move this calculation into a function, it's a small detail that takes too much space (and would be covered in a single statement if this was not C).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My opinion is that this function is quite simple and this represent the the non-trivial thing the function does.

app/src/tx_metadata.c Show resolved Hide resolved
@@ -48,7 +48,7 @@ endif

APPVERSION_M=0
APPVERSION_N=10
APPVERSION_P=0
APPVERSION_P=4
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we jumping from 0.10.0 to 0.10.4?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am leaving space. We plan to insert a small release containing staking transactions. This will be 0.10.1. I can see 0.10.2 somehow happen. Maybe we should change it to 0.11.0. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Because of the breaking change?

@Wolog2021 Wolog2021 merged commit be60ce3 into master Oct 20, 2022
@relatko relatko mentioned this pull request Oct 25, 2022
@relatko relatko deleted the merkle_tree branch November 29, 2022 08:11
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

Successfully merging this pull request may close these issues.

4 participants