Skip to content

Commit

Permalink
Fix contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Sep 15, 2023
1 parent fb834bb commit e789c71
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 77 deletions.
82 changes: 42 additions & 40 deletions contracts/crypto-verify/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
public_key,
} => to_binary(&query_verify_cosmos(
deps,
&message.0,
&signature.0,
&public_key.0,
message.as_slice(),
signature.as_slice(),
public_key.as_slice(),
)?),
QueryMsg::VerifyEthereumText {
message,
Expand Down Expand Up @@ -69,9 +69,9 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
public_key,
} => to_binary(&query_verify_tendermint(
deps,
&message.0,
&signature.0,
&public_key.0,
message.as_slice(),
signature.as_slice(),
public_key.as_slice(),
)?),
QueryMsg::VerifyTendermintBatch {
messages,
Expand Down Expand Up @@ -268,9 +268,9 @@ mod tests {
let public_key = hex::decode(SECP256K1_PUBLIC_KEY_HEX).unwrap();

let verify_msg = QueryMsg::VerifyCosmosSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
Expand All @@ -290,9 +290,9 @@ mod tests {
let public_key = hex::decode(SECP256K1_PUBLIC_KEY_HEX).unwrap();

let verify_msg = QueryMsg::VerifyCosmosSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
Expand All @@ -310,9 +310,9 @@ mod tests {
let public_key = vec![];

let verify_msg = QueryMsg::VerifyCosmosSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let res = query(deps.as_ref(), mock_env(), verify_msg);
Expand Down Expand Up @@ -457,15 +457,15 @@ mod tests {

let messages = [ED25519_MESSAGE_HEX, ED25519_MESSAGE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
let public_keys = [ED25519_PUBLIC_KEY_HEX, ED25519_PUBLIC_KEY2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -487,18 +487,18 @@ mod tests {
// One message
let messages = [ED25519_MESSAGE_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// Multiple signatures
//FIXME: Use different signatures / pubkeys
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// Multiple pubkeys
let public_keys = [ED25519_PUBLIC_KEY_HEX, ED25519_PUBLIC_KEY_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -521,18 +521,18 @@ mod tests {
//FIXME: Use different messages
let messages = [ED25519_MESSAGE_HEX, ED25519_MESSAGE_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// Multiple signatures
//FIXME: Use different signatures
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// One pubkey
let public_keys = [ED25519_PUBLIC_KEY_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -553,17 +553,19 @@ mod tests {

let mut messages: Vec<Binary> = [ED25519_MESSAGE_HEX, ED25519_MESSAGE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// Alter one of the messages
messages[0].0[0] ^= 0x01;
let mut b: Vec<_> = messages.pop().unwrap().into();
b[0] ^= 0x01;
messages.push(Binary::new(b));
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
let public_keys = [ED25519_PUBLIC_KEY_HEX, ED25519_PUBLIC_KEY2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -584,16 +586,16 @@ mod tests {

let messages = [ED25519_MESSAGE_HEX, ED25519_MESSAGE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// One of the public keys is empty
let public_keys = ["", ED25519_PUBLIC_KEY_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -620,9 +622,9 @@ mod tests {
let public_key = hex::decode(ED25519_PUBLIC_KEY_HEX).unwrap();

let verify_msg = QueryMsg::VerifyTendermintSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
Expand All @@ -642,9 +644,9 @@ mod tests {
let public_key = hex::decode(ED25519_PUBLIC_KEY_HEX).unwrap();

let verify_msg = QueryMsg::VerifyTendermintSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let raw = query(deps.as_ref(), mock_env(), verify_msg).unwrap();
Expand All @@ -662,9 +664,9 @@ mod tests {
let public_key = vec![];

let verify_msg = QueryMsg::VerifyTendermintSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};
let res = query(deps.as_ref(), mock_env(), verify_msg);
assert!(res.is_err());
Expand Down
71 changes: 37 additions & 34 deletions contracts/crypto-verify/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ fn cosmos_signature_verify_works() {
let public_key = hex::decode(SECP256K1_PUBLIC_KEY_HEX).unwrap();

let verify_msg = QueryMsg::VerifyCosmosSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let raw = query(&mut deps, mock_env(), verify_msg).unwrap();
Expand All @@ -101,9 +101,9 @@ fn cosmos_signature_verify_fails() {
let public_key = hex::decode(SECP256K1_PUBLIC_KEY_HEX).unwrap();

let verify_msg = QueryMsg::VerifyCosmosSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let raw = query(&mut deps, mock_env(), verify_msg).unwrap();
Expand All @@ -121,9 +121,9 @@ fn cosmos_signature_verify_errors() {
let public_key = vec![];

let verify_msg = QueryMsg::VerifyCosmosSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};
let res = query(&mut deps, mock_env(), verify_msg);
assert_eq!(
Expand Down Expand Up @@ -262,9 +262,9 @@ fn tendermint_signature_verify_works() {
let public_key = hex::decode(ED25519_PUBLIC_KEY_HEX).unwrap();

let verify_msg = QueryMsg::VerifyTendermintSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let raw = query(&mut deps, mock_env(), verify_msg).unwrap();
Expand All @@ -284,9 +284,9 @@ fn tendermint_signature_verify_fails() {
let public_key = hex::decode(ED25519_PUBLIC_KEY_HEX).unwrap();

let verify_msg = QueryMsg::VerifyTendermintSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};

let raw = query(&mut deps, mock_env(), verify_msg).unwrap();
Expand All @@ -304,9 +304,9 @@ fn tendermint_signature_verify_errors() {
let public_key = vec![];

let verify_msg = QueryMsg::VerifyTendermintSignature {
message: Binary(message),
signature: Binary(signature),
public_key: Binary(public_key),
message: Binary::new(message),
signature: Binary::new(signature),
public_key: Binary::new(public_key),
};
let res = query(&mut deps, mock_env(), verify_msg);
assert_eq!(
Expand All @@ -321,15 +321,15 @@ fn tendermint_signatures_batch_verify_works() {

let messages = [ED25519_MESSAGE_HEX, ED25519_MESSAGE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
let public_keys = [ED25519_PUBLIC_KEY_HEX, ED25519_PUBLIC_KEY2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -351,18 +351,18 @@ fn tendermint_signatures_batch_verify_message_multisig_works() {
// One message
let messages = [ED25519_MESSAGE_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// Multiple signatures
//FIXME: Use different signatures / pubkeys
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// Multiple pubkeys
let public_keys = [ED25519_PUBLIC_KEY_HEX, ED25519_PUBLIC_KEY_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -385,17 +385,17 @@ fn tendermint_signatures_batch_verify_single_public_key_works() {
//FIXME: Use different messages / signatures
let messages = [ED25519_MESSAGE_HEX, ED25519_MESSAGE_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// Multiple signatures
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// One pubkey
let public_keys = [ED25519_PUBLIC_KEY_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -416,17 +416,20 @@ fn tendermint_signatures_batch_verify_fails() {

let mut messages: Vec<Binary> = [ED25519_MESSAGE_HEX, ED25519_MESSAGE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// Alter one of the messages
messages[1].0[0] ^= 0x01;
let mut b: Vec<_> = messages.swap_remove(0).into();
b[0] ^= 0x01;
messages.push(Binary::new(b));
messages.swap(0, 1); // swap them again to old order
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
let public_keys = [ED25519_PUBLIC_KEY_HEX, ED25519_PUBLIC_KEY2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand All @@ -447,16 +450,16 @@ fn tendermint_signatures_batch_verify_errors() {

let messages = [ED25519_MESSAGE_HEX, ED25519_MESSAGE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
let signatures = [ED25519_SIGNATURE_HEX, ED25519_SIGNATURE2_HEX]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();
// One of the public keys is empty
let public_keys = [ED25519_PUBLIC_KEY_HEX, ""]
.iter()
.map(|m| Binary(hex::decode(m).unwrap()))
.map(|m| Binary::new(hex::decode(m).unwrap()))
.collect();

let verify_msg = QueryMsg::VerifyTendermintBatch {
Expand Down
Loading

0 comments on commit e789c71

Please sign in to comment.