Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
Add tests for seal_ecdsa_* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cmichi committed May 19, 2022
1 parent 2944150 commit f783b21
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ env_logger = "0.8"
which = "4.1.0"
portpicker = "0.1.1"
lazy_static = "1.4.0"
hex = "0.4.3"

lang_macro = { path = "./lang_macro"}

Expand Down
9 changes: 9 additions & 0 deletions examples/seal-ecdsa/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore build artifacts from the local tests sub-crate.
/target/

# Ignore backup files creates by cargo fmt.
**/*.rs.bk

# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
36 changes: 36 additions & 0 deletions examples/seal-ecdsa/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "seal_ecdsa"
version = "3.0.1"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
publish = false

[dependencies]
ink_primitives = { path = "../ink/crates/primitives", default-features = false }
ink_metadata = { path = "../ink/crates/metadata", default-features = false, features = ["derive"], optional = true }
ink_env = { path = "../ink/crates/env", default-features = false }
ink_storage = { path = "../ink/crates/storage", default-features = false }
ink_lang = { path = "../ink/crates/lang", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

[lib]
name = "seal_ecdsa"
path = "lib.rs"
crate-type = ["cdylib"]

[features]
default = ["std"]
std = [
"ink_primitives/std",
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_lang/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []

[workspace]
35 changes: 35 additions & 0 deletions examples/seal-ecdsa/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![cfg_attr(not(feature = "std"), no_std)]

//! This contract provides simple E2E tests for the functions
//! `seal_ecdsa_recover` and `seal_ecdsa_to_eth_address`.
use ink_lang as ink;

#[ink::contract]
mod seal_ecdsa {
#[ink(storage)]
pub struct SealEcdsa {}

impl SealEcdsa {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}

/// Tests `seal_ecdsa_recover`.
#[ink(message)]
pub fn recover(&self, signature: [u8; 65], message_hash: [u8; 32]) -> [u8; 33] {
let mut output = [0; 33];
ink_env::ecdsa_recover(&signature, &message_hash, &mut output);
output
}

/// Tests `seal_ecdsa_to_eth_address`.
#[ink(message)]
pub fn to_eth_address(&self, pub_key: [u8; 33]) -> [u8; 20] {
let mut output = [0; 20];
ink_env::ecdsa_to_eth_address(&pub_key, &mut output);
output
}
}
}
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod forward_calls;
mod multisig;
mod rand_extension;
mod seal_code_hash;
mod seal_ecdsa;
mod set_code_hash;
mod trait_erc20;
mod trait_flipper;
Expand Down
99 changes: 99 additions & 0 deletions src/tests/seal_ecdsa.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2018-2021 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Tests for the `seal_ecdsa` example.
use crate::{
uis::{
Call,
Result,
Ui,
Upload,
},
utils::{
self,
cargo_contract,
},
};
use lang_macro::waterfall_test;

#[waterfall_test(example = "seal_ecdsa")]
async fn seal_ecdsa_recover(mut ui: Ui) -> Result<()> {
// given
let contract_path =
cargo_contract::build(&utils::example_path("seal-ecdsa/Cargo.toml"))
.expect("contract build failed");

// when
let addr = ui.execute_upload(Upload::new(contract_path)).await?;

// then
const SIGNATURE: [u8; 65] = [
161, 234, 203, 74, 147, 96, 51, 212, 5, 174, 231, 9, 142, 48, 137, 201, 162, 118,
192, 67, 239, 16, 71, 216, 125, 86, 167, 139, 70, 7, 86, 241, 33, 87, 154, 251,
81, 29, 160, 4, 176, 239, 88, 211, 244, 232, 232, 52, 211, 234, 100, 115, 230,
47, 80, 44, 152, 166, 62, 50, 8, 13, 86, 175, 28,
];
const MESSSAGE_HASH: [u8; 32] = [
162, 28, 244, 179, 96, 76, 244, 178, 188, 83, 230, 248, 143, 106, 77, 117, 239,
95, 244, 171, 65, 95, 62, 153, 174, 166, 182, 28, 130, 73, 196, 208,
];
const EXPECTED_COMPRESSED_PUBLIC_KEY: [u8; 33] = [
2, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, 7, 2,
155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, 152,
];
let recovered_pk = ui
.execute_rpc(
Call::new(&addr, "recover")
.push_value("signature", &format!("0x{}", hex::encode(SIGNATURE)))
.push_value("message_hash", &format!("0x{}", hex::encode(MESSSAGE_HASH))),
)
.await?;
assert_eq!(
recovered_pk,
format!("0x{}", hex::encode(EXPECTED_COMPRESSED_PUBLIC_KEY))
);

Ok(())
}

#[waterfall_test(example = "seal_ecdsa")]
async fn seal_ecdsa_to_eth_address(mut ui: Ui) -> Result<()> {
// given
let contract_path =
cargo_contract::build(&utils::example_path("seal-ecdsa/Cargo.toml"))
.expect("contract build failed");

// when
let addr = ui.execute_upload(Upload::new(contract_path)).await?;

// then
let PUB_KEY = [
2, 141, 181, 91, 5, 219, 134, 192, 177, 120, 108, 164, 159, 9, 93, 118, 52, 76,

This comment has been minimized.

Copy link
@agryaznov

agryaznov May 19, 2022

Contributor

I'm wondering, maybe we could test versus different public keys in different tests? because currently it checks it with the same values in all tests: in pallet, in doc comments examples in ink! and finally in e2e waterfall tests. If it was a mistake in the origin test (false reference values), all others will not catch the bug as well

158, 96, 86, 178, 240, 39, 1, 167, 231, 243, 194, 10, 171, 253, 145,
];
let EXPECTED_ETH_ADDRESS = [
9, 35, 29, 167, 177, 154, 1, 111, 158, 87, 109, 35, 177, 98, 119, 6, 47, 77, 70,
168,
];
let eth_addr = ui
.execute_rpc(
Call::new(&addr, "to_eth_address")
.push_value("pub_key", &format!("0x{}", hex::encode(PUB_KEY))),
)
.await?;
assert_eq!(eth_addr, format!("0x{}", hex::encode(EXPECTED_ETH_ADDRESS)));

Ok(())
}

0 comments on commit f783b21

Please sign in to comment.