From 3c191a25a707656900b9b538379a264f375a8b2f Mon Sep 17 00:00:00 2001 From: Dan Baker Date: Sat, 7 May 2022 13:39:21 -0400 Subject: [PATCH] include 'hello' in data --- src/client/hello_world.ts | 2 +- src/program-rust/src/lib.rs | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/client/hello_world.ts b/src/client/hello_world.ts index 3e33f9e4..65a5deb1 100644 --- a/src/client/hello_world.ts +++ b/src/client/hello_world.ts @@ -203,7 +203,7 @@ export async function sayHello(): Promise { const instruction = new TransactionInstruction({ keys: [{pubkey: greetedPubkey, isSigner: false, isWritable: true}], programId, - data: Buffer.alloc(0), // All instructions are hellos + data: Buffer.from('hello', 'utf8'), // say 'hello' }); await sendAndConfirmTransaction( connection, diff --git a/src/program-rust/src/lib.rs b/src/program-rust/src/lib.rs index 88714a8b..5368db50 100644 --- a/src/program-rust/src/lib.rs +++ b/src/program-rust/src/lib.rs @@ -22,13 +22,22 @@ entrypoint!(process_instruction); pub fn process_instruction( program_id: &Pubkey, // Public key of the account the hello world program was loaded into accounts: &[AccountInfo], // The account to say hello to - _instruction_data: &[u8], // Ignored, all helloworld instructions are hellos + instruction_data: &[u8], // The message from the greeter ) -> ProgramResult { msg!("Hello World Rust program entrypoint"); // Iterating accounts is safer than indexing let accounts_iter = &mut accounts.iter(); + let message_from_sender = String::from_utf8_lossy(instruction_data); + + if message_from_sender != "hello" { + msg!("You Forgot To Say 'hello'"); + msg!("You said '{:?}'",String::from_utf8_lossy(instruction_data)); + + return Err(ProgramError::InvalidInstructionData); + } + // Get the account to say hello to let account = next_account_info(accounts_iter)?;