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: starknet message_id and address format #726

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
31 changes: 23 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[workspace]
members = [
"ampd",
"contracts/*",
"external-gateways/*",
"integration-tests",
"packages/*"
"ampd",
"contracts/*",
"external-gateways/*",
"integration-tests",
"packages/*",
]
resolver = "2"

Expand All @@ -13,8 +13,12 @@ rust-version = "1.78.0" # be sure there is an optimizer release supporting this
edition = "2021"

[workspace.dependencies]
alloy-primitives = { version = "0.7.6", default-features = false, features = ["std"] }
alloy-sol-types = { version = "0.7.6", default-features = false, features = ["std"] }
alloy-primitives = { version = "0.7.6", default-features = false, features = [
"std",
] }
alloy-sol-types = { version = "0.7.6", default-features = false, features = [
"std",
] }
anyhow = "1.0.89"
assert_ok = "1.0"
axelar-wasm-std = { version = "^1.0.0", path = "packages/axelar-wasm-std" }
Expand All @@ -31,7 +35,9 @@ cw-storage-plus = { version = "1.2.0", features = ["iterator", "macro"] }
cw2 = "1.1.0"
ed25519-dalek = { version = "2.1.1", default-features = false }
error-stack = { version = "0.4.0", features = ["eyre"] }
ethers-contract = { version = "2.0.14", default-features = false, features = ["abigen"] }
ethers-contract = { version = "2.0.14", default-features = false, features = [
"abigen",
] }
ethers-core = "2.0.14"
events = { version = "^1.0.0", path = "packages/events" }
events-derive = { version = "^1.0.0", path = "packages/events-derive" }
Expand Down Expand Up @@ -69,6 +75,10 @@ stellar-xdr = { version = "21.2.0" }
strum = { version = "0.25", default-features = false, features = ["derive"] }
sui-gateway = { version = "^1.0.0", path = "packages/sui-gateway" }
sui-types = { version = "^1.0.0", path = "packages/sui-types" }
starknet-types-core = { version = "0.1.7" }
starknet-types = { version = "^1.0.0", path = "packages/starknet-types" }
starknet-core = "0.12.0"
starknet-providers = "0.12.0"
syn = "2.0.68"
thiserror = "1.0.61"
tofn = { version = "1.1" }
Expand All @@ -77,6 +87,11 @@ tokio-stream = "0.1.11"
tokio-util = "0.7.11"
voting-verifier = { version = "^1.1.0", path = "contracts/voting-verifier" }
axelar-core-std = { version = "^1.0.0", path = "packages/axelar-core-std" }
# Async
futures-concurrency = "7.4"
futures-util = "0.3"
futures = "0.3"
async-trait = "0"

[workspace.lints.clippy]
arithmetic_side_effects = "deny"
Expand Down
6 changes: 2 additions & 4 deletions contracts/voting-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ rust-version = { workspace = true }
edition = { workspace = true }
description = "Voting verifier contract"

exclude = [
"contract.wasm",
"hash.txt"
]
exclude = ["contract.wasm", "hash.txt"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
Expand Down Expand Up @@ -60,6 +57,7 @@ integration-tests = { workspace = true }
multisig = { workspace = true, features = ["test", "library"] }
rand = { workspace = true }
sha3 = { workspace = true }
starknet-core = { workspace = true }

[lints]
workspace = true
76 changes: 74 additions & 2 deletions contracts/voting-verifier/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ mod test {
use assert_ok::assert_ok;
use axelar_wasm_std::address::AddressFormat;
use axelar_wasm_std::msg_id::{
Base58SolanaTxSignatureAndEventIndex, Base58TxDigestAndEventIndex, HexTxHash,
HexTxHashAndEventIndex, MessageIdFormat,
Base58SolanaTxSignatureAndEventIndex, Base58TxDigestAndEventIndex,
FieldElementAndEventIndex, HexTxHash, HexTxHashAndEventIndex, MessageIdFormat,
};
use axelar_wasm_std::voting::Vote;
use axelar_wasm_std::{
Expand All @@ -143,6 +143,7 @@ mod test {
AuthorizationState, BondingState, Verifier, WeightedVerifier, VERIFIER_WEIGHT,
};
use sha3::{Digest, Keccak256, Keccak512};
use starknet_core::types::Felt;

use super::*;
use crate::error::ContractError;
Expand Down Expand Up @@ -237,6 +238,17 @@ mod test {

fn message_id(id: &str, index: u64, msg_id_format: &MessageIdFormat) -> nonempty::String {
match msg_id_format {
MessageIdFormat::FieldElementAndEventIndex => {
let mut id_bytes: [u8; 32] = Keccak256::digest(id.as_bytes()).into();
id_bytes[0] = 0; // felt is ~31 bytes
FieldElementAndEventIndex {
tx_hash: Felt::from_bytes_be(&id_bytes),
event_index: index,
}
.to_string()
.parse()
.unwrap()
}
MessageIdFormat::HexTxHashAndEventIndex => HexTxHashAndEventIndex {
tx_hash: Keccak256::digest(id.as_bytes()).into(),
event_index: index,
Expand Down Expand Up @@ -350,10 +362,53 @@ mod test {
address_format: AddressFormat::Eip55,
should_fail: true,
},
TestCase {
source_gateway_address:
// 63 chars
"0x06cdc5221388566e09e1a9be3dcfd4b1bbb4abf98296bb4674401a79373cce5"
.to_string()
.to_lowercase(),
address_format: AddressFormat::Starknet,
should_fail: true,
},
TestCase {
source_gateway_address:
// 62 chars
"0x6cdc5221388566e09e1a9be3dcfd4b1bbb4abf98296bb4674401a79373cce5"
.to_string()
.to_lowercase(),
address_format: AddressFormat::Starknet,
should_fail: true,
},
TestCase {
source_gateway_address:
// 64 chars, but out of prime field range
"0xff6cdc5221388566e09e1a9be3dcfd4b1bbb4abf98296bb4674401a79373cce5"
.to_string()
.to_lowercase(),
address_format: AddressFormat::Starknet,
should_fail: true,
},
TestCase {
source_gateway_address:
"0x006cdc5221388566e09e1a9be3dcfd4b1bbb4abf98296bb4674401a79373cce5"
.to_string()
.to_lowercase(),
address_format: AddressFormat::Starknet,
should_fail: false,
},
TestCase {
source_gateway_address: "0x4F4495243837681061C4743b74B3eEdf548D56A5"
.to_string()
.to_lowercase(),
address_format: AddressFormat::Starknet,
should_fail: true,
},
TestCase {
source_gateway_address:
"0xdb1473ed56ddede13225b99d779ebf9d9011874e26acbb8bfec8b6a43d0fbcaa"
.to_string()
.to_uppercase(),
address_format: AddressFormat::Sui,
should_fail: true,
},
Expand Down Expand Up @@ -469,6 +524,22 @@ mod test {
);
}

#[test]
fn should_fail_if_messages_have_hex_msg_id_but_contract_expects_field_element() {
let msg_id_format = MessageIdFormat::FieldElementAndEventIndex;
let verifiers = verifiers(2);
let mut deps = setup(verifiers.clone(), &msg_id_format);

let messages = messages(1, &MessageIdFormat::HexTxHashAndEventIndex);
let msg = ExecuteMsg::VerifyMessages(messages.clone());

let err = execute(deps.as_mut(), mock_env(), mock_info(SENDER, &[]), msg).unwrap_err();
assert_contract_err_strings_equal(
err,
ContractError::InvalidMessageID(messages[0].cc_id.message_id.to_string()),
);
}

#[test]
fn should_fail_if_messages_have_hex_msg_id_but_contract_expects_base58() {
let msg_id_format = MessageIdFormat::Base58TxDigestAndEventIndex;
Expand Down Expand Up @@ -838,6 +909,7 @@ mod test {
[
(v, s, MessageIdFormat::HexTxHashAndEventIndex),
(v, s, MessageIdFormat::Base58TxDigestAndEventIndex),
(v, s, MessageIdFormat::FieldElementAndEventIndex),
]
})
.collect::<Vec<_>>();
Expand Down
14 changes: 12 additions & 2 deletions contracts/voting-verifier/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::str::FromStr;
use std::vec::Vec;

use axelar_wasm_std::msg_id::{
Base58SolanaTxSignatureAndEventIndex, Base58TxDigestAndEventIndex, Bech32mFormat, HexTxHash,
HexTxHashAndEventIndex, MessageIdFormat,
Base58SolanaTxSignatureAndEventIndex, Base58TxDigestAndEventIndex, Bech32mFormat,
FieldElementAndEventIndex, HexTxHash, HexTxHashAndEventIndex, MessageIdFormat,
};
use axelar_wasm_std::voting::{PollId, Vote};
use axelar_wasm_std::{nonempty, VerificationStatus};
Expand Down Expand Up @@ -160,6 +160,16 @@ fn parse_message_id(
.map_err(|_| ContractError::InvalidMessageID(message_id.to_string()))?,
))
}
MessageIdFormat::FieldElementAndEventIndex => {
let id = FieldElementAndEventIndex::from_str(message_id)
.map_err(|_| ContractError::InvalidMessageID(message_id.to_string()))?;

Ok((
id.tx_hash_as_hex(),
u32::try_from(id.event_index)
.map_err(|_| ContractError::InvalidMessageID(message_id.to_string()))?,
))
}
MessageIdFormat::HexTxHashAndEventIndex => {
let id = HexTxHashAndEventIndex::from_str(message_id)
.map_err(|_| ContractError::InvalidMessageID(message_id.to_string()))?;
Expand Down
14 changes: 8 additions & 6 deletions packages/axelar-wasm-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ rust-version = { workspace = true }
edition = { workspace = true }
description = "Axelar cosmwasm standard library crate"

exclude = [
"contract.wasm",
"hash.txt"
]
exclude = ["contract.wasm", "hash.txt"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
Expand Down Expand Up @@ -41,7 +38,10 @@ into-inner-derive = { workspace = true }
itertools = { workspace = true }
lazy_static = "1.4.0"
num-traits = { workspace = true }
regex = { version = "1.10.0", default-features = false, features = ["perf", "std"] }
regex = { version = "1.10.0", default-features = false, features = [
"perf",
"std",
] }
report = { workspace = true }
schemars = "0.8.10"
semver = { workspace = true }
Expand All @@ -50,17 +50,19 @@ serde_json = "1.0.89"
serde_with = { version = "3.11.0", features = ["macros"] }
sha3 = { workspace = true }
stellar-xdr = { workspace = true }
starknet-types-core = { workspace = true }
strum = { workspace = true }
sui-types = { workspace = true }
thiserror = { workspace = true }
valuable = { version = "0.1.0", features = ["derive"] }
crypto-bigint = { version = "0.5.5", features = ["rand_core"] }

[dev-dependencies]
assert_ok = { workspace = true }
cw-multi-test = "0.15.1"
goldie = { workspace = true }
hex = { version = "0.4.3", default-features = false }
rand = { workspace = true }
hex = { version = "0.4.3", default-features = false }

[lints]
workspace = true
Loading