@@ -31,7 +31,7 @@ import {
31
31
import { SolanaSendSigner } from "@wormhole-foundation/sdk-solana" ;
32
32
import { signAndSendWait } from "@wormhole-foundation/sdk-connect" ;
33
33
import { Chain , contracts } from "@wormhole-foundation/sdk-base" ;
34
- import { Program , utils } from "@coral-xyz/anchor" ;
34
+ import { AnchorError , Program , utils } from "@coral-xyz/anchor" ;
35
35
import { ExternalProgram } from "./artifacts/external_program.ts" ;
36
36
import externalProgramIdl from "./artifacts/external_program.json" ;
37
37
import BN from "bn.js" ;
@@ -72,6 +72,17 @@ describe("receive_message", () => {
72
72
let messageExecutor : PublicKey ;
73
73
let externalProgram : Program < ExternalProgram > ;
74
74
75
+ const confirm = async ( signature : string ) : Promise < string > => {
76
+ const block =
77
+ await stakeConnection . provider . connection . getLatestBlockhash ( ) ;
78
+ await stakeConnection . provider . connection . confirmTransaction ( {
79
+ signature,
80
+ ...block ,
81
+ } ) ;
82
+
83
+ return signature ;
84
+ } ;
85
+
75
86
before ( async ( ) => {
76
87
// Read the Anchor configuration from the specified path
77
88
const config = readAnchorConfig ( ANCHOR_CONFIG_PATH ) ;
@@ -141,6 +152,60 @@ describe("receive_message", () => {
141
152
controller . abort ( ) ;
142
153
} ) ;
143
154
155
+ it ( "should fail if invalid wormhole chain id" , async ( ) => {
156
+ const { messagePayloadBuffer, remainingAccounts } =
157
+ await generateTransferInstruction ( stakeConnection , payer , BigInt ( 2 ) ) ;
158
+
159
+ // Generate the VAA
160
+ const { publicKey, hash } = await postReceiveMessageVaa (
161
+ stakeConnection . provider . connection ,
162
+ payer ,
163
+ MOCK_GUARDIANS ,
164
+ Array . from ( Buffer . alloc ( 32 , "f0" , "hex" ) ) ,
165
+ BigInt ( 1 ) ,
166
+ messagePayloadBuffer ,
167
+ { sourceChain : "Ethereum" } ,
168
+ ) ;
169
+
170
+ // Prepare the seeds
171
+ const messageReceivedSeed = Buffer . from ( "message_received" ) ;
172
+ const emitterChainSeed = Buffer . alloc ( 2 ) ;
173
+ emitterChainSeed . writeUInt16BE ( 2 , 0 ) ;
174
+ const emitterAddressSeed = Buffer . alloc ( 32 , "f0" , "hex" ) ;
175
+ const sequenceSeed = Buffer . alloc ( 8 ) ;
176
+ sequenceSeed . writeBigUInt64BE ( BigInt ( 1 ) , 0 ) ;
177
+
178
+ // Prepare PDA for message_received
179
+ const [ messageReceivedPDA ] = PublicKey . findProgramAddressSync (
180
+ [ messageReceivedSeed , emitterChainSeed , emitterAddressSeed , sequenceSeed ] ,
181
+ stakeConnection . program . programId ,
182
+ ) ;
183
+
184
+ try {
185
+ await stakeConnection . program . methods
186
+ . receiveMessage ( )
187
+ . accounts ( {
188
+ payer : payer . publicKey ,
189
+ messageReceived : messageReceivedPDA ,
190
+ airlock : airlockPDA ,
191
+ messageExecutor : messageExecutorPDA ,
192
+ postedVaa : publicKey ,
193
+ wormholeProgram : CORE_BRIDGE_PID ,
194
+ systemProgram : SystemProgram . programId ,
195
+ } )
196
+ . remainingAccounts ( remainingAccounts )
197
+ . signers ( [ payer ] )
198
+ . rpc ( )
199
+ . then ( confirm ) ;
200
+
201
+ assert . fail ( "Expected error was not thrown" ) ;
202
+ } catch ( e ) {
203
+ assert (
204
+ ( e as AnchorError ) . error ?. errorCode ?. code === "InvalidWormholeChainId" ,
205
+ ) ;
206
+ }
207
+ } ) ;
208
+
144
209
it ( "should process receive_message correctly" , async ( ) => {
145
210
const { messagePayloadBuffer, remainingAccounts } =
146
211
await generateTransferInstruction ( stakeConnection , payer ) ;
@@ -467,6 +532,7 @@ describe("receive_message", () => {
467
532
export async function generateTransferInstruction (
468
533
stakeConnection : StakeConnection ,
469
534
payer : Keypair ,
535
+ wormholeChainId : bigint = BigInt ( 1 ) ,
470
536
) : Promise < { messagePayloadBuffer : Buffer ; remainingAccounts : any [ ] } > {
471
537
const recipientKeypair = Keypair . generate ( ) ;
472
538
const lamportsForRecipient =
@@ -511,7 +577,6 @@ export async function generateTransferInstruction(
511
577
512
578
// Prepare the message
513
579
const messageId = BigInt ( 1 ) ;
514
- const wormholeChainId = BigInt ( 1 ) ;
515
580
const instructions = [ instructionData ] ;
516
581
517
582
// Prepare the message without instructionsLength
0 commit comments