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

Improve test CLI following removing permissioned mode #770

Merged
merged 6 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions crates/shared/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use super::types::EncodedVerifyingKey;
use hex_literal::hex;
use lazy_static::lazy_static;
use sp_core::H256;
use sp_std::vec;
use sp_std::vec::Vec;

pub const DEFAULT_VERIFYING_KEY_NOT_REGISTERED: EncodedVerifyingKey =
[10; VERIFICATION_KEY_LENGTH as usize];
pub const DAVE_VERIFYING_KEY: EncodedVerifyingKey = [1; VERIFICATION_KEY_LENGTH as usize];
// This key is associated with a constant key share generation from DETERMINISTIC_KEY_SHARE
pub const EVE_VERIFYING_KEY: EncodedVerifyingKey = [
2, 78, 59, 129, 175, 156, 34, 52, 202, 208, 157, 103, 156, 230, 3, 94, 209, 57, 35, 71, 206,
100, 206, 64, 95, 93, 205, 54, 34, 138, 37, 222, 110,
];
pub const FERDIE_VERIFYING_KEY: EncodedVerifyingKey = [3; VERIFICATION_KEY_LENGTH as usize];
pub const DEFAULT_VERIFYING_KEY: EncodedVerifyingKey = [0; VERIFICATION_KEY_LENGTH as usize];

