@solana/react 6.3.1 → 6.3.2-canary-20260313143218

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,168 @@
1
+ import {
2
+ SolanaSignAndSendTransaction,
3
+ SolanaSignAndSendTransactionFeature,
4
+ SolanaSignAndSendTransactionInput,
5
+ SolanaSignAndSendTransactionOutput,
6
+ } from '@solana/wallet-standard-features';
7
+ import {
8
+ WALLET_STANDARD_ERROR__FEATURES__WALLET_ACCOUNT_CHAIN_UNSUPPORTED,
9
+ WalletStandardError,
10
+ } from '@wallet-standard/errors';
11
+ import { getWalletAccountFeature, type UiWalletAccount } from '@wallet-standard/ui';
12
+ import { getWalletAccountForUiWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } from '@wallet-standard/ui-registry';
13
+ import { useCallback } from 'react';
14
+
15
+ import { OnlySolanaChains } from './chain';
16
+
17
+ type Input = Readonly<
18
+ Omit<SolanaSignAndSendTransactionInput, 'account' | 'chain' | 'options'> & {
19
+ options?: Readonly<{
20
+ minContextSlot?: bigint;
21
+ }>;
22
+ }
23
+ >;
24
+ type Output = SolanaSignAndSendTransactionOutput;
25
+
26
+ /**
27
+ * Use this to get a function capable of signing a serialized transaction with the private key of a
28
+ * {@link UiWalletAccount} and sending it to the network for processing.
29
+ *
30
+ * @param chain The identifier of the chain the transaction is destined for. Wallets may use this to
31
+ * simulate the transaction for the user.
32
+ *
33
+ * @example
34
+ * ```tsx
35
+ * import { getBase58Decoder } from '@solana/codecs-strings';
36
+ * import { useSignAndSendTransaction } from '@solana/react';
37
+ *
38
+ * function SignAndSendTransactionButton({ account, transactionBytes }) {
39
+ * const signAndSendTransaction = useSignAndSendTransaction(account, 'solana:devnet');
40
+ * return (
41
+ * <button
42
+ * onClick={async () => {
43
+ * try {
44
+ * const { signature } = await signAndSendTransaction({
45
+ * transaction: transactionBytes,
46
+ * });
47
+ * const base58TransactionSignature = getBase58Decoder().decode(signature);
48
+ * window.alert(
49
+ * `View transaction: https://explorer.solana.com/tx/${base58TransactionSignature}?cluster=devnet`,
50
+ * );
51
+ * } catch (e) {
52
+ * console.error('Failed to send transaction', e);
53
+ * }
54
+ * }}
55
+ * >
56
+ * Sign and Send Transaction
57
+ * </button>
58
+ * );
59
+ * }
60
+ * ```
61
+ */
62
+ export function useSignAndSendTransaction<TWalletAccount extends UiWalletAccount>(
63
+ uiWalletAccount: TWalletAccount,
64
+ chain: OnlySolanaChains<TWalletAccount['chains']>,
65
+ ): (input: Input) => Promise<Output>;
66
+ export function useSignAndSendTransaction<TWalletAccount extends UiWalletAccount>(
67
+ uiWalletAccount: TWalletAccount,
68
+ chain: `solana:${string}`,
69
+ ): (input: Input) => Promise<Output>;
70
+ export function useSignAndSendTransaction<TWalletAccount extends UiWalletAccount>(
71
+ uiWalletAccount: TWalletAccount,
72
+ chain: `solana:${string}`,
73
+ ): (input: Input) => Promise<Output> {
74
+ const signAndSendTransactions = useSignAndSendTransactions(uiWalletAccount, chain);
75
+ return useCallback(
76
+ async input => {
77
+ const [result] = await signAndSendTransactions(input);
78
+ return result;
79
+ },
80
+ [signAndSendTransactions],
81
+ );
82
+ }
83
+
84
+ /**
85
+ * Use this to get a function capable of signing one or more serialized transactions with the private
86
+ * key of a {@link UiWalletAccount} and sending them to the network for processing. This supports
87
+ * wallets that allow batching multiple transactions in a single request.
88
+ *
89
+ * @example
90
+ * ```tsx
91
+ * import { useSignAndSendTransactions } from '@solana/react';
92
+ *
93
+ * function SignAndSendTransactionsButton({ account, transactionBytes1, transactionBytes2 }) {
94
+ * const signAndSendTransactions = useSignAndSendTransactions(account, 'solana:devnet');
95
+ * return (
96
+ * <button
97
+ * onClick={async () => {
98
+ * try {
99
+ * const [first, second] = await signAndSendTransactions(
100
+ * { transaction: transactionBytes1 },
101
+ * { transaction: transactionBytes2 },
102
+ * );
103
+ * window.alert(
104
+ * `Transaction signatures: ${first.signature.toString()}, ${second.signature.toString()}`,
105
+ * );
106
+ * } catch (e) {
107
+ * console.error('Failed to send transactions', e);
108
+ * }
109
+ * }}
110
+ * >
111
+ * Sign and Send Transactions
112
+ * </button>
113
+ * );
114
+ * }
115
+ * ```
116
+ */
117
+ export function useSignAndSendTransactions<TWalletAccount extends UiWalletAccount>(
118
+ uiWalletAccount: TWalletAccount,
119
+ chain: OnlySolanaChains<TWalletAccount['chains']>,
120
+ ): (...inputs: readonly Input[]) => Promise<readonly Output[]>;
121
+ export function useSignAndSendTransactions<TWalletAccount extends UiWalletAccount>(
122
+ uiWalletAccount: TWalletAccount,
123
+ chain: `solana:${string}`,
124
+ ): (...inputs: readonly Input[]) => Promise<readonly Output[]>;
125
+ export function useSignAndSendTransactions<TWalletAccount extends UiWalletAccount>(
126
+ uiWalletAccount: TWalletAccount,
127
+ chain: `solana:${string}`,
128
+ ): (...inputs: readonly Input[]) => Promise<readonly Output[]> {
129
+ if (!uiWalletAccount.chains.includes(chain)) {
130
+ throw new WalletStandardError(WALLET_STANDARD_ERROR__FEATURES__WALLET_ACCOUNT_CHAIN_UNSUPPORTED, {
131
+ address: uiWalletAccount.address,
132
+ chain,
133
+ featureName: SolanaSignAndSendTransaction,
134
+ supportedChains: [...uiWalletAccount.chains],
135
+ supportedFeatures: [...uiWalletAccount.features],
136
+ });
137
+ }
138
+ const signAndSendTransactionFeature = getWalletAccountFeature(
139
+ uiWalletAccount,
140
+ SolanaSignAndSendTransaction,
141
+ ) as SolanaSignAndSendTransactionFeature[typeof SolanaSignAndSendTransaction];
142
+ const account = getWalletAccountForUiWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(uiWalletAccount);
143
+ return useCallback(
144
+ async (...inputs) => {
145
+ if (inputs.length === 0) {
146
+ return [];
147
+ }
148
+ const inputsWithChainAndAccount = inputs.map(({ options, ...rest }) => {
149
+ const minContextSlot = options?.minContextSlot;
150
+ return {
151
+ ...rest,
152
+ account,
153
+ chain,
154
+ ...(minContextSlot != null
155
+ ? {
156
+ options: {
157
+ minContextSlot: Number(minContextSlot),
158
+ },
159
+ }
160
+ : null),
161
+ };
162
+ });
163
+ const results = await signAndSendTransactionFeature.signAndSendTransaction(...inputsWithChainAndAccount);
164
+ return results;
165
+ },
166
+ [account, chain, signAndSendTransactionFeature],
167
+ );
168
+ }
@@ -0,0 +1,124 @@
1
+ import { Address } from '@solana/addresses';
2
+ import {
3
+ SolanaSignIn,
4
+ SolanaSignInFeature,
5
+ SolanaSignInInput,
6
+ SolanaSignInOutput,
7
+ } from '@solana/wallet-standard-features';
8
+ import {
9
+ getWalletAccountFeature,
10
+ getWalletFeature,
11
+ UiWallet,
12
+ UiWalletAccount,
13
+ UiWalletHandle,
14
+ } from '@wallet-standard/ui';
15
+ import {
16
+ getOrCreateUiWalletAccountForStandardWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
17
+ getWalletAccountForUiWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
18
+ getWalletForHandle_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
19
+ } from '@wallet-standard/ui-registry';
20
+ import { useCallback } from 'react';
21
+
22
+ type Input = SolanaSignInInput;
23
+ type Output = Omit<SolanaSignInOutput, 'account' | 'signatureType'> &
24
+ Readonly<{
25
+ account: UiWalletAccount;
26
+ }>;
27
+
28
+ /**
29
+ * Use the ['Sign In With Solana'](https://phantom.app/learn/developers/sign-in-with-solana) feature
30
+ * of a {@link UiWallet} or {@link UiWalletAccount}.
31
+ *
32
+ * @returns A function that you can call to sign in with the particular wallet and address specified
33
+ * by the supplied {@link UiWalletAccount}
34
+ *
35
+ * @example
36
+ * ```tsx
37
+ * import { useSignIn } from '@solana/react';
38
+ *
39
+ * function SignInButton({ wallet }) {
40
+ * const csrfToken = useCsrfToken();
41
+ * const signIn = useSignIn(wallet);
42
+ * return (
43
+ * <button
44
+ * onClick={async () => {
45
+ * try {
46
+ * const { account, signedMessage, signature } = await signIn({
47
+ * requestId: csrfToken,
48
+ * });
49
+ * // Authenticate the user, typically on the server, by verifying that
50
+ * // `signedMessage` was signed by the person who holds the private key for
51
+ * // `account.publicKey`.
52
+ * //
53
+ * // Authorize the user, also on the server, by decoding `signedMessage` as the
54
+ * // text of a Sign In With Solana message, verifying that it was not modified
55
+ * // from the values your application expects, and that its content is sufficient
56
+ * // to grant them access.
57
+ * window.alert(`You are now signed in with the address ${account.address}`);
58
+ * } catch (e) {
59
+ * console.error('Failed to sign in', e);
60
+ * }
61
+ * }}
62
+ * >
63
+ * Sign In
64
+ * </button>
65
+ * );
66
+ * }
67
+ * ```
68
+ */
69
+ export function useSignIn(uiWalletAccount: UiWalletAccount): (input?: Omit<Input, 'address'>) => Promise<Output>;
70
+ /**
71
+ * @returns A function that you can call to sign in with the supplied {@link UiWallet}
72
+ */
73
+ export function useSignIn(uiWallet: UiWallet): (input?: Input) => Promise<Output>;
74
+ export function useSignIn(uiWalletHandle: UiWalletHandle): (input?: Input) => Promise<Output> {
75
+ const signIns = useSignIns(uiWalletHandle);
76
+ return useCallback(
77
+ async input => {
78
+ const [result] = await signIns(input);
79
+ return result;
80
+ },
81
+ [signIns],
82
+ );
83
+ }
84
+
85
+ function useSignIns(
86
+ uiWalletHandle: UiWalletHandle,
87
+ ): (...inputs: readonly (Input | undefined)[]) => Promise<readonly Output[]> {
88
+ let signMessageFeature: SolanaSignInFeature[typeof SolanaSignIn];
89
+ if ('address' in uiWalletHandle && typeof uiWalletHandle.address === 'string') {
90
+ getWalletAccountForUiWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(uiWalletHandle as UiWalletAccount);
91
+ signMessageFeature = getWalletAccountFeature(
92
+ uiWalletHandle as UiWalletAccount,
93
+ SolanaSignIn,
94
+ ) as SolanaSignInFeature[typeof SolanaSignIn];
95
+ } else {
96
+ signMessageFeature = getWalletFeature(uiWalletHandle, SolanaSignIn) as SolanaSignInFeature[typeof SolanaSignIn];
97
+ }
98
+ const wallet = getWalletForHandle_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(uiWalletHandle);
99
+ return useCallback(
100
+ async (...inputs) => {
101
+ const inputsWithAddressAndChainId = inputs.map(input => ({
102
+ ...input,
103
+ // Prioritize the `UiWalletAccount` address if it exists.
104
+ ...('address' in uiWalletHandle ? { address: uiWalletHandle.address as Address } : null),
105
+ }));
106
+ const results = await signMessageFeature.signIn(...inputsWithAddressAndChainId);
107
+ const resultsWithoutSignatureType = results.map(
108
+ ({
109
+ account,
110
+ signatureType: _, // Solana signatures are always of type `ed25519` so drop this property.
111
+ ...rest
112
+ }) => ({
113
+ ...rest,
114
+ account: getOrCreateUiWalletAccountForStandardWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(
115
+ wallet,
116
+ account,
117
+ ),
118
+ }),
119
+ );
120
+ return resultsWithoutSignatureType;
121
+ },
122
+ [signMessageFeature, uiWalletHandle, wallet],
123
+ );
124
+ }
@@ -0,0 +1,78 @@
1
+ import {
2
+ SolanaSignMessage,
3
+ SolanaSignMessageFeature,
4
+ SolanaSignMessageInput,
5
+ SolanaSignMessageOutput,
6
+ } from '@solana/wallet-standard-features';
7
+ import { getWalletAccountFeature, UiWalletAccount } from '@wallet-standard/ui';
8
+ import { getWalletAccountForUiWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } from '@wallet-standard/ui-registry';
9
+ import { useCallback } from 'react';
10
+
11
+ type Input = Omit<SolanaSignMessageInput, 'account'>;
12
+ type Output = Omit<SolanaSignMessageOutput, 'signatureType'>;
13
+
14
+ /**
15
+ * Use this to get a function capable of signing a message with the private key of a
16
+ * {@link UiWalletAccount}
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * import { useSignMessage } from '@solana/react';
21
+ *
22
+ * function SignMessageButton({ account, messageBytes }) {
23
+ * const signMessage = useSignMessage(account);
24
+ * return (
25
+ * <button
26
+ * onClick={async () => {
27
+ * try {
28
+ * const { signature } = await signMessage({
29
+ * message: messageBytes,
30
+ * });
31
+ * window.alert(`Signature bytes: ${signature.toString()}`);
32
+ * } catch (e) {
33
+ * console.error('Failed to sign message', e);
34
+ * }
35
+ * }}
36
+ * >
37
+ * Sign Message
38
+ * </button>
39
+ * );
40
+ * }
41
+ * ```
42
+ */
43
+ export function useSignMessage<TWalletAccount extends UiWalletAccount>(
44
+ ...config: Parameters<typeof useSignMessages<TWalletAccount>>
45
+ ): (input: Input) => Promise<Output> {
46
+ const signMessages = useSignMessages(...config);
47
+ return useCallback(
48
+ async input => {
49
+ const [result] = await signMessages(input);
50
+ return result;
51
+ },
52
+ [signMessages],
53
+ );
54
+ }
55
+
56
+ function useSignMessages<TWalletAccount extends UiWalletAccount>(
57
+ uiWalletAccount: TWalletAccount,
58
+ ): (...inputs: readonly Input[]) => Promise<readonly Output[]> {
59
+ const signMessageFeature = getWalletAccountFeature(
60
+ uiWalletAccount,
61
+ SolanaSignMessage,
62
+ ) as SolanaSignMessageFeature[typeof SolanaSignMessage];
63
+ const account = getWalletAccountForUiWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(uiWalletAccount);
64
+ return useCallback(
65
+ async (...inputs) => {
66
+ const inputsWithAccount = inputs.map(input => ({ ...input, account }));
67
+ const results = await signMessageFeature.signMessage(...inputsWithAccount);
68
+ const resultsWithoutSignatureType = results.map(
69
+ ({
70
+ signatureType: _, // Solana signatures are always of type `ed25519` so drop this property.
71
+ ...rest
72
+ }) => rest,
73
+ );
74
+ return resultsWithoutSignatureType;
75
+ },
76
+ [signMessageFeature, account],
77
+ );
78
+ }
@@ -0,0 +1,164 @@
1
+ import {
2
+ SolanaSignTransaction,
3
+ SolanaSignTransactionFeature,
4
+ SolanaSignTransactionInput,
5
+ SolanaSignTransactionOutput,
6
+ } from '@solana/wallet-standard-features';
7
+ import {
8
+ WALLET_STANDARD_ERROR__FEATURES__WALLET_ACCOUNT_CHAIN_UNSUPPORTED,
9
+ WalletStandardError,
10
+ } from '@wallet-standard/errors';
11
+ import { getWalletAccountFeature, UiWalletAccount } from '@wallet-standard/ui';
12
+ import { getWalletAccountForUiWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED } from '@wallet-standard/ui-registry';
13
+ import { useCallback } from 'react';
14
+
15
+ import { OnlySolanaChains } from './chain';
16
+
17
+ type Input = Readonly<
18
+ Omit<SolanaSignTransactionInput, 'account' | 'chain' | 'options'> & {
19
+ options?: Readonly<{
20
+ minContextSlot?: bigint;
21
+ }>;
22
+ }
23
+ >;
24
+ type Output = SolanaSignTransactionOutput;
25
+
26
+ /**
27
+ * Use this to get a function capable of signing a serialized transaction with the private key of a
28
+ * {@link UiWalletAccount}
29
+ *
30
+ * @param chain The identifier of the chain the transaction is destined for. Wallets may use this to
31
+ * simulate the transaction for the user.
32
+ *
33
+ * @example
34
+ * ```tsx
35
+ * import { useSignTransaction } from '@solana/react';
36
+ *
37
+ * function SignTransactionButton({ account, transactionBytes }) {
38
+ * const signTransaction = useSignTransaction(account, 'solana:devnet');
39
+ * return (
40
+ * <button
41
+ * onClick={async () => {
42
+ * try {
43
+ * const { signedTransaction } = await signTransaction({
44
+ * transaction: transactionBytes,
45
+ * });
46
+ * window.alert(`Signed transaction bytes: ${signedTransaction.toString()}`);
47
+ * } catch (e) {
48
+ * console.error('Failed to sign transaction', e);
49
+ * }
50
+ * }}
51
+ * >
52
+ * Sign Transaction
53
+ * </button>
54
+ * );
55
+ * }
56
+ * ```
57
+ */
58
+ export function useSignTransaction<TWalletAccount extends UiWalletAccount>(
59
+ uiWalletAccount: TWalletAccount,
60
+ chain: OnlySolanaChains<TWalletAccount['chains']>,
61
+ ): (input: Input) => Promise<Output>;
62
+ export function useSignTransaction<TWalletAccount extends UiWalletAccount>(
63
+ uiWalletAccount: TWalletAccount,
64
+ chain: `solana:${string}`,
65
+ ): (input: Input) => Promise<Output>;
66
+ export function useSignTransaction<TWalletAccount extends UiWalletAccount>(
67
+ uiWalletAccount: TWalletAccount,
68
+ chain: `solana:${string}`,
69
+ ): (input: Input) => Promise<Output> {
70
+ const signTransactions = useSignTransactions(uiWalletAccount, chain);
71
+ return useCallback(
72
+ async input => {
73
+ const [result] = await signTransactions(input);
74
+ return result;
75
+ },
76
+ [signTransactions],
77
+ );
78
+ }
79
+
80
+ /**
81
+ * Use this to get a function capable of signing one or more serialized transactions with the private
82
+ * key of a {@link UiWalletAccount}. This supports batching multiple transactions in a single wallet
83
+ * prompt when the wallet implementation allows it.
84
+ *
85
+ * @example
86
+ * ```tsx
87
+ * import { useSignTransactions } from '@solana/react';
88
+ *
89
+ * function SignTransactionsButton({ account, transactionBytes1, transactionBytes2 }) {
90
+ * const signTransactions = useSignTransactions(account, 'solana:devnet');
91
+ * return (
92
+ * <button
93
+ * onClick={async () => {
94
+ * try {
95
+ * const [first, second] = await signTransactions(
96
+ * { transaction: transactionBytes1 },
97
+ * { transaction: transactionBytes2 },
98
+ * );
99
+ * window.alert(
100
+ * `First transaction bytes: ${first.signedTransaction.toString()}, second transaction bytes: ${second.signedTransaction.toString()}`,
101
+ * );
102
+ * } catch (e) {
103
+ * console.error('Failed to sign transactions', e);
104
+ * }
105
+ * }}
106
+ * >
107
+ * Sign Transactions
108
+ * </button>
109
+ * );
110
+ * }
111
+ * ```
112
+ */
113
+ export function useSignTransactions<TWalletAccount extends UiWalletAccount>(
114
+ uiWalletAccount: TWalletAccount,
115
+ chain: OnlySolanaChains<TWalletAccount['chains']>,
116
+ ): (...inputs: readonly Input[]) => Promise<readonly Output[]>;
117
+ export function useSignTransactions<TWalletAccount extends UiWalletAccount>(
118
+ uiWalletAccount: TWalletAccount,
119
+ chain: `solana:${string}`,
120
+ ): (...inputs: readonly Input[]) => Promise<readonly Output[]>;
121
+ export function useSignTransactions<TWalletAccount extends UiWalletAccount>(
122
+ uiWalletAccount: TWalletAccount,
123
+ chain: `solana:${string}`,
124
+ ): (...inputs: readonly Input[]) => Promise<readonly Output[]> {
125
+ if (!uiWalletAccount.chains.includes(chain)) {
126
+ throw new WalletStandardError(WALLET_STANDARD_ERROR__FEATURES__WALLET_ACCOUNT_CHAIN_UNSUPPORTED, {
127
+ address: uiWalletAccount.address,
128
+ chain,
129
+ featureName: SolanaSignTransaction,
130
+ supportedChains: [...uiWalletAccount.chains],
131
+ supportedFeatures: [...uiWalletAccount.features],
132
+ });
133
+ }
134
+ const signTransactionFeature = getWalletAccountFeature(
135
+ uiWalletAccount,
136
+ SolanaSignTransaction,
137
+ ) as SolanaSignTransactionFeature[typeof SolanaSignTransaction];
138
+ const account = getWalletAccountForUiWalletAccount_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(uiWalletAccount);
139
+ return useCallback(
140
+ async (...inputs) => {
141
+ if (inputs.length === 0) {
142
+ return [];
143
+ }
144
+ const inputsWithAccountAndChain = inputs.map(({ options, ...rest }) => {
145
+ const minContextSlot = options?.minContextSlot;
146
+ return {
147
+ ...rest,
148
+ account,
149
+ chain,
150
+ ...(minContextSlot != null
151
+ ? {
152
+ options: {
153
+ minContextSlot: Number(minContextSlot),
154
+ },
155
+ }
156
+ : null),
157
+ };
158
+ });
159
+ const results = await signTransactionFeature.signTransaction(...inputsWithAccountAndChain);
160
+ return results;
161
+ },
162
+ [signTransactionFeature, account, chain],
163
+ );
164
+ }
@@ -0,0 +1,98 @@
1
+ import { Address, address } from '@solana/addresses';
2
+ import { bytesEqual } from '@solana/codecs-core';
3
+ import { SOLANA_ERROR__SIGNER__WALLET_MULTISIGN_UNIMPLEMENTED, SolanaError } from '@solana/errors';
4
+ import { SignatureBytes } from '@solana/keys';
5
+ import { getAbortablePromise } from '@solana/promises';
6
+ import { MessageModifyingSigner, SignableMessage } from '@solana/signers';
7
+ import type { UiWalletAccount } from '@wallet-standard/ui';
8
+ import { useMemo } from 'react';
9
+
10
+ import { useSignMessage } from './useSignMessage';
11
+
12
+ /**
13
+ * Use this to get a {@link MessageSigner} capable of signing messages with the private key of a
14
+ * {@link UiWalletAccount}
15
+ *
16
+ * @returns A {@link MessageModifyingSigner}. This is a conservative assumption based on the fact
17
+ * that your application can not control whether or not the wallet will modify the message before
18
+ * signing it. Otherwise this method could more specifically return a {@link MessageSigner} or a
19
+ * {@link MessagePartialSigner}.
20
+ *
21
+ * @example
22
+ * ```tsx
23
+ * import { useWalletAccountMessageSigner } from '@solana/react';
24
+ * import { createSignableMessage } from '@solana/signers';
25
+ *
26
+ * function SignMessageButton({ account, text }) {
27
+ * const messageSigner = useWalletAccountMessageSigner(account);
28
+ * return (
29
+ * <button
30
+ * onClick={async () => {
31
+ * try {
32
+ * const signableMessage = createSignableMessage(text);
33
+ * const [signedMessage] = await messageSigner.modifyAndSignMessages([signableMessage]);
34
+ * const messageWasModified = signableMessage.content !== signedMessage.content;
35
+ * const signatureBytes = signedMessage.signatures[messageSigner.address];
36
+ * window.alert(
37
+ * `Signature bytes: ${signatureBytes.toString()}${
38
+ * messageWasModified ? ' (message was modified)' : ''
39
+ * }`,
40
+ * );
41
+ * } catch (e) {
42
+ * console.error('Failed to sign message', e);
43
+ * }
44
+ * }}
45
+ * >
46
+ * Sign Message: {text}
47
+ * </button>
48
+ * );
49
+ * }
50
+ * ```
51
+ */
52
+ export function useWalletAccountMessageSigner<TWalletAccount extends UiWalletAccount>(
53
+ uiWalletAccount: TWalletAccount,
54
+ ): MessageModifyingSigner<TWalletAccount['address']> {
55
+ const signMessage = useSignMessage(uiWalletAccount);
56
+ return useMemo(
57
+ () => ({
58
+ address: address(uiWalletAccount.address),
59
+ async modifyAndSignMessages(messages, config) {
60
+ config?.abortSignal?.throwIfAborted();
61
+ if (messages.length > 1) {
62
+ throw new SolanaError(SOLANA_ERROR__SIGNER__WALLET_MULTISIGN_UNIMPLEMENTED);
63
+ }
64
+ if (messages.length === 0) {
65
+ return messages;
66
+ }
67
+ const { content: originalMessage, signatures: originalSignatureMap } = messages[0];
68
+ const input = {
69
+ message: originalMessage,
70
+ };
71
+ const { signedMessage, signature } = await getAbortablePromise(signMessage(input), config?.abortSignal);
72
+ const messageWasModified =
73
+ originalMessage.length !== signedMessage.length ||
74
+ originalMessage.some((originalByte, ii) => originalByte !== signedMessage[ii]);
75
+ const originalSignature = originalSignatureMap[uiWalletAccount.address as Address<string>] as
76
+ | SignatureBytes
77
+ | undefined;
78
+ const signatureIsNew = originalSignature === undefined || !bytesEqual(originalSignature, signature);
79
+ if (!signatureIsNew && !messageWasModified) {
80
+ // We already had this exact signature, and the message wasn't modified.
81
+ // Don't replace the existing message object.
82
+ return messages;
83
+ }
84
+ const nextSignatureMap = messageWasModified
85
+ ? { [uiWalletAccount.address]: signature }
86
+ : { ...originalSignatureMap, [uiWalletAccount.address]: signature };
87
+ const outputMessages = Object.freeze([
88
+ Object.freeze({
89
+ content: signedMessage,
90
+ signatures: Object.freeze(nextSignatureMap),
91
+ }) as SignableMessage,
92
+ ]);
93
+ return outputMessages;
94
+ },
95
+ }),
96
+ [uiWalletAccount, signMessage],
97
+ );
98
+ }