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

[solana] Add check wormhole chain id #222

Merged
merged 1 commit into from
Jan 8, 2025
Merged
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
2 changes: 2 additions & 0 deletions solana/programs/staking/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,6 @@ pub enum MessageExecutorError {
VaaNotFinalized,
#[msg("Missing Remaining account")]
MissedRemainingAccount,
#[msg("Message is not meant for this chain")]
InvalidWormholeChainId,
}
6 changes: 6 additions & 0 deletions solana/programs/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,12 @@ pub mod staking {
bump: ctx.bumps.message_received,
});

require!(
posted_vaa.payload.1.wormhole_chain_id.clone()
== ctx.accounts.message_executor.spoke_chain_id.clone(),
MessageExecutorError::InvalidWormholeChainId
);

// Execute the instructions in the message.
for instruction in posted_vaa.payload.1.instructions.clone() {
// Prepare AccountInfo vector for the instruction.
Expand Down
69 changes: 67 additions & 2 deletions solana/tests/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
import { SolanaSendSigner } from "@wormhole-foundation/sdk-solana";
import { signAndSendWait } from "@wormhole-foundation/sdk-connect";
import { Chain, contracts } from "@wormhole-foundation/sdk-base";
import { Program, utils } from "@coral-xyz/anchor";
import { AnchorError, Program, utils } from "@coral-xyz/anchor";
import { ExternalProgram } from "./artifacts/external_program.ts";
import externalProgramIdl from "./artifacts/external_program.json";
import BN from "bn.js";
Expand Down Expand Up @@ -72,6 +72,17 @@ describe("receive_message", () => {
let messageExecutor: PublicKey;
let externalProgram: Program<ExternalProgram>;

const confirm = async (signature: string): Promise<string> => {
const block =
await stakeConnection.provider.connection.getLatestBlockhash();
await stakeConnection.provider.connection.confirmTransaction({
signature,
...block,
});

return signature;
};

before(async () => {
// Read the Anchor configuration from the specified path
const config = readAnchorConfig(ANCHOR_CONFIG_PATH);
Expand Down Expand Up @@ -141,6 +152,60 @@ describe("receive_message", () => {
controller.abort();
});

it("should fail if invalid wormhole chain id", async () => {
const { messagePayloadBuffer, remainingAccounts } =
await generateTransferInstruction(stakeConnection, payer, BigInt(2));

// Generate the VAA
const { publicKey, hash } = await postReceiveMessageVaa(
stakeConnection.provider.connection,
payer,
MOCK_GUARDIANS,
Array.from(Buffer.alloc(32, "f0", "hex")),
BigInt(1),
messagePayloadBuffer,
{ sourceChain: "Ethereum" },
);

// Prepare the seeds
const messageReceivedSeed = Buffer.from("message_received");
const emitterChainSeed = Buffer.alloc(2);
emitterChainSeed.writeUInt16BE(2, 0);
const emitterAddressSeed = Buffer.alloc(32, "f0", "hex");
const sequenceSeed = Buffer.alloc(8);
sequenceSeed.writeBigUInt64BE(BigInt(1), 0);

// Prepare PDA for message_received
const [messageReceivedPDA] = PublicKey.findProgramAddressSync(
[messageReceivedSeed, emitterChainSeed, emitterAddressSeed, sequenceSeed],
stakeConnection.program.programId,
);

try {
await stakeConnection.program.methods
.receiveMessage()
.accounts({
payer: payer.publicKey,
messageReceived: messageReceivedPDA,
airlock: airlockPDA,
messageExecutor: messageExecutorPDA,
postedVaa: publicKey,
wormholeProgram: CORE_BRIDGE_PID,
systemProgram: SystemProgram.programId,
})
.remainingAccounts(remainingAccounts)
.signers([payer])
.rpc()
.then(confirm);

assert.fail("Expected error was not thrown");
} catch (e) {
assert(
(e as AnchorError).error?.errorCode?.code === "InvalidWormholeChainId",
);
}
});

it("should process receive_message correctly", async () => {
const { messagePayloadBuffer, remainingAccounts } =
await generateTransferInstruction(stakeConnection, payer);
Expand Down Expand Up @@ -467,6 +532,7 @@ describe("receive_message", () => {
export async function generateTransferInstruction(
stakeConnection: StakeConnection,
payer: Keypair,
wormholeChainId: bigint = BigInt(1),
): Promise<{ messagePayloadBuffer: Buffer; remainingAccounts: any[] }> {
const recipientKeypair = Keypair.generate();
const lamportsForRecipient =
Expand Down Expand Up @@ -511,7 +577,6 @@ export async function generateTransferInstruction(

// Prepare the message
const messageId = BigInt(1);
const wormholeChainId = BigInt(1);
const instructions = [instructionData];

// Prepare the message without instructionsLength
Expand Down
Loading