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

feat(multisig): public key registration #64

Merged
merged 15 commits into from
Sep 7, 2023
31 changes: 15 additions & 16 deletions ampd/src/handlers/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cosmrs::cosmwasm::MsgExecuteContract;
use cosmwasm_std::{HexBinary, Uint64};
use ecdsa::{RecoveryId, VerifyingKey};
use error_stack::ResultExt;
use hex::{encode, FromHex};
use hex::encode;
use k256::Secp256k1;
use serde::de::Error as DeserializeError;
use serde::{Deserialize, Deserializer};
Expand Down Expand Up @@ -44,21 +44,18 @@ fn deserialize_public_keys<'de, D>(
where
D: Deserializer<'de>,
{
let keys_by_address: HashMap<TMAddress, String> = HashMap::deserialize(deserializer)?;
let keys_by_address: HashMap<TMAddress, multisig::key::PublicKey> =
HashMap::deserialize(deserializer)?;

keys_by_address
.into_iter()
.map(|(address, hex)| {
Ok((
.map(|(address, pk)| match pk {
multisig::key::PublicKey::Ecdsa(hex) => Ok((
address,
VerifyingKey::from_sec1_bytes(
<Vec<u8>>::from_hex(hex)
.map_err(D::Error::custom)?
.as_slice(),
)
.map_err(D::Error::custom)?
.into(),
))
VerifyingKey::from_sec1_bytes(hex.as_ref())
.map_err(D::Error::custom)?
.into(),
)),
})
.collect()
}
Expand Down Expand Up @@ -209,6 +206,7 @@ mod test {
use error_stack::{Report, Result};
use ethers::types::Signature as EthersSignature;
use generic_array::GenericArray;
use hex::FromHex;
use k256::ecdsa::{Signature as K256Signature, VerifyingKey};
use rand::rngs::OsRng;
use tendermint::abci;
Expand All @@ -220,7 +218,8 @@ mod test {
use crate::tofnd::grpc::{MockEcdsaClient, SharableEcdsaClient};
use crate::types;
use multisig::events::Event::SigningStarted;
use multisig::types::{KeyID, MsgToSign, PublicKey};
use multisig::key::PublicKey;
use multisig::types::{KeyID, MsgToSign};

const MULTISIG_ADDRESS: &str = "axelarvaloper1zh9wrak6ke4n6fclj5e8yk397czv430ygs5jz7";

Expand All @@ -231,8 +230,8 @@ mod test {
.into()
}

fn rand_public_key() -> PublicKey {
PublicKey::unchecked(HexBinary::from(
fn rand_public_key() -> multisig::key::PublicKey {
multisig::key::PublicKey::Ecdsa(HexBinary::from(
types::PublicKey::from(SigningKey::random(&mut OsRng).verifying_key()).to_bytes(),
))
}
Expand Down Expand Up @@ -317,7 +316,7 @@ mod test {
let mut map: HashMap<String, PublicKey> = HashMap::new();
map.insert(
rand_account().to_string(),
PublicKey::unchecked(HexBinary::from(invalid_pub_key.as_slice())),
PublicKey::Ecdsa(HexBinary::from(invalid_pub_key.as_slice())),
);
match event {
events::Event::Abci {
Expand Down
8 changes: 5 additions & 3 deletions ampd/tests/report.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use error_stack::Report;

use ampd::report::{Error, LoggableError};
use std::env;

// Do not move this test or the location field checks break
#[test]
fn correct_error_log() {
env::set_var("RUST_BACKTRACE", "1");
let report = Report::new(Error::new("error1".to_string()))
.attach_printable("foo1")
.change_context(Error::new("error2".to_string()))
Expand All @@ -25,15 +27,15 @@ fn correct_error_log() {
let expected_err = LoggableError {
msg: "error3".to_string(),
attachments: vec!["opaque attachment".to_string()],
location: "ampd/tests/report.rs:13:10".to_string(),
location: "ampd/tests/report.rs:15:10".to_string(),
cause: Some(Box::new(LoggableError {
msg: "error2".to_string(),
attachments: vec!["test1".to_string(), "test2".to_string()],
location: "ampd/tests/report.rs:10:10".to_string(),
location: "ampd/tests/report.rs:12:10".to_string(),
cause: Some(Box::new(LoggableError {
msg: "error1".to_string(),
attachments: vec!["foo1".to_string()],
location: "ampd/tests/report.rs:8:18".to_string(),
location: "ampd/tests/report.rs:10:18".to_string(),
cause: None,
backtrace: None,
})),
Expand Down
4 changes: 2 additions & 2 deletions contracts/multisig-prover/src/encoding/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl TryFrom<Signer> for Operator {

fn try_from(signer: Signer) -> Result<Self, Self::Error> {
Ok(Self {
address: evm_address((&signer.pub_key).into())?,
address: evm_address(signer.pub_key.as_ref())?,
weight: signer.weight,
signature: signer.signature,
})
Expand Down Expand Up @@ -131,7 +131,7 @@ impl CommandBatch {
)));

if let Some(signature) = &operator.signature {
signatures.push(Token::Bytes(signature.into()));
signatures.push(Token::Bytes(<Vec<u8>>::from(signature.clone())));
}

(addresses, weights, signatures)
Expand Down
7 changes: 6 additions & 1 deletion contracts/multisig-prover/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use cosmwasm_std::{
to_binary, wasm_execute, Addr, BlockInfo, DepsMut, Env, HexBinary, QuerierWrapper,
QueryRequest, Response, SubMsg, WasmMsg, WasmQuery,
};
use multisig::key::KeyType;

use std::{collections::HashMap, str::FromStr};

Expand Down Expand Up @@ -95,7 +96,11 @@ pub fn rotate_snapshot(
msg: to_binary(&multisig::msg::ExecuteMsg::KeyGen {
key_id: key_id.clone(),
snapshot: snapshot.clone(),
pub_keys: pub_keys.clone(),
pub_keys: pub_keys
.clone()
.into_iter()
.map(|(k, v)| (k, (KeyType::Ecdsa, v)))
.collect(),
})?,
funds: vec![],
};
Expand Down
5 changes: 5 additions & 0 deletions contracts/multisig-prover/src/test/mocks/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ pub fn execute(
snapshot: _,
pub_keys: _,
} => Ok(Response::default()),
ExecuteMsg::RegisterPublicKey { public_key: _ } => unimplemented!(),
}
}

pub fn query(_deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetMultisig { session_id: _ } => to_binary(&query::query_success()),
QueryMsg::GetKey { key_id: _ } => unimplemented!(),
QueryMsg::GetPublicKey {
worker_address: _,
key_type: _,
} => unimplemented!(),
}
}

Expand Down
8 changes: 4 additions & 4 deletions contracts/multisig-prover/src/test/test_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use axelar_wasm_std::{nonempty, Threshold};
use connection_router::msg::Message;
use cosmwasm_std::{Addr, HexBinary, Uint256, Uint64};
use multisig::types::Signature;
use multisig::key::{KeyType, Signature};

fn legacy_cmd_id_input(
source_transaction: HexBinary,
Expand Down Expand Up @@ -111,7 +111,7 @@ pub fn threshold() -> Threshold {
#[derive(Debug)]
pub struct TestOperator {
pub address: Addr,
pub pub_key: multisig::types::PublicKey,
pub pub_key: multisig::key::PublicKey,
pub operator: HexBinary,
pub weight: Uint256,
pub signature: Option<Signature>,
Expand Down Expand Up @@ -194,10 +194,10 @@ pub fn operators() -> Vec<TestOperator> {
.map(|(address, pub_key, operator, weight, signature)| {
TestOperator {
address: Addr::unchecked(address),
pub_key: HexBinary::from_hex(pub_key).unwrap().try_into().unwrap(),
pub_key: (KeyType::Ecdsa,HexBinary::from_hex(pub_key).unwrap()).try_into().unwrap(),
operator: HexBinary::from_hex(operator).unwrap(),
weight: Uint256::from(weight),
signature: signature.map(|s| HexBinary::from_hex(s).unwrap().try_into().unwrap()) }
signature: signature.map(|s| (KeyType::Ecdsa, HexBinary::from_hex(s).unwrap()).try_into().unwrap()) }
})
.collect()
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/multisig-prover/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Display;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{from_binary, HexBinary, StdResult, Uint256};
use cw_storage_plus::{Key, KeyDeserialize, PrimaryKey};
use multisig::types::Signature;
use multisig::key::Signature;
use sha3::{Digest, Keccak256};

use crate::encoding::Data;
Expand Down
Loading
Loading