From 6e3a086adb6c01ee7dd74d6d707c660147544bb7 Mon Sep 17 00:00:00 2001 From: Milap Sheth Date: Tue, 27 Aug 2024 04:45:17 +0400 Subject: [PATCH] address comments --- common/utils.js | 8 ++++- cosmwasm/utils.js | 17 +++++----- evm/utils.js | 5 +-- sui/gateway.js | 80 +++++++++++++++++------------------------------ 4 files changed, 46 insertions(+), 64 deletions(-) diff --git a/common/utils.js b/common/utils.js index 1a135693..3aacb270 100644 --- a/common/utils.js +++ b/common/utils.js @@ -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'); @@ -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); @@ -450,4 +454,6 @@ module.exports = { getChainConfig, getMultisigProof, getContractConfig, + getSaltFromKey, + calculateDomainSeparator, }; diff --git a/cosmwasm/utils.js b/cosmwasm/utils.js index 52417740..33c5239d 100644 --- a/cosmwasm/utils.js +++ b/cosmwasm/utils.js @@ -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'); @@ -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'; @@ -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; diff --git a/evm/utils.js b/evm/utils.js index 60f08e6a..2cf54448 100644 --- a/evm/utils.js +++ b/evm/utils.js @@ -34,6 +34,7 @@ const { sleep, findProjectRoot, timeout, + getSaltFromKey, } = require('../common'); const { create3DeployContract, @@ -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); diff --git a/sui/gateway.js b/sui/gateway.js index 3700891f..5d20dc95 100644 --- a/sui/gateway.js +++ b/sui/gateway.js @@ -16,11 +16,13 @@ const { executeDataStruct, proofStruct, addBaseOptions, + addOptionsToCommands, getSigners, getWallet, printWalletInfo, getRawPrivateKey, broadcast, + suiClockAddress, } = require('./utils'); const secp256k1 = require('secp256k1'); @@ -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; @@ -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); @@ -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(); @@ -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; @@ -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); @@ -244,7 +226,7 @@ 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(); @@ -252,23 +234,18 @@ async function rotateSigners(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(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); @@ -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()), ], @@ -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); } @@ -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 ', 'JSON with the initial signer set')) @@ -326,7 +307,7 @@ if (require.main === module) { mainProcessor(rotate, [], options); }); - const approveCmd = program + program .command('approve ') .description('Approve messages at the gateway contract') .addOption(new Option('--proof ', 'JSON of the proof')) @@ -335,21 +316,21 @@ if (require.main === module) { mainProcessor(approve, [sourceChain, messageId, sourceAddress, destinationId, payloadHash], options); }); - const approveMessagesCmd = program + program .command('approveMessages ') .description('Approve messages at the gateway contract from amplifier proof') .action((multisigSessionId, options) => { mainProcessor(approveMessages, [multisigSessionId], options); }); - const rotateSignersCmd = program + program .command('rotateSigners ') .description('Rotate signers at the gateway contract from amplifier proof') .action((multisigSessionId, options) => { mainProcessor(rotateSigners, [multisigSessionId], options); }); - const callContractCmd = program + program .command('call-contract ') .description('Initiate sending a cross-chain message via the gateway') .addOption(new Option('--channel ', 'Existing channel ID to initiate a cross-chain message over')) @@ -357,12 +338,7 @@ if (require.main === module) { mainProcessor(callContract, [destinationChain, destinationAddress, payload], options); }); - addBaseOptions(program); - addBaseOptions(rotateSignersCmd); - addBaseOptions(rotateCmd); - addBaseOptions(callContractCmd); - addBaseOptions(approveCmd); - addBaseOptions(approveMessagesCmd); + addOptionsToCommands(program, addBaseOptions); program.parse(); }