-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CoinbaseWalletProvider for solana (#2879)
Co-authored-by: tomiir <[email protected]>
- Loading branch information
Showing
5 changed files
with
206 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
'@reown/appkit-adapter-ethers5': patch | ||
'@reown/appkit-adapter-ethers': patch | ||
'@reown/appkit-adapter-solana': patch | ||
'@reown/appkit-adapter-wagmi': patch | ||
'@reown/appkit-wallet': patch | ||
'@reown/appkit-core': patch | ||
'@apps/demo': patch | ||
'@apps/gallery': patch | ||
'@apps/laboratory': patch | ||
'@examples/html-ethers': patch | ||
'@examples/html-ethers5': patch | ||
'@examples/html-wagmi': patch | ||
'@examples/next-ethers': patch | ||
'@examples/next-wagmi': patch | ||
'@examples/react-ethers': patch | ||
'@examples/react-ethers5': patch | ||
'@examples/react-solana': patch | ||
'@examples/react-wagmi': patch | ||
'@examples/vue-ethers5': patch | ||
'@examples/vue-solana': patch | ||
'@examples/vue-wagmi': patch | ||
'@reown/appkit-adapter-polkadot': patch | ||
'@reown/appkit': patch | ||
'@reown/appkit-utils': patch | ||
'@reown/appkit-cdn': patch | ||
'@reown/appkit-common': patch | ||
'@reown/appkit-ethers': patch | ||
'@reown/appkit-ethers5': patch | ||
'@reown/appkit-polyfills': patch | ||
'@reown/appkit-scaffold-ui': patch | ||
'@reown/appkit-siwe': patch | ||
'@reown/appkit-solana': patch | ||
'@reown/appkit-ui': patch | ||
'@reown/appkit-wagmi': patch | ||
--- | ||
|
||
Add Solana CoinbaseWalletProvider to allow connecting with coinbase extension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/adapters/solana/src/providers/CoinbaseWalletProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { type AnyTransaction, type Provider } from '@reown/appkit-utils/solana' | ||
import { ProviderEventEmitter } from './shared/ProviderEventEmitter.js' | ||
import type { Connection, PublicKey, SendOptions } from '@solana/web3.js' | ||
import { solana } from '../utils/chains.js' | ||
import type { CaipNetwork } from '@reown/appkit-common' | ||
|
||
export type SolanaCoinbaseWallet = { | ||
publicKey?: PublicKey | ||
signTransaction<T extends AnyTransaction>(transaction: T): Promise<T> | ||
signAllTransactions<T extends AnyTransaction>(transactions: T[]): Promise<T[]> | ||
signAndSendTransaction<T extends AnyTransaction>( | ||
transaction: T, | ||
options?: SendOptions | ||
): Promise<{ signature: string }> | ||
signMessage(message: Uint8Array): Promise<{ signature: Uint8Array }> | ||
connect(): Promise<void> | ||
disconnect(): Promise<void> | ||
emit(event: string, ...args: unknown[]): void | ||
} | ||
|
||
export type CoinbaseWalletProviderConfig = { | ||
provider: SolanaCoinbaseWallet | ||
chains: CaipNetwork[] | ||
getActiveChain: () => CaipNetwork | undefined | ||
} | ||
|
||
export class CoinbaseWalletProvider extends ProviderEventEmitter implements Provider { | ||
public readonly name = 'Coinbase Wallet' | ||
public readonly type = 'ANNOUNCED' | ||
public readonly icon = | ||
'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAyNCIgaGVpZ2h0PSIxMDI0IiB2aWV3Qm94PSIwIDAgMTAyNCAxMDI0IiBmaWxsPSJub25lIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgo8Y2lyY2xlIGN4PSI1MTIiIGN5PSI1MTIiIHI9IjUxMiIgZmlsbD0iIzAwNTJGRiIvPgo8cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZD0iTTE1MiA1MTJDMTUyIDcxMC44MjMgMzEzLjE3NyA4NzIgNTEyIDg3MkM3MTAuODIzIDg3MiA4NzIgNzEwLjgyMyA4NzIgNTEyQzg3MiAzMTMuMTc3IDcxMC44MjMgMTUyIDUxMiAxNTJDMzEzLjE3NyAxNTIgMTUyIDMxMy4xNzcgMTUyIDUxMlpNNDIwIDM5NkM0MDYuNzQ1IDM5NiAzOTYgNDA2Ljc0NSAzOTYgNDIwVjYwNEMzOTYgNjE3LjI1NSA0MDYuNzQ1IDYyOCA0MjAgNjI4SDYwNEM2MTcuMjU1IDYyOCA2MjggNjE3LjI1NSA2MjggNjA0VjQyMEM2MjggNDA2Ljc0NSA2MTcuMjU1IDM5NiA2MDQgMzk2SDQyMFoiIGZpbGw9IndoaXRlIi8+Cjwvc3ZnPgo=' | ||
|
||
private provider: SolanaCoinbaseWallet | ||
private requestedChains: CaipNetwork[] | ||
|
||
constructor(params: CoinbaseWalletProviderConfig) { | ||
super() | ||
this.provider = params.provider | ||
this.requestedChains = params.chains | ||
} | ||
|
||
public get chains() { | ||
// For Coinbase Wallet, we only support the Solana mainnet | ||
return this.requestedChains.filter(chain => chain.chainId === solana.chainId) | ||
} | ||
|
||
public get publicKey() { | ||
return this.provider.publicKey | ||
} | ||
|
||
public async connect() { | ||
try { | ||
await this.provider.connect() | ||
const account = this.getAccount(true) | ||
this.provider.emit('connect', this.provider.publicKey) | ||
this.emit('connect', account) | ||
|
||
return account.toBase58() | ||
} catch (error) { | ||
this.provider.emit('error', error) | ||
throw error | ||
} | ||
} | ||
|
||
public async disconnect() { | ||
await this.provider.disconnect() | ||
this.provider.emit('disconnect', undefined) | ||
this.emit('disconnect', undefined) | ||
} | ||
|
||
public async signMessage(message: Uint8Array) { | ||
const result = await this.provider.signMessage(message) | ||
|
||
return result.signature | ||
} | ||
|
||
public async signTransaction<T extends AnyTransaction>(transaction: T) { | ||
return this.provider.signTransaction(transaction) | ||
} | ||
|
||
public async signAndSendTransaction<T extends AnyTransaction>( | ||
transaction: T, | ||
sendOptions?: SendOptions | ||
) { | ||
const result = await this.provider.signAndSendTransaction(transaction, sendOptions) | ||
|
||
return result.signature | ||
} | ||
|
||
public async sendTransaction( | ||
transaction: AnyTransaction, | ||
connection: Connection, | ||
options?: SendOptions | ||
) { | ||
const signedTransaction = await this.signTransaction(transaction) | ||
const signature = await connection.sendRawTransaction(signedTransaction.serialize(), options) | ||
|
||
return signature | ||
} | ||
|
||
public async signAllTransactions<T extends AnyTransaction[]>(transactions: T): Promise<T> { | ||
return (await this.provider.signAllTransactions(transactions)) as T | ||
} | ||
|
||
private getAccount<Required extends boolean>( | ||
required?: Required | ||
): Required extends true ? PublicKey : PublicKey | undefined { | ||
const account = this.provider.publicKey | ||
if (required && !account) { | ||
throw new Error('Not connected') | ||
} | ||
|
||
return account as Required extends true ? PublicKey : PublicKey | undefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/adapters/solana/src/tests/mocks/CoinbaseWallet.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { vi } from 'vitest' | ||
import type { SolanaCoinbaseWallet } from '../../providers/CoinbaseWalletProvider.js' | ||
import { TestConstants } from '../util/TestConstants.js' | ||
|
||
export function mockCoinbaseWallet(): SolanaCoinbaseWallet { | ||
return { | ||
publicKey: TestConstants.accounts[0].publicKey, | ||
connect: vi.fn().mockResolvedValue(undefined), | ||
disconnect: vi.fn().mockResolvedValue(undefined), | ||
signMessage: vi.fn().mockResolvedValue({ signature: new Uint8Array() }), | ||
signTransaction: vi.fn(tx => tx), | ||
signAllTransactions: vi.fn(tx => tx), | ||
signAndSendTransaction: vi.fn().mockResolvedValue({ signature: '' }), | ||
emit: vi.fn() | ||
} | ||
} |