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

fix!: multisig ISM (#1) #144

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
26 changes: 13 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[workspace]
members = [
"contracts/core/*",
"contracts/hooks/*",
"contracts/igps/*",
"contracts/isms/*",
"contracts/mocks/*",
"contracts/warp/*",
"packages/*",
"integration-test",
"contracts/core/*",
"contracts/hooks/*",
"contracts/igps/*",
"contracts/isms/*",
"contracts/mocks/*",
"contracts/warp/*",
"packages/*",
"integration-test",
]
resolver = "2"

Expand All @@ -27,11 +27,11 @@ panic = "abort"
rpath = false

[workspace.package]
version = "0.0.6"
version = "0.0.7"
authors = [
"byeongsu-hong <[email protected]>",
"Eric <[email protected]>",
"Steve <[email protected]>",
"byeongsu-hong <[email protected]>",
"Eric <[email protected]>",
"Steve <[email protected]>",
]
edition = "2021"
license = "Apache-2.0"
Expand Down Expand Up @@ -68,7 +68,7 @@ serde_json = "1.0.96"
serde-json-wasm = "1.0.0"
schemars = "0.8.12"
prost = { version = "0.12.1", default-features = false, features = [
"prost-derive",
"prost-derive",
] }
prost-types = { version = "0.12.1", default-features = false }
hex-literal = { version = "0.4.1" }
Expand Down
41 changes: 41 additions & 0 deletions context/injective-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "v0.0.7",
"artifacts": {
"hpl_hook_aggregate": 381,
"hpl_hook_fee": 382,
"hpl_hook_merkle": 383,
"hpl_hook_pausable": 384,
"hpl_hook_routing": 385,
"hpl_hook_routing_custom": 386,
"hpl_hook_routing_fallback": 387,
"hpl_igp": 388,
"hpl_igp_oracle": 389,
"hpl_ism_aggregate": 390,
"hpl_ism_multisig": 1183,
"hpl_ism_pausable": 392,
"hpl_ism_routing": 393,
"hpl_mailbox": 394,
"hpl_test_mock_hook": 395,
"hpl_test_mock_ism": 396,
"hpl_test_mock_msg_receiver": 397,
"hpl_validator_announce": 398,
"hpl_warp_cw20": 399,
"hpl_warp_native": 400
},
"deployments": {
"core": {
"mailbox": {
"type": "hpl_mailbox",
"address": "inj1palm2wtp6urg0c6j4f2ukv5u5ahdcrqek0sapt"
},
"validator_announce": {
"type": "hpl_validator_announce",
"address": "inj1r7eztvhul0n4uc22r438m6tl7dezgthdw2cjjx"
}
},
"isms": {
"type": "hpl_ism_multisig",
"address": "inj17hfpmd6yqy3gs4sxc0hh779k3nvldu5rpjkepn"
}
}
}
60 changes: 55 additions & 5 deletions contracts/isms/multisig/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use cosmwasm_std::{Deps, HexBinary};
use hpl_interface::{
ism::{IsmType, ModuleTypeResponse, VerifyInfoResponse, VerifyResponse},
Expand Down Expand Up @@ -35,20 +37,25 @@ pub fn verify_message(

let hashed_message = eth_hash(multisig_hash)?;

// pizza :)
let validators = VALIDATORS.load(deps.storage, message.origin_domain)?;
let mut threshold = THRESHOLD.load(deps.storage, message.origin_domain)?;

let mut matched_validators = HashSet::new();

for signature in metadata.signatures {
let signature = signature.as_slice();
let pubkey = deps.api.secp256k1_recover_pubkey(
let signer_pubkey = deps.api.secp256k1_recover_pubkey(
&hashed_message,
&signature[..64],
signature[64] - 27,
)?;

if validators.contains(&eth_addr(pubkey.into())?) {
let signer_addr = eth_addr(signer_pubkey.into())?;

if validators.contains(&signer_addr) && !matched_validators.contains(&signer_addr) {
threshold -= 1;
matched_validators.insert(signer_addr);

if threshold == 0 {
break;
}
Expand Down Expand Up @@ -81,7 +88,7 @@ mod test {
use cosmwasm_std::{testing::mock_dependencies, HexBinary};
use hpl_interface::{
ism::{IsmType, ModuleTypeResponse, VerifyResponse},
types::{eth_addr, Message},
types::{eth_addr, Message, MessageIdMultisigIsmMetadata},
};
use ibcx_test_utils::hex;
use k256::{ecdsa::SigningKey, elliptic_curve::rand_core::OsRng};
Expand All @@ -104,7 +111,7 @@ mod test {
#[rstest]
#[case(
hex("0000000000000068220000000000000000000000000d1255b09d94659bb0888e0aa9fca60245ce402a0000682155208cd518cffaac1b5d8df216a9bd050c9a03f0d4f3ba88e5268ac4cd12ee2d68656c6c6f"),
hex("986a1625d44e4b3969b08a5876171b2b4fcdf61b3e5c70a86ad17b304f17740a9f45d99ea6bec61392a47684f4e5d1416ddbcb5fdef0f132c27d7034e9bbff1c00000000ba9911d78ec6d561413e3589f920388cbd7554fbddd8ce50739337250853ec3577a51fa40e727c05b50f15db13f5aad5857c89d432644be48d70325ea83fdb6c1c"),
hex("986a1625d44e4b3969b08a5876171b2b4fcdf61b3e5c70a86ad17b304f17740a9f45d99ea6bec61392a47684f4e5d1416ddbcb5fdef0f132c27d7034e9bbff1c00000000ba9911d78ec6d561413e3589f920388cbd7554fbddd8ce50739337250853ec3577a51fa40e727c05b50f15db13f5aad5857c89d432644be48d70325ea83fdb6c1c"),
vec![
hex("122e0663ccc190266427e7fc0ed6589b5d7d36db"),
hex("01d7525e91dfc3f594fd366aad70f956b398de9e"),
Expand Down Expand Up @@ -139,6 +146,49 @@ mod test {
assert_eq!(res, VerifyResponse { verified: true });
}

#[test]
fn test_verify_prevent_reuse_of_validator_signature() {
let mut deps = mock_dependencies();

let threshold = 2u8;
let validators = vec![
hex("ebc301013b6cd2548e347c28d2dc43ec20c068f2"),
hex("315db9868fc8813b221b1694f8760ece39f45447"),
hex("17517c98358c5937c5d9ee47ce1f5b4c2b7fc9f5"),
];

let message = Message {
version: 3,
nonce: 36,
origin_domain: 80001,
sender: hex("00000000000000000000000004980c17e2ce26578c82f81207e706e4505fae3b"),
dest_domain: 43113,
recipient: hex("0000000000000000000000000b1c1b54f45e02552331d3106e71f5e0b573d5d4"),
body: hex("48656c6c6f21"),
};

let metadata = MessageIdMultisigIsmMetadata {
origin_merkle_tree: hex("0000000000000000000000009af85731edd41e2e50f81ef8a0a69d2fb836edf9"),
merkle_root: hex("a84430f822e0e9b5942faace72bd5b97f0b59a58a9b8281231d9e5c393b5859c"),
merkle_index: hex("00000024"),
signatures: [
// same signature
hex("539feceace17782697e29e74151006dc7b47227cf48aba02926336cb5f7fa38b3d05e8293045f7b5811eda3ae8aa070116bb5fbf57c79e143a69e909df90cefa1b"),
hex("539feceace17782697e29e74151006dc7b47227cf48aba02926336cb5f7fa38b3d05e8293045f7b5811eda3ae8aa070116bb5fbf57c79e143a69e909df90cefa1b")
].to_vec()
};

VALIDATORS
.save(deps.as_mut().storage, message.origin_domain, &validators)
.unwrap();
THRESHOLD
.save(deps.as_mut().storage, message.origin_domain, &threshold)
.unwrap();

let res = verify_message(deps.as_ref(), metadata.into(), message.into()).unwrap();
assert_eq!(res, VerifyResponse { verified: false });
}

#[test]
fn test_get_verify_info() {
let raw_message = hex("0000000000000068220000000000000000000000000d1255b09d94659bb0888e0aa9fca60245ce402a0000682155208cd518cffaac1b5d8df216a9bd050c9a03f0d4f3ba88e5268ac4cd12ee2d68656c6c6f");
Expand Down
4 changes: 1 addition & 3 deletions integration-test/tests/contracts/cw/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ impl Hook {
let hook = wasm
.instantiate(
codes.hook_merkle,
&hook::merkle::InstantiateMsg {
mailbox,
},
&hook::merkle::InstantiateMsg { mailbox },
Some(deployer.address().as_str()),
Some("cw-hpl-hook-merkle"),
&[],
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@cosmjs/tendermint-rpc": "^0.32.4",
"@hyperlane-xyz/sdk": "^3.7.0",
"@hyperlane-xyz/utils": "^3.7.0",
"@injectivelabs/sdk-ts": "1.14.13",
"axios": "^1.6.7",
"colors": "^1.4.0",
"commander": "^11.1.0",
Expand Down
10 changes: 3 additions & 7 deletions packages/interface/src/hook/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ pub enum ExecuteMsg {

#[cw_serde]
pub enum FeeHookMsg {
SetFee {
fee: Coin,
},
Claim {
recipient: Option<Addr>
}
SetFee { fee: Coin },
Claim { recipient: Option<Addr> },
}

#[cw_serde]
Expand All @@ -43,7 +39,7 @@ pub enum QueryMsg {
#[derive(QueryResponses)]
pub enum FeeHookQueryMsg {
#[returns(FeeResponse)]
Fee {}
Fee {},
}

#[cw_serde]
Expand Down
2 changes: 1 addition & 1 deletion packages/interface/src/hook/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub mod aggregate;
pub mod fee;
pub mod merkle;
pub mod pausable;
pub mod routing;
pub mod routing_custom;
pub mod routing_fallback;
pub mod fee;

use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{
Expand Down
2 changes: 1 addition & 1 deletion packages/interface/src/ism/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod aggregate;
pub mod multisig;
pub mod routing;
pub mod pausable;
pub mod routing;

use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, CustomQuery, HexBinary, QuerierWrapper, StdResult};
Expand Down
11 changes: 7 additions & 4 deletions packages/interface/src/ism/pausable.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
use cosmwasm_schema::{cw_serde, QueryResponses};

use crate::{ownable::{OwnableMsg, OwnableQueryMsg}, pausable::{PausableMsg, PausableQueryMsg}};
use crate::{
ownable::{OwnableMsg, OwnableQueryMsg},
pausable::{PausableMsg, PausableQueryMsg},
};

use super::IsmQueryMsg;

#[cw_serde]
pub struct InstantiateMsg {
pub owner: String,
pub paused: bool
pub paused: bool,
}

#[cw_serde]
pub enum ExecuteMsg {
Ownable(OwnableMsg),
Pausable(PausableMsg)
Pausable(PausableMsg),
}

#[cw_serde]
Expand All @@ -22,5 +25,5 @@ pub enum ExecuteMsg {
pub enum QueryMsg {
Ownable(OwnableQueryMsg),
Ism(IsmQueryMsg),
Pausable(PausableQueryMsg)
Pausable(PausableQueryMsg),
}
31 changes: 28 additions & 3 deletions script/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command } from 'commander';

import { MsgExecuteContract } from '@injectivelabs/sdk-ts';
import { deployHook, deployIsm } from '../deploy';
import { saveAgentConfig } from '../shared/agent';
import { Client, config, getNetwork } from '../shared/config';
Expand All @@ -19,9 +20,33 @@ async function handleDeploy(_: object, cmd: Command) {
ctx.deployments = ctx.deployments || {};
ctx.deployments.core = await deployCore(opts, ctx, client);
ctx.deployments.isms = await deployIsms(ctx, client);
ctx.deployments.hooks = await deployHooks(opts, ctx, client);
ctx.deployments.warp = { native: [], cw20: [] };
ctx.deployments.test = await deployTest(opts, ctx, client);
// ctx.deployments.hooks = await deployHooks(opts, ctx, client);
// ctx.deployments.warp = { native: [], cw20: [] };
// ctx.deployments.test = await deployTest(opts, ctx, client);

// const updateMsg = MsgUpdateAdmin.fromJSON({
// sender: client.injective_signer,
// newAdmin: 'inj1ac6qpt57vhtfzdecd2an052elwgenwtxcn9chl',
// contract: ctx.deployments.isms?.address!,
// });

const initMsg = MsgExecuteContract.fromJSON({
contractAddress: ctx.deployments.isms?.address!,
sender: client.injective_signer,
msg: {
ownable: {
init_ownership_transfer: {
next_owner: 'inj1ac6qpt57vhtfzdecd2an052elwgenwtxcn9chl'
}
}
}
})

const resp = await client.injective.broadcast({
msgs: initMsg,
});

console.log({resp});

if (!ctx.deployments.core?.mailbox)
throw new Error('deployed Mailbox contract not found on context');
Expand Down
2 changes: 1 addition & 1 deletion script/deploy/ism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function deployIsm(
// deploy multisig ism
case 'multisig': {
const multisig = await deployContract(ctx, client, 'hpl_ism_multisig', {
owner: ism.owner === '<signer>' ? client.signer : ism.owner,
owner: ism.owner === '<signer>' ? client.injective_signer : ism.owner,
});

if (ism.validators != undefined) {
Expand Down
16 changes: 16 additions & 0 deletions script/shared/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,14 @@ export type Config = {
};
};

import { Network } from "@injectivelabs/networks";
import { MsgBroadcasterWithPk } from '@injectivelabs/sdk-ts';

export class Client {
wasm: SigningCosmWasmClient;
stargate: SigningStargateClient;
injective: MsgBroadcasterWithPk;
injective_signer: string;
signer: string;
signer_addr: string;
signer_pubkey: string;
Expand Down Expand Up @@ -202,8 +207,17 @@ export async function getSigningClient(networkId: string): Promise<Client> {
: await DirectSecp256k1Wallet.fromKey(Buffer.from(signer, 'hex'), hrp);

const [account] = await wallet.getAccounts();

const gasPrice = GasPrice.fromString(`${gas.price}${gas.denom}`);

const injective = new MsgBroadcasterWithPk({
privateKey: signer,
network: Network.Mainnet
});

// TODO: figure out how to derive this
const injective_signer = "inj1xe7h6urta6mftjl8ncud2fv42wgdtdad8ackjm";

const wasm = await SigningCosmWasmClient.connectWithSigner(
endpoint.rpc,
wallet,
Expand All @@ -221,6 +235,8 @@ export async function getSigningClient(networkId: string): Promise<Client> {
return {
wasm,
stargate,
injective,
injective_signer,
signer: account.address,
signer_addr: Buffer.from(ethaddr).toString('hex'),
signer_pubkey: Buffer.from(account.pubkey).toString('hex'),
Expand Down
Loading
Loading