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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/contract/assembled_transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Address,
BASE_FEE,
Contract,
Keypair,
Operation,
SorobanDataBuilder,
TransactionBuilder,
Expand All @@ -15,6 +16,7 @@ import type {
AssembledTransactionOptions,
ClientOptions,
MethodOptions,
SignTransaction,
Tx,
WalletError,
XDR_BASE64,
Expand All @@ -32,6 +34,7 @@ import {
import { DEFAULT_TIMEOUT } from "./types";
import { SentTransaction } from "./sent_transaction";
import { Spec } from "./spec";
import { basicNodeSigner } from './basic_node_signer';

/** @module contract */

Expand Down Expand Up @@ -682,7 +685,17 @@ export class AssembledTransaction<T> {
);
}

if (!signTransaction) {
// 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;
}

if (!signFunction) {
throw new AssembledTransaction.Errors.NoSigner(
"You must provide a signTransaction function, either when calling " +
"`signAndSend` or when initializing your Client"
Expand All @@ -708,15 +721,18 @@ export class AssembledTransaction<T> {
.setTimeout(timeoutInSeconds)
.build();

const signOpts: Parameters<NonNullable<ClientOptions['signTransaction']>>[1] = {
// const signOpts: Parameters<NonNullable<ClientOptions['signTransaction']>>[1] = {
// networkPassphrase: this.options.networkPassphrase,
// };
const signOpts: Parameters<SignTransaction>[1] = {
networkPassphrase: this.options.networkPassphrase,
};

if (this.options.address) signOpts.address = this.options.address;
if (this.options.submit !== undefined) signOpts.submit = this.options.submit;
if (this.options.submitUrl) signOpts.submitUrl = this.options.submitUrl;

const { signedTxXdr: signature, error } = await signTransaction(
const { signedTxXdr: signature, error } = await signFunction(
this.built.toXDR(),
signOpts,
);
Expand Down
3 changes: 2 additions & 1 deletion src/contract/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Memo, MemoType, Operation, Transaction, xdr } from "@stellar/stellar-base";
import type { Client } from "./client";
import { Keypair } from "@stellar/stellar-base";

export type XDR_BASE64 = string;
/**
Expand Down Expand Up @@ -131,7 +132,7 @@ export type ClientOptions = {
*
* Matches signature of `signTransaction` from Freighter.
*/
signTransaction?: SignTransaction;
signTransaction?: SignTransaction | Keypair;
/**
* A function to sign a specific auth entry for a transaction, using the
* private key corresponding to the provided `publicKey`. This is only needed
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/src/test-non-invoker-signing-by-contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ describe("cross-contract auth", function () {
async () => await this.context.tx.sign({ force: true }),
).to.not.throw();
});

it("signs transaction using Keypair", async function () {
const result = await this.context.tx.sign({
signTransaction: this.context.keypair,
force: true,
});
expect(result).to.be.undefined;
});


it("signs transaction using basicNodeSigner", async function () {
const signer = contract.basicNodeSigner(this.context.keypair, "Standalone Network ; February 2017");
const result = await this.context.tx.sign({
signTransaction: signer.signTransaction,
force: true,
});
expect(result).to.be.undefined;
});
});

describe("signAuthEntries with custom authorizeEntry", function () {
Expand Down
8 changes: 5 additions & 3 deletions test/e2e/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ module.exports.generateFundedKeypair = generateFundedKeypair;
*/
async function clientFor(name, { keypair, contractId } = {}) {
const internalKeypair = keypair ?? (await generateFundedKeypair());
const signer = contract.basicNodeSigner(internalKeypair, networkPassphrase);

// Pass the Keypair directly instead of using basicNodeSigner
const signer = internalKeypair; // Use Keypair directly

if (contractId) {
return {
Expand All @@ -99,7 +101,7 @@ async function clientFor(name, { keypair, contractId } = {}) {
rpcUrl,
allowHttp: true,
publicKey: internalKeypair.publicKey(),
...signer,
signTransaction: signer, // Pass Keypair here
}),
contractId,
keypair,
Expand All @@ -118,7 +120,7 @@ async function clientFor(name, { keypair, contractId } = {}) {
allowHttp: true,
wasmHash: wasmHash,
publicKey: internalKeypair.publicKey(),
...signer,
signTransaction: signer, // Pass Keypair here
},
);
const { result: client } = await deploy.signAndSend();
Expand Down