-
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.
- Loading branch information
Showing
2 changed files
with
130 additions
and
130 deletions.
There are no files selected for viewing
228 changes: 114 additions & 114 deletions
228
packages/base/adapters/solana/web3js/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 |
---|---|---|
@@ -1,114 +1,114 @@ | ||
import { type AnyTransaction, type Chain, type Provider } from '@web3modal/scaffold-utils/solana' | ||
import { ProviderEventEmitter } from './shared/ProviderEventEmitter.js' | ||
import type { Connection, PublicKey, SendOptions } from '@solana/web3.js' | ||
import { solana } from '../utils/chains.js' | ||
|
||
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: Chain[] | ||
getActiveChain: () => Chain | 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: Chain[] | ||
|
||
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 | ||
} | ||
} | ||
import { type AnyTransaction, type Chain, type Provider } from '@web3modal/scaffold-utils/solana' | ||
import { ProviderEventEmitter } from './shared/ProviderEventEmitter.js' | ||
import type { Connection, PublicKey, SendOptions } from '@solana/web3.js' | ||
import { solana } from '../utils/chains.js' | ||
|
||
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: Chain[] | ||
getActiveChain: () => Chain | 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: Chain[] | ||
|
||
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 | ||
} | ||
} |
32 changes: 16 additions & 16 deletions
32
packages/base/adapters/solana/web3js/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 |
---|---|---|
@@ -1,16 +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() | ||
} | ||
} | ||
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() | ||
} | ||
} |