lazy_static! {
pub static ref DEFAULT_VERIFYING_KEY_NOT_REGISTERED: Vec<u8> = vec![10; VERIFICATION_KEY_LENGTH as usize];
pub static ref DAVE_VERIFYING_KEY: Vec<u8> = vec![1; VERIFICATION_KEY_LENGTH as usize];
// this key is associated with a constant key share generation from DETERMINISTIC_KEY_SHARE
pub static ref EVE_VERIFYING_KEY: Vec<u8> = vec![2, 78, 59, 129, 175, 156, 34, 52, 202, 208, 157, 103, 156, 230, 3, 94, 209, 57, 35, 71, 206, 100, 206, 64, 95, 93, 205, 54, 34, 138, 37, 222, 110];
pub static ref FERDIE_VERIFYING_KEY: Vec<u8> = vec![3; VERIFICATION_KEY_LENGTH as usize];
pub static ref DEFAULT_VERIFYING_KEY: Vec<u8> = vec![0; VERIFICATION_KEY_LENGTH as usize];
// key used to create a deterministic key share taken from here https://docs.rs/k256/latest/k256/ecdsa/index.html
pub static ref DETERMINISTIC_KEY_SHARE: [u8; 32] = hex!("4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318");
// hash used to find DEVICE_KEY_PROXY onchain
Expand Down
4 changes: 4 additions & 0 deletions crates/shared/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![allow(dead_code)]
use super::constants::VERIFICATION_KEY_LENGTH;
#[cfg(not(feature = "wasm"))]
use codec::alloc::vec::Vec;
use codec::{Decode, Encode, MaxEncodedLen};
Expand Down Expand Up @@ -106,3 +107,6 @@ pub enum HashingAlgorithm {
Keccak,
Custom(usize),
}

/// A compressed, serialized [synedrion::ecdsa::VerifyingKey<k256::Secp256k1>]
pub type EncodedVerifyingKey = [u8; VERIFICATION_KEY_LENGTH as usize];
39 changes: 20 additions & 19 deletions crates/test-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::{
time::Instant,
};

use anyhow::ensure;
use anyhow::{anyhow, ensure};
use clap::{Parser, Subcommand};
use colored::Colorize;
use entropy_testing_utils::{
Expand All @@ -34,7 +34,7 @@ use entropy_testing_utils::{
constants::TEST_PROGRAM_WASM_BYTECODE,
test_client::{
get_accounts, get_api, get_programs, get_rpc, register, sign, store_program,
update_programs, KeyParams, KeyShare, KeyVisibility,
update_programs, KeyParams, KeyShare, KeyVisibility, VERIFYING_KEY_LENGTH,
},
};
use sp_core::{sr25519, DeriveJunction, Hasher, Pair};
Expand Down Expand Up @@ -99,18 +99,17 @@ enum CliCommand {
///
/// Optionally may be preceeded with "//", eg: "//Alice"
user_account_name: String,
/// The verifying key of the account to sign with
#[arg(short, long)]
signature_verifying_key: Vec<u8>,
/// The verifying key of the account to sign with, given as hex
signature_verifying_key: String,
/// The message to be signed
message: String,
/// Optional auxiliary data passed to the program, given as hex
auxilary_data: Option<String>,
},
/// Update the program for a particular account
UpdatePrograms {
/// The verifying key of the account to update their programs
signature_verifying_key: Vec<u8>,
/// The verifying key of the account to update their programs, given as hex
signature_verifying_key: String,
/// A name from which to generate a program modification keypair, eg: "Bob"
///
/// Optionally may be preceeded with "//", eg: "//Bob"
Expand Down Expand Up @@ -259,6 +258,11 @@ async fn run_command() -> anyhow::Result<String> {
(keyshare, x25519_secret)
});

let signature_verifying_key: [u8; VERIFYING_KEY_LENGTH] =
hex::decode(signature_verifying_key)?
.try_into()
.map_err(|_| anyhow!("Verifying key must be 33 bytes"))?;

let recoverable_signature = sign(
&api,
&rpc,
Expand Down Expand Up @@ -312,14 +316,12 @@ async fn run_command() -> anyhow::Result<String> {
);
}

update_programs(
&api,
&rpc,
signature_verifying_key,
&program_keypair,
BoundedVec(programs_info),
)
.await?;
let verifying_key: [u8; VERIFYING_KEY_LENGTH] = hex::decode(signature_verifying_key)?
.try_into()
.map_err(|_| anyhow!("Verifying key must be 33 bytes"))?;

update_programs(&api, &rpc, verifying_key, &program_keypair, BoundedVec(programs_info))
.await?;

Ok("Programs updated".to_string())
},
Expand All @@ -331,16 +333,15 @@ async fn run_command() -> anyhow::Result<String> {
);
if !accounts.is_empty() {
println!(
"{:<48} {:<12} {:<66} Programs:",
"Signature request account ID:".green(),
"{:<64} {:<12} Programs:",
"Verifying key:".green(),
"Visibility:".purple(),
"Verifying key: ".cyan(),
);
for (account_id, info) in accounts {
let visibility: Visibility = info.key_visibility.0.into();
println!(
"{} {:<12} {}",
format!("{:?}", account_id.to_vec()).green(),
hex::encode(account_id).green(),
format!("{}", visibility).purple(),
format!(
"{:?}",
Expand Down
17 changes: 9 additions & 8 deletions crates/testing-utils/src/test_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ pub use entropy_protocol::{sign_and_encrypt::EncryptedSignedMessage, KeyParams};
use entropy_shared::HashingAlgorithm;
pub use entropy_shared::{KeyVisibility, SIGNING_PARTY_SIZE};
pub use synedrion::KeyShare;

use std::time::SystemTime;
pub const VERIFYING_KEY_LENGTH: usize = entropy_shared::VERIFICATION_KEY_LENGTH as usize;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why re-export this instead of using the one from entropy_shared?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because i need a usize and in entropy_shared its a u32 or something. I am unsure about about making it a usize there because of potential issues with usize being different on wasm.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually maybe it will be fine, lets try....


use anyhow::{anyhow, ensure};
use entropy_protocol::{
Expand All @@ -43,6 +42,7 @@ use entropy_tss::{
};
use futures::future;
use sp_core::{crypto::AccountId32, sr25519, Pair};
use std::time::SystemTime;
use subxt::{
backend::legacy::LegacyRpcMethods,
events::EventsClient,
Expand Down Expand Up @@ -159,7 +159,7 @@ pub async fn sign(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
user_keypair: sr25519::Pair,
signature_verifying_key: Vec<u8>,
signature_verifying_key: [u8; VERIFYING_KEY_LENGTH],
message: Vec<u8>,
private: Option<(KeyShare<KeyParams>, StaticSecret)>,
auxilary_data: Option<Vec<u8>>,
Expand All @@ -175,7 +175,7 @@ pub async fn sign(
validators_info: validators_info.clone(),
timestamp: SystemTime::now(),
hash: HashingAlgorithm::Keccak,
signature_verifying_key,
signature_verifying_key: signature_verifying_key.to_vec(),
};

let signature_request_vec = serde_json::to_vec(&signature_request)?;
Expand Down Expand Up @@ -293,13 +293,13 @@ pub async fn store_program(
pub async fn update_programs(
entropy_api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
verifying_key: Vec<u8>,
verifying_key: [u8; VERIFYING_KEY_LENGTH],
deployer_pair: &sr25519::Pair,
program_instance: BoundedVec<ProgramInstance>,
) -> anyhow::Result<()> {
let update_pointer_tx = entropy::tx()
.registry()
.change_program_instance(BoundedVec(verifying_key), program_instance);
.change_program_instance(BoundedVec(verifying_key.to_vec()), program_instance);
let deployer = PairSigner::<EntropyConfig, sr25519::Pair>::new(deployer_pair.clone());
submit_transaction(entropy_api, rpc, &deployer, &update_pointer_tx, None).await?;
Ok(())
Expand All @@ -308,14 +308,15 @@ pub async fn update_programs(
pub async fn get_accounts(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
) -> anyhow::Result<Vec<([u8; 32], RegisteredInfo)>> {
) -> anyhow::Result<Vec<([u8; VERIFYING_KEY_LENGTH], RegisteredInfo)>> {
let block_hash =
rpc.chain_get_block_hash(None).await?.ok_or_else(|| anyhow!("Error getting block hash"))?;
let storage_address = entropy::storage().registry().registered_iter();
let mut iter = api.storage().at(block_hash).iter(storage_address).await?;
let mut accounts = Vec::new();
while let Some(Ok(kv)) = iter.next().await {
let key: [u8; 32] = kv.key_bytes[kv.key_bytes.len() - 32..].try_into()?;
let key: [u8; VERIFYING_KEY_LENGTH] =
kv.key_bytes[kv.key_bytes.len() - VERIFYING_KEY_LENGTH..].try_into()?;
accounts.push((key, kv.value))
}
Ok(accounts)
Expand Down
12 changes: 6 additions & 6 deletions crates/threshold-signature-server/src/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async fn test_sign_tx_no_chain() {
update_programs(
&entropy_api,
&rpc,
DAVE_VERIFYING_KEY.to_vec(),
DAVE_VERIFYING_KEY,
&one.pair(),
OtherBoundedVec(vec![
OtherProgramInstance { program_pointer: program_hash, program_config: vec![] },
Expand Down Expand Up @@ -502,7 +502,7 @@ async fn test_program_with_config() {
update_programs(
&entropy_api,
&rpc,
DAVE_VERIFYING_KEY.to_vec(),
DAVE_VERIFYING_KEY,
&one.pair(),
OtherBoundedVec(vec![
OtherProgramInstance { program_pointer: program_hash, program_config: config.to_vec() },
Expand Down Expand Up @@ -548,7 +548,7 @@ async fn test_fail_signing_group() {
update_programs(
&entropy_api,
&rpc,
DAVE_VERIFYING_KEY.to_vec(),
DAVE_VERIFYING_KEY,
&dave.pair(),
OtherBoundedVec(vec![OtherProgramInstance {
program_pointer: program_hash,
Expand Down Expand Up @@ -1071,7 +1071,7 @@ async fn test_sign_tx_user_participates() {
update_programs(
&entropy_api,
&rpc,
verifying_key.clone(),
verifying_key.clone().try_into().unwrap(),
&one.pair(),
OtherBoundedVec(vec![OtherProgramInstance {
program_pointer: program_hash,
Expand Down Expand Up @@ -1493,7 +1493,7 @@ async fn test_fail_infinite_program() {
update_programs(
&entropy_api,
&rpc,
DAVE_VERIFYING_KEY.to_vec(),
DAVE_VERIFYING_KEY,
&one.pair(),
OtherBoundedVec(vec![OtherProgramInstance {
program_pointer: program_hash,
Expand Down Expand Up @@ -1604,7 +1604,7 @@ async fn test_device_key_proxy() {
update_programs(
&entropy_api,
&rpc,
DAVE_VERIFYING_KEY.to_vec(),
DAVE_VERIFYING_KEY,
&one.pair(),
OtherBoundedVec(vec![OtherProgramInstance {
program_pointer: *DEVICE_KEY_HASH,
Expand Down
2 changes: 1 addition & 1 deletion crates/threshold-signature-server/tests/protocol_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async fn test_wasm_sign_tx_user_participates() {
update_programs(
&entropy_api,
&rpc,
verifying_key.clone(),
verifying_key.clone().try_into().unwrap(),
&one.pair(),
BoundedVec(vec![ProgramInstance { program_pointer, program_config: vec![] }]),
)
Expand Down
7 changes: 4 additions & 3 deletions crates/threshold-signature-server/tests/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async fn integration_test_sign_public() {
test_client::update_programs(
&api,
&rpc,
DAVE_VERIFYING_KEY.to_vec(),
DAVE_VERIFYING_KEY,
&pre_registered_public_user.pair(),
BoundedVec(vec![ProgramInstance { program_pointer, program_config: vec![] }]),
)
Expand All @@ -78,7 +78,7 @@ async fn integration_test_sign_public() {
&api,
&rpc,
request_author.pair(),
DAVE_VERIFYING_KEY.to_vec(),
DAVE_VERIFYING_KEY,
PREIMAGE_SHOULD_SUCCEED.to_vec(),
None,
Some(AUXILARY_DATA_SHOULD_SUCCEED.to_vec()),
Expand Down Expand Up @@ -108,7 +108,8 @@ async fn integration_test_sign_private() {
let api = get_api(&substrate_context.node_proc.ws_url).await.unwrap();
let rpc = get_rpc(&substrate_context.node_proc.ws_url).await.unwrap();
let keyshare = keyshare_option.unwrap();
let verifying_key = keyshare.clone().verifying_key().to_encoded_point(true).as_bytes().to_vec();
let verifying_key: [u8; 33] =
keyshare.clone().verifying_key().to_encoded_point(true).as_bytes().try_into().unwrap();

let program_pointer = test_client::store_program(
&api,
Expand Down
4 changes: 2 additions & 2 deletions crates/threshold-signature-server/tests/sign_eth_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async fn integration_test_sign_eth_tx() {
test_client::update_programs(
&api,
&rpc,
FERDIE_VERIFYING_KEY.to_vec(),
FERDIE_VERIFYING_KEY,
&pre_registered_user.pair(),
BoundedVec(vec![ProgramInstance { program_pointer, program_config: vec![] }]),
)
Expand All @@ -91,7 +91,7 @@ async fn integration_test_sign_eth_tx() {
&api,
&rpc,
pre_registered_user.pair(),
FERDIE_VERIFYING_KEY.to_vec(),
FERDIE_VERIFYING_KEY,
message,
None,
Some(AUXILARY_DATA_SHOULD_SUCCEED.to_vec()),
Expand Down