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

Let anchor-client use any Signer instead of only Keypair #975

Merged
merged 8 commits into from Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -32,6 +32,7 @@ incremented for features.
### Breaking

* lang, ts: Error codes have been mapped to new numbers to allow for more errors per namespace ([#1096](https://github.com/project-serum/anchor/pull/1096)).
* 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)).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to move this into the unrelased section.


## [0.18.2] - 2021-11-14

Expand Down
3 changes: 2 additions & 1 deletion client/example/src/main.rs
Expand Up @@ -22,6 +22,7 @@ use composite::accounts::{Bar, CompositeUpdate, Foo, Initialize};
use composite::instruction as composite_instruction;
use composite::{DummyA, DummyB};
use rand::rngs::OsRng;
use std::rc::Rc;
use std::time::Duration;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -51,7 +52,7 @@ fn main() -> Result<()> {
);

// 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.
composite(&client, opts.composite_pid)?;
Expand Down
27 changes: 16 additions & 11 deletions client/src/lib.rs
Expand Up @@ -13,9 +13,10 @@ use solana_client::rpc_client::RpcClient;
use solana_client::rpc_config::{RpcTransactionLogsConfig, RpcTransactionLogsFilter};
use solana_client::rpc_response::{Response as RpcResponse, RpcLogsResponse};
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 std::convert::Into;
use std::rc::Rc;
use thiserror::Error;

pub use anchor_lang;
Expand All @@ -36,7 +37,7 @@ pub struct Client {
}

impl Client {
pub fn new(cluster: Cluster, payer: Keypair) -> Self {
pub fn new(cluster: Cluster, payer: Rc<dyn Signer>) -> Self {
Self {
cfg: Config {
cluster,
Expand All @@ -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 {
cfg: Config {
cluster,
Expand All @@ -62,7 +67,7 @@ impl Client {
cfg: Config {
cluster: self.cfg.cluster.clone(),
options: self.cfg.options,
payer: Keypair::from_bytes(&self.cfg.payer.to_bytes()).unwrap(),
payer: self.cfg.payer.clone(),
},
}
}
Expand All @@ -71,7 +76,7 @@ impl Client {
// Internal configuration for a client.
struct Config {
cluster: Cluster,
payer: Keypair,
payer: Rc<dyn Signer>,
options: Option<CommitmentConfig>,
}

Expand All @@ -91,7 +96,7 @@ impl Program {
RequestBuilder::from(
self.program_id,
self.cfg.cluster.url(),
Keypair::from_bytes(&self.cfg.payer.to_bytes()).unwrap(),
self.cfg.payer.clone(),
self.cfg.options,
RequestNamespace::Global,
)
Expand All @@ -102,7 +107,7 @@ impl Program {
RequestBuilder::from(
self.program_id,
self.cfg.cluster.url(),
Keypair::from_bytes(&self.cfg.payer.to_bytes()).unwrap(),
self.cfg.payer.clone(),
self.cfg.options,
RequestNamespace::State { new: false },
)
Expand Down Expand Up @@ -321,7 +326,7 @@ pub struct RequestBuilder<'a> {
accounts: Vec<AccountMeta>,
options: CommitmentConfig,
instructions: Vec<Instruction>,
payer: Keypair,
payer: Rc<dyn Signer>,
// Serialized instruction data for the target RPC.
instruction_data: Option<Vec<u8>>,
signers: Vec<&'a dyn Signer>,
Expand All @@ -343,7 +348,7 @@ impl<'a> RequestBuilder<'a> {
pub fn from(
program_id: Pubkey,
cluster: &str,
payer: Keypair,
payer: Rc<dyn Signer>,
options: Option<CommitmentConfig>,
namespace: RequestNamespace,
) -> Self {
Expand All @@ -360,7 +365,7 @@ impl<'a> RequestBuilder<'a> {
}
}

pub fn payer(mut self, payer: Keypair) -> Self {
pub fn payer(mut self, payer: Rc<dyn Signer>) -> Self {
self.payer = payer;
self
}
Expand Down Expand Up @@ -451,7 +456,7 @@ impl<'a> RequestBuilder<'a> {
let instructions = self.instructions()?;

let mut signers = self.signers;
signers.push(&self.payer);
signers.push(&*self.payer);

let rpc_client = RpcClient::new_with_commitment(self.cluster, self.options);

Expand Down