Skip to content

Commit

Permalink
Update the keccak used in compute_chain_id util (#67)
Browse files Browse the repository at this point in the history
* Use the "tiny_keccak" crate for "compute_chain_id" util

* Update the integration tests for "(v)anchor" contracts

* Uncomment the integration tests

* Update .gitignore
  • Loading branch information
duguorong009 authored Aug 4, 2022
1 parent 924d828 commit 24cd666
Show file tree
Hide file tree
Showing 13 changed files with 2,819 additions and 2,862 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ test-scripts/src/config/localjunoConstants.ts
test-scripts/src/config/wasmPaths.ts
test-scripts/src/config/wasms/*.wasm

.DS_Store
.DS_Store

test-scripts/yarn.lock
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contracts/anchor/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ fn deposit_cw20(

// No need to handle any cw20 token transfer
// since "TokenWrapper" tokens are already sent to this contract
println!("commitment: {:?}", commitment);
Ok(
Response::new().add_event(Event::new("anchor-deposit").add_attributes(vec![
attr("action", "deposit_cw20"),
Expand Down
2 changes: 1 addition & 1 deletion contracts/anchor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::contract::{execute, instantiate};
use crate::test_util::Element;

const MAX_EDGES: u32 = 2;
const CHAIN_ID: u64 = 2145598729; // chain_id: "cosmos-testnet-14002"
const CHAIN_ID: u64 = 3620629146; // chain_id: "cosmos-testnet-14002"
const LEVELS: u32 = 30;
const TOKENWRAPPER_ADDR: &str = "terra1340t6lqq6jxhm8d6gtz0hzz5jzcszvm27urkn2"; // Cw20 token
const DEPOSIT_SIZE: u128 = 1_000_000;
Expand Down
8 changes: 1 addition & 7 deletions contracts/vanchor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use protocol_cosmwasm::vanchor::{
use protocol_cosmwasm::zeroes::zeroes;

const CHAIN_TYPE: [u8; 2] = [4, 0]; // 0x0400
const CHAIN_ID: u64 = 2145598729; // chain_id: "cosmos-testnet-14002"
const CHAIN_ID: u64 = 3620629146; // chain_id: "cosmos-testnet-14002"

const MAX_EDGES: u32 = 2;
const LEVELS: u32 = 30;
Expand Down Expand Up @@ -1477,9 +1477,6 @@ fn test_vanchor_withdraw_and_unwrap_native() {
ext_data_hash.0,
);

println!("proof_data:{:?}\n", proof_data);
println!("ext_data:{:?}", ext_data);

// Should "transact" with success.
let info = mock_info(CW20_ADDRESS, &[]);
let deposit_cw20_msg = ExecuteMsg::Receive(Cw20ReceiveMsg {
Expand Down Expand Up @@ -1553,9 +1550,6 @@ fn test_vanchor_withdraw_and_unwrap_native() {
ext_data_hash.0,
);

println!("proof_data:{:?}\n", proof_data);
println!("ext_data:{:?}", ext_data);

// Should "transact" with success.
let info = mock_info(CW20_ADDRESS, &[]);
let withdraw_cw20_msg = ExecuteMsg::TransactWithdrawUnwrap {
Expand Down
1 change: 1 addition & 0 deletions packages/protocol_cosmwasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cosmwasm-std = "1.0.0"
schemars = "0.8.1"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.26" }
tiny-keccak = { version = "2.0.2", features = ["sha3"] }

ark-ff = { version = "^0.3.0", default-features = false }
ark-std = { version = "^0.3.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol_cosmwasm/src/treasury.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct InstantiateMsg {
pub treasury_handler: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
/// Send the (native or cw20) tokens to destination address
Expand Down
11 changes: 8 additions & 3 deletions packages/protocol_cosmwasm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::keccak::Keccak256;
use tiny_keccak::{Hasher, Keccak};

/// Slice the length of the bytes array into 32bytes
pub fn element_encoder(v: &[u8]) -> [u8; 32] {
Expand Down Expand Up @@ -37,8 +37,13 @@ pub fn compute_chain_id_type(chain_id: u64, chain_type: &[u8]) -> u64 {
/// 2. Slice the last 4 bytes & convert it to `u32` numeric value
/// eg: 8a294d21(hex) -> 2317962529(decimal)
pub fn compute_chain_id(chain_id_str: &str) -> u32 {
let hash_value = Keccak256::hash(chain_id_str.as_bytes()).expect("chain-id hashing error");
let last_4_bytes = &hash_value[28..];
let mut keccak = Keccak::v256();
keccak.update(chain_id_str.as_bytes());

let mut output = [0u8; 32];
keccak.finalize(&mut output);

let last_4_bytes = &output[28..];

let mut buf = [0u8; 4];
buf[0..4].copy_from_slice(last_4_bytes);
Expand Down
8 changes: 4 additions & 4 deletions test-scripts/src/processes/tests/anchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function testAnchorInitialize(
// Succeed to "deposit"
const result = await wallet3_client.execute(localjuno.addresses.wallet3, localjuno.contracts.anchor, {
wrap_and_deposit: {
commitment: [37, 182, 146, 58, 233, 152, 60, 235, 245, 80, 102, 165, 73, 117, 83, 34, 18, 215, 241, 142, 54, 189, 23, 80, 211, 122, 177, 140, 184, 200, 9, 47],
commitment: [168, 247, 178, 1, 255, 94, 65, 44, 230, 251, 95, 202, 71, 133, 55, 218, 109, 250, 240, 191, 208, 58, 227, 252, 77, 90, 234, 98, 145, 24, 141, 43],
amount: ucosm_amount,
}
}, "auto", undefined, [coin(ucosm_to_send, "ucosm")])
Expand All @@ -98,7 +98,7 @@ export async function testAnchorInitialize(
await expect(
wallet2_client.execute(localjuno.addresses.wallet2, anchor, {
withdraw_and_unwrap: {
proof_bytes: [254, 221, 51, 90, 6, 117, 43, 109, 75, 64, 147, 126, 195, 246, 6, 35, 118, 181, 239, 213, 58, 121, 13, 49, 217, 124, 183, 146, 127, 73, 28, 172, 9, 222, 120, 126, 122, 54, 111, 247, 204, 235, 149, 87, 3, 254, 200, 51, 99, 26, 188, 44, 164, 205, 143, 195, 238, 12, 222, 155, 129, 89, 148, 28, 36, 197, 176, 99, 16, 173, 222, 27, 81, 136, 227, 125, 188, 250, 118, 148, 8, 193, 71, 34, 170, 133, 150, 164, 65, 242, 125, 70, 242, 142, 196, 157, 239, 202, 126, 206, 47, 103, 51, 70, 222, 69, 95, 16, 196, 47, 248, 128, 12, 162, 134, 172, 165, 178, 16, 1, 217, 35, 127, 73, 6, 92, 217, 46],
proof_bytes: [102, 172, 207, 127, 45, 32, 170, 13, 146, 22, 201, 77, 129, 236, 140, 134, 138, 157, 89, 187, 57, 214, 228, 100, 48, 2, 156, 130, 157, 81, 29, 130, 8, 229, 108, 16, 176, 143, 130, 96, 150, 170, 252, 72, 33, 69, 174, 214, 34, 50, 253, 22, 30, 30, 12, 202, 243, 199, 173, 84, 137, 110, 230, 2, 174, 119, 59, 165, 185, 179, 54, 221, 134, 194, 126, 110, 176, 236, 32, 141, 5, 152, 64, 245, 16, 185, 22, 253, 1, 136, 167, 215, 66, 205, 152, 37, 208, 104, 45, 88, 112, 35, 38, 9, 67, 106, 51, 12, 15, 163, 105, 64, 169, 57, 196, 32, 99, 7, 60, 40, 207, 11, 183, 205, 27, 97, 158, 129],
roots: [],
nullifier_hash: [183, 160, 141, 89, 98, 241, 220, 87, 120, 249, 242, 56, 92, 41, 28, 230, 247, 111, 155, 7, 94, 2, 142, 101, 0, 243, 39, 32, 59, 235, 198, 31],
recipient: localjuno.addresses.wallet2,
Expand All @@ -117,8 +117,8 @@ export async function testAnchorInitialize(

const result1 = await wallet2_client.execute(localjuno.addresses.wallet2, anchor, {
withdraw_and_unwrap: {
proof_bytes: [254, 221, 51, 90, 6, 117, 43, 109, 75, 64, 147, 126, 195, 246, 6, 35, 118, 181, 239, 213, 58, 121, 13, 49, 217, 124, 183, 146, 127, 73, 28, 172, 9, 222, 120, 126, 122, 54, 111, 247, 204, 235, 149, 87, 3, 254, 200, 51, 99, 26, 188, 44, 164, 205, 143, 195, 238, 12, 222, 155, 129, 89, 148, 28, 36, 197, 176, 99, 16, 173, 222, 27, 81, 136, 227, 125, 188, 250, 118, 148, 8, 193, 71, 34, 170, 133, 150, 164, 65, 242, 125, 70, 242, 142, 196, 157, 239, 202, 126, 206, 47, 103, 51, 70, 222, 69, 95, 16, 196, 47, 248, 128, 12, 162, 134, 172, 165, 178, 16, 1, 217, 35, 127, 73, 6, 92, 217, 46],
roots: [[175, 83, 6, 17, 157, 29, 197, 241, 102, 254, 25, 90, 172, 136, 69, 45, 105, 80, 35, 14, 135, 47, 253, 121, 150, 71, 5, 151, 124, 124, 61, 27], [175, 83, 6, 17, 157, 29, 197, 241, 102, 254, 25, 90, 172, 136, 69, 45, 105, 80, 35, 14, 135, 47, 253, 121, 150, 71, 5, 151, 124, 124, 61, 27]],
proof_bytes: [102, 172, 207, 127, 45, 32, 170, 13, 146, 22, 201, 77, 129, 236, 140, 134, 138, 157, 89, 187, 57, 214, 228, 100, 48, 2, 156, 130, 157, 81, 29, 130, 8, 229, 108, 16, 176, 143, 130, 96, 150, 170, 252, 72, 33, 69, 174, 214, 34, 50, 253, 22, 30, 30, 12, 202, 243, 199, 173, 84, 137, 110, 230, 2, 174, 119, 59, 165, 185, 179, 54, 221, 134, 194, 126, 110, 176, 236, 32, 141, 5, 152, 64, 245, 16, 185, 22, 253, 1, 136, 167, 215, 66, 205, 152, 37, 208, 104, 45, 88, 112, 35, 38, 9, 67, 106, 51, 12, 15, 163, 105, 64, 169, 57, 196, 32, 99, 7, 60, 40, 207, 11, 183, 205, 27, 97, 158, 129],
roots: [[193, 227, 188, 65, 69, 113, 210, 2, 150, 35, 62, 52, 41, 169, 144, 213, 69, 59, 17, 14, 106, 181, 222, 51, 208, 134, 119, 184, 130, 215, 145, 46], [193, 227, 188, 65, 69, 113, 210, 2, 150, 35, 62, 52, 41, 169, 144, 213, 69, 59, 17, 14, 106, 181, 222, 51, 208, 134, 119, 184, 130, 215, 145, 46]],
nullifier_hash: [183, 160, 141, 89, 98, 241, 220, 87, 120, 249, 242, 56, 92, 41, 28, 230, 247, 111, 155, 7, 94, 2, 142, 101, 0, 243, 39, 32, 59, 235, 198, 31],
recipient: localjuno.addresses.wallet2,
relayer: localjuno.addresses.wallet3,
Expand Down
5 changes: 5 additions & 0 deletions test-scripts/src/processes/tests/signatureBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,13 @@ export async function testSignatureBridgeExecProposalWithSignature(

// proposal of `set_handler`
const resource_id: Buffer = genResourceId(localjuno.contracts.anchor);
const func_sig: Buffer = Buffer.from([0x00, 0x00, 0x00, 0x00]);
const nonce: Buffer = Buffer.alloc(4);
nonce.writeUInt16BE(beforeNonce + 3, 0);
const data = Buffer.concat([
resource_id,
func_sig,
nonce,
Buffer.from(toEncodedBinary({
set_handler: {
handler: localjuno.contracts.anchorHandler,
Expand Down
52 changes: 26 additions & 26 deletions test-scripts/src/processes/tests/testnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,41 +63,41 @@ export async function testExecute(
treasuryHandler: string,
): Promise<void> {
console.log(chalk.yellow("\nStep 3. Running Tests"));
// // SignatureBridge
// await testSignatureBridgeInitialize(junod, signatureBridge);
// SignatureBridge
await testSignatureBridgeInitialize(junod, signatureBridge);
await testSignatureBridgeAdminSetResWithSignature(junod, wallet1, signatureBridge);
// await testSignatureBridgeExecProposalWithSignature(junod, wallet1, signatureBridge);
await testSignatureBridgeExecProposalWithSignature(junod, wallet1, signatureBridge);

// // TokenWrapper
// await testTokenWrapperInitialize(junod, tokenWrapper);
// TokenWrapper
await testTokenWrapperInitialize(junod, tokenWrapper);

// // TokenWrapperHandler
// TokenWrapperHandler

// // AnchorHandler
// AnchorHandler

// // Anchor
// await testAnchorInitialize(junod, anchor);
// await testAnchorDepositWithdraw(junod, anchor, wallet1, wallet2, wallet3, "1000000");
// await testAnchorWrapNative(junod, anchor, wallet3, "100000");
// await testAnchorUnwrapNative(junod, anchor, wallet3, "100");
// await testAnchorWrapCw20(junod, anchor, tokenWrapper, cw20, wallet3, "10000");
// await testAnchorUnwrapCw20(junod, anchor, tokenWrapper, cw20, wallet3, "100");
// Anchor
await testAnchorInitialize(junod, anchor);
await testAnchorDepositWithdraw(junod, anchor, wallet1, wallet2, wallet3, "1000000");
await testAnchorWrapNative(junod, anchor, wallet3, "100000");
await testAnchorUnwrapNative(junod, anchor, wallet3, "100");
await testAnchorWrapCw20(junod, anchor, tokenWrapper, cw20, wallet3, "10000");
await testAnchorUnwrapCw20(junod, anchor, tokenWrapper, cw20, wallet3, "100");

// // VAnchor
// await testVAnchorInitialize(junod, vanchor);
// await testVAnchorDepositWithdraw(junod, vanchor, cw20, wallet1, wallet2, wallet3, "10", "10", "0", "-7", "-5", "2");
// await testVAnchorWrapNative(junod, vanchor, wallet3, "100000");
// await testVAnchorUnwrapNative(junod, vanchor, wallet3, "100");
// await testVAnchorWrapCw20(junod, vanchor, tokenWrapper, cw20, wallet3, "10000");
// await testVAnchorUnwrapCw20(junod, vanchor, tokenWrapper, cw20, wallet3, "100");
// VAnchor
await testVAnchorInitialize(junod, vanchor);
await testVAnchorDepositWithdraw(junod, vanchor, cw20, wallet1, wallet2, wallet3, "10", "10", "0", "-7", "-5", "2");
await testVAnchorWrapNative(junod, vanchor, wallet3, "100000");
await testVAnchorUnwrapNative(junod, vanchor, wallet3, "100");
await testVAnchorWrapCw20(junod, vanchor, tokenWrapper, cw20, wallet3, "10000");
await testVAnchorUnwrapCw20(junod, vanchor, tokenWrapper, cw20, wallet3, "100");

// // Mixer
// await testMixerInitialize(junod, mixer);
// await testMixerDepositNativeToken(junod, mixer, wallet3, "1000000");
// await testMixerWithdrawNativeToken(junod, mixer, wallet1, wallet2, wallet3, "1000000");
// Mixer
await testMixerInitialize(junod, mixer);
await testMixerDepositNativeToken(junod, mixer, wallet3, "1000000");
await testMixerWithdrawNativeToken(junod, mixer, wallet1, wallet2, wallet3, "1000000");

// Treasury
// await testTreasuryInitialize(junod, treasury);
await testTreasuryInitialize(junod, treasury);

process.exit();
}
Loading

0 comments on commit 24cd666

Please sign in to comment.