Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
milapsheth committed Aug 27, 2024
1 parent 11a7a3e commit 6e3a086
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 64 deletions.
8 changes: 7 additions & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const readlineSync = require('readline-sync');
const { CosmWasmClient } = require('@cosmjs/cosmwasm-stargate');
const { ethers } = require('hardhat');
const {
utils: { keccak256 },
utils: { keccak256, defaultAbiCoder },
} = ethers;
const { normalizeBech32 } = require('@cosmjs/encoding');

Expand Down Expand Up @@ -347,6 +347,10 @@ const isValidCosmosAddress = (str) => {
}
};

const getSaltFromKey = (key) => {
return keccak256(defaultAbiCoder.encode(['string'], [key.toString()]));
};

const getContractConfig = async (config, chain) => {
const key = Buffer.from('config');
const client = await CosmWasmClient.connect(config.axelar.rpc);
Expand Down Expand Up @@ -450,4 +454,6 @@ module.exports = {
getChainConfig,
getMultisigProof,
getContractConfig,
getSaltFromKey,
calculateDomainSeparator,
};
17 changes: 10 additions & 7 deletions cosmwasm/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
'use strict';

const zlib = require('zlib');
const { ethers } = require('hardhat');
const {
utils: { keccak256 },
} = ethers;
const { createHash } = require('crypto');

const { readFileSync } = require('fs');
Expand All @@ -20,7 +16,16 @@ const {
ExecuteContractProposal,
} = require('cosmjs-types/cosmwasm/wasm/v1/proposal');
const { AccessType } = require('cosmjs-types/cosmwasm/wasm/v1/types');
const { getSaltFromKey, isString, isStringArray, isKeccak256Hash, isNumber, toBigNumberString, getChainConfig } = require('../evm/utils');
const {
isString,
isStringArray,
isKeccak256Hash,
isNumber,
toBigNumberString,
getChainConfig,
getSaltFromKey,
calculateDomainSeparator,
} = require('../common');
const { normalizeBech32 } = require('@cosmjs/encoding');

const governanceAddress = 'axelar10d07y265gmmuvt4z0w9aw880jnsr700j7v9daj';
Expand All @@ -46,8 +51,6 @@ const isValidCosmosAddress = (str) => {

const fromHex = (str) => new Uint8Array(Buffer.from(str.replace('0x', ''), 'hex'));

const calculateDomainSeparator = (chain, router, network) => keccak256(Buffer.from(`${chain}${router}${network}`));

const getSalt = (salt, contractName, chainNames) => fromHex(getSaltFromKey(salt || contractName.concat(chainNames)));

const getLabel = ({ contractName, label }) => label || contractName;
Expand Down
5 changes: 1 addition & 4 deletions evm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const {
sleep,
findProjectRoot,
timeout,
getSaltFromKey,
} = require('../common');
const {
create3DeployContract,
Expand All @@ -48,10 +49,6 @@ const IDeployer = require('@axelar-network/axelar-gmp-sdk-solidity/interfaces/ID
const { exec } = require('child_process');
const { verifyContract } = require(`${__dirname}/../axelar-chains-config`);

const getSaltFromKey = (key) => {
return keccak256(defaultAbiCoder.encode(['string'], [key.toString()]));
};

const deployCreate = async (wallet, contractJson, args = [], options = {}, verifyOptions = null, chain = {}) => {
const factory = new ContractFactory(contractJson.abi, contractJson.bytecode, wallet);

Expand Down
80 changes: 28 additions & 52 deletions sui/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ const {
executeDataStruct,
proofStruct,
addBaseOptions,
addOptionsToCommands,
getSigners,
getWallet,
printWalletInfo,
getRawPrivateKey,
broadcast,
suiClockAddress,
} = require('./utils');
const secp256k1 = require('secp256k1');

Expand Down Expand Up @@ -106,12 +108,7 @@ function getProof(keypair, commandType, data, contractConfig, options) {
return encodedProof;
}

async function callContract(keypair, client, config, chain, args, options) {
if (!chain.contracts.AxelarGateway) {
throw new Error('Axelar Gateway package not found.');
}

const contractConfig = chain.contracts.AxelarGateway;
async function callContract(keypair, client, config, chain, contractConfig, args, options) {
const packageId = contractConfig.address;

const [destinationChain, destinationAddress, payload] = args;
Expand Down Expand Up @@ -150,12 +147,7 @@ async function callContract(keypair, client, config, chain, args, options) {
printInfo('Contract called');
}

async function approveMessages(keypair, client, config, chain, args, options) {
if (!chain.contracts.AxelarGateway) {
throw new Error('Axelar Gateway package not found.');
}

const contractConfig = chain.contracts.AxelarGateway;
async function approveMessages(keypair, client, config, chain, contractConfig, args, options) {
const packageId = contractConfig.address;
const [multisigSessionId] = args;
const { payload, status } = await getMultisigProof(config, chain.axelarId, multisigSessionId);
Expand All @@ -168,7 +160,7 @@ async function approveMessages(keypair, client, config, chain, args, options) {
throw new Error('Multisig session not completed');
}

const executeData = executeDataStruct.parse(new Uint8Array(arrayify('0x' + status.completed.execute_data)));
const executeData = executeDataStruct.parse(arrayify('0x' + status.completed.execute_data));

const tx = new Transaction();

Expand All @@ -181,17 +173,12 @@ async function approveMessages(keypair, client, config, chain, args, options) {
],
});

await broadcast(client, keypair, tx);
const receipt = await broadcast(client, keypair, tx);

printInfo('Approved messages');
printInfo('Approved messages', receipt.digest);
}

async function approve(keypair, client, config, chain, args, options) {
if (!chain.contracts.AxelarGateway) {
throw new Error('Axelar Gateway package not found.');
}

const contractConfig = chain.contracts.AxelarGateway;
async function approve(keypair, client, config, chain, contractConfig, args, options) {
const packageId = contractConfig.address;
const [sourceChain, messageId, sourceAddress, destinationId, payloadHash] = args;

Expand Down Expand Up @@ -221,17 +208,12 @@ async function approve(keypair, client, config, chain, args, options) {
],
});

await broadcast(client, keypair, tx);
const receipt = await broadcast(client, keypair, tx);

printInfo('Approved messages');
printInfo('Approved messages', receipt.digest);
}

async function rotateSigners(keypair, client, config, chain, args, options) {
if (!chain.contracts.AxelarGateway) {
throw new Error('Axelar Gateway package not found.');
}

const contractConfig = chain.contracts.AxelarGateway;
async function rotateSigners(keypair, client, config, chain, contractConfig, args, options) {
const packageId = contractConfig.address;
const [multisigSessionId] = args;
const { payload, status } = await getMultisigProof(config, chain.axelarId, multisigSessionId);
Expand All @@ -244,31 +226,26 @@ async function rotateSigners(keypair, client, config, chain, args, options) {
throw new Error('Multisig session not completed');
}

const executeData = executeDataStruct.parse(new Uint8Array(arrayify('0x' + status.completed.execute_data)));
const executeData = executeDataStruct.parse(arrayify('0x' + status.completed.execute_data));

const tx = new Transaction();

tx.moveCall({
target: `${packageId}::gateway::rotate_signers`,
arguments: [
tx.object(contractConfig.objects.Gateway),
tx.object('0x6'),
tx.object(suiClockAddress),
tx.pure(bcs.vector(bcs.u8()).serialize(new Uint8Array(executeData.payload)).toBytes()),
tx.pure(bcs.vector(bcs.u8()).serialize(new Uint8Array(executeData.proof)).toBytes()),
],
});

await broadcast(client, keypair, tx);
const receipt = await broadcast(client, keypair, tx);

printInfo('Signers rotated succesfully');
printInfo('Signers rotated', receipt.digest);
}

async function rotate(keypair, client, config, chain, args, options) {
if (!chain.contracts.AxelarGateway) {
throw new Error('Axelar Gateway package not found.');
}

const contractConfig = chain.contracts.AxelarGateway;
async function rotate(keypair, client, config, chain, contractConfig, args, options) {
const packageId = contractConfig.address;
const signers = await getSigners(keypair, config, chain.axelarId, options);

Expand All @@ -288,7 +265,7 @@ async function rotate(keypair, client, config, chain, args, options) {
target: `${packageId}::gateway::rotate_signers`,
arguments: [
tx.object(contractConfig.objects.Gateway),
tx.object('0x6'),
tx.object(suiClockAddress),
tx.pure(bcs.vector(bcs.u8()).serialize(encodedSigners).toBytes()),
tx.pure(bcs.vector(bcs.u8()).serialize(encodedProof).toBytes()),
],
Expand All @@ -305,7 +282,11 @@ async function mainProcessor(processor, args, options) {
const [keypair, client] = getWallet(config.sui, options);
await printWalletInfo(keypair, client, config.sui, options);

await processor(keypair, client, config, config.sui, args, options);
if (!config.sui.contracts?.AxelarGateway) {
throw new Error('Axelar Gateway package not found.');
}

await processor(keypair, client, config, config.sui, config.sui.contracts.AxelarGateway, args, options);

saveConfig(config, options.env);
}
Expand All @@ -315,7 +296,7 @@ if (require.main === module) {

program.name('gateway').description('Gateway contract operations.');

const rotateCmd = program
program
.command('rotate')
.description('Rotate signers of the gateway contract')
.addOption(new Option('--signers <signers>', 'JSON with the initial signer set'))
Expand All @@ -326,7 +307,7 @@ if (require.main === module) {
mainProcessor(rotate, [], options);
});

const approveCmd = program
program
.command('approve <sourceChain> <messageId> <sourceAddress> <destinationId> <payloadHash>')
.description('Approve messages at the gateway contract')
.addOption(new Option('--proof <proof>', 'JSON of the proof'))
Expand All @@ -335,34 +316,29 @@ if (require.main === module) {
mainProcessor(approve, [sourceChain, messageId, sourceAddress, destinationId, payloadHash], options);
});

const approveMessagesCmd = program
program
.command('approveMessages <multisigSessionId>')
.description('Approve messages at the gateway contract from amplifier proof')
.action((multisigSessionId, options) => {
mainProcessor(approveMessages, [multisigSessionId], options);
});

const rotateSignersCmd = program
program
.command('rotateSigners <multisigSessionId>')
.description('Rotate signers at the gateway contract from amplifier proof')
.action((multisigSessionId, options) => {
mainProcessor(rotateSigners, [multisigSessionId], options);
});

const callContractCmd = program
program
.command('call-contract <destinationChain> <destinationAddress> <payload>')
.description('Initiate sending a cross-chain message via the gateway')
.addOption(new Option('--channel <channel>', 'Existing channel ID to initiate a cross-chain message over'))
.action((destinationChain, destinationAddress, payload, options) => {
mainProcessor(callContract, [destinationChain, destinationAddress, payload], options);
});

addBaseOptions(program);
addBaseOptions(rotateSignersCmd);
addBaseOptions(rotateCmd);
addBaseOptions(callContractCmd);
addBaseOptions(approveCmd);
addBaseOptions(approveMessagesCmd);
addOptionsToCommands(program, addBaseOptions);

program.parse();
}

0 comments on commit 6e3a086

Please sign in to comment.