Browse Source

client: use any Signer instead of only Keypair (#975)

Drew Nutter 3 years ago
parent
commit
34c4e50917

+ 1 - 0
CHANGELOG.md

@@ -24,6 +24,7 @@ incremented for features.
 
 
 ### Breaking
 ### Breaking
 
 
+* client: Client::new and Client::new_with_options now accept `Rc<dyn Signer>` instead of `Keypair` ([#975](https://github.com/project-serum/anchor/pull/975)).
 * lang, ts: Change error enum name and message for 'wrong program ownership' account validation ([#1154](https://github.com/project-serum/anchor/pull/1154)).
 * lang, ts: Change error enum name and message for 'wrong program ownership' account validation ([#1154](https://github.com/project-serum/anchor/pull/1154)).
 
 
 ## [0.19.0] - 2021-12-08
 ## [0.19.0] - 2021-12-08

+ 2 - 1
client/example/src/main.rs

@@ -22,6 +22,7 @@ use composite::accounts::{Bar, CompositeUpdate, Foo, Initialize};
 use composite::instruction as composite_instruction;
 use composite::instruction as composite_instruction;
 use composite::{DummyA, DummyB};
 use composite::{DummyA, DummyB};
 use rand::rngs::OsRng;
 use rand::rngs::OsRng;
+use std::rc::Rc;
 use std::time::Duration;
 use std::time::Duration;
 
 
 #[derive(Parser, Debug)]
 #[derive(Parser, Debug)]
@@ -51,7 +52,7 @@ fn main() -> Result<()> {
     );
     );
 
 
     // Client.
     // Client.
-    let client = Client::new_with_options(url, payer, CommitmentConfig::processed());
+    let client = Client::new_with_options(url, Rc::new(payer), CommitmentConfig::processed());
 
 
     // Run tests.
     // Run tests.
     composite(&client, opts.composite_pid)?;
     composite(&client, opts.composite_pid)?;

+ 16 - 11
client/src/lib.rs

@@ -13,9 +13,10 @@ use solana_client::rpc_client::RpcClient;
 use solana_client::rpc_config::{RpcTransactionLogsConfig, RpcTransactionLogsFilter};
 use solana_client::rpc_config::{RpcTransactionLogsConfig, RpcTransactionLogsFilter};
 use solana_client::rpc_response::{Response as RpcResponse, RpcLogsResponse};
 use solana_client::rpc_response::{Response as RpcResponse, RpcLogsResponse};
 use solana_sdk::commitment_config::CommitmentConfig;
 use solana_sdk::commitment_config::CommitmentConfig;
-use solana_sdk::signature::{Keypair, Signature, Signer};
+use solana_sdk::signature::{Signature, Signer};
 use solana_sdk::transaction::Transaction;
 use solana_sdk::transaction::Transaction;
 use std::convert::Into;
 use std::convert::Into;
+use std::rc::Rc;
 use thiserror::Error;
 use thiserror::Error;
 
 
 pub use anchor_lang;
 pub use anchor_lang;
@@ -36,7 +37,7 @@ pub struct Client {
 }
 }
 
 
 impl Client {
 impl Client {
-    pub fn new(cluster: Cluster, payer: Keypair) -> Self {
+    pub fn new(cluster: Cluster, payer: Rc<dyn Signer>) -> Self {
         Self {
         Self {
             cfg: Config {
             cfg: Config {
                 cluster,
                 cluster,
@@ -46,7 +47,11 @@ impl Client {
         }
         }
     }
     }
 
 
-    pub fn new_with_options(cluster: Cluster, payer: Keypair, options: CommitmentConfig) -> Self {
+    pub fn new_with_options(
+        cluster: Cluster,
+        payer: Rc<dyn Signer>,
+        options: CommitmentConfig,
+    ) -> Self {
         Self {
         Self {
             cfg: Config {
             cfg: Config {
                 cluster,
                 cluster,
@@ -62,7 +67,7 @@ impl Client {
             cfg: Config {
             cfg: Config {
                 cluster: self.cfg.cluster.clone(),
                 cluster: self.cfg.cluster.clone(),
                 options: self.cfg.options,
                 options: self.cfg.options,
-                payer: Keypair::from_bytes(&self.cfg.payer.to_bytes()).unwrap(),
+                payer: self.cfg.payer.clone(),
             },
             },
         }
         }
     }
     }
@@ -72,7 +77,7 @@ impl Client {
 #[derive(Debug)]
 #[derive(Debug)]
 struct Config {
 struct Config {
     cluster: Cluster,
     cluster: Cluster,
-    payer: Keypair,
+    payer: Rc<dyn Signer>,
     options: Option<CommitmentConfig>,
     options: Option<CommitmentConfig>,
 }
 }
 
 
@@ -93,7 +98,7 @@ impl Program {
         RequestBuilder::from(
         RequestBuilder::from(
             self.program_id,
             self.program_id,
             self.cfg.cluster.url(),
             self.cfg.cluster.url(),
-            Keypair::from_bytes(&self.cfg.payer.to_bytes()).unwrap(),
+            self.cfg.payer.clone(),
             self.cfg.options,
             self.cfg.options,
             RequestNamespace::Global,
             RequestNamespace::Global,
         )
         )
@@ -104,7 +109,7 @@ impl Program {
         RequestBuilder::from(
         RequestBuilder::from(
             self.program_id,
             self.program_id,
             self.cfg.cluster.url(),
             self.cfg.cluster.url(),
-            Keypair::from_bytes(&self.cfg.payer.to_bytes()).unwrap(),
+            self.cfg.payer.clone(),
             self.cfg.options,
             self.cfg.options,
             RequestNamespace::State { new: false },
             RequestNamespace::State { new: false },
         )
         )
@@ -323,7 +328,7 @@ pub struct RequestBuilder<'a> {
     accounts: Vec<AccountMeta>,
     accounts: Vec<AccountMeta>,
     options: CommitmentConfig,
     options: CommitmentConfig,
     instructions: Vec<Instruction>,
     instructions: Vec<Instruction>,
-    payer: Keypair,
+    payer: Rc<dyn Signer>,
     // Serialized instruction data for the target RPC.
     // Serialized instruction data for the target RPC.
     instruction_data: Option<Vec<u8>>,
     instruction_data: Option<Vec<u8>>,
     signers: Vec<&'a dyn Signer>,
     signers: Vec<&'a dyn Signer>,
@@ -345,7 +350,7 @@ impl<'a> RequestBuilder<'a> {
     pub fn from(
     pub fn from(
         program_id: Pubkey,
         program_id: Pubkey,
         cluster: &str,
         cluster: &str,
-        payer: Keypair,
+        payer: Rc<dyn Signer>,
         options: Option<CommitmentConfig>,
         options: Option<CommitmentConfig>,
         namespace: RequestNamespace,
         namespace: RequestNamespace,
     ) -> Self {
     ) -> Self {
@@ -363,7 +368,7 @@ impl<'a> RequestBuilder<'a> {
     }
     }
 
 
     #[must_use]
     #[must_use]
-    pub fn payer(mut self, payer: Keypair) -> Self {
+    pub fn payer(mut self, payer: Rc<dyn Signer>) -> Self {
         self.payer = payer;
         self.payer = payer;
         self
         self
     }
     }
@@ -462,7 +467,7 @@ impl<'a> RequestBuilder<'a> {
         let instructions = self.instructions()?;
         let instructions = self.instructions()?;
 
 
         let mut signers = self.signers;
         let mut signers = self.signers;
-        signers.push(&self.payer);
+        signers.push(&*self.payer);
 
 
         let rpc_client = RpcClient::new_with_commitment(self.cluster, self.options);
         let rpc_client = RpcClient::new_with_commitment(self.cluster, self.options);
 
 

+ 2 - 1
tests/zero-copy/programs/zero-copy/tests/compute_unit_test.rs

@@ -1,6 +1,7 @@
 #![cfg(feature = "test-bpf")]
 #![cfg(feature = "test-bpf")]
 
 
 use {
 use {
+    std::rc::Rc,
     anchor_client::{
     anchor_client::{
         anchor_lang::Discriminator,
         anchor_lang::Discriminator,
         solana_sdk::{
         solana_sdk::{
@@ -42,7 +43,7 @@ async fn update_foo() {
 
 
     let client = Client::new_with_options(
     let client = Client::new_with_options(
         Cluster::Debug,
         Cluster::Debug,
-        Keypair::new(),
+        Rc::new(Keypair::new()),
         CommitmentConfig::processed(),
         CommitmentConfig::processed(),
     );
     );
     let program = client.program(zero_copy::id());
     let program = client.program(zero_copy::id());