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

cw20-merkle-airdrop: change hashing to sha256 #374

Merged
merged 11 commits into from
Aug 4, 2021

Conversation

orkunkl
Copy link
Contributor

@orkunkl orkunkl commented Aug 4, 2021

No description provided.

Copy link
Member

@ethanfrey ethanfrey left a comment

Choose a reason for hiding this comment

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

Good stuff. Some nit-pick comments to improve coding style, but it looks good.

Glad to see the test vector. Can you just document the command you use in the TS code to generate that test data?

```

**Generate proof:**
```shell
merkle-airdrop-cli generateProofs --file ../testdata/airdrop_stage_2.json \
merkle-airdrop-cli generateProofs --file ../testdata/airdrop_stage_2_list.json \
Copy link
Member

Choose a reason for hiding this comment

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

Ah, did you use the output of these as input to rust tests?

It does look like you have a place to generate test vectors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep exactly

if (hashBuf < proofBuf) {
hashBuf = keccak256(Buffer.concat([hashBuf, proofBuf]));
hashBuf = Buffer.from(sha256(Buffer.concat([hashBuf, proofBuf]).toString()));
Copy link
Member

Choose a reason for hiding this comment

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

why type does sha256 return? Why Buffer.from(sha256(a).toString())?
Seems like there may be an easier way to transform it.

(Also if using Buffer.from, I think best to specify the input type. I think the default is hex, but let's be specific)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sha256 returns CryptoJS/WordArray which is hard to work with. Only toString method available.

Copy link
Member

Choose a reason for hiding this comment

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

Well, best you could do with what you got.

@@ -16,6 +15,7 @@ use crate::msg::{
MerkleRootResponse, MigrateMsg, QueryMsg,
};
use crate::state::{Config, CLAIM, CONFIG, LATEST_STAGE, MERKLE_ROOT};
use sha2::Digest;
Copy link
Member

Choose a reason for hiding this comment

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

I'd move this up, same line where sha3 was

@@ -359,9 +360,33 @@ mod tests {
);
}

const TEST_DATA_1: &str = "./testdata/airdrop_stage_1_test_data.json";
Copy link
Member

Choose a reason for hiding this comment

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

I like it!

Copy link
Member

Choose a reason for hiding this comment

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

Looking above, I don't see the command that generates these test data.
Can you please add this to the README of the ts side (or a comment here)?

proofs: Vec<String>,
}

fn read_test_data(path: &str) -> Encoded {
Copy link
Member

Choose a reason for hiding this comment

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

There is a nice trick you can use...

Replace:

const TEST_DATA_1: &str = "./testdata/airdrop_stage_1_test_data.json";
let test_data = read_test_data(TEST_DATA_1);

with

const TEST_DATA_1: &[u8] = include_bytes!("./testdata/airdrop_stage_1_test_data.json");
let test_data: Encoded = from_slice(TEST_DATA_1).unwrap();

Then you can remove read_test_data.

"280777995d054081cbf208bccb70f8d736c1766b81d90a1fd21cd97d2d83a5cc".to_string(),
"3946ea1758a5a2bf55bae1186168ad35aa0329805bc8bff1ca3d51345faec04a".to_string(),
],
proof: test_data.proofs,
Copy link
Member

Choose a reason for hiding this comment

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

Nice to use this from those files.

@orkunkl
Copy link
Contributor Author

orkunkl commented Aug 4, 2021

Merging now, helper.ts works contract works. I will leave the rest to CosmJS DevRel person for learning content!

@orkunkl orkunkl merged commit 4a7cdfa into main Aug 4, 2021
@orkunkl orkunkl deleted the cw20-merkle-airdrop-sha256 branch August 4, 2021 17:04
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.

2 participants