-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
Why dynamic dispatch instead of static? |
Mainly because it retains more backwards compatibility by keeping the struct's parameters simpler. In this implementation I just added a lifetime parameter, but for static dispatch, I would have to add a lifetime parameter and a type parameter. Lifetime parameters can be omitted in some situations where type parameters cannot be omitted. |
Type parameter also can be omitted if we set the default type. Not sure that I understand why we need lifetime in addition to type parameter 🤔 struct Config<S: Signer = Keypair> {
cluster: Cluster,
payer: S,
options: Option<CommitmentConfig>,
} |
That's cool. I wasn't aware of that feature in rust.
The payer needs to be stored somewhere. Client, Program, and RequestBuilder each need access to it, and none of them nest each other. Since Client instantiates Program, and Program instantiates RequestBuilder, either Client needs to own it and the others store a reference, or a broader context owns it and all of them store references to it. Alternatively, the payer could be cloned or copied but that's not ideal because it imposes another type constraint. |
Another issue is that the solana libraries return |
Actually, we don't need to add lifetime parameters if we use smart pointers instead of references. And if we store the payer as The complication is with The solution that fully retains backwards compatibility while also supporting the Solana libraries is to add new constructors to Client that accept a smart pointer while only deprecating the existing functions. The cost is that it clutters the API. What do we value more, backwards compatibility or a clean API? I updated the PR to use |
Clean API. Happy to make a breaking change. |
Alright. I took out the extra constructors I just added, and changed the original ones to be more generic. |
Can you add a CHANGELOG |
I added a note for the breaking change in the unreleased version 0.19.0. Is this sufficient or should I add instructions about how usages need to change? |
The repo has this new requirement that didn't exist when I first created the PR:
The easiest way seems to be squashing all the commits into a single commit and signing that one, then force pushing. So that's what I did just now. But it's the exact same change otherwise. |
CHANGELOG.md
Outdated
@@ -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)). |
There was a problem hiding this comment.
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.
Need to merge master for the build to pass. I tried but I don't have write access to the fork. |
The rust anchor client can only work with a Keypair as the payer, which means the private key must be loaded into any client. This is a deal breaker for a lot of use cases - for example, any use of a hardware wallet. So I only find this library useful for building a proof-of-concept, and I'm forced to fork the library with these changes if I want to build a tool that can be used on mainnet.
The change in this PR allows clients to use any Signer.
EDIT: See discussion below, I changed it to use
Rc<dyn Signer>
instead of references to retain backwards compatibility. The Client constructors break compatibility however so we can have a clean api.Original Comment: