Skip to content

Commit

Permalink
added a test to rotate signers
Browse files Browse the repository at this point in the history
  • Loading branch information
Foivos committed Jun 20, 2024
1 parent d0a4879 commit b143ef4
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 14 deletions.
4 changes: 2 additions & 2 deletions move/abi/Move.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "Abi"
version = "0.1.0"
published-at = "0x0"
published-at = "0x845c6fa54ccb69bde81098dabac0e1fb11dd9ae217a034992203ce99801a3b67"
edition = "2024.beta"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.25.3" }

[addresses]
abi = "0x0"
abi = "0x845c6fa54ccb69bde81098dabac0e1fb11dd9ae217a034992203ce99801a3b67"
4 changes: 2 additions & 2 deletions move/axelar_gateway/Move.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "AxelarGateway"
version = "0.1.0"
published-at = "0x1611b7e3e958f74e4ef9f4727333a98d095a9f71baf4c814d310d986db1f5662"
published-at = "0x7bc9ff900d613dba5c53b5f84869a229ebb791be61b6f30cfec6d4f6130492c7"
edition = "2024.beta"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.25.3" }

[addresses]
axelar_gateway = "0x1611b7e3e958f74e4ef9f4727333a98d095a9f71baf4c814d310d986db1f5662"
axelar_gateway = "0x7bc9ff900d613dba5c53b5f84869a229ebb791be61b6f30cfec6d4f6130492c7"
clock = "0x6"
4 changes: 2 additions & 2 deletions move/gas_service/Move.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "GasService"
version = "0.1.0"
published-at = "0xe356c310916a4f0c76a2c50b1101f66a328eee2626f709b1138e5ca2a537d438"
published-at = "0x9249adc3fac09edb21a0848af2ccf4901ffb3689124bc91d67bc613903ca643c"
edition = "2024.beta"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.25.3" }

[addresses]
gas_service = "0xe356c310916a4f0c76a2c50b1101f66a328eee2626f709b1138e5ca2a537d438"
gas_service = "0x9249adc3fac09edb21a0848af2ccf4901ffb3689124bc91d67bc613903ca643c"
4 changes: 2 additions & 2 deletions move/governance/Move.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "Governance"
version = "0.1.0"
published-at = "0x5b07e6dd81eda8900a3f7e5cab693a0b9e32eaec30d27f26830611e536dead13"
published-at = "0x0cba440261d721bde9c85eafe18aae673ee72702fa95da567675b34224f5ad77"
edition = "2024.beta"

[dependencies]
Expand All @@ -10,4 +10,4 @@ AxelarGateway = { local = "../axelar_gateway" }
Abi = { local = "../abi" }

[addresses]
governance = "0x5b07e6dd81eda8900a3f7e5cab693a0b9e32eaec30d27f26830611e536dead13"
governance = "0x0cba440261d721bde9c85eafe18aae673ee72702fa95da567675b34224f5ad77"
4 changes: 2 additions & 2 deletions move/its/Move.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ITS"
version = "0.1.0"
published-at = "0x542952f6e1278d7183d2fe788efe472a96f204c45328af28fa4e6fba36b432f2"
published-at = "0x89967fea5cc3bdf2d44cabe901aca920aa2a0485ca9a479f9932814c55d5f9b3"
edition = "2024.beta"

[dependencies]
Expand All @@ -10,4 +10,4 @@ AxelarGateway = { local = "../axelar_gateway" }
Governance = { local = "../governance" }

[addresses]
its = "0x542952f6e1278d7183d2fe788efe472a96f204c45328af28fa4e6fba36b432f2"
its = "0x89967fea5cc3bdf2d44cabe901aca920aa2a0485ca9a479f9932814c55d5f9b3"
4 changes: 2 additions & 2 deletions move/squid/Move.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "Squid"
version = "0.1.0"
published-at = "0x69c9099c4b7da929dca09ac9991163b6e7d3c3fc0e3b9c692ed0c3f02dae16ff"
published-at = "0x742acc52f94b86d3a2a4fdc39b1e0d75812f05253f88443754326c1e31cae873"
edition = "2024.beta"

[dependencies]
Expand All @@ -11,4 +11,4 @@ AxelarGateway = { local = "../axelar_gateway" }
ITS = { local = "../its" }

[addresses]
squid = "0x69c9099c4b7da929dca09ac9991163b6e7d3c3fc0e3b9c692ed0c3f02dae16ff"
squid = "0x742acc52f94b86d3a2a4fdc39b1e0d75812f05253f88443754326c1e31cae873"
4 changes: 2 additions & 2 deletions move/test/Move.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "Test"
version = "0.1.0"
published-at = "0x9110bc3d96f77d7ddd6ef058627fb9369c6bfbedfb169228e9d20959a5f893cd"
published-at = "0x64cb77be32074cff87043543664dafb4eb766f980d08694cffca8c8136321872"
edition = "2024.beta"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.25.3" }
AxelarGateway = { local = "../axelar_gateway" }

[addresses]
test = "0x9110bc3d96f77d7ddd6ef058627fb9369c6bfbedfb169228e9d20959a5f893cd"
test = "0x64cb77be32074cff87043543664dafb4eb766f980d08694cffca8c8136321872"
150 changes: 150 additions & 0 deletions test/axelar-gateway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
const { SuiClient, getFullnodeUrl } = require('@mysten/sui.js/client');
const { Ed25519Keypair } = require('@mysten/sui.js/keypairs/ed25519');
const { Secp256k1Keypair } = require('@mysten/sui.js/keypairs/secp256k1');
const { requestSuiFromFaucetV0, getFaucetHost } = require('@mysten/sui.js/faucet');
const { publishPackage, getRandomBytes32 } = require('./utils');
const { TxBuilder } = require('../dist/tx-builder');
const { bcsStructs: { axelarStructs } } = require('../dist/bcs');
const { arrayify, hexlify, keccak256 } = require('ethers/lib/utils');
const secp256k1 = require('secp256k1');

const COMMAND_TYPE_ROTATE_SIGNERS = 1;

describe.only('test', () => {
let client;
const operator = Ed25519Keypair.fromSecretKey(arrayify(getRandomBytes32()));
const deployer = Ed25519Keypair.fromSecretKey(arrayify(getRandomBytes32()));
const keypair = Ed25519Keypair.fromSecretKey(arrayify(getRandomBytes32()));
const domainSeparator = getRandomBytes32();
let operatorKeys;
let signers;
let nonce = 0;
let packageId;


function calculateNextSigners() {
operatorKeys = [getRandomBytes32(), getRandomBytes32(), getRandomBytes32()];
pubkeys = operatorKeys.map(key => Secp256k1Keypair.fromSecretKey(arrayify(key)).getPublicKey().toRawBytes());
const keys = operatorKeys.map((key,index) => { return { privkey: key, pubkey: pubkeys[index]} });
keys.sort((key1, key2) => {
for(let i = 0; i < 33; i ++) {
if (key1.pubkey[i] < key2.pubkey[i]) return -1;
if (key1.pubkey[i] > key2.pubkey[i]) return 1;
}
return 0;
});
operatorKeys = keys.map(key => key.privkey);
signers = {
signers: keys.map((key) => {
return { pubkey: key.pubkey, weight: 1 };
}),
threshold: 2,
nonce: hexlify([++nonce]),
};
}

function hashMessage(data) {
const toHash = new Uint8Array(data.length + 1);
toHash[0] = COMMAND_TYPE_ROTATE_SIGNERS;
toHash.set(data, 1);

return keccak256(toHash);
}

function sign(privKeys, messageToSign) {
const signatures = [];
for(const privKey of privKeys) {
const { signature, recid } = secp256k1.ecdsaSign(arrayify(keccak256(messageToSign)), arrayify(privKey));
signatures.push(new Uint8Array([...signature, recid]));
}
return signatures;
}

async function sleep(ms = 1000) {
await new Promise((resolve) => setTimeout(resolve, ms));
}

const minimumRotationDelay = 1000;
before(async () => {
client = new SuiClient({ url: getFullnodeUrl('localnet') });

await Promise.all([operator, deployer, keypair].map(
(keypair) => requestSuiFromFaucetV0({
host: getFaucetHost('localnet'),
recipient: keypair.toSuiAddress(),
})
))

let result = await publishPackage(client, deployer, 'axelar_gateway');
packageId = result.packageId;
const creatorCap = result.publishTxn.objectChanges.find((change) => change.objectType === `${packageId}::gateway::CreatorCap`).objectId;

calculateNextSigners();

const encodedSigners = axelarStructs.WeightedSigners.serialize(signers).toBytes();
const builder = new TxBuilder(client);

const separator = await builder.moveCall({
target: `${packageId}::bytes32::new`,
arguments: [domainSeparator],
});

await builder.moveCall({
target: `${packageId}::gateway::setup`,
arguments: [
creatorCap,
operator.toSuiAddress(),
separator,
minimumRotationDelay,
encodedSigners,
'0x6',
]
});

result = await builder.signAndExecute(deployer);

gateway = result.objectChanges.find((change) => change.objectType === `${packageId}::gateway::Gateway`).objectId;
});
it('Should Rotate Signers', async () => {
await sleep(2000);
const proofSigners = signers;
const proofKeys = operatorKeys;
calculateNextSigners();

const encodedSigners = axelarStructs.WeightedSigners
.serialize(signers)
.toBytes();

const hashed = hashMessage(encodedSigners);

const message = axelarStructs.MessageToSign
.serialize({
domain_separator: domainSeparator,
signers_hash: keccak256(axelarStructs.WeightedSigners.serialize(proofSigners).toBytes()),
data_hash: hashed,
})
.toBytes();

const signatures = sign(proofKeys, message);
const encodedProof = axelarStructs.Proof
.serialize({
signers: proofSigners,
signatures,
})
.toBytes();

const builder = new TxBuilder(client);

await builder.moveCall({
target: `${packageId}::gateway::rotate_signers`,
arguments: [
gateway,
'0x6',
encodedSigners,
encodedProof,
],
});

await builder.signAndExecute(keypair);
});
});
6 changes: 6 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { keccak256, defaultAbiCoder } = require('ethers/lib/utils');
const { TxBuilder } = require('../dist/tx-builder');
const { updateMoveToml } = require('../dist/utils');

Expand All @@ -12,6 +13,11 @@ async function publishPackage(client, keypair, packageName) {
return { packageId, publishTxn };
}

function getRandomBytes32() {
return keccak256(defaultAbiCoder.encode(['string'], [Math.random().toString()]));
}

module.exports = {
publishPackage,
getRandomBytes32,
};

0 comments on commit b143ef4

Please sign in to comment.