Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Repair the type of the chain argument to useSignTransaction and `…
Browse files Browse the repository at this point in the history
…useSignAndSendTransaction`
steveluscher committed Jun 14, 2024
1 parent bef21e0 commit ef5372a
Showing 4 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable react-hooks/rules-of-hooks */

import { address } from '@solana/addresses';
import { UiWalletAccount } from '@wallet-standard/ui';

import { useSignAndSendTransaction } from '../useSignAndSendTransaction';

const mockWalletAccount = {
address: address('123'),
chains: ['solana:danknet', 'bitcoin:mainnet'] as const,
features: [],
publicKey: new Uint8Array([1, 2, 3]),
'~uiWalletHandle': null as unknown as UiWalletAccount['~uiWalletHandle'],
} as const;

// [DESCRIBE] useSignAndSendTransaction.
{
// It accepts any chain in the solana namespace
useSignAndSendTransaction(mockWalletAccount, 'solana:danknet');
useSignAndSendTransaction(mockWalletAccount, 'solana:basednet');

// It accepts one of the chains actually supported by the wallet account
useSignAndSendTransaction(mockWalletAccount, 'solana:danknet');

// It rejects a chain in a non-Solana namespace
useSignAndSendTransaction(
mockWalletAccount,
// @ts-expect-error Non-Solana chain
'bitcoin:mainnet',
);
}
31 changes: 31 additions & 0 deletions packages/react/src/__typetests__/useSignTransaction-typetest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable react-hooks/rules-of-hooks */

import { address } from '@solana/addresses';
import { UiWalletAccount } from '@wallet-standard/ui';

import { useSignTransaction } from '../useSignTransaction';

const mockWalletAccount = {
address: address('123'),
chains: ['solana:danknet', 'bitcoin:mainnet'] as const,
features: [],
publicKey: new Uint8Array([1, 2, 3]),
'~uiWalletHandle': null as unknown as UiWalletAccount['~uiWalletHandle'],
} as const;

// [DESCRIBE] useSignTransaction.
{
// It accepts any chain in the solana namespace
useSignTransaction(mockWalletAccount, 'solana:danknet');
useSignTransaction(mockWalletAccount, 'solana:basednet');

// It accepts one of the chains actually supported by the wallet account
useSignTransaction(mockWalletAccount, 'solana:danknet');

// It rejects a chain in a non-Solana namespace
useSignTransaction(
mockWalletAccount,
// @ts-expect-error Non-Solana chain
'bitcoin:mainnet',
);
}
16 changes: 13 additions & 3 deletions packages/react/src/useSignAndSendTransaction.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import {
SolanaSignAndSendTransactionInput,
SolanaSignAndSendTransactionOutput,
} from '@solana/wallet-standard-features';
import { IdentifierString } from '@wallet-standard/base';
import {
WALLET_STANDARD_ERROR__FEATURES__WALLET_ACCOUNT_CHAIN_UNSUPPORTED,
WalletStandardError,
@@ -27,9 +28,18 @@ type Output = SolanaSignAndSendTransactionOutput;
* Returns a function you can call to sign and send a serialized transaction.
*/
export function useSignAndSendTransaction<TWalletAccount extends UiWalletAccount>(
...config: Parameters<typeof useSignAndSendTransactions<TWalletAccount>>
uiWalletAccount: TWalletAccount,
chain: OnlySolanaChains<TWalletAccount['chains']>,
): (input: Input) => Promise<Output>;
export function useSignAndSendTransaction<TWalletAccount extends UiWalletAccount>(
uiWalletAccount: TWalletAccount,
chain: `solana:${string}`,
): (input: Input) => Promise<Output>;
export function useSignAndSendTransaction<TWalletAccount extends UiWalletAccount>(
uiWalletAccount: TWalletAccount,
chain: `solana:${string}`,
): (input: Input) => Promise<Output> {
const signAndSendTransactions = useSignAndSendTransactions(...config);
const signAndSendTransactions = useSignAndSendTransactions(uiWalletAccount, chain);
return useCallback(
async input => {
const [result] = await signAndSendTransactions(input);
@@ -41,7 +51,7 @@ export function useSignAndSendTransaction<TWalletAccount extends UiWalletAccount

function useSignAndSendTransactions<TWalletAccount extends UiWalletAccount>(
uiWalletAccount: TWalletAccount,
chain: OnlySolanaChains<TWalletAccount['chains']>,
chain: `solana:${string}`,
): (...inputs: readonly Input[]) => Promise<readonly Output[]> {
if (!uiWalletAccount.chains.includes(chain)) {
throw new WalletStandardError(WALLET_STANDARD_ERROR__FEATURES__WALLET_ACCOUNT_CHAIN_UNSUPPORTED, {
15 changes: 12 additions & 3 deletions packages/react/src/useSignTransaction.ts
Original file line number Diff line number Diff line change
@@ -28,9 +28,18 @@ type Output = SolanaSignTransactionOutput;
* Returns a function you can call to sign a serialized transaction.
*/
export function useSignTransaction<TWalletAccount extends UiWalletAccount>(
...config: Parameters<typeof useSignTransactions<TWalletAccount>>
uiWalletAccount: TWalletAccount,
chain: OnlySolanaChains<TWalletAccount['chains']>,
): (input: Input) => Promise<Output>;
export function useSignTransaction<TWalletAccount extends UiWalletAccount>(
uiWalletAccount: TWalletAccount,
chain: `solana:${string}`,
): (input: Input) => Promise<Output>;
export function useSignTransaction<TWalletAccount extends UiWalletAccount>(
uiWalletAccount: TWalletAccount,
chain: `solana:${string}`,
): (input: Input) => Promise<Output> {
const signTransactions = useSignTransactions(...config);
const signTransactions = useSignTransactions(uiWalletAccount, chain);
return useCallback(
async input => {
const [result] = await signTransactions(input);
@@ -42,7 +51,7 @@ export function useSignTransaction<TWalletAccount extends UiWalletAccount>(

function useSignTransactions<TWalletAccount extends UiWalletAccount>(
uiWalletAccount: TWalletAccount,
chain: OnlySolanaChains<TWalletAccount['chains']>,
chain: `solana:${string}`,
): (...inputs: readonly Input[]) => Promise<readonly Output[]> {
if (!uiWalletAccount.chains.includes(chain)) {
throw new WalletStandardError(WALLET_STANDARD_ERROR__FEATURES__WALLET_ACCOUNT_CHAIN_UNSUPPORTED, {

0 comments on commit ef5372a

Please sign in to comment.