From f94f9a3c8daa900d0fba51d535c7a806e838ba34 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Thu, 5 Dec 2024 14:56:30 +0200 Subject: [PATCH] Add sendTransaction method --- packages/core/docs/guides/custom-wallets.md | 9 ++++++++- .../wallet-modules/wallet-modules.service.ts | 11 +++++++++++ packages/core/src/lib/wallet/wallet.types.ts | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/core/docs/guides/custom-wallets.md b/packages/core/docs/guides/custom-wallets.md index 6d8d310c9..457f0b4de 100644 --- a/packages/core/docs/guides/custom-wallets.md +++ b/packages/core/docs/guides/custom-wallets.md @@ -174,4 +174,11 @@ This method is similar to `signMessage` but instead signs and returns a `SignedT This method composes and signs a SignedDelegate action to be executed in a transaction. Returns the `SignedDelegateWithHash` object. -> Note: Browser wallets (i.e. MyNearWallet) are unable to return the transaction outcome as they can trigger a redirect. The return type in this case is `Promise` instead of the usual `Promise`. \ No newline at end of file +> Note: Browser wallets (i.e. MyNearWallet) are unable to return the transaction outcome as they can trigger a redirect. The return type in this case is `Promise` instead of the usual `Promise`. + + +### `sendTransaction` + +This method sends a previously signed transaction to the network. It takes a transaction hash and a signed transaction object, and optionally a callback URL. Returns a `FinalExecutionOutcome` when successful. + +> Note: Browser wallets (i.e. MyNearWallet) are unable to return the transaction outcome as they can trigger a redirect. The return type in this case is `Promise` instead of the usual `Promise`. diff --git a/packages/core/src/lib/services/wallet-modules/wallet-modules.service.ts b/packages/core/src/lib/services/wallet-modules/wallet-modules.service.ts index 409cda0e7..041c049f6 100644 --- a/packages/core/src/lib/services/wallet-modules/wallet-modules.service.ts +++ b/packages/core/src/lib/services/wallet-modules/wallet-modules.service.ts @@ -302,6 +302,7 @@ export class WalletModules { const _signOut = wallet.signOut; const _signMessage = wallet.signMessage; const _signTransaction = wallet.signTransaction; + const _sendTransaction = wallet.sendTransaction; const _signDelegateAction = wallet.signDelegateAction; wallet.signIn = async (params: never) => { @@ -352,6 +353,16 @@ export class WalletModules { } }; + wallet.sendTransaction = async (signedTransaction: never) => { + if (_sendTransaction === undefined) { + throw new Error( + `The sendTransaction method is not supported by ${wallet.metadata.name}` + ); + } + + return await _sendTransaction(signedTransaction); + }; + wallet.signDelegateAction = async (params: never) => { if (_signDelegateAction === undefined) { throw new Error( diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index 3d798077d..1887cad03 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -239,6 +239,14 @@ interface BaseWalletBehaviour { signTransaction?( params: SignTransactionParams ): Promise<[Uint8Array, SignedTransaction] | void>; + /** + * Sends a signed transaction to the network. + */ + sendTransaction?(params: { + hash: Uint8Array; + signedTransaction: SignedTransaction; + callbackUrl?: string; + }): Promise; /** * Composes and signs a SignedDelegate action to be executed in a transaction */ @@ -338,6 +346,14 @@ export type BrowserWalletBehaviour = Modify< signAndSendTransactions( params: BrowserWalletSignAndSendTransactionsParams ): Promise; + signTransaction?( + params: SignTransactionParams + ): Promise<[Uint8Array, SignedTransaction] | void>; + sendTransaction?(params: { + hash: Uint8Array; + signedTransaction: SignedTransaction; + callbackUrl?: string; + }): Promise; } >;