-
Notifications
You must be signed in to change notification settings - Fork 973
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
Add versioned transaction methods to adapter interface #558
Changes from 21 commits
5269c16
5ce4a3c
1529a5b
9ce97d6
d5e9fd8
6762d53
8098e7b
f2f13b2
f395866
af2257a
f731ea1
98ca6e8
aed4d7a
793b161
4b9132b
eb4011d
20bc5ed
04a6205
80d1d48
f2d50d6
46ef36c
a46ff71
43cde10
6505731
aa1e381
1e8b5ee
87b3943
6cc7d90
4369e40
8819905
bde0bb2
c2d14da
a801e06
ebe79ec
1c0b0f6
6266bd8
6533919
18878eb
ddf04df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
import type { Adapter, SendTransactionOptions, WalletError, WalletName } from '@solana/wallet-adapter-base'; | ||
import type { | ||
Adapter, | ||
MessageSignerWalletAdapterProps, | ||
SignerWalletAdapterProps, | ||
WalletAdapterProps, | ||
WalletError, | ||
WalletName, | ||
} from '@solana/wallet-adapter-base'; | ||
import { WalletNotConnectedError, WalletNotReadyError, WalletReadyState } from '@solana/wallet-adapter-base'; | ||
import type { Connection, PublicKey, Transaction } from '@solana/web3.js'; | ||
import type { PublicKey } from '@solana/web3.js'; | ||
import type { FC, ReactNode } from 'react'; | ||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; | ||
import { WalletNotSelectedError } from './errors.js'; | ||
|
@@ -230,8 +237,8 @@ export const WalletProvider: FC<WalletProviderProps> = ({ | |
}, [isDisconnecting, adapter, setName]); | ||
|
||
// Send a transaction using the provided connection | ||
const sendTransaction = useCallback( | ||
async (transaction: Transaction, connection: Connection, options?: SendTransactionOptions) => { | ||
const sendTransaction: WalletAdapterProps['sendTransaction'] = useCallback( | ||
async (transaction, connection, options) => { | ||
jordaaash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!adapter) throw handleError(new WalletNotSelectedError()); | ||
if (!connected) throw handleError(new WalletNotConnectedError()); | ||
return await adapter.sendTransaction(transaction, connection, options); | ||
|
@@ -240,10 +247,10 @@ export const WalletProvider: FC<WalletProviderProps> = ({ | |
); | ||
|
||
// Sign a transaction if the wallet supports it | ||
const signTransaction = useMemo( | ||
const signTransaction: SignerWalletAdapterProps['signTransaction'] | undefined = useMemo( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type is still not perfect in the sense that a dev will see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems totally fine to me |
||
() => | ||
adapter && 'signTransaction' in adapter | ||
? async (transaction: Transaction): Promise<Transaction> => { | ||
? async (transaction) => { | ||
if (!connected) throw handleError(new WalletNotConnectedError()); | ||
return await adapter.signTransaction(transaction); | ||
} | ||
|
@@ -252,10 +259,10 @@ export const WalletProvider: FC<WalletProviderProps> = ({ | |
); | ||
|
||
// Sign multiple transactions if the wallet supports it | ||
const signAllTransactions = useMemo( | ||
const signAllTransactions: SignerWalletAdapterProps['signAllTransactions'] | undefined = useMemo( | ||
() => | ||
adapter && 'signAllTransactions' in adapter | ||
? async (transactions: Transaction[]): Promise<Transaction[]> => { | ||
? async (transactions) => { | ||
if (!connected) throw handleError(new WalletNotConnectedError()); | ||
return await adapter.signAllTransactions(transactions); | ||
} | ||
|
@@ -264,10 +271,10 @@ export const WalletProvider: FC<WalletProviderProps> = ({ | |
); | ||
|
||
// Sign an arbitrary message if the wallet supports it | ||
const signMessage = useMemo( | ||
const signMessage: MessageSignerWalletAdapterProps['signMessage'] | undefined = useMemo( | ||
() => | ||
adapter && 'signMessage' in adapter | ||
? async (message: Uint8Array): Promise<Uint8Array> => { | ||
? async (message) => { | ||
if (!connected) throw handleError(new WalletNotConnectedError()); | ||
return await adapter.signMessage(message); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ export class AlphaWalletAdapter extends BaseMessageSignerWalletAdapter { | |
url = 'https://github.com/babilu-online/alpha-wallet'; | ||
icon = | ||
'data:image/svg+xml;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAACxEAAAsRAX9kX5EAAAbrSURBVHhe7ZprbBRVFMfvvPru0paG1pZSim2gEkwQFUsIaqMQEkWpVgMRLSDSRB4tNSZ8kC8aE4w8pCCrJgjBaDQaXiHR8MEgMRGJEtRk01IeBsEaVlQKtNDdGf937l3a3bkz7IuZGvcXztxzZi/dOeeeuffcmSUZMmTIkCHD/xeJt2mjq6vLt+b6ie6D2qV8fiqW3yGTSX3rIDO9ReZt2hgcHFzaEq4sQ2QLYIqkDvIUZESQ1gD0nDypypK0eqJcSO4P+/hZIW289Zy0BkDXjWZJlqtlWSaL9Ap+Vsh0EvDP4LqnpDUAsiJ3UOepzCIlpMbI5Z8IGRFZkLYAnD1zphGOT4sEQIE855wF85EF1Vz3jLQFQFGUDuq0GQCFtfPIGFJENN7DggpZyVTvSEsALpw/PxkBmBsZ/YjkSyppJuW8l5ClyIJCrntCWgKgqirufQXzX3QAqDSTikvoYrCeFoogLUz1hpQDEAwGKxRVXRhJ+1ipzfK9hW6HWG8hq5AFCtddJ+UAaKq2Eo5my1Ks8wpBYK6MKip6H902sd5CaiGPMdV9UgpAX19foaIqy02HBRmQpWk7R48e/Re6fgUJmP9JTDtvXSelvcDAwECbruubwuEwMSUUIiGu6zjk5uVNQgB6zM4B/0s4vmfqYvZCQkxNiWwIXWFE/AFZgn3IzTkp6QDAPw3Od6MdbzofEwD84b1l5eXzeXcagDwcf4WUmrY3rIPzr3PdJOlbACn+tCIr42PTPiJZ2dkbeVdGfes1HOl84BX0+7czdYikAmAYBs2cDkmWhM5rqnqsuLj4COsdxTbIDaa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tBDDq9vIoOhL4Y7bQoPBAqfWefuXnrr+sEG/BXP+ABit12l17XK1G6E2i33PZdsQzqaivMUzwKA0aUPLJy2rC0zftw9G6M/03TYMvkZJEeX3uZ9k8bLDKBsgdjV7YWN+ZWfRjvNiyBMfqpunGqUSh2f98WDpwFAFtBi6yCzrLxYNbVIpdMUrf5MGRp9VSeb9ty3wPF5Xzx4nQEU2zdF1blFpKlskiX9Jd34s1RXEy57RYyEAHwNOcFUK20TGm6OeiQQim5s/+3Btqu8S0p4HgDcBvDKPgsaSqrI9KLKoQwwjAFNlxLa8TkxEjKA8gnEtpRtj2SBWfkZu/sfXUt/6ZEWRkQAkAV092f58UKEporJZFyOjwZBlw1PnkDfflAZjoH0Q4Ss7z5ikH3rnN4B/veBn52QXpEEr1/trT600ek1WIYMGTJkyJAhEQj5Fz7jUetUp2rXAAAAAElFTkSuQmCC'; | ||
readonly supportedTransactionVersions = null; | ||
jordaaash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private _connecting: boolean; | ||
private _wallet: AlphaWallet | null; | ||
|
@@ -185,13 +186,13 @@ export class AlphaWalletAdapter extends BaseMessageSignerWalletAdapter { | |
} | ||
} | ||
|
||
async signTransaction(transaction: Transaction): Promise<Transaction> { | ||
async signTransaction<T extends Transaction>(transaction: T): Promise<T> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We know that transaction will only ever be |
||
try { | ||
const wallet = this._wallet; | ||
if (!wallet) throw new WalletNotConnectedError(); | ||
|
||
try { | ||
return (await wallet.signTransaction(transaction)) || transaction; | ||
return ((await wallet.signTransaction(transaction)) as T) || transaction; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we have to perform the one ugly bit, where we cast |
||
} catch (error: any) { | ||
throw new WalletSignTransactionError(error?.message, error); | ||
} | ||
|
@@ -201,13 +202,13 @@ export class AlphaWalletAdapter extends BaseMessageSignerWalletAdapter { | |
} | ||
} | ||
|
||
async signAllTransactions(transactions: Transaction[]): Promise<Transaction[]> { | ||
async signAllTransactions<T extends Transaction>(transactions: T[]): Promise<T[]> { | ||
try { | ||
const wallet = this._wallet; | ||
if (!wallet) throw new WalletNotConnectedError(); | ||
|
||
try { | ||
return (await wallet.signAllTransactions(transactions)) || transactions; | ||
return ((await wallet.signAllTransactions(transactions)) as T[]) || transactions; | ||
} catch (error: any) { | ||
throw new WalletSignTransactionError(error?.message, error); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I think these types are very reasonable now. By inferring
SupportedTransactionVersions
from the actual implementer, and making the methods return the type they were called with, existing code that expectssignTransaction(transaction: Transaction): Promise<Transaction>
will just work.