Browse Source

client: Add `DynSigner` (#2550)

chalda 2 năm trước cách đây
mục cha
commit
401d526150
2 tập tin đã thay đổi với 31 bổ sung0 xóa
  1. 1 0
      CHANGELOG.md
  2. 30 0
      client/src/lib.rs

+ 1 - 0
CHANGELOG.md

@@ -13,6 +13,7 @@ The minor version will be incremented upon a breaking change and the patch versi
 ### Features
 ### Features
 
 
 - lang: Add `get_lamports`, `add_lamports` and `sub_lamports` methods for all account types ([#2552](https://github.com/coral-xyz/anchor/pull/2552)).
 - lang: Add `get_lamports`, `add_lamports` and `sub_lamports` methods for all account types ([#2552](https://github.com/coral-xyz/anchor/pull/2552)).
+- client: Add a helper struct `DynSigner` to simplify use of `Client<C> where <C: Clone + Deref<Target = impl Signer>>` with Solana clap CLI utils that loads `Signer` as `Box<dyn Signer>` ([#2550](https://github.com/coral-xyz/anchor/pull/2550)).
 
 
 ### Fixes
 ### Fixes
 
 

+ 30 - 0
client/src/lib.rs

@@ -98,6 +98,36 @@ impl<C: Clone + Deref<Target = impl Signer>> Client<C> {
     }
     }
 }
 }
 
 
+/// Auxiliary data structure to align the types of the Solana CLI utils with Anchor client.
+/// Client<C> implementation requires <C: Clone + Deref<Target = impl Signer>> which does not comply with Box<dyn Signer>
+/// that's used when loaded Signer from keypair file. This struct is used to wrap the usage.
+pub struct DynSigner(pub Arc<dyn Signer>);
+
+impl Signer for DynSigner {
+    fn pubkey(&self) -> Pubkey {
+        self.0.pubkey()
+    }
+
+    fn try_pubkey(&self) -> Result<Pubkey, solana_sdk::signer::SignerError> {
+        self.0.try_pubkey()
+    }
+
+    fn sign_message(&self, message: &[u8]) -> solana_sdk::signature::Signature {
+        self.0.sign_message(message)
+    }
+
+    fn try_sign_message(
+        &self,
+        message: &[u8],
+    ) -> Result<solana_sdk::signature::Signature, solana_sdk::signer::SignerError> {
+        self.0.try_sign_message(message)
+    }
+
+    fn is_interactive(&self) -> bool {
+        self.0.is_interactive()
+    }
+}
+
 // Internal configuration for a client.
 // Internal configuration for a client.
 #[derive(Debug)]
 #[derive(Debug)]
 pub struct Config<C> {
 pub struct Config<C> {