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

Program config storage #593

Merged
merged 14 commits into from
Jan 18, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ At the moment this project **does not** adhere to
- Wasm API to functions formerly in the x25515chacha20poly1305 repo also now have camelCase function names ([#563](https://github.com/entropyxyz/entropy-core/pull/563))
- Register and change program pointer interface changed to accept a vecotor of programs. As well pass an index for which containts the hashing code if it custom hashing ([#568](https://github.com/entropyxyz/entropy-core/pull/568))
- If a user is sending additive data through it now needs to be in a vector and the index needs to match up with where the program pointer is in the program pointer vector. ([#577](https://github.com/entropyxyz/entropy-core/pull/577))
- A user now needs to add a program config, which gets packaged with their program pointer to create program info. This changes both register and change_program_info (previously change_program_pointer). As well set_program now also takes program_type_definition. ([#593](https://github.com/entropyxyz/entropy-core/pull/593))

### Added
- Test CLI which calls the same code as in integration tests ([#417](https://github.com/entropyxyz/entropy-core/pull/417))
Expand All @@ -30,6 +31,7 @@ At the moment this project **does not** adhere to
- Custom Hashing Algorithms [#553](https://github.com/entropyxyz/entropy-core/pull/553/)
- Add ref counter to programs [#585](https://github.com/entropyxyz/entropy-core/pull/585/)
- Add `--setup-only` flag ([#588](https://github.com/entropyxyz/entropy-core/pull/588/))
- Program config storage ([#593](https://github.com/entropyxyz/entropy-core/pull/593))

### Changed
- Crate name refactor ([#561](https://github.com/entropyxyz/entropy-core/pull/561))
Expand Down
28 changes: 25 additions & 3 deletions crates/test-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use std::{
use clap::{Parser, Subcommand};
use colored::Colorize;
use entropy_testing_utils::{
chain_api::entropy::runtime_types::bounded_collections::bounded_vec::BoundedVec,
chain_api::{
entropy::runtime_types::bounded_collections::bounded_vec::BoundedVec,
entropy::runtime_types::pallet_relayer::pallet::ProgramData,
},
constants::{AUXILARY_DATA_SHOULD_SUCCEED, TEST_PROGRAM_WASM_BYTECODE},
test_client::{
derive_static_secret, get_accounts, get_api, get_rpc, register, sign, update_program,
Expand Down Expand Up @@ -70,6 +73,8 @@ enum CliCommand {
key_visibility: Visibility,
/// The hash of the initial program for the account
program_hashes: Vec<H256>,
/// The program configs of the initial programs for the account
program_configs: Vec<Vec<u8>>,
},
/// Ask the network to sign a given message
Sign {
Expand All @@ -94,6 +99,8 @@ enum CliCommand {
program_account_name: String,
/// The path to a .wasm file containing the program (defaults to test program)
program_file: Option<PathBuf>,
/// The path to a file containing the program config (defaults to empty)
program_config_file: Option<PathBuf>,
},
/// Display a list of registered Entropy accounts
Status,
Expand Down Expand Up @@ -157,6 +164,7 @@ async fn run_command() -> anyhow::Result<String> {
program_account_name,
key_visibility,
program_hashes,
program_configs,
} => {
let signature_request_keypair: sr25519::Pair =
SeedString::new(signature_request_account_name).try_into()?;
Expand All @@ -176,14 +184,22 @@ async fn run_command() -> anyhow::Result<String> {
},
Visibility::Public => KeyVisibility::Public,
};
let mut programs_info = vec![];

for i in 0..program_hashes.len() {
programs_info.push(ProgramData {
program_pointer: program_hashes[i],
program_config: program_configs[i].clone(),
});
}

let (registered_info, keyshare_option) = register(
&api,
&rpc,
signature_request_keypair.clone(),
program_account,
key_visibility_converted,
BoundedVec(program_hashes),
BoundedVec(programs_info),
)
.await?;

Expand Down Expand Up @@ -225,6 +241,7 @@ async fn run_command() -> anyhow::Result<String> {
signature_request_account_name,
program_account_name,
program_file,
program_config_file,
} => {
let signature_request_keypair: sr25519::Pair =
SeedString::new(signature_request_account_name).try_into()?;
Expand All @@ -235,9 +252,14 @@ async fn run_command() -> anyhow::Result<String> {
None => TEST_PROGRAM_WASM_BYTECODE.to_owned(),
};

let program_config = match program_config_file {
Some(file_name) => fs::read(file_name)?,
None => vec![],
};

let program_keypair: sr25519::Pair =
SeedString::new(program_account_name).try_into()?;
update_program(&api, &program_keypair, program).await?;
update_program(&api, &program_keypair, program, program_config).await?;
Ok("Program updated".to_string())
},
CliCommand::Status => {
Expand Down
18 changes: 10 additions & 8 deletions crates/testing-utils/src/test_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use entropy_protocol::{
use entropy_tss::{
chain_api::{
entropy, entropy::runtime_types::bounded_collections::bounded_vec::BoundedVec,
entropy::runtime_types::pallet_relayer::pallet::ProgramData,
entropy::runtime_types::pallet_relayer::pallet::RegisteredInfo, EntropyConfig,
},
common::{get_current_subgroup_signers, Hasher, UserSignatureRequest},
Expand All @@ -46,7 +47,7 @@ use sp_core::{crypto::AccountId32, sr25519, Bytes, Pair};
use subxt::{
backend::legacy::LegacyRpcMethods,
tx::{PairSigner, Signer},
utils::{AccountId32 as SubxtAccountId32, Static, H256},
utils::{AccountId32 as SubxtAccountId32, Static},
Config, OnlineClient,
};
use synedrion::k256::ecdsa::{RecoveryId, Signature as k256Signature, VerifyingKey};
Expand All @@ -71,7 +72,7 @@ pub async fn register(
signature_request_keypair: sr25519::Pair,
program_account: SubxtAccountId32,
key_visibility: KeyVisibility,
program_hashes: BoundedVec<H256>,
programs_data: BoundedVec<ProgramData>,
) -> anyhow::Result<(RegisteredInfo, Option<KeyShare<KeyParams>>)> {
// Check if user is already registered
let account_id32: AccountId32 = signature_request_keypair.public().into();
Expand All @@ -89,7 +90,7 @@ pub async fn register(
signature_request_keypair.clone(),
program_account,
key_visibility,
program_hashes,
programs_data,
)
.await?;

Expand Down Expand Up @@ -249,8 +250,9 @@ pub async fn update_program(
api: &OnlineClient<EntropyConfig>,
program_modification_keypair: &sr25519::Pair,
program: Vec<u8>,
program_config: Vec<u8>,
) -> anyhow::Result<<EntropyConfig as Config>::Hash> {
let update_program_tx = entropy::tx().programs().set_program(program);
let update_program_tx = entropy::tx().programs().set_program(program, program_config);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah so this is defined as type_definition in the Programs pallet but it's a config here. So what is it supposed to be?

let program_modification_account =
PairSigner::<EntropyConfig, sr25519::Pair>::new(program_modification_keypair.clone());

Expand All @@ -273,14 +275,14 @@ pub async fn update_pointer(
rpc: &LegacyRpcMethods<EntropyConfig>,
signature_request_account: &sr25519::Pair,
pointer_modification_account: &sr25519::Pair,
program_hashes: BoundedVec<<EntropyConfig as Config>::Hash>,
program_data: BoundedVec<ProgramData>,
) -> anyhow::Result<()> {
let block_hash =
rpc.chain_get_block_hash(None).await?.ok_or_else(|| anyhow!("Error getting block hash"))?;

let update_pointer_tx = entropy::tx()
.relayer()
.change_program_pointer(signature_request_account.public().into(), program_hashes);
.change_program_data(signature_request_account.public().into(), program_data);

let account_id32: AccountId32 = pointer_modification_account.public().into();
let account_id: <EntropyConfig as Config>::AccountId = account_id32.into();
Expand Down Expand Up @@ -329,15 +331,15 @@ pub async fn put_register_request_on_chain(
signature_request_keypair: sr25519::Pair,
program_modification_account: SubxtAccountId32,
key_visibility: KeyVisibility,
program_hashes: BoundedVec<H256>,
program_data: BoundedVec<ProgramData>,
) -> anyhow::Result<()> {
let signature_request_pair_signer =
PairSigner::<EntropyConfig, sp_core::sr25519::Pair>::new(signature_request_keypair);

let registering_tx = entropy::tx().relayer().register(
program_modification_account,
Static(key_visibility),
program_hashes,
program_data,
);

api.tx()
Expand Down
Binary file modified crates/threshold-signature-server/entropy_metadata.scale
Binary file not shown.
3 changes: 2 additions & 1 deletion crates/threshold-signature-server/src/helpers/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ pub async fn update_programs(
entropy_api: &OnlineClient<EntropyConfig>,
program_modification_account: &sr25519::Pair,
initial_program: Vec<u8>,
program_config: Vec<u8>,
) -> <EntropyConfig as Config>::Hash {
// update/set their programs
let update_program_tx = entropy::tx().programs().set_program(initial_program);
let update_program_tx = entropy::tx().programs().set_program(initial_program, program_config);

let program_modification_account =
PairSigner::<EntropyConfig, sr25519::Pair>::new(program_modification_account.clone());
Expand Down
12 changes: 6 additions & 6 deletions crates/threshold-signature-server/src/helpers/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ use sha1::{Digest as Sha1Digest, Sha1};
use sha2::{Digest as Sha256Digest, Sha256};
use sha3::{Digest as Sha3Digest, Keccak256, Sha3_256};
use sp_core::{sr25519, Bytes, Pair};
use subxt::{
backend::legacy::LegacyRpcMethods, tx::PairSigner, utils::AccountId32, Config, OnlineClient,
};
use subxt::{backend::legacy::LegacyRpcMethods, tx::PairSigner, utils::AccountId32, OnlineClient};
use synedrion::KeyShare;
use tokio::time::timeout;
use x25519_dalek::PublicKey;

use crate::{
chain_api::{entropy, EntropyConfig},
chain_api::{
entropy, entropy::runtime_types::pallet_relayer::pallet::ProgramData, EntropyConfig,
},
helpers::substrate::get_program,
signing_client::{protocol_transport::open_protocol_connections, Listener, ListenerState},
user::{api::UserRegistrationInfo, errors::UserErr},
Expand Down Expand Up @@ -174,7 +174,7 @@ pub async fn compute_hash(
rpc: &LegacyRpcMethods<EntropyConfig>,
hashing_algorithm: &HashingAlgorithm,
runtime: &mut Runtime,
program_pointers: &[<EntropyConfig as Config>::Hash],
programs_data: &[ProgramData],
message: &[u8],
) -> Result<[u8; 32], UserErr> {
match hashing_algorithm {
Expand Down Expand Up @@ -211,7 +211,7 @@ pub async fn compute_hash(
Ok(hash)
},
HashingAlgorithm::Custom(i) => {
let program = get_program(api, rpc, &program_pointers[*i]).await?;
let program = get_program(api, rpc, &programs_data[*i].program_pointer).await?;
runtime.custom_hash(program.as_slice(), message).map_err(|e| e.into())
},
}
Expand Down
12 changes: 6 additions & 6 deletions crates/threshold-signature-server/src/user/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,25 @@ pub async fn sign_tx(

let message = hex::decode(&user_sig_req.message)?;

if user_details.program_pointers.0.is_empty() {
if user_details.programs_data.0.is_empty() {
return Err(UserErr::NoProgramPointerDefined());
}
// handle aux data padding, if it is not explicit by client for ease send through None, error if incorrect length
let auxilary_data_vec;
if let Some(auxilary_data) = user_sig_req.clone().auxilary_data {
if auxilary_data.len() < user_details.program_pointers.0.len() {
if auxilary_data.len() < user_details.programs_data.0.len() {
return Err(UserErr::MismatchAuxData);
} else {
auxilary_data_vec = auxilary_data;
}
} else {
auxilary_data_vec = vec![None; user_details.program_pointers.0.len()];
auxilary_data_vec = vec![None; user_details.programs_data.0.len()];
}

let mut runtime = Runtime::new(ProgramConfig { fuel: MAX_INSTRUCTIONS_PER_PROGRAM });

for (i, program_pointer) in user_details.program_pointers.0.iter().enumerate() {
let program = get_program(&api, &rpc, program_pointer).await?;
for (i, program_info) in user_details.programs_data.0.iter().enumerate() {
let program = get_program(&api, &rpc, &program_info.program_pointer).await?;
let auxilary_data = auxilary_data_vec[i].as_ref().map(hex::decode).transpose()?;
let signature_request = SignatureRequest { message: message.clone(), auxilary_data };
runtime.evaluate(&program, &signature_request)?;
Expand All @@ -189,7 +189,7 @@ pub async fn sign_tx(
&rpc,
&user_sig_req.hash,
&mut runtime,
&user_details.program_pointers.0,
&user_details.programs_data.0,
message.as_slice(),
)
.await?;
Expand Down
Loading