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

enhancement: Change signature of ClientOption.signTransaction to be able to pass KeyPair #1063 #1127

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

od-hunter
Copy link

Change signature of ClientOption.signTransaction to be able to pass KeyPair #1063

Closes #1063

Pull Request Description

Summary

This pull request modifies the ClientOptions type to allow the signTransaction property to accept a Keypair in addition to a function. This change simplifies the process of signing transactions, especially for testing and simple applications, by allowing users to directly use a Keypair without needing to implement a custom signing function.

Changes Made

  1. Updated ClientOptions Type:
  • Modified the signTransaction property to accept either a SignTransaction function or a Keypair.
export type ClientOptions = {
  // ... other properties
  signTransaction?: SignTransaction | Keypair;
  // ... other properties
};
  1. Modified AssembledTransaction Class:
  • Updated the sign method to handle the case where signTransaction is a Keypair.
  • If a Keypair is provided, it uses the basicNodeSigner to create a signing function.
sign = async ({
  force = false,
  signTransaction = this.options.signTransaction,
}: {
  force?: boolean;
  signTransaction?: ClientOptions["signTransaction"];
} = {}): Promise<void> => {
  // ... existing logic

  // Check if signTransaction is a Keypair
  let signFunction: SignTransaction | undefined;

  if (signTransaction instanceof Keypair) {
    const keypair = signTransaction;
    signFunction = basicNodeSigner(keypair, this.options.networkPassphrase).signTransaction;
  } else if (typeof signTransaction === 'function') {
    signFunction = signTransaction;
  }
  // ... existing logic
    const signOpts: Parameters<SignTransaction>[1] = {
      networkPassphrase: this.options.networkPassphrase,
    };
};
  1. Type Safety:
  • Ensured that TypeScript correctly infers types when preparing signing options.

@od-hunter
Copy link
Author

Hi @chadoh , @Shaptic please review.

@od-hunter
Copy link
Author

Happy holidays @Shaptic , @chadoh please review 🥲

Copy link
Contributor

@chadoh chadoh left a comment

Choose a reason for hiding this comment

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

This looks pretty good! Please update at least some of the e2e tests to show that it works. If it's easiest, you can change just test/e2e/src/util.ts to pass the keypair instead of using basicNodeSigner, which will update all the tests. But then we will need one new test using basicNodeSigner to ensure that both approaches continue to work.

@od-hunter
Copy link
Author

@chadoh changes implemented.

@od-hunter
Copy link
Author

Hi @chadoh , please review, change has been implemented.

@od-hunter od-hunter marked this pull request as draft January 5, 2025 18:01
@od-hunter od-hunter marked this pull request as ready for review January 5, 2025 18:02
@od-hunter
Copy link
Author

Hi @chadoh , please review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Needs Review
Development

Successfully merging this pull request may close these issues.

Change signature of ClientOption.signTransaction to be able to pass KeyPair
2 participants