From 4e92d0abb1783c36a9296603fdc40a3e271e30d1 Mon Sep 17 00:00:00 2001 From: wiseaidev Date: Thu, 25 Jul 2024 13:21:19 +0300 Subject: [PATCH 1/3] fix(client): Add support for multiple signers in a single method call --- client/src/blocking.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client/src/blocking.rs b/client/src/blocking.rs index a0522d7ed0..6ad90fe57f 100644 --- a/client/src/blocking.rs +++ b/client/src/blocking.rs @@ -138,8 +138,10 @@ impl<'a, C: Deref + Clone> RequestBuilder<'a, C, Box(mut self, signer: T) -> Self { - self.signers.push(Box::new(signer)); + pub fn signers(mut self, signers: &[T]) -> Self { + for signer in signers { + self.signers.push(Box::new(signer.clone())); + } self } From 84fd8f515126d3da845a189b741aa67dd580e7cb Mon Sep 17 00:00:00 2001 From: wiseaidev Date: Thu, 25 Jul 2024 14:04:16 +0300 Subject: [PATCH 2/3] fix: update examples --- client/example/src/blocking.rs | 8 +++----- client/example/src/nonblocking.rs | 10 ++++------ client/src/lib.rs | 2 +- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/client/example/src/blocking.rs b/client/example/src/blocking.rs index eaf7a01b28..4ba0c93980 100644 --- a/client/example/src/blocking.rs +++ b/client/example/src/blocking.rs @@ -122,8 +122,7 @@ pub fn composite + Clone>( 500, &program.id(), )) - .signer(&dummy_a) - .signer(&dummy_b) + .signers(&[dummy_a, dummy_b]) .accounts(Initialize { dummy_a: dummy_a.pubkey(), dummy_b: dummy_b.pubkey(), @@ -181,7 +180,7 @@ pub fn basic_2 + Clone>( // Build and send a transaction. program .request() - .signer(&counter) + .signers(&[counter]) .accounts(basic_2_accounts::Create { counter: counter.pubkey(), user: authority, @@ -300,8 +299,7 @@ pub fn optional + Clone>( DataAccount::LEN as u64, &program.id(), )) - .signer(&data_account_keypair) - .signer(&required_keypair) + .signers(&[data_account_keypair, required_keypair]) .accounts(OptionalInitialize { payer: Some(program.payer()), required: required_keypair.pubkey(), diff --git a/client/example/src/nonblocking.rs b/client/example/src/nonblocking.rs index 4c7b0fd66b..8146966b43 100644 --- a/client/example/src/nonblocking.rs +++ b/client/example/src/nonblocking.rs @@ -74,7 +74,7 @@ pub async fn test_tokio(client: Client>, pid: Pubkey) -> Result<()> // Build and send a transaction. program .request() - .signer(counter) + .signers(&[counter]) .accounts(basic_2_accounts::Create { counter: counter_pubkey, user: authority, @@ -132,8 +132,7 @@ pub async fn composite + Clone>( 500, &program.id(), )) - .signer(dummy_a.clone()) - .signer(dummy_b.clone()) + .signers(&[dummy_a.clone(), dummy_b.clone()]) .accounts(Initialize { dummy_a: dummy_a.pubkey(), dummy_b: dummy_b.pubkey(), @@ -190,7 +189,7 @@ pub async fn basic_2 + Clone>( // Build and send a transaction. program .request() - .signer(counter.clone()) + .signers(&[counter.clone()]) .accounts(basic_2_accounts::Create { counter: counter.pubkey(), user: authority, @@ -313,8 +312,7 @@ pub async fn optional + Clone>( DataAccount::LEN as u64, &program.id(), )) - .signer(data_account_keypair.clone()) - .signer(required_keypair.clone()) + .signer(&[required_keypair.clone(), data_account_keypair.clone()]) .accounts(OptionalInitialize { payer: Some(program.payer()), required: required_keypair.pubkey(), diff --git a/client/src/lib.rs b/client/src/lib.rs index 59d21e6995..b7202e2aa1 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -37,7 +37,7 @@ //! system_program: system_program::ID, //! }) //! .args(instruction::Initialize { field: 42 }) -//! .signer(&my_account_kp) +//! .signers(&[my_account_kp]) //! .send()?; //! //! // Fetch an account From 9b0757ede5e0639d258cf6d6df78c7d0f9fbe0ad Mon Sep 17 00:00:00 2001 From: wiseaidev Date: Thu, 25 Jul 2024 17:17:43 +0300 Subject: [PATCH 3/3] fix: refactor `signers` method --- client/example/src/blocking.rs | 16 ++++++++-------- client/example/src/nonblocking.rs | 2 +- client/src/blocking.rs | 12 +++++++----- client/src/nonblocking.rs | 10 +++++++--- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/client/example/src/blocking.rs b/client/example/src/blocking.rs index 4ba0c93980..f7d45bcce2 100644 --- a/client/example/src/blocking.rs +++ b/client/example/src/blocking.rs @@ -102,8 +102,8 @@ pub fn composite + Clone>( let program = client.program(pid)?; // `Initialize` parameters. - let dummy_a = Keypair::new(); - let dummy_b = Keypair::new(); + let dummy_a = Arc::new(Keypair::new()); + let dummy_b = Arc::new(Keypair::new()); // Build and send a transaction. program @@ -122,7 +122,7 @@ pub fn composite + Clone>( 500, &program.id(), )) - .signers(&[dummy_a, dummy_b]) + .signers(&[dummy_a.clone(), dummy_b.clone()]) .accounts(Initialize { dummy_a: dummy_a.pubkey(), dummy_b: dummy_b.pubkey(), @@ -174,13 +174,13 @@ pub fn basic_2 + Clone>( let program = client.program(pid)?; // `Create` parameters. - let counter = Keypair::new(); + let counter = Arc::new(Keypair::new()); let authority = program.payer(); // Build and send a transaction. program .request() - .signers(&[counter]) + .signers(&[counter.clone()]) .accounts(basic_2_accounts::Create { counter: counter.pubkey(), user: authority, @@ -277,13 +277,13 @@ pub fn optional + Clone>( let program = client.program(pid)?; // `Initialize` parameters. - let data_account_keypair = Keypair::new(); + let data_account_keypair = Arc::new(Keypair::new()); let data_account_key = data_account_keypair.pubkey(); let data_pda_seeds = &[DataPda::PREFIX.as_ref(), data_account_key.as_ref()]; let data_pda_key = Pubkey::find_program_address(data_pda_seeds, &pid).0; - let required_keypair = Keypair::new(); + let required_keypair = Arc::new(Keypair::new()); let value: u64 = 10; // Build and send a transaction. @@ -299,7 +299,7 @@ pub fn optional + Clone>( DataAccount::LEN as u64, &program.id(), )) - .signers(&[data_account_keypair, required_keypair]) + .signers(&[data_account_keypair.clone(), required_keypair.clone()]) .accounts(OptionalInitialize { payer: Some(program.payer()), required: required_keypair.pubkey(), diff --git a/client/example/src/nonblocking.rs b/client/example/src/nonblocking.rs index 8146966b43..d5430e5bff 100644 --- a/client/example/src/nonblocking.rs +++ b/client/example/src/nonblocking.rs @@ -312,7 +312,7 @@ pub async fn optional + Clone>( DataAccount::LEN as u64, &program.id(), )) - .signer(&[required_keypair.clone(), data_account_keypair.clone()]) + .signers(&[required_keypair.clone(), data_account_keypair.clone()]) .accounts(OptionalInitialize { payer: Some(program.payer()), required: required_keypair.pubkey(), diff --git a/client/src/blocking.rs b/client/src/blocking.rs index 6ad90fe57f..0bfb759281 100644 --- a/client/src/blocking.rs +++ b/client/src/blocking.rs @@ -137,11 +137,13 @@ impl<'a, C: Deref + Clone> RequestBuilder<'a, C, Box(mut self, signers: &[T]) -> Self { - for signer in signers { - self.signers.push(Box::new(signer.clone())); - } + #[must_use = "The signers must be provided to ensure the transaction is valid and authorized."] + pub fn signers(mut self, signers: &'a [T]) -> Self { + self.signers.extend( + signers + .iter() + .map(|signer| Box::new(signer) as Box), + ); self } diff --git a/client/src/nonblocking.rs b/client/src/nonblocking.rs index 317bc0775a..e5c9e6bf61 100644 --- a/client/src/nonblocking.rs +++ b/client/src/nonblocking.rs @@ -141,9 +141,13 @@ impl<'a, C: Deref + Clone> RequestBuilder<'a, C, Arc(mut self, signer: T) -> Self { - self.signers.push(Arc::new(signer)); + #[must_use = "The signers must be provided to ensure the transaction is valid and authorized."] + pub fn signers<'b, T: ThreadSafeSigner + Clone + 'static>(mut self, signers: &'b [T]) -> Self { + self.signers.extend( + signers + .iter() + .map(|signer| Arc::new(signer.clone()) as Arc), + ); self }