From 5429380c2f81337bb230e6fc015bcd026f5b0ac0 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Tue, 1 Oct 2024 13:36:16 +0300 Subject: [PATCH 01/17] Add signTransaction() to BaseWalletBehaviour interface --- packages/core/src/lib/wallet/wallet.types.ts | 62 ++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index 9ef2c7cae..7a6e57bf4 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -10,6 +10,11 @@ import type { ReadOnlyStore } from "../store.types"; import type { Transaction, Action } from "./transactions.types"; import type { Modify, Optional } from "../utils.types"; import type { FinalExecutionOutcome } from "near-api-js/lib/providers"; +import type { + SignedTransaction, + Transaction as Tx, +} from "near-api-js/lib/transaction"; +import type { Signer } from "near-api-js/lib/signer"; interface BaseWalletMetadata { /** @@ -121,6 +126,56 @@ interface SignAndSendTransactionsParams { transactions: Array>; } +interface SignTransactionParams { + /** + * The Transaction object to sign + */ + transaction: Tx; + /** + * The {Signer} object that assists with signing keys + */ + signer: Signer; + /** + * The human-readable NEAR account name + */ + accountId?: string; + /** + * The targeted network. (ex. default, betanet, etc…) + */ + networkId?: string; +} + +interface SignTransactionActionsParams { + /** + * The NEAR account ID of the transaction receiver. + */ + receiverId: string; + /** + * Tx nonce. + */ + nonce: bigint; + /** + * NEAR Action(s) to sign and send to the network (e.g. `FunctionCall`). You can find more information on `Action` {@link https://github.com/near/wallet-selector/blob/main/packages/core/docs/api/transactions.md | here}. + */ + actions: Array; + /** + * Block hash + */ + blockHash: Uint8Array; + /** + * The {Signer} object that assists with signing keys + */ + signer: Signer; + /** + * The human-readable NEAR account name + */ + accountId?: string; + /** + * The targeted network. (ex. default, betanet, etc…) + */ + networkId?: string; +} + interface BaseWalletBehaviour { /** * Programmatically sign in. Hardware wallets (e.g. Ledger) require `derivationPaths` to validate access key permissions. @@ -155,6 +210,13 @@ interface BaseWalletBehaviour { params: SignAndSendTransactionsParams ): Promise>; signMessage?(params: SignMessageParams): Promise; + /** + * Signs one or more NEAR Actions which can be broadcasted to the network. + * The user must be signed in to call this method. + */ + signTransaction( + params: SignTransactionParams | SignTransactionActionsParams + ): Promise<[Uint8Array, SignedTransaction]>; } type BaseWallet< From 2676f27fab7034d7ce4c1f7325dd8e18d190350e Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Tue, 1 Oct 2024 13:56:06 +0300 Subject: [PATCH 02/17] make signTransaction optional --- packages/core/src/lib/wallet/wallet.types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index 7a6e57bf4..bd22218d8 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -214,7 +214,7 @@ interface BaseWalletBehaviour { * Signs one or more NEAR Actions which can be broadcasted to the network. * The user must be signed in to call this method. */ - signTransaction( + signTransaction?( params: SignTransactionParams | SignTransactionActionsParams ): Promise<[Uint8Array, SignedTransaction]>; } From 55623dff6e0c474f8ddc9407710a829089642f51 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Tue, 1 Oct 2024 15:09:40 +0300 Subject: [PATCH 03/17] Add signTransaction in decorateWallet() --- .../services/wallet-modules/wallet-modules.service.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) 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 df954b930..298ba3666 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 @@ -301,6 +301,7 @@ export class WalletModules { const _signIn = wallet.signIn; const _signOut = wallet.signOut; const _signMessage = wallet.signMessage; + const _signTransaction = wallet.signTransaction; wallet.signIn = async (params: never) => { const accounts = await _signIn(params); @@ -332,6 +333,16 @@ export class WalletModules { return await _signMessage(params); }; + wallet.signTransaction = async (params: never) => { + if (_signTransaction === undefined) { + throw Error( + `The signTransaction method is not supported by ${wallet.metadata.name}` + ); + } + + return await _signTransaction(params); + }; + return wallet; } From 7465b61b87592da05a77ddd8e051c1c33ec27b5f Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Wed, 2 Oct 2024 10:18:00 +0300 Subject: [PATCH 04/17] Refactor signTransaction --- .../wallet-modules/wallet-modules.service.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 298ba3666..7202a7525 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 @@ -335,12 +335,20 @@ export class WalletModules { wallet.signTransaction = async (params: never) => { if (_signTransaction === undefined) { - throw Error( + throw new Error( `The signTransaction method is not supported by ${wallet.metadata.name}` ); } - return await _signTransaction(params); + try { + return await _signTransaction(params); + } catch (error) { + throw new Error( + `Failed to sign transaction: ${ + error instanceof Error ? error.message : String(error) + }` + ); + } }; return wallet; From de49ad1caf829cb507ad58acd2180d0959e2265c Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Wed, 16 Oct 2024 15:25:48 +0300 Subject: [PATCH 05/17] Add signDelegateAction to BaseWalletBehaviour --- .../wallet-modules/wallet-modules.service.ts | 19 ++++++ packages/core/src/lib/wallet/wallet.types.ts | 64 +++++++++++++++++++ 2 files changed, 83 insertions(+) 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 7202a7525..409cda0e7 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 _signDelegateAction = wallet.signDelegateAction; wallet.signIn = async (params: never) => { const accounts = await _signIn(params); @@ -351,6 +352,24 @@ export class WalletModules { } }; + wallet.signDelegateAction = async (params: never) => { + if (_signDelegateAction === undefined) { + throw new Error( + `The signDelegateAction method is not supported by ${wallet.metadata.name}` + ); + } + + try { + return await _signDelegateAction(params); + } catch (error) { + throw new Error( + `Failed to sign delegate action: ${ + error instanceof Error ? error.message : String(error) + }` + ); + } + }; + return wallet; } diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index bd22218d8..454189e3e 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -13,8 +13,10 @@ import type { FinalExecutionOutcome } from "near-api-js/lib/providers"; import type { SignedTransaction, Transaction as Tx, + Signature, } from "near-api-js/lib/transaction"; import type { Signer } from "near-api-js/lib/signer"; +import type { PublicKey } from "near-api-js/lib/utils"; interface BaseWalletMetadata { /** @@ -176,6 +178,62 @@ interface SignTransactionActionsParams { networkId?: string; } +interface MessageSigner { + sign(message: Uint8Array): Promise; +} + +interface DelegateAction { + /** + * Account ID for the intended signer of the delegate action + */ + senderId: string; + /** + * The set of actions to be included in the meta transaction + */ + actions: Array; + /** + * Number of blocks past the current block height for which the SignedDelegate action may be included in a meta transaction + */ + blockHeightTtl: number; + /** + * Account ID for the intended receiver of the meta transaction + */ + receiverId: string; + /** + * Current nonce on the access key used to sign the delegate action + */ + nonce: bigint; + /** + * The maximum block height for which this action can be executed as part of a transaction + */ + maxBlockHeight: bigint; + /** + * Public key for the access key used to sign the delegate action + */ + publicKey: PublicKey; +} + +interface SignDelegateOptions { + /** + * Delegate action to be signed by the meta transaction sender + */ + delegateAction: DelegateAction; + /** + * Signer instance for the meta transaction sender + */ + signer: MessageSigner; +} + +interface SignedDelegate { + delegateAction: DelegateAction; + signature: Signature; +} + +interface SignedDelegateWithHash { + hash: Uint8Array; + signedDelegateAction: SignedDelegate; +} + interface BaseWalletBehaviour { /** * Programmatically sign in. Hardware wallets (e.g. Ledger) require `derivationPaths` to validate access key permissions. @@ -217,6 +275,12 @@ interface BaseWalletBehaviour { signTransaction?( params: SignTransactionParams | SignTransactionActionsParams ): Promise<[Uint8Array, SignedTransaction]>; + /** + * Composes and signs a SignedDelegate action to be executed in a transaction + */ + signDelegateAction?( + params: SignDelegateOptions + ): Promise; } type BaseWallet< From 1fd5864e354b3a49137020a49e80cd4e3cc67736 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Tue, 29 Oct 2024 15:45:11 +0200 Subject: [PATCH 06/17] Update signTransaction interface --- packages/core/src/lib/wallet/wallet.types.ts | 60 +++----------------- 1 file changed, 8 insertions(+), 52 deletions(-) diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index 454189e3e..fffb5abbc 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -12,10 +12,8 @@ import type { Modify, Optional } from "../utils.types"; import type { FinalExecutionOutcome } from "near-api-js/lib/providers"; import type { SignedTransaction, - Transaction as Tx, Signature, } from "near-api-js/lib/transaction"; -import type { Signer } from "near-api-js/lib/signer"; import type { PublicKey } from "near-api-js/lib/utils"; interface BaseWalletMetadata { @@ -129,45 +127,14 @@ interface SignAndSendTransactionsParams { } interface SignTransactionParams { - /** - * The Transaction object to sign - */ - transaction: Tx; - /** - * The {Signer} object that assists with signing keys - */ - signer: Signer; - /** - * The human-readable NEAR account name - */ - accountId?: string; - /** - * The targeted network. (ex. default, betanet, etc…) - */ - networkId?: string; -} - -interface SignTransactionActionsParams { /** * The NEAR account ID of the transaction receiver. */ receiverId: string; - /** - * Tx nonce. - */ - nonce: bigint; /** * NEAR Action(s) to sign and send to the network (e.g. `FunctionCall`). You can find more information on `Action` {@link https://github.com/near/wallet-selector/blob/main/packages/core/docs/api/transactions.md | here}. */ actions: Array; - /** - * Block hash - */ - blockHash: Uint8Array; - /** - * The {Signer} object that assists with signing keys - */ - signer: Signer; /** * The human-readable NEAR account name */ @@ -176,10 +143,7 @@ interface SignTransactionActionsParams { * The targeted network. (ex. default, betanet, etc…) */ networkId?: string; -} - -interface MessageSigner { - sign(message: Uint8Array): Promise; + callbackUrl?: string; } interface DelegateAction { @@ -213,17 +177,6 @@ interface DelegateAction { publicKey: PublicKey; } -interface SignDelegateOptions { - /** - * Delegate action to be signed by the meta transaction sender - */ - delegateAction: DelegateAction; - /** - * Signer instance for the meta transaction sender - */ - signer: MessageSigner; -} - interface SignedDelegate { delegateAction: DelegateAction; signature: Signature; @@ -273,14 +226,17 @@ interface BaseWalletBehaviour { * The user must be signed in to call this method. */ signTransaction?( - params: SignTransactionParams | SignTransactionActionsParams - ): Promise<[Uint8Array, SignedTransaction]>; + params: SignTransactionParams + ): Promise<[Uint8Array, SignedTransaction] | void>; /** * Composes and signs a SignedDelegate action to be executed in a transaction */ signDelegateAction?( - params: SignDelegateOptions - ): Promise; + senderId: string, + receiverId: string, + actions: Array, + callbackUrl?: string + ): Promise; } type BaseWallet< From e5aa0c5ae0a1125a248a33bd2e76c50e212b0f52 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Mon, 11 Nov 2024 14:45:33 +0200 Subject: [PATCH 07/17] fix signDelegateAction params --- packages/core/src/lib/wallet/wallet.types.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index fffb5abbc..38bb8d599 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -10,10 +10,7 @@ import type { ReadOnlyStore } from "../store.types"; import type { Transaction, Action } from "./transactions.types"; import type { Modify, Optional } from "../utils.types"; import type { FinalExecutionOutcome } from "near-api-js/lib/providers"; -import type { - SignedTransaction, - Signature, -} from "near-api-js/lib/transaction"; +import type { SignedTransaction, Signature } from "near-api-js/lib/transaction"; import type { PublicKey } from "near-api-js/lib/utils"; interface BaseWalletMetadata { @@ -146,6 +143,13 @@ interface SignTransactionParams { callbackUrl?: string; } +interface SignDelegateActionParams { + senderId: string; + receiverId: string; + actions: Array; + callbackUrl?: string; +} + interface DelegateAction { /** * Account ID for the intended signer of the delegate action @@ -232,10 +236,7 @@ interface BaseWalletBehaviour { * Composes and signs a SignedDelegate action to be executed in a transaction */ signDelegateAction?( - senderId: string, - receiverId: string, - actions: Array, - callbackUrl?: string + params: SignDelegateActionParams ): Promise; } From 3f94ddf607968651726f7a93e104dd2cbdda707b Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Tue, 19 Nov 2024 15:27:31 +0200 Subject: [PATCH 08/17] Add signAndSendTransactionAsync interface --- packages/core/docs/api/transactions.md | 2 +- packages/core/docs/api/wallet.md | 39 ++++++++++++++++++++ packages/core/docs/guides/custom-wallets.md | 21 +++++++++++ packages/core/src/lib/wallet/wallet.types.ts | 8 ++++ 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/packages/core/docs/api/transactions.md b/packages/core/docs/api/transactions.md index 6ae9e8d46..fe7333fef 100644 --- a/packages/core/docs/api/transactions.md +++ b/packages/core/docs/api/transactions.md @@ -14,7 +14,7 @@ interface Transaction { ### Actions -Below are the 8 supported NEAR Actions used by `signAndSendTransaction` and `signAndSendTransactions`: +Below are the 8 supported NEAR Actions used by `signAndSendTransaction`, `signAndSendTransactions` and `signAndSendTransactionAsync`: ```ts interface CreateAccountAction { diff --git a/packages/core/docs/api/wallet.md b/packages/core/docs/api/wallet.md index c2295e0bc..d182efd22 100644 --- a/packages/core/docs/api/wallet.md +++ b/packages/core/docs/api/wallet.md @@ -263,6 +263,45 @@ Signs one or more NEAR Actions before sending to the network. The user must be s })(); ``` +### `.signAndSendTransactionAsync(params)` + +**Parameters** + +- `params` (`object`) + - `signerId` (`string?`): Account ID used to sign the transaction. Defaults to the first account. + - `receiverId` (`string?`): Account ID to receive the transaction. Defaults to `contractId` defined in `.init`. + - `actions` (`Array`): NEAR Action(s) to sign and send to the network (e.g. `FunctionCall`). You can find more information on `Action` [here](./transactions.md). + - `callbackUrl` (`string?`): Applicable to browser wallets (e.g. MyNearWallet). This the callback url once the transaction is approved. + +**Returns** + +- `Uint8Array | void`: Browser wallets won't return the transaction outcome as they may need to redirect for signing. More details on this can be found [here](https://docs.near.org/api/rpc/transactions#send-transaction-await). + +**Description** + +Signs one or more NEAR Actions before sending to the network. The user must be signed in to call this method as there's at least charges for gas spent. + +> Note: Sender only supports `"FunctionCall"` action types right now. If you wish to use other NEAR Actions in your dApp, it's recommended to remove this wallet in your configuration. + +**Example** + +```ts +(async () => { + const wallet = await selector.wallet("sender"); + const txHash = wallet.signAndSendTransactionAsync({ + actions: [{ + type: "FunctionCall", + params: { + methodName: "addMessage", + args: { text: "Hello World!" }, + gas: "30000000000000", + deposit: "10000000000000000000000", + } + }] + }); +})(); +``` + ### `.signAndSendTransactions(params)` **Parameters** diff --git a/packages/core/docs/guides/custom-wallets.md b/packages/core/docs/guides/custom-wallets.md index 59d27f9d2..d88da4b49 100644 --- a/packages/core/docs/guides/custom-wallets.md +++ b/packages/core/docs/guides/custom-wallets.md @@ -58,6 +58,13 @@ const MyWallet: WalletBehaviourFactory = ({ return provider.sendTransaction(signedTx); }, + async signAndSendTransactionAsync({ signerId, receiverId, actions }) { + // Sign a list of NEAR Actions before sending via an RPC endpoint. + // An RPC provider is injected to make this process easier and configured based on options.network. + + return provider.sendTransaction(signedTx); + }, + async signAndSendTransactions({ transactions }) { // Sign a list of Transactions before sending via an RPC endpoint. // An RPC provider is injected to make this process easier and configured based on options.network. @@ -144,6 +151,12 @@ Where you might have to construct NEAR Transactions and send them yourself, you > 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`. +### `signAndSendTransactionAsync` + +This method is similar to `signAndSendTransaction` but instead returns the transaction hash as a `Uint8Array` instead of a `FinalExecutionOutcome`, allowing the users to monitor the transaction success or failure. + +> 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 `Uint8Array`. + ### `signAndSendTransactions` This method is similar to `signAndSendTransaction` but instead sends a batch of Transactions. @@ -154,3 +167,11 @@ This method is similar to `signAndSendTransaction` but instead sends a batch of This method allows users to sign a message for a specific recipient using their NEAR account. Returns the `SignedMessage` based on the [NEP413](https://github.com/near/NEPs/blob/master/neps/nep-0413.md). + +### `signTransaction` + +This method is similar to `signMessage` but instead signs and returns a `SignedTransaction` which can be broadcasted to the network. + +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 diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index 38bb8d599..496e6b4b0 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -217,6 +217,11 @@ interface BaseWalletBehaviour { signAndSendTransaction( params: SignAndSendTransactionParams ): Promise; + /** + * Signs one or more NEAR Actions before sending to the network. + * The user must be signed in to call this method as there's at least charges for gas spent. + */ + signAndSendTransactionAsync(params: SignAndSendTransactionParams): Uint8Array; /** * Signs one or more transactions before sending to the network. * The user must be signed in to call this method as there's at least charges for gas spent. @@ -325,6 +330,9 @@ export type BrowserWalletBehaviour = Modify< signAndSendTransaction( params: BrowserWalletSignAndSendTransactionParams ): Promise; + signAndSendTransactionAsync( + params: BrowserWalletSignAndSendTransactionParams + ): Uint8Array | void; signAndSendTransactions( params: BrowserWalletSignAndSendTransactionsParams ): Promise; From 0e33136aa4da3bf0758d31f1f51e7cfdca960e85 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Tue, 19 Nov 2024 15:33:34 +0200 Subject: [PATCH 09/17] Fix signAndSendTransactionAsync interface --- packages/core/docs/api/wallet.md | 4 ++-- packages/core/docs/guides/custom-wallets.md | 2 +- packages/core/src/lib/wallet/wallet.types.ts | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/core/docs/api/wallet.md b/packages/core/docs/api/wallet.md index d182efd22..65e1fa5a4 100644 --- a/packages/core/docs/api/wallet.md +++ b/packages/core/docs/api/wallet.md @@ -275,7 +275,7 @@ Signs one or more NEAR Actions before sending to the network. The user must be s **Returns** -- `Uint8Array | void`: Browser wallets won't return the transaction outcome as they may need to redirect for signing. More details on this can be found [here](https://docs.near.org/api/rpc/transactions#send-transaction-await). +- `Promise`: Browser wallets won't return the transaction outcome as they may need to redirect for signing. More details on this can be found [here](https://docs.near.org/api/rpc/transactions#send-transaction-await). **Description** @@ -288,7 +288,7 @@ Signs one or more NEAR Actions before sending to the network. The user must be s ```ts (async () => { const wallet = await selector.wallet("sender"); - const txHash = wallet.signAndSendTransactionAsync({ + const txHash = await wallet.signAndSendTransactionAsync({ actions: [{ type: "FunctionCall", params: { diff --git a/packages/core/docs/guides/custom-wallets.md b/packages/core/docs/guides/custom-wallets.md index d88da4b49..6d8d310c9 100644 --- a/packages/core/docs/guides/custom-wallets.md +++ b/packages/core/docs/guides/custom-wallets.md @@ -62,7 +62,7 @@ const MyWallet: WalletBehaviourFactory = ({ // Sign a list of NEAR Actions before sending via an RPC endpoint. // An RPC provider is injected to make this process easier and configured based on options.network. - return provider.sendTransaction(signedTx); + return provider.sendTransactionAsync(signedTx); }, async signAndSendTransactions({ transactions }) { diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index 496e6b4b0..4d513f27a 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -221,7 +221,9 @@ interface BaseWalletBehaviour { * Signs one or more NEAR Actions before sending to the network. * The user must be signed in to call this method as there's at least charges for gas spent. */ - signAndSendTransactionAsync(params: SignAndSendTransactionParams): Uint8Array; + signAndSendTransactionAsync( + params: SignAndSendTransactionParams + ): Promise; /** * Signs one or more transactions before sending to the network. * The user must be signed in to call this method as there's at least charges for gas spent. @@ -332,7 +334,7 @@ export type BrowserWalletBehaviour = Modify< ): Promise; signAndSendTransactionAsync( params: BrowserWalletSignAndSendTransactionParams - ): Uint8Array | void; + ): Promise; signAndSendTransactions( params: BrowserWalletSignAndSendTransactionsParams ): Promise; From 5808436a7681395d99ac37c791864c892dcf906c Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Tue, 19 Nov 2024 15:46:01 +0200 Subject: [PATCH 10/17] Fix build error --- packages/core/src/lib/wallet/wallet.types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index 4d513f27a..8d063b368 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -221,7 +221,7 @@ interface BaseWalletBehaviour { * Signs one or more NEAR Actions before sending to the network. * The user must be signed in to call this method as there's at least charges for gas spent. */ - signAndSendTransactionAsync( + signAndSendTransactionAsync?( params: SignAndSendTransactionParams ): Promise; /** @@ -332,7 +332,7 @@ export type BrowserWalletBehaviour = Modify< signAndSendTransaction( params: BrowserWalletSignAndSendTransactionParams ): Promise; - signAndSendTransactionAsync( + signAndSendTransactionAsync?( params: BrowserWalletSignAndSendTransactionParams ): Promise; signAndSendTransactions( From d0624e0d8526dd6ba86457f5cba0f426329d2bfd Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Wed, 4 Dec 2024 13:02:40 +0200 Subject: [PATCH 11/17] Fix return type --- packages/core/src/lib/wallet/wallet.types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index 8d063b368..3d798077d 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -223,7 +223,7 @@ interface BaseWalletBehaviour { */ signAndSendTransactionAsync?( params: SignAndSendTransactionParams - ): Promise; + ): Promise; /** * Signs one or more transactions before sending to the network. * The user must be signed in to call this method as there's at least charges for gas spent. @@ -334,7 +334,7 @@ export type BrowserWalletBehaviour = Modify< ): Promise; signAndSendTransactionAsync?( params: BrowserWalletSignAndSendTransactionParams - ): Promise; + ): Promise; signAndSendTransactions( params: BrowserWalletSignAndSendTransactionsParams ): Promise; From e5de64bc696aa5048bb68c82a66d0a2cc02b5c75 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Wed, 4 Dec 2024 15:32:53 +0200 Subject: [PATCH 12/17] Add sendTransactionAsync method in Provuder service --- packages/core/src/lib/services/provider/provider.service.ts | 4 ++++ .../core/src/lib/services/provider/provider.service.types.ts | 3 +++ 2 files changed, 7 insertions(+) diff --git a/packages/core/src/lib/services/provider/provider.service.ts b/packages/core/src/lib/services/provider/provider.service.ts index 6cf257b6b..49a296b29 100644 --- a/packages/core/src/lib/services/provider/provider.service.ts +++ b/packages/core/src/lib/services/provider/provider.service.ts @@ -50,6 +50,10 @@ export class Provider implements ProviderService { return this.provider.sendTransaction(signedTransaction); } + sendTransactionAsync(signedTransaction: SignedTransaction) { + return this.provider.sendTransactionAsync(signedTransaction); + } + private urlsToProviders(urls: Array) { return urls && urls.length > 0 ? urls.map((url) => new JsonRpcProvider({ url })) diff --git a/packages/core/src/lib/services/provider/provider.service.types.ts b/packages/core/src/lib/services/provider/provider.service.types.ts index ecc041d93..caf3de22d 100644 --- a/packages/core/src/lib/services/provider/provider.service.types.ts +++ b/packages/core/src/lib/services/provider/provider.service.types.ts @@ -23,4 +23,7 @@ export interface ProviderService { sendTransaction( signedTransaction: SignedTransaction ): Promise; + sendTransactionAsync( + signedTransaction: SignedTransaction + ): Promise; } From f94f9a3c8daa900d0fba51d535c7a806e838ba34 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Thu, 5 Dec 2024 14:56:30 +0200 Subject: [PATCH 13/17] 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; } >; From bce9451c1bab4f44e5bee042dd797c9214ee2483 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Mon, 9 Dec 2024 16:20:26 +0200 Subject: [PATCH 14/17] Update signDelegateAction interface --- .../core/src/lib/wallet/transactions.types.ts | 13 ++++++++++- packages/core/src/lib/wallet/wallet.types.ts | 22 +++++++------------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/packages/core/src/lib/wallet/transactions.types.ts b/packages/core/src/lib/wallet/transactions.types.ts index c62d651e3..3fb731440 100644 --- a/packages/core/src/lib/wallet/transactions.types.ts +++ b/packages/core/src/lib/wallet/transactions.types.ts @@ -67,6 +67,16 @@ export interface DeleteAccountAction { }; } +export interface SignDelegateAction { + type: "SignDelegateAction"; + params: { + blockHeightTtl: number; + receiverId: string; + actions: Array; + callbackUrl?: string; + }; +} + export type Action = | CreateAccountAction | DeployContractAction @@ -75,7 +85,8 @@ export type Action = | StakeAction | AddKeyAction | DeleteKeyAction - | DeleteAccountAction; + | DeleteAccountAction + | SignDelegateAction; export type ActionType = Action["type"]; diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index 1887cad03..20c604067 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -143,8 +143,8 @@ interface SignTransactionParams { callbackUrl?: string; } -interface SignDelegateActionParams { - senderId: string; +export interface SignDelegateActionParams { + blockHeightTtl: number; receiverId: string; actions: Array; callbackUrl?: string; @@ -159,10 +159,6 @@ interface DelegateAction { * The set of actions to be included in the meta transaction */ actions: Array; - /** - * Number of blocks past the current block height for which the SignedDelegate action may be included in a meta transaction - */ - blockHeightTtl: number; /** * Account ID for the intended receiver of the meta transaction */ @@ -181,16 +177,11 @@ interface DelegateAction { publicKey: PublicKey; } -interface SignedDelegate { +export interface SignedDelegate { delegateAction: DelegateAction; signature: Signature; } -interface SignedDelegateWithHash { - hash: Uint8Array; - signedDelegateAction: SignedDelegate; -} - interface BaseWalletBehaviour { /** * Programmatically sign in. Hardware wallets (e.g. Ledger) require `derivationPaths` to validate access key permissions. @@ -248,11 +239,11 @@ interface BaseWalletBehaviour { callbackUrl?: string; }): Promise; /** - * Composes and signs a SignedDelegate action to be executed in a transaction + * Composes and signs a Signed Delegate Action to be executed in a transaction */ signDelegateAction?( params: SignDelegateActionParams - ): Promise; + ): Promise; } type BaseWallet< @@ -354,6 +345,9 @@ export type BrowserWalletBehaviour = Modify< signedTransaction: SignedTransaction; callbackUrl?: string; }): Promise; + signDelegateAction?( + params: SignDelegateActionParams + ): Promise; } >; From 0404fc8e1e854ab0dc5324daccd42e27020ae05f Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Tue, 10 Dec 2024 13:23:30 +0200 Subject: [PATCH 15/17] Fix delegateAction interface --- packages/core/src/lib/wallet/transactions.types.ts | 3 ++- packages/core/src/lib/wallet/wallet.types.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/core/src/lib/wallet/transactions.types.ts b/packages/core/src/lib/wallet/transactions.types.ts index 3fb731440..a8cd37f0e 100644 --- a/packages/core/src/lib/wallet/transactions.types.ts +++ b/packages/core/src/lib/wallet/transactions.types.ts @@ -86,7 +86,8 @@ export type Action = | AddKeyAction | DeleteKeyAction | DeleteAccountAction - | SignDelegateAction; + +export type DelegateAction = Action | SignDelegateAction; export type ActionType = Action["type"]; diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index 20c604067..6bb1e4b0f 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -146,7 +146,7 @@ interface SignTransactionParams { export interface SignDelegateActionParams { blockHeightTtl: number; receiverId: string; - actions: Array; + actions: Array; callbackUrl?: string; } From f57900fcfd34c316e24d10e8e962389715fb352c Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Tue, 10 Dec 2024 13:37:10 +0200 Subject: [PATCH 16/17] Remoev interface SignDelegateAction --- packages/core/src/lib/wallet/transactions.types.ts | 12 ------------ packages/core/src/lib/wallet/wallet.types.ts | 2 +- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/packages/core/src/lib/wallet/transactions.types.ts b/packages/core/src/lib/wallet/transactions.types.ts index a8cd37f0e..9190d2790 100644 --- a/packages/core/src/lib/wallet/transactions.types.ts +++ b/packages/core/src/lib/wallet/transactions.types.ts @@ -67,16 +67,6 @@ export interface DeleteAccountAction { }; } -export interface SignDelegateAction { - type: "SignDelegateAction"; - params: { - blockHeightTtl: number; - receiverId: string; - actions: Array; - callbackUrl?: string; - }; -} - export type Action = | CreateAccountAction | DeployContractAction @@ -87,8 +77,6 @@ export type Action = | DeleteKeyAction | DeleteAccountAction -export type DelegateAction = Action | SignDelegateAction; - export type ActionType = Action["type"]; export interface Transaction { diff --git a/packages/core/src/lib/wallet/wallet.types.ts b/packages/core/src/lib/wallet/wallet.types.ts index 6bb1e4b0f..20c604067 100644 --- a/packages/core/src/lib/wallet/wallet.types.ts +++ b/packages/core/src/lib/wallet/wallet.types.ts @@ -146,7 +146,7 @@ interface SignTransactionParams { export interface SignDelegateActionParams { blockHeightTtl: number; receiverId: string; - actions: Array; + actions: Array; callbackUrl?: string; } From c768b6c4d10a20fc20578fb16f7205ba76a3916e Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Wed, 11 Dec 2024 09:52:55 +0200 Subject: [PATCH 17/17] Fix linter error --- packages/core/src/lib/wallet/transactions.types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/lib/wallet/transactions.types.ts b/packages/core/src/lib/wallet/transactions.types.ts index 9190d2790..c62d651e3 100644 --- a/packages/core/src/lib/wallet/transactions.types.ts +++ b/packages/core/src/lib/wallet/transactions.types.ts @@ -75,7 +75,7 @@ export type Action = | StakeAction | AddKeyAction | DeleteKeyAction - | DeleteAccountAction + | DeleteAccountAction; export type ActionType = Action["type"